flitjes

lcd.c

Mar 4th, 2013
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.04 KB | None | 0 0
  1. /*
  2.  *  LCD interface example
  3.  *  Uses routines from delay.c
  4.  *  This code will interface to a standard LCD controller
  5.  *  like the Hitachi HD44780. It uses it in 4 bit mode, with
  6.  *  the hardware connected as follows (the standard 14 pin
  7.  *  LCD connector is used):
  8.  * 
  9.  *  PORTC bits 0-3 are connected to the LCD data bits 4-7 (high nibble)
  10.  *  PORTBA bit 4 is connected to the LCD RS input (register select)
  11.  *  PORTA bit 6 is connected to the LCD EN bit (enable)
  12.  * 
  13.  *  To use these routines, set up the port I/O (TRISA, TRISC) then
  14.  *  call lcd_init(), then other routines as required.
  15.  * 
  16.  */
  17.  
  18. #include    <pic.h>
  19. #include    "lcd.h"
  20. #include    "delay.h"
  21.  
  22. #define LCD_RS RA0
  23. #define LCD_RW RA1
  24. #define LCD_EN RA2
  25.  
  26. #define LCD_DATA    PORTC
  27.  
  28. #define LCD_STROBE()    ((LCD_EN = 1),(LCD_EN=0))
  29.  
  30. /* write a byte to the LCD in 4 bit mode */
  31.  
  32. void
  33. lcd_write(unsigned char c)
  34. {
  35.     DelayUs(40);
  36.     LCD_DATA = ( ( c >> 4 ) & 0x0F );
  37.     LCD_STROBE();
  38.     LCD_DATA = ( c & 0x0F );
  39.     LCD_STROBE();
  40. }
  41.  
  42. /*
  43.  *  Clear and home the LCD
  44.  */
  45.  
  46. void
  47. lcd_clear(void)
  48. {
  49.     LCD_RS = 0;
  50.     lcd_write(0x1);
  51.     DelayMs(2);
  52. }
  53.  
  54. /* write a string of chars to the LCD */
  55.  
  56. void
  57. lcd_puts(const char * s)
  58. {
  59.     LCD_RS = 1; // write characters
  60.     while(*s)
  61.         lcd_write(*s++);
  62. }
  63.  
  64. /* write one character to the LCD */
  65.  
  66. void
  67. lcd_putch(char c)
  68. {
  69.     LCD_RS = 1; // write characters
  70.     lcd_write( c );
  71. }
  72.  
  73.  
  74. /*
  75.  * Go to the specified position
  76.  */
  77.  
  78. void
  79. lcd_goto(unsigned char pos)
  80. {
  81.     LCD_RS = 0;
  82.     lcd_write(0x80+pos);
  83. }
  84.    
  85. /* initialise the LCD - put into 4 bit mode */
  86. void
  87. lcd_init()
  88. {
  89.     char init_value;
  90.     init_value = 0x3;
  91.     TRISA=0;
  92.     TRISC=0;
  93.  
  94.     LCD_RS = 0;
  95.     LCD_EN = 0;
  96.     LCD_RW = 0;
  97.    
  98.     DelayMs(15);    // wait 15mSec after power applied,
  99.     LCD_DATA     = init_value;
  100.     LCD_STROBE();
  101.     DelayMs(5);
  102.     LCD_STROBE();
  103.     DelayUs(200);
  104.     LCD_STROBE();
  105.     DelayUs(200);
  106.     LCD_DATA = 2;   // Four bit mode
  107.     LCD_STROBE();
  108.  
  109.     lcd_write(0x28); // Set interface length
  110.     lcd_write(0xF); // Display On, Cursor On, Cursor Blink
  111.     lcd_clear();    // Clear screen
  112.     lcd_write(0x6); // Set entry Mode
  113. }
Advertisement
Add Comment
Please, Sign In to add comment