Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*------------------------------------------
- LCD library 4 bit data bus. HD44780 Dot
- Matrix LCD Controller/river
- -------------------------------------------*/
- #define LCD_RS RD1
- #define LCD_RW RD2
- #define LCD_EN RD0
- #define LCD_LIGHT RD3
- #define LCD_DATA PORTD //D7-D4
- #define LCD_PULSE lcd_pulse
- #define _XTAL_FREQ 20000000
- #define FUNCTION_SET_8_BIT 0b00110000
- #define FUNCTION_SET_4_BIT 0b00100000
- #define DISPLAY_CLEAR 0b00000001
- #define __delay_cycles _delay
- void LCD_command(unsigned char command);
- void LCD_data(unsigned char data);
- void lcd_init(void);
- void clear_LCD();
- void display_LCD(char line_number,char position, unsigned char *message);
- void lcd_putch (unsigned char c);
- void lcd_putc (unsigned char *s1);
- void lcd_string (unsigned char *s1);
- void lcd_gotoxy(char line_number,char position);
- void LCD_delay (void);
- void LCD_command(unsigned char command)
- {
- unsigned char temp;
- LCD_RS = 0; //Pin RS should be LOW for sending command
- // To send MSB part of command
- temp = (command & 0xf0); //[(0x38 & 0xf0)= 0x30 ==> 0x30]
- LCD_DATA &= 0x0F;
- LCD_DATA |= temp;
- LCD_EN = 1;
- __delay_us(2);
- LCD_EN = 0;
- LCD_delay();
- temp = ((command & 0x0f)<<4); //[(0x38 & 0x0f)= 0x08<<4 ==> 0x80]
- LCD_DATA &= 0x0F;
- LCD_DATA |= temp;
- LCD_EN = 1;
- __delay_us(2);
- LCD_EN = 0;
- __delay_ms(1);
- }
- void LCD_data(unsigned char data)
- {
- unsigned char temp;
- LCD_RS=1; //Pin RS should be HIGH for sending data
- temp =(data & 0xf0);
- LCD_DATA &= 0x0F;
- LCD_DATA |= temp;
- LCD_EN = 1;
- __delay_us(2);
- LCD_EN = 0;
- temp = ((data & 0x0f)<<4);
- LCD_DATA &= 0x0F;
- LCD_DATA |= temp;
- LCD_EN = 1;
- __delay_us(2);
- LCD_EN = 0;
- __delay_ms(1);
- }
- void lcd_init(void)
- {
- char a;
- TRISD = 0x00;
- LCD_DATA = 0x00; //DATA or COMMAND i/p to LCD
- //CONTROL i/p to LCD
- LCD_EN=0;
- LCD_RW=0;
- __delay_ms(50);
- LCD_DATA|=0x30;
- LCD_RS=0;
- LCD_EN=1;
- for (a=0;a<=3;a++)
- {
- LCD_EN=1;
- __delay_ms(1);
- LCD_EN=0;
- __delay_ms(1);
- }
- LCD_DATA = LCD_DATA & 0x0F;
- LCD_DATA = LCD_DATA | 0x20;
- __delay_ms(50);
- LCD_EN = 0;
- LCD_EN = 1;
- __delay_ms(50);
- LCD_EN = 0;
- __delay_ms(2);
- LCD_command(0x28); //set 4-bit mode and 2 lines
- __delay_ms(2);
- LCD_command(0x10); //cursor move & shift left
- __delay_ms(2);
- LCD_command(0x06); //entry mode = increment
- __delay_ms(2);
- LCD_command(0x0c); //display on - cursor blink on
- __delay_ms(2);
- LCD_command(0x01); //clear display
- __delay_ms(2);
- __delay_ms(1);
- }
- void clear_LCD( )
- {
- LCD_command(0x01);
- __delay_ms(1);
- }
- void display_LCD(char line_number,char position, unsigned char *message)
- {
- int a;
- if(line_number==1)
- {
- a=0x80+position;
- LCD_command(a);
- }
- else if(line_number==2)
- {
- a=0xC0+position;
- LCD_command(a);
- }
- while(*message!=0)
- { //printf (" %c", *message);
- LCD_data(*message);
- __delay_ms(15);
- message++;
- }
- }
- void lcd_putch (unsigned char c)
- {
- if (c == '\f')
- {
- clear_LCD( ); //Clear LCD
- }
- else if (c == '\n')
- {
- LCD_command (0XC0); //New Line
- }
- else
- {
- LCD_data( c); //Show the character
- }
- }
- void lcd_putc (unsigned char *s1)
- {
- while (*s1!=0)
- {
- lcd_putch( *s1);
- __delay_ms(5);
- s1++;
- }
- }
- void lcd_string (unsigned char *s1)
- {
- lcd_putc (*s1);
- }
- void lcd_gotoxy(char line_number,char position)
- {
- int a;
- if(line_number==1)
- {
- a=0x80+position;
- LCD_command(a);
- }
- else if(line_number==2)
- {
- a=0xC0+position;
- LCD_command(a);
- }
- }
- void LCD_delay (void)
- {
- int t;
- for (t=0; t<250; t++);
- }
Advertisement
Add Comment
Please, Sign In to add comment