i have written C code to interface 16x2 LCD with PIC16f876 using Mikroc compiler and with help of the RICKEY'S WORLD tutorial at
http://www.8051projects.net/lcd-interfacing/
which was very helpful and i was successful with 8 bit programing mode and thats is the result
http://www.8051projects.net/lcd-interfacing/
which was very helpful and i was successful with 8 bit programing mode and thats is the result
Rich (BB code):
/*LCD 8 bit example
rs => Register Select [Command : Data]
0 => Command
1 => Data
rw => Read/Write [Write : Read]
0 => Write
1 => Read
en => Enable signal [High to Low]
*/
#define LCD_data PORTB
#define LCD_rs PORTC.B0
#define LCD_rw PORTC.B1
#define LCD_en PORTC.B2
#define LCD_D7 PORTB.B7
void LCD_busy() {
delay_ms(5);//it is very important to return to the manual for it
}
void LCD_write_command () {
LCD_rs = 0; //Selected command register
LCD_rw = 0; //We are writing in data register
LCD_en = 1; //Enable H->L
LCD_en = 0;
LCD_busy(); //Wait for LCD to process the command
}
void LCD_clear_display () {
LCD_data = 0x01; //Clear LCD
LCD_write_command();
}
void LCD_return_home () {
LCD_data = 0x02; //Return LCD to Home
LCD_write_command();
}
void LCD_entry_mode_set (unsigned int ID,unsigned int S) {
/*
ID => [Decrement : Increment]
0 => Decrement
1 => Increment
S => accompanies display shift
0 => OFF
1 => ON
*/
LCD_data = 0x00;
ID = ID << 1;
LCD_data = LCD_data + 0x04 + ID + S;
LCD_write_command();
}
void LCD_display_control (unsigned int D,unsigned int C,unsigned int B) {
/*
D => Display ON/OFF
0 => OFF
1 => ON
C => Cursor ON/OFF
0 => OFF
1 => ON
B => Blink the character at the cursor position
0 => OFF
1 => ON
*/
LCD_data = 0x00;
C = C << 1;
D = D << 2;
LCD_data = LCD_data + 0x08 + D + C + B;
LCD_write_command();
}
void LCD_cursor_display_shift (unsigned int SC,unsigned int RL) {
/*
Moves the cursor and shifts the display without changing the DDRAM contents
SC => [Cursor : Display]
0 => Cursor
1 => Display
RL => [Left : Right]
0 => Left
1 => Right
*/
LCD_data = 0x00;
RL = RL << 2;
SC = SC << 3;
LCD_data = LCD_data + 0x10 + SC + RL;
LCD_write_command();
}
void LCD_function_set (unsigned int DL,unsigned int N,unsigned int F) {
/*
DL => Data Width [4 bit : 8 bit]
0 => 4 bit
1 => 8 bit
N => Number of lines in display[1 : 2]
0 => 1 Line
1 => 2 Lines
F => Character font[5x7 : 5x10]
0 => 5x7
1 => 5x10
*/
LCD_data = 0x00;
F = F << 2;
N = N << 3;
DL = DL << 4;
LCD_data = LCD_data + 0x20 + DL + N + F;
LCD_write_command();
}
void LCD_cgram_address (unsigned int cgadrs) {
/*
cgadrs => CGRAM Address Binary 6 bit
i didn't use it yet
*/
LCD_data = 0x00;
LCD_data = cgadrs;
LCD_write_command();
}
void LCD_ddram_address (unsigned int ddadrs) {
/*
ddadrs => DDRAM Address Binary 7 bit
Line 1 Column 1
Binary Address : 0B10000000
HEX Address : 0X80
Line 1 Column 16
Binary Address : 0B10001111
HEX Address : 0X8F
Line 2 Column 1
Binary Address : 0B11000000
HEX Address : 0XC0
Line 2 Column 16
Binary Address : 0B11001111
HEX Address : 0XCF
[][][][][][][][][][][][][][][][]
[][][][][][][][][][][][][][][][]
C1 C2 C3 C4 C5 C6 C7 C8 C9 C10 C11 C12 C13 C14 C15 C16
L1 [0X80][0X81][0X82][0X83][0X84][0X85][0X86][0X87][0X88][0X89][0X8A][0X8B][0X8C][0X8D][0X8E][0X8F]
L2 [0XC0][0XC1][0XC2][0XC3][0XC4][0XC5][0XC6][0XC7][0XC8][0XC9][0XCA][0XCB][0XCC][0XCD][0XCE][0XCF]
*/
LCD_data = 0x00;
LCD_data = ddadrs;
LCD_write_command();
}
void LCD_write_data(unsigned int output) {
LCD_data = output; //
LCD_rs = 1; //Selected data register
LCD_rw = 0; //We are writing in data register
LCD_en = 1; //Enable H->L
LCD_en = 0;
LCD_busy();
}
/*void LCD_write_string(unsigned char output) {
LCD_data = output; //Entry mode, auto increment with no shift
LCD_rs = 1; //Selected data register
LCD_rw = 0; //We are writing in data register
LCD_en = 1; //Enable H->L
LCD_en = 0;
LCD_busy();
}*/
void LCD_init() {
LCD_function_set (1,1,0);//Function set: 2 Line, 8-bit, 5x7 dots
LCD_display_control (1,1,1);//Display on, Curson blinking command
LCD_clear_display();
LCD_entry_mode_set (1,0);//Entry mode, auto increment with no shift
LCD_ddram_address(0X80);//Line 1 Column 1
}
void main() {
TRISB = 0x00;
TRISC = 0x00;
LCD_init();
LCD_ddram_address(0X83);
//"All"
LCD_write_data(0x41);//'A'
LCD_write_data(0x6C);//'l'
LCD_write_data(0x6C);//'l'
//space
LCD_write_data(0x20);//space
//"About"
LCD_write_data(0x41);//'A'
LCD_write_data(0x62);//'b'
LCD_write_data(0x6F);//'o'
LCD_write_data(0x75);//'u'
LCD_write_data(0x74);//'t'
LCD_ddram_address(0XC4);
//"Circuits"
LCD_write_data(0x43);//'C'
LCD_write_data(0x69);//'i'
LCD_write_data(0x72);//'r'
LCD_write_data(0x63);//'c'
LCD_write_data(0x75);//'u'
LCD_write_data(0x69);//'i'
LCD_write_data(0x74);//'t'
LCD_write_data(0x73);//'s'
}
Attachments
-
174.3 KB Views: 95
Last edited by a moderator: