sp3ctrm5tr

HD44780 Dot Matrix LCD Controller/Driver, PIC16F877A

Apr 6th, 2012
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.53 KB | None | 0 0
  1. /*------------------------------------------
  2. LCD library 4 bit data bus. HD44780 Dot
  3. Matrix LCD Controller/river
  4.  
  5. -------------------------------------------*/
  6. #define LCD_RS      RD1
  7. #define LCD_RW      RD2
  8. #define LCD_EN      RD0
  9. #define LCD_LIGHT   RD3
  10. #define LCD_DATA    PORTD       //D7-D4
  11. #define LCD_PULSE lcd_pulse
  12. #define _XTAL_FREQ  20000000
  13. #define FUNCTION_SET_8_BIT  0b00110000
  14. #define FUNCTION_SET_4_BIT  0b00100000
  15. #define DISPLAY_CLEAR   0b00000001
  16. #define __delay_cycles _delay  
  17. void LCD_command(unsigned char command);
  18. void LCD_data(unsigned char data);
  19. void lcd_init(void);
  20. void clear_LCD();
  21. void display_LCD(char line_number,char position, unsigned char *message);
  22. void lcd_putch (unsigned char c);
  23. void lcd_putc (unsigned char *s1);
  24. void lcd_string (unsigned char *s1);
  25. void lcd_gotoxy(char line_number,char position);
  26. void LCD_delay (void);
  27.  
  28. void LCD_command(unsigned char command)
  29. {  
  30.     unsigned char temp;
  31.     LCD_RS = 0;  //Pin RS should be LOW for sending command
  32.  
  33.     // To send MSB part of command
  34.     temp = (command & 0xf0); //[(0x38 & 0xf0)= 0x30 ==> 0x30]
  35.     LCD_DATA &= 0x0F;
  36.     LCD_DATA |= temp;
  37.     LCD_EN = 1;
  38.     __delay_us(2);
  39.     LCD_EN = 0;
  40.     LCD_delay();
  41.  
  42.     temp = ((command & 0x0f)<<4); //[(0x38 & 0x0f)= 0x08<<4 ==> 0x80]
  43.     LCD_DATA &= 0x0F;
  44.     LCD_DATA |= temp;
  45.     LCD_EN = 1;
  46.     __delay_us(2);
  47.     LCD_EN = 0;
  48.     __delay_ms(1);
  49. }
  50.  
  51. void LCD_data(unsigned char data)
  52. {  
  53.     unsigned char temp;
  54.     LCD_RS=1; //Pin RS should be HIGH for sending data
  55.  
  56.     temp =(data & 0xf0);
  57.     LCD_DATA &= 0x0F;
  58.     LCD_DATA |= temp;
  59.     LCD_EN = 1;
  60.     __delay_us(2);
  61.     LCD_EN = 0;
  62.    
  63.     temp = ((data & 0x0f)<<4);
  64.     LCD_DATA &= 0x0F;
  65.     LCD_DATA |= temp;
  66.     LCD_EN = 1;
  67.     __delay_us(2);
  68.     LCD_EN = 0;
  69.     __delay_ms(1);
  70. }
  71.  
  72. void lcd_init(void)
  73. {  
  74.     char a;
  75.     TRISD = 0x00;
  76.     LCD_DATA = 0x00; //DATA or COMMAND i/p to LCD
  77.     //CONTROL i/p to LCD
  78.  
  79.     LCD_EN=0;
  80.     LCD_RW=0;
  81.     __delay_ms(50);
  82.     LCD_DATA|=0x30;
  83.     LCD_RS=0;
  84.     LCD_EN=1;
  85.     for (a=0;a<=3;a++)
  86.     {  
  87.         LCD_EN=1;
  88.         __delay_ms(1);
  89.         LCD_EN=0;
  90.         __delay_ms(1);
  91.     }
  92.     LCD_DATA = LCD_DATA & 0x0F;
  93.     LCD_DATA = LCD_DATA | 0x20;
  94.     __delay_ms(50);
  95.     LCD_EN = 0;
  96.     LCD_EN = 1;
  97.     __delay_ms(50);
  98.     LCD_EN = 0;
  99.  
  100.     __delay_ms(2);
  101.     LCD_command(0x28);  //set 4-bit mode and 2 lines
  102.     __delay_ms(2);
  103.     LCD_command(0x10);  //cursor move & shift left
  104.     __delay_ms(2);
  105.     LCD_command(0x06);  //entry mode = increment
  106.     __delay_ms(2);
  107.     LCD_command(0x0c);  //display on - cursor blink on
  108.     __delay_ms(2);
  109.     LCD_command(0x01);  //clear display
  110.     __delay_ms(2);
  111.     __delay_ms(1);
  112. }
  113.  
  114. void clear_LCD( )
  115. {  
  116.     LCD_command(0x01);
  117.     __delay_ms(1);
  118. }
  119.  
  120. void display_LCD(char line_number,char position, unsigned char *message)
  121. {  
  122.     int a;
  123.     if(line_number==1)
  124.     {  
  125.         a=0x80+position;
  126.         LCD_command(a);
  127.     }
  128.     else if(line_number==2)
  129.     {  
  130.         a=0xC0+position;
  131.         LCD_command(a);
  132.     }
  133.     while(*message!=0)
  134.     {   //printf (" %c", *message);
  135.         LCD_data(*message);
  136.         __delay_ms(15);
  137.         message++;
  138.     }
  139. }
  140.  
  141. void lcd_putch (unsigned char c)
  142. {  
  143.     if (c == '\f')
  144.     {  
  145.         clear_LCD( ); //Clear LCD
  146.     }
  147.     else if (c == '\n')
  148.     {  
  149.         LCD_command (0XC0); //New Line
  150.     }
  151.     else
  152.     {  
  153.         LCD_data( c); //Show the character
  154.     }  
  155. }
  156.  
  157. void lcd_putc (unsigned char *s1)
  158. {  
  159.     while (*s1!=0)
  160.     {  
  161.         lcd_putch( *s1);
  162.         __delay_ms(5);
  163.         s1++;      
  164.     }
  165. }
  166.  
  167. void lcd_string (unsigned char *s1)
  168. {  
  169.     lcd_putc (*s1);
  170. }
  171.  
  172. void lcd_gotoxy(char line_number,char position)
  173. {  
  174.     int a;
  175.     if(line_number==1)
  176.     {  
  177.         a=0x80+position;
  178.         LCD_command(a);
  179.     }
  180.     else if(line_number==2)
  181.     {  
  182.         a=0xC0+position;
  183.         LCD_command(a);
  184.     }  
  185. }
  186.  
  187. void LCD_delay (void)
  188. {
  189.     int t;
  190.     for (t=0; t<250; t++);
  191. }
Advertisement
Add Comment
Please, Sign In to add comment