Rules | Recent posts | topic RSS | Search | Register  | Log in

LCD simple codes

 
Post new topic  Reply to topic    EDAboard.com Forum Index -> Microcontrollers
Author Message
ericmar



Joined: 14 Apr 2004
Posts: 279
Location: Singapore


Post30 Jul 2004 6:22   LCD simple codes

Hi, can anyone pls post a simple LCD code for me? I just need to know how to show a word in a 2x16 LCD. BTW, I'm using Microchip PIC16F877!

Preferably in Hi-tech PIC C language!

I try the routine provided by them but still doesnt work.

I'hv totally no idea on programming LCD yet.

Thanks for help anyway!

Smile
Back to top
C-Man



Joined: 19 Jul 2001
Posts: 1303
Helped: 73


Post30 Jul 2004 7:43   Re: LCD simple codes

You should read one of the LCD Faq's thats around, for example here:
http://www.repairfaq.org/filipg/LINK/F_LCD_HD44780.html

For Hi-Tech code examples check here:
http://www.microchipc.com/sourcecode/

best regards
Back to top
meax98



Joined: 25 Nov 2001
Posts: 446


Post30 Jul 2004 7:48   Re: LCD simple codes

This is example in PicBasic Pro, but the logic is the same. Check your lcd connections and pin use.

' PicBasic program to demonstrate operation of an LCD in 4-bit mode
'
' LCD should be connected as follows:
' LCD PIC
' DB4 PortA.0
' DB5 PortA.1
' DB6 PortA.2
' DB7 PortA.3
' RS PortA.4 (add 4.7K pullup resistor to 5 volts)
' E PortB.3
' RW Ground
' Vdd 5 volts
' Vss Ground
' Vo 20K potentiometer (or ground)
' DB0-3 No connect

ADCON1 = 7 'portA to digital

Lcdout $fe,1
Pause 500 ' Wait for LCD to startup

loop: Lcdout $fe, 1 ' Clear LCD screen
Lcdout "Hello" ' Display Hello
Pause 500 ' Wait .5 second

Lcdout $fe, $c0 'write in second line
Lcdout "World"
Pause 500 ' Wait .5 second

Goto loop ' Do it forever

regards

meax98
Back to top
meax98



Joined: 25 Nov 2001
Posts: 446


Post30 Jul 2004 7:51   Re: LCD simple codes

Also this is set of control codes for HD44780 based displays:

1 = Clear screen.
2 = Send cursor to top-left position.
8 = Blank without clearing.
12 = Make cursor invisible/restore display if blanked.
13 = Turn on visible blinking cursor.
14 = Turn on visible underline cursor.
16 = Move cursor one character left.
20 = Move cursor one character right.
24 = Scroll display one character left (all lines).
26 = Poll Keypad
28 = Scroll display one character right (all lines).
35 = Place Large Digit
58 = Enter Buffer Return Status Mode
59 = Exit Buffer Return Status Mode
61 = Make Vertical Bar Graph
65 = Auto Transmit Keypresses on
66 = Backlight On
67 = Auto Line wrapping on.................(default).
68 = Auto Line wrapping off.
69 = Clear Key Buffer
70 = Backlight Off
71 = Go to position.
72 = Go to top left.
74 = Cursor on.
75 = Cursor off.................................(default).
76 = Cursor left.
77 = Cursor right.
78 = Create Custom Character
79 = Auto Transmit Keypresses Off
80 = Contrast
81 = Auto scroll on.
82 = Auto scroll off..........................(default).
83 = Blink on.
84 = Blink off (default).
85 = Set Debounce Time
86 = General purpose output on.
87 = General purpose output off........(default).
88 = Clear display.
96 = Auto Repeat Mode Off
104 = Initialize Horizontal Bar Graph
110 = Initialize Large Digits
115 = Initialize Thin Bar Vertical Graph
118 = Initialize Thick Vertical Bar Graph
124 = Make Horizontal Bar Graph
126 = Auto Repeat Mode On
192 = Move cursor to first position on second line.

Use it with [ $FE,code ], like LcdOut $FE,1 - clear LCD ...

meax98
Back to top
ericmar



Joined: 14 Apr 2004
Posts: 279
Location: Singapore


Post30 Jul 2004 8:20   Re: LCD simple codes

This is my coding in Hi-Tech PIC C!

#define XTAL_FREQ 8MHZ

__CONFIG(XT & WDTDIS & PWRTEN & LVPDIS & DUNPROT & DEBUGDIS);

void main()
{


TRISA=0x00;
TRISB=0x00;
PORTA=0;
PORTB=0;

//-- initialise LCD --
lcd_init();

//-- Clear the display --
lcd_clear();

//-- Display opening message --
lcd_puts("Testing the L.C.D.");
DelayMs(40);
PORTB=0x00;
DelayMs(40);
}
Back to top
bro



Joined: 30 Jul 2004
Posts: 7
Location: Estonia


Post30 Jul 2004 16:48   Re: LCD simple codes

does anyone have simple sorce code for using LCD with PIC16F84.LCD is 16x1 and with HD44780 controller.It would be good if it would be in assembler.Probably basic language would be lot easyer but i havnt found any freeware compilers for basic...if there is any...

Thanks!Smile
Back to top
angeliz



Joined: 30 Mar 2004
Posts: 2


Post30 Jul 2004 20:36   Re: LCD simple codes

hello
see my site but in french


http://angeliz.free.fr

bye
Back to top
imp



Joined: 07 Jun 2004
Posts: 10


Post03 Aug 2004 7:56   LCD simple codes

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);
}
Back to top
ericmar



Joined: 14 Apr 2004
Posts: 279
Location: Singapore


Post04 Aug 2004 3:20   Re: LCD simple codes

imp wrote:
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);
}


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?

Can u explain to me wat's this for?
static bit LCD_RS @ ((unsigned)&PORTE*8+1); // LCD Reg. Select
Why PORTE*8 ???

Wat r these 2 lines for?
ADCON1= 0b10000010;
TRISA = 0x3F;

For some reasons, it doesnt work for scrolling to right!???

Thanks for help!
Back to top
checkmate



Joined: 25 Feb 2004
Posts: 489
Helped: 35
Location: Toilet Seat


Post04 Aug 2004 5:47   Re: LCD simple codes

ericmar wrote:
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?

Nothing.

ericmar wrote:

Can u explain to me wat's this for?
static bit LCD_RS @ ((unsigned)&PORTE*8+1); // LCD Reg. Select
Why PORTE*8 ???

It's mapping the bit address for LCD_RS to the E1 I/O pin.
Multiply by 8 bcos each register is 8-bits.
Since PORTE is just the index of the register, the bit address for pin E0 is PORTE*8. Pin E1 is bit address of pin E0 plus 1 and so on.

ericmar wrote:

Wat r these 2 lines for?
ADCON1= 0b10000010; // Sets port E as digital I/O pins
TRISA = 0x3F; // Redundant

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.
Back to top
imp



Joined: 07 Jun 2004
Posts: 10


Post04 Aug 2004 7:08   Re: LCD simple codes

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.

Previously the code includes the ADC part as well,i removed it because
of ur que., relates to LCD only,but i haven't remembered to comment
TRISA,if it makes confusion sorry for the mistake.

For better understanding go thro' the datasheet.

IMP
Back to top
ericmar



Joined: 14 Apr 2004
Posts: 279
Location: Singapore


Post04 Aug 2004 18:35   Re: LCD simple codes

imp wrote:
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

So when do we need to set the pins as analog input and when do we need to set them as I/O?

Besides, is PORTB = 0b00001000 same as RB3 = 1?
I'm a bit confuse with the TRISB, PORTB, RBX...
Back to top
nooknikz



Joined: 22 Apr 2002
Posts: 398
Helped: 20
Location: thailand


Post05 Aug 2004 4:56   Re: LCD simple codes

Hi-tech PIC C language , please see in site
http://www.microchipc.com/

Surprised nooknikz
Back to top
muko



Joined: 03 Mar 2004
Posts: 37
Helped: 1


Post06 Aug 2004 15:16   LCD simple codes

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(Cool;
PORTD= 0x31;
E=1;
Nop();
E=0;
Delay10KTCYx(Cool;
PORTD= 0x31;
E=1;
Nop();
E=0;
Delay10TCYx(Cool;
PORTD= 0x31;
E=1;
Nop();
E=0;
Delay10KTCYx(Cool;
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(Cool;

}

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(Cool;
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);
}
Back to top
ericmar



Joined: 14 Apr 2004
Posts: 279
Location: Singapore


Post08 Aug 2004 17:48   Re: LCD simple codes

muko wrote:
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(Cool;
PORTD= 0x31;
E=1;
Nop();
E=0;
Delay10KTCYx(Cool;
PORTD= 0x31;
E=1;
Nop();
E=0;
Delay10TCYx(Cool;
PORTD= 0x31;
E=1;
Nop();
E=0;
Delay10KTCYx(Cool;
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(Cool;

}

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(Cool;
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);
}

Hi, thanks for ur code. But is it in CCS or Hi-Tech PIC C?

Regards.
Back to top
Robo



Joined: 22 Jan 2005
Posts: 27


Post02 Feb 2005 5:25   Re: LCD simple codes

Does anyone has a code to initialize "Truly LCD Module MTC-C162DPRN-2N"?
BasicStamp programming is preferred. Thank you very much!
Back to top
Nick C.



Joined: 20 Dec 2002
Posts: 184
Helped: 4


Post14 Feb 2005 2:17   Re: LCD simple codes

Robo wrote:
Does anyone has a code to initialize "Truly LCD Module MTC-C162DPRN-2N"?
BasicStamp programming is preferred. Thank you very much!


The MTC-C162DPRN-2N contains a Samsung KS0070B controller,
which is compatible with the standard Hitachi HD44780 controller.

Any general LCD Basic Stamp routines should work
with this 16 x 2 character module.

A good place to start is the Basic Stamp manual. Also, this app note
should work with your display - however, your pinout may be different.
The software should work with any HD44780-compatible display.

http://www.parallax.com/dl/docs/prod/audiovis/lcd2x16Par.pdf

Nick
Back to top
Robo



Joined: 22 Jan 2005
Posts: 27


Post14 Feb 2005 2:58   Re: LCD simple codes

Wow, it looks like is a good place to start with. I'll try it out...
Thank you Nick!
Back to top
king_kong214



Joined: 16 Mar 2006
Posts: 2


Post16 Mar 2006 9:55   Re: LCD simple codes

imp wrote:
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);
}



Hi, i've tried the above codes but failed to get the LCD to work. All it does is to show black boxes on the 1st line and nothing on the second line (exactly the same if i were to simply plug in 5 v into the lcd module without any u-controller). I'm guessing its due to the initialisation process. Or maybe my mcu running at 20MHz.

The LCD module is ATM1602B with S6A0069 Controller, PIC16F877 running at 20MHz as the microcontroller and Hi-Tech Picc as the language suite.

Connections as below:

Mcu................LCD
RE1................R/S
RE2................R/W
RD4...............DB4
RD5...............DB5
RD6...............DB6
RD7...............DB7

Ground..........E
Ground..........DB0 to DB3

Please please advise wat went wrong. I've done multiple checks on my connection and this LCD is driving me mad...
Back to top
garg29



Joined: 17 Nov 2004
Posts: 277
Helped: 3


Post28 May 2006 4:33   Re: LCD simple codes

Quote:
Ground..........E
Ground..........DB0 to DB3


u r grounding ebnable pin instead of r/w pin of lcd.
Back to top
Post new topic  Reply to topic    EDAboard.com Forum Index -> Microcontrollers
Page 1 of 1 All times are GMT + 1 Hour


Abuse
Administrator
Moderators
topic RSS 
sitemap