Advertisement
Tempist

DriverModule

Oct 22nd, 2019
341
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.00 KB | None | 0 0
  1. ///////////////////////////////////////////////////////////////////////////
  2. ////                        DriverModule.c                             ////
  3. ////                 Driver for common LCD modules                     ////
  4. ////                                                                   ////
  5. ////  lcd_init()   Must be called before any other function.           ////
  6. ////                                                                   ////
  7. ////  lcd_putc(c)  Will display c on the next position of the LCD.     ////
  8. ////                     The following have special meaning:           ////
  9. ////                      \f  Clear display                            ////
  10. ////                      \n  Go to start of second line               ////
  11. ////                      \b  Move back one position                   ////
  12. ////                                                                   ////
  13. ////  lcd_gotoxy(x,y) Set write position on LCD (upper left is 1,1)    ////
  14. ////                                                                   ////
  15. ////  lcd_getc(x,y)   Returns character at position x,y on LCD         ////
  16. ////                                                                   ////
  17. ///////////////////////////////////////////////////////////////////////////
  18.  
  19. // As defined in the following structure the pin connection is as follows:
  20. //     D0  enable
  21. //     D1  rs
  22. //     D2  rw
  23. //     D4  D4
  24. //     D5  D5
  25. //     D6  D6
  26. //     D7  D7
  27. //
  28. //   LCD pins D0-D3 are not used and PIC D3 is not used.
  29.  
  30. // Un-comment the following define to use port B
  31. // #define use_portb_lcd TRUE
  32.  
  33.  
  34. struct lcd_pin_map {                 // This structure is overlayed
  35.            BOOLEAN enable;           // on to an I/O port to gain
  36.            BOOLEAN rs;               // access to the LCD pins.
  37.            BOOLEAN rw;               // The bits are allocated from
  38.            BOOLEAN unused;           // low order up.  ENABLE will
  39.            int     data : 4;         // be pin B0.
  40.         } lcd;
  41.  
  42.  
  43. #if defined use_portb_lcd
  44.    //#locate lcd = getenv("sfr:PORTB")    // This puts the entire structure over the port
  45.    #ifdef __pch__
  46.     #locate lcd = 0xf81
  47.    #else
  48.     #locate lcd = 6
  49.    #endif
  50.    #define set_tris_lcd(x) set_tris_b(x)
  51. #else
  52.    //#locate lcd = getenv("sfr:PORTD")    // This puts the entire structure over the port
  53.    #ifdef __pch__
  54.     #locate lcd = 0xf83
  55.    #else
  56.     #locate lcd = 8
  57.    #endif
  58.    #define set_tris_lcd(x) set_tris_d(x)
  59. #endif
  60.  
  61. #ifndef lcd_type
  62. #define lcd_type 2           // 0=5x7, 1=5x10, 2=2 lines
  63. #endif
  64.  
  65. #define lcd_line_two 0x40    // LCD RAM address for the second line
  66.  
  67.  
  68. BYTE const LCD_INIT_STRING[4] = {0x20 | (lcd_type << 2), 0xc, 1, 6};
  69.                              // These bytes need to be sent to the LCD
  70.                              // to start it up.
  71.  
  72.  
  73.                              // The following are used for setting
  74.                              // the I/O port direction register.
  75.  
  76. struct lcd_pin_map const LCD_WRITE = {0,0,0,0,0}; // For write mode all pins are out
  77. struct lcd_pin_map const LCD_READ = {0,0,0,0,15}; // For read mode data pins are in
  78.  
  79.  
  80.  
  81. BYTE lcd_read_byte() {
  82.       BYTE low,high;
  83.       set_tris_lcd(LCD_READ);
  84.       lcd.rw = 1;
  85.       delay_cycles(1);
  86.       lcd.enable = 1;
  87.       delay_cycles(1);
  88.       high = lcd.data;
  89.       lcd.enable = 0;
  90.       delay_cycles(1);
  91.       lcd.enable = 1;
  92.       delay_us(1);
  93.       low = lcd.data;
  94.       lcd.enable = 0;
  95.       set_tris_lcd(LCD_WRITE);
  96.       return( (high<<4) | low);
  97. }
  98.  
  99.  
  100. void lcd_send_nibble( BYTE n ) {
  101.       lcd.data = n;
  102.       delay_cycles(1);
  103.       lcd.enable = 1;
  104.       delay_us(2);
  105.       lcd.enable = 0;
  106. }
  107.  
  108.  
  109. void lcd_send_byte( BYTE address, BYTE n ) {
  110.  
  111.       lcd.rs = 0;
  112.       while ( bit_test(lcd_read_byte(),7) ) ;
  113.       lcd.rs = address;
  114.       delay_cycles(1);
  115.       lcd.rw = 0;
  116.       delay_cycles(1);
  117.       lcd.enable = 0;
  118.       lcd_send_nibble(n >> 4);
  119.       lcd_send_nibble(n & 0xf);
  120. }
  121.  
  122.  
  123. void lcd_init() {
  124.     BYTE i;
  125.     set_tris_lcd(LCD_WRITE);
  126.     lcd.rs = 0;
  127.     lcd.rw = 0;
  128.     lcd.enable = 0;
  129.     delay_ms(15);
  130.     for(i=1;i<=3;++i) {
  131.        lcd_send_nibble(3);
  132.        delay_ms(5);
  133.     }
  134.     lcd_send_nibble(2);
  135.     for(i=0;i<=3;++i)
  136.        lcd_send_byte(0,LCD_INIT_STRING[i]);
  137. }
  138.  
  139.  
  140. void lcd_gotoxy( BYTE x, BYTE y) {
  141.    BYTE address;
  142.  
  143.    if(y!=1)
  144.      address=lcd_line_two;
  145.    else
  146.      address=0;
  147.    address+=x-1;
  148.    lcd_send_byte(0,0x80|address);
  149. }
  150.  
  151. void lcd_putc( char c) {
  152.    switch (c) {
  153.      case '\f'   : lcd_send_byte(0,1);
  154.                    delay_ms(2);
  155.                                            break;
  156.      case '\n'   : lcd_gotoxy(1,2);        break;
  157.      case '\b'   : lcd_send_byte(0,0x10);  break;
  158.      default     : lcd_send_byte(1,c);     break;
  159.    }
  160. }
  161.  
  162. char lcd_getc( BYTE x, BYTE y) {
  163.    char value;
  164.  
  165.     lcd_gotoxy(x,y);
  166.     while ( bit_test(lcd_read_byte(),7) ); // wait until busy flag is low
  167.     lcd.rs=1;
  168.     value = lcd_read_byte();
  169.     lcd.rs=0;
  170.     return(value);
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement