/* * use only lcd_init()--- to init LCD lcd_prit()- to display a variable */ #include "lcd.h" //Function to Reset LCD void lcd_set_4bit() { _delay_ms(1); cbit(lcd_PORT,RS); //RS=0 --- Command Input cbit(lcd_PORT,RW); //RW=0 --- Writing to LCD lcd_PORT = 0x30; //Sending 3 in the upper nibble sbit(lcd_PORT,EN); //Set Enable Pin _delay_ms(5); //delay cbit(lcd_PORT,EN); //Clear Enable Pin _delay_ms(1); cbit(lcd_PORT,RS); //RS=0 --- Command Input cbit(lcd_PORT,RW); //RW=0 --- Writing to LCD lcd_PORT = 0x30; //Sending 3 in the upper nibble sbit(lcd_PORT,EN); //Set Enable Pin _delay_ms(5); //delay cbit(lcd_PORT,EN); //Clear Enable Pin _delay_ms(1); cbit(lcd_PORT,RS); //RS=0 --- Command Input cbit(lcd_PORT,RW); //RW=0 --- Writing to LCD lcd_PORT = 0x30; //Sending 3 in the upper nibble sbit(lcd_PORT,EN); //Set Enable Pin _delay_ms(5); //delay cbit(lcd_PORT,EN); //Clear Enable Pin _delay_ms(1); cbit(lcd_PORT,RS); //RS=0 --- Command Input cbit(lcd_PORT,RW); //RW=0 --- Writing to LCD lcd_PORT = 0x20; //Sending 2 in the upper nibble to initialize LCD 4-bit mode sbit(lcd_PORT,EN); //Set Enable Pin _delay_ms(5); //delay cbit(lcd_PORT,EN); //Clear Enable Pin } //------------LCD init-------------------------- void display_clear(void) { lcd_wr_command(0x01); } void lcd_home() { lcd_wr_command(0x80); } //Position the LCD cursor at "row", "column" void lcd_cursor (char row, char column) { switch (row) { case 1: lcd_wr_command (0x80 + column - 1); break; case 2: lcd_wr_command (0xc0 + column - 1); break; case 3: lcd_wr_command (0x94 + column - 1); break; case 4: lcd_wr_command (0xd4 + column - 1); break; default: break; } } void lcd_PORT_config (void) { lcd_DDR = DDRC | 0xF7; // all the LCD pin's direction set as output lcd_PORT = PORTC & 0x80; // all the LCD pins are set to logic 0 except PORTC 7 } //Function to Initialize LCD void lcd_init() { lcd_PORT_config(); lcd_set_4bit(); _delay_ms(1); lcd_wr_command(0x28); //4-bit mode and 5x8 dot character font lcd_wr_command(0x01); //Clear LCD display lcd_wr_command(0x06); //Auto increment cursor position lcd_wr_command(0x0E); //Turn on LCD and cursor lcd_wr_command(0x80); //Set cursor position } //----------------basic LCD function--------------------------------------------------- //Function to print any input value up to the desired digit on LCD void lcd_print (char row, char coloumn, unsigned int value, int digits) { unsigned char flag=0; if(row==0||coloumn==0) { lcd_home(); } else { lcd_cursor(row,coloumn); } if(digits==5 || flag==1) { million=value/10000+48; lcd_wr_char(million); flag=1; } if(digits==4 || flag==1) { temp = value/1000; thousand = temp%10 + 48; lcd_wr_char(thousand); flag=1; } if(digits==3 || flag==1) { temp = value/100; hundred = temp%10 + 48; lcd_wr_char(hundred); flag=1; } if(digits==2 || flag==1) { temp = value/10; tens = temp%10 + 48; lcd_wr_char(tens); flag=1; } if(digits==1 || flag==1) { unit = value%10 + 48; lcd_wr_char(unit); } if(digits>5) { lcd_wr_char('E'); } } //Function to Print String on LCD void lcd_string(char *str) { while(*str != '\0') { lcd_wr_char(*str); str++; } } //Function to write data on LCD void lcd_wr_char(char letter) { char temp; temp = letter; temp = (temp & 0xF0); lcd_PORT &= 0x0F; lcd_PORT |= temp; sbit(lcd_PORT,RS); cbit(lcd_PORT,RW); sbit(lcd_PORT,EN); _delay_ms(5); cbit(lcd_PORT,EN); letter = letter & 0x0F; letter = letter<<4; lcd_PORT &= 0x0F; lcd_PORT |= letter; sbit(lcd_PORT,RS); cbit(lcd_PORT,RW); sbit(lcd_PORT,EN); _delay_ms(5); cbit(lcd_PORT,EN); } //Function to write command on LCD void lcd_wr_command(unsigned char cmd) { unsigned char temp; temp = cmd; temp = temp & 0xF0; lcd_PORT &= 0x0F; lcd_PORT |= temp; cbit(lcd_PORT,RS); cbit(lcd_PORT,RW); sbit(lcd_PORT,EN); _delay_ms(5); cbit(lcd_PORT,EN); cmd = cmd & 0x0F; cmd = cmd<<4; lcd_PORT &= 0x0F; lcd_PORT |= cmd; cbit(lcd_PORT,RS); cbit(lcd_PORT,RW); sbit(lcd_PORT,EN); _delay_ms(5); cbit(lcd_PORT,EN); }