imp said:try this one...
//Xtal=8Mhz
//Lcd 4-bit Mode
//16x2 LCD
#include <pic.h>
#include "delay.h"
#include "delay.c"
static bit LCD_RS @ ((unsigned)&PORTE*8+1); // LCD Reg. Select
static bit LCD_EN @ ((unsigned)&PORTE*8+2); // LCD Enable
#define LCD_STROBE ((LCD_EN = 1),(LCD_EN=0))
unsigned char SCROLL_LEFT,CURSOR_OFF;
void lcd_init(void);
void port_init(void);
void lcd_write(unsigned char c);
void lcd_clear(void);
void lcd_puts(const char * s);
void lcd_putch(char c);
void lcd_goto(unsigned char c);
void lcd_scroll(char Direction);
void lcd_home(void);
void lcd_cursor(char onoff);
main()
{
port_init();
lcd_init();
lcd_goto(0x04);
lcd_puts("LCD TEST");
lcd_clear();
while(1)
{
lcd_scroll(SCROLL_LEFT);
lcd_goto(0x40);
lcd_puts("WELCOME TO LCD TEST");
DelayMs(150);
}
}
void port_init(void)
{
ADCON1= 0b10000010;
TRISA = 0x3F;
TRISE = 0b00000000; //PORTE as output port
TRISD = 0b00000000; //PORTD as output port
}
void lcd_write(unsigned char c)
{
PORTD = (PORTD & 0xF0) | (c >> 4);
LCD_STROBE;
PORTD = (PORTD & 0xF0) | (c & 0x0F);
LCD_STROBE;
DelayUs(40);
}
void lcd_clear(void)
{
LCD_RS = 0;
lcd_write(0x1); //-- Clear the Display
DelayMs(2);
lcd_write(0x02); //-- Home the display
DelayMs(2);
}
void lcd_puts(const char * s)
{
LCD_RS = 1; // write characters
while(*s)
lcd_write(*s++);
}
void lcd_goto(unsigned char pos)
{
LCD_RS = 0;
lcd_write(0x80+pos); //-- sets the DDRAM Address
}
void lcd_init(void)
{
LCD_RS = 0; // write control bytes
DelayMs(15); // power on delay
PORTD = 0x3; // attention!
LCD_STROBE;
DelayMs(5);
LCD_STROBE;
DelayUs(100);
LCD_STROBE;
DelayMs(5);
PORTD = 0x2; // set 4 bit mode
LCD_STROBE;
DelayUs(40);
lcd_write(0x28); // 4 bit mode, 1/16 duty, 5x8 font
lcd_write(0x08); // display off
lcd_write(0x0F); // display on, blink curson on
lcd_write(0x06); // entry mode
}
void putch(char c)
{
LCD_RS=1; //- write characters
lcd_write(c);
}
void puts(char *s)
{
LCD_RS = 1; // write characters
while(*s)
lcd_write(*s++);
}
void lcdprint(unsigned char x,unsigned char *str)
{
lcd_goto(x);
lcd_puts(str);
}
void lcd_scroll(char Direction)
{
LCD_RS=0; //-- write command
if(Direction==SCROLL_LEFT)
{
lcd_write(0x18); //- S/C=1 R/L=0 => Scroll LEFT
}
else
{
lcd_write(0x1b); //- S/C=1 R/L=1 => Scroll RIGHT
}
DelayMs(1);
}
void lcd_home(void)
{
LCD_RS=0; //-- write command
lcd_write(0x02); //-- Home Everything
DelayMs(2);
}
void lcd_cursor(char onoff)
{
LCD_RS=0;
if(onoff==CURSOR_OFF) //- 0000 1DCB
{ //- D= Display C= Cursor B=Blink
lcd_write(0x0C); //- D on/off= 1 C on/off=0 B on/off=0
}
else
{
lcd_write(0x0F); //- D=1 C=1 B=1 => Cursor ON + Blink
}
DelayMs(1);
}
Nothing.ericmar said:hi imp,
Thanks for ur help! This is definitely a great example for me. However, may I know wat is PORTA used for in ur code?
It's mapping the bit address for LCD_RS to the E1 I/O pin.ericmar said:Can u explain to me wat's this for?
static bit LCD_RS @ ((unsigned)&PORTE*8+1); // LCD Reg. Select
Why PORTE*8 ???
Let me give you a suggestion. It's quite obvious he's using a 40-pin PIC here. You can easily refer to any 40-pin PIC16 datasheet (I used the 16F877A one) and figure out what is implied by those registers. Reading the datasheet is the most important reading that you will ever need to do PIC development.ericmar said:Wat r these 2 lines for?
ADCON1= 0b10000010; // Sets port E as digital I/O pins
TRISA = 0x3F; // Redundant
So when do we need to set the pins as analog input and when do we need to set them as I/O?imp said:hi ericmar,
The code is written for PIC16F877,its having inbuild ADC.
ADCON1 is used to configure the port pins of PORT-A and PORT-E.
ADCON1= 0b10000010;
--> means that all the pins of PORT-A are analog i/p.
and all the pins of PORT-E are digital i/o pins.
IMP
Hi, thanks for ur code. But is it in CCS or Hi-Tech PIC C?muko said:lcd.h
//filename: lcd.h
//***********************************************
//#define NOP() asm("NOP");
#define E PORTDbits.RD1
#define WR PORTDbits.RD2
#define RS PORTDbits.RD3
//***********************************************
//***********************************************
void Cursor_On(void);
void Lcd_Clear(void);
void Lcd_Line2(void);
void Lcd_Line1(void);
void Lcd_Shift(void);
void Cursor_Right(void);
void Cursor_Left(void);
void Lcd_Puts(const rom char * s);
void Init_Lcd( void );
void Write_Lcd_Cmd(unsigned char cmd );
void Write_Lcd_Data(unsigned char data );
void Lcd_Goto(unsigned char pos);
//***********************************************
lcd.c
//filename:lcd.c
#include <delays.h>
#include "lcd.h"
void Init_Lcd( void ) // initialize LCD display
{
PORTD = 0x01;
TRISD= 0;
Delay10KTCYx(8);
PORTD= 0x31;
E=1;
Nop();
E=0;
Delay10KTCYx(8);
PORTD= 0x31;
E=1;
Nop();
E=0;
Delay10TCYx(8);
PORTD= 0x31;
E=1;
Nop();
E=0;
Delay10KTCYx(8);
PORTD= 0x21;
E=1;
Nop();
E=0;
Delay10KTCYx(3);
Write_Lcd_Cmd(0x28); // define 4 bit interface, 2 lines. 5x7 dots
Write_Lcd_Cmd(0x0c); // display on, cursor on, blink off
Write_Lcd_Cmd(0x01); // clear display
Write_Lcd_Cmd(0x06); // entry mode set..
Write_Lcd_Cmd(0x28);
}
void Write_Lcd_Cmd(unsigned char cmd ) // subroutiune for lcd commands
{
char x;
x = cmd | 0x01;
PORTD = x & 0xf1;
E=1;
Nop();
E=0;
x = (cmd << 4) | 0x01 ;
PORTD= x & 0xf1 ;
E=1;
Nop();
E=0;
Delay10KTCYx(8);
}
void Write_Lcd_Data(unsigned char data ) // subroutine for lcd data
{
unsigned char x;
PORTD = 0x01;
x= data | 0x01;
RS=1;
PORTD= x & 0xf1;
RS=1;
E=1;
Nop();
E=0;
x= (data << 4) | 0x01;
PORTD= x & 0xf1;
RS=1;
E=1;
Nop();
E=0;
RS=0;
Delay10KTCYx(8);
PORTDbits.RD0=1;
}
void Cursor_On(void) { Write_Lcd_Cmd(0x0f); }
void Lcd_Clear(void) { Write_Lcd_Cmd(0x01); }
void Lcd_Line2(void) { Write_Lcd_Cmd(0xc0); }
void Lcd_Line1(void) { Write_Lcd_Cmd(0x02); }
void Lcd_Shift(void) { Write_Lcd_Cmd(0x1c); }
void Cursor_Right(void) { Write_Lcd_Cmd(0x14); }
void Cursor_Left(void) { Write_Lcd_Cmd(0x10); }
void Lcd_Puts(const rom char * s)
{
while(*s)
{
Write_Lcd_Data(*s);
s++;
}
}
void Lcd_Goto(unsigned char pos)
{
Write_Lcd_Cmd(0x80+pos);
}
Robo said:Does anyone has a code to initialize "Truly LCD Module MTC-C162DPRN-2N"?
BasicStamp programming is preferred. Thank you very much!
imp said:try this one...
//Xtal=8Mhz
//Lcd 4-bit Mode
//16x2 LCD
#include <pic.h>
#include "delay.h"
#include "delay.c"
static bit LCD_RS @ ((unsigned)&PORTE*8+1); // LCD Reg. Select
static bit LCD_EN @ ((unsigned)&PORTE*8+2); // LCD Enable
#define LCD_STROBE ((LCD_EN = 1),(LCD_EN=0))
unsigned char SCROLL_LEFT,CURSOR_OFF;
void lcd_init(void);
void port_init(void);
void lcd_write(unsigned char c);
void lcd_clear(void);
void lcd_puts(const char * s);
void lcd_putch(char c);
void lcd_goto(unsigned char c);
void lcd_scroll(char Direction);
void lcd_home(void);
void lcd_cursor(char onoff);
main()
{
port_init();
lcd_init();
lcd_goto(0x04);
lcd_puts("LCD TEST");
lcd_clear();
while(1)
{
lcd_scroll(SCROLL_LEFT);
lcd_goto(0x40);
lcd_puts("WELCOME TO LCD TEST");
DelayMs(150);
}
}
void port_init(void)
{
ADCON1= 0b10000010;
TRISA = 0x3F;
TRISE = 0b00000000; //PORTE as output port
TRISD = 0b00000000; //PORTD as output port
}
void lcd_write(unsigned char c)
{
PORTD = (PORTD & 0xF0) | (c >> 4);
LCD_STROBE;
PORTD = (PORTD & 0xF0) | (c & 0x0F);
LCD_STROBE;
DelayUs(40);
}
void lcd_clear(void)
{
LCD_RS = 0;
lcd_write(0x1); //-- Clear the Display
DelayMs(2);
lcd_write(0x02); //-- Home the display
DelayMs(2);
}
void lcd_puts(const char * s)
{
LCD_RS = 1; // write characters
while(*s)
lcd_write(*s++);
}
void lcd_goto(unsigned char pos)
{
LCD_RS = 0;
lcd_write(0x80+pos); //-- sets the DDRAM Address
}
void lcd_init(void)
{
LCD_RS = 0; // write control bytes
DelayMs(15); // power on delay
PORTD = 0x3; // attention!
LCD_STROBE;
DelayMs(5);
LCD_STROBE;
DelayUs(100);
LCD_STROBE;
DelayMs(5);
PORTD = 0x2; // set 4 bit mode
LCD_STROBE;
DelayUs(40);
lcd_write(0x28); // 4 bit mode, 1/16 duty, 5x8 font
lcd_write(0x08); // display off
lcd_write(0x0F); // display on, blink curson on
lcd_write(0x06); // entry mode
}
void putch(char c)
{
LCD_RS=1; //- write characters
lcd_write(c);
}
void puts(char *s)
{
LCD_RS = 1; // write characters
while(*s)
lcd_write(*s++);
}
void lcdprint(unsigned char x,unsigned char *str)
{
lcd_goto(x);
lcd_puts(str);
}
void lcd_scroll(char Direction)
{
LCD_RS=0; //-- write command
if(Direction==SCROLL_LEFT)
{
lcd_write(0x18); //- S/C=1 R/L=0 => Scroll LEFT
}
else
{
lcd_write(0x1b); //- S/C=1 R/L=1 => Scroll RIGHT
}
DelayMs(1);
}
void lcd_home(void)
{
LCD_RS=0; //-- write command
lcd_write(0x02); //-- Home Everything
DelayMs(2);
}
void lcd_cursor(char onoff)
{
LCD_RS=0;
if(onoff==CURSOR_OFF) //- 0000 1DCB
{ //- D= Display C= Cursor B=Blink
lcd_write(0x0C); //- D on/off= 1 C on/off=0 B on/off=0
}
else
{
lcd_write(0x0F); //- D=1 C=1 B=1 => Cursor ON + Blink
}
DelayMs(1);
}
Ground..........E
Ground..........DB0 to DB3
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?