/* mini_hello_lcd.c */ /* LCD two line 16 characthers display */ /* Line 1 starts at adress 0x00 line 2 at 0xC0 */ #include "16F690.h" #pragma config |= 0x00D4 /* I/O-pin definitions */ /* change if you need a pin for a different purpose */ #pragma bit RS @ PORTB.4 #pragma bit EN @ PORTB.6 #pragma bit D7 @ PORTC.3 #pragma bit D6 @ PORTC.2 #pragma bit D5 @ PORTC.1 #pragma bit D4 @ PORTC.0 void delay( char ); // ms delay function void lcd_init( void ); void lcd_putchar( char ); void lcd_putline( char start, const char * text ); void main( void) { /* I/O-pin direction in/out definitions, change if needed */ ANSEL=0; // PORTC digital I/O ANSELH=0; TRISC = 0b1111.0000; /* RC3,2,1,0 out*/ TRISB.4=0; /* RB4, RB6 out */ TRISB.6=0; lcd_init(); lcd_putline( 0, "Hello World!"); lcd_putline( 0xC0, "Hello Kitty!"); while(1) nop(); } /* *********************************** */ /* FUNCTIONS */ /* *********************************** */ void lcd_init( void ) // must be run once before using the display { delay(40); // give LCD time to settle RS = 0; // LCD in command-mode lcd_putchar(0b0011.0011); /* LCD starts in 8 bit mode */ lcd_putchar(0b0011.0010); /* change to 4 bit mode */ lcd_putchar(0b00101000); /* two line (8+8 chars in the row) */ lcd_putchar(0b00001100); /* display on, cursor off, blink off */ lcd_putchar(0b00000001); /* display clear */ lcd_putchar(0b00000110); /* increment mode, shift off */ RS = 1; // LCD in character-mode // initialization is done! } void lcd_putchar( char data ) { // must set LCD-mode before calling this function! // RS = 1 LCD in character-mode // RS = 0 LCD in command-mode // upper Nybble D7 = data.7; D6 = data.6; D5 = data.5; D4 = data.4; EN = 0; nop(); EN = 1; delay(5); // lower Nybble D7 = data.3; D6 = data.2; D5 = data.1; D4 = data.0; EN = 0; nop(); EN = 1; delay(5); } void lcd_putline(char start, const char * text) { RS = 0; // LCD in command-mode lcd_putchar( start ); // move to text position RS = 1; // LCD in character-mode char i, k; for(i = 0 ; ; i++) { k = text[i]; if( k == '\0') return; // found end of string lcd_putchar(k); } return; } void delay( char millisec) /* Delays a multiple of 1 milliseconds at 4 MHz (16F690 internal clock) using the TMR0 timer */ { OPTION = 2; /* prescaler divide by 8 */ do { TMR0 = 0; while ( TMR0 < 125) /* 125 * 8 = 1000 */ ; } while ( -- millisec > 0); } /* *********************************** */ /* HARDWARE */ /* *********************************** */ /* ___________ ___________ | \/ | +5V---|Vdd 16F690 Vss|---GND |RA5 RA0/AN0/(PGD)| |RA4 RA1/(PGC)| |RA3/!MCLR/(Vpp) RA2/INT| |RC5/CCP RC0|->-D4 |RC4 RC1|->-D5 D7 -<-|RC3 RC2|->-D6 |RC6 RB4|->- RS |RC7 RB5/Rx| |RB7/Tx RB6|->- EN |________________________| */ /* LCD two lines, Line length 16 characters Internal ic: HD44780A00 _______________ | | | Vss 1|--- GND | Vdd 2|--- +5V | Contrast 3|-<- Pot | RS 4|-<- RB4 | RD/!WR 5|--- 0, GND | EN 6|-<- RB6 | D0 7| | D1 8| | D2 9| | D3 10| | D4 11|-<- RC0 | D5 12|-<- RC1 | D6 13|-<- RC2 | D7 14|-<- RC3 |_______________| */