Advertisement
Guest User

LCD1602.h

a guest
Oct 18th, 2017
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.67 KB | None | 0 0
  1. #ifndef LCD1602_H
  2. #define LCD1602_H
  3.  
  4. #include <delay.h>
  5.  
  6. //* Define port masks like (1 << 3) for GPIO_PIN_3.
  7. #define RS     GPIO_PIN_5
  8. #define RnW    GPIO_PIN_6
  9. #define E      GPIO_PIN_7
  10. #define DB7    GPIO_PIN_7
  11. #define DB6    GPIO_PIN_6
  12. #define DB5    GPIO_PIN_5
  13. #define DB4    GPIO_PIN_0
  14.  
  15. //* Define port names like (GPIOA) for port A.
  16. #define RS_PORT           GPIOC
  17. #define RnW_PORT          GPIOC
  18. #define E_PORT            GPIOC
  19. #define DATA_PINS_PORT    GPIOD
  20. #define DB7_PORT          GPIOD
  21. #define DB6_PORT          GPIOD
  22. #define DB5_PORT          GPIOD
  23. #define DB4_PORT          GPIOD
  24.  
  25.  
  26. #define DATA_PINS ((DB7)|(DB6)|(DB5)|(DB4))
  27. // #define DATA_PINS ((DB7)|(DB6)|(DB5)|(DB4))
  28. // #define DATA_PINS_OFFSET 4
  29.  
  30. #define PIN_LOW(X)    X##_PORT->ODR &= ~X
  31. #define PIN_HIGH(X)   X##_PORT->ODR |=  X
  32.  
  33. #define PIN_OUTPUT(X) X##_PORT->DDR |=  X
  34. #define PIN_INPUT(X)  X##_PORT->DDR &= ~X
  35.  
  36. #define PIN_READ(PIN_NAME) (PIN_NAME##_PORT->IDR & (PIN_NAME))
  37.  
  38. // #define LCD_CMD00_CLEAR
  39. // #define LCD_CMD01_RETURN
  40. // #define LCD_CMD02_ENTRY_MODESET
  41. // #define LCD_CMD02_INC_nDEC
  42. // #define LCD_CMD02_SHIFT_DISP_nCURSOR
  43. // #define LCD_CMD03_DISP_ON_OFF_CTL
  44. // #define LCD_CMD03_DISPLAY
  45. // #define LCD_CMD03_CURSOR
  46. // #define LCD_CMD03_BLINK
  47. // #define LCD_CMD04_DISP_CURSOR_SHIFT
  48. // #define LCD_CMD04_SCREEN_CURSOR
  49. // #define LCD_CMD04_RIGHT_LEFT
  50. // #define LCD_CMD05_FUNCT_SET
  51. // #define LCD_CMD05_DATA_LENGTH
  52. // #define LCD_CMD05_LINES_NUM
  53. // #define LCD_CMD05_FONT
  54. // #define LCD_CMD0
  55. // #define LCD_CMD0
  56. // #define LCD_CMD0
  57. // #define LCD_CMD0
  58. // #define LCD_CMD0
  59. // #define LCD_CMD0
  60. // #define LCD_CMD0
  61. #define LCD_CMD  0
  62. #define LCD_DATA 1
  63.  
  64.  
  65. // Using project specific pin assigment
  66. uint8_t read_data_pins()
  67. {
  68. //   return (((DATA_PINS_PORT->IDR & DATA_PINS) >> DATA_PINS_OFFSET) & 0x0F);
  69.     uint8_t tmp;
  70.    
  71.     tmp = DATA_PINS_PORT->IDR & DATA_PINS;
  72.     tmp = (tmp >> 4)|(tmp & 0x01);
  73.    
  74.     return tmp;
  75. }
  76.  
  77. // Using project specific pin assigment
  78. void write_data_pins(uint8_t data)
  79. {
  80. //   DATA_PINS_PORT->ODR = (DATA_PINS_PORT->ODR & ~DATA_PINS)|(data << DATA_PINS_OFFSET);
  81.     uint8_t tmp;
  82.    
  83.     tmp = ((data << 4) & 0xE0)|(data & 0x01);
  84.    
  85.     __critical
  86.     {
  87.         DATA_PINS_PORT->ODR = (DATA_PINS_PORT->ODR & ~DATA_PINS)|(tmp);
  88.     }
  89. }
  90.  
  91. uint8_t read_nibble()
  92. {
  93.   uint8_t nibble;
  94.  
  95.   PIN_HIGH(E);
  96.   __asm__("nop;\nnop;\n");
  97.   nibble = read_data_pins();
  98.   PIN_LOW(E);
  99.   __asm__("nop;\nnop;\n");
  100.  
  101.   return nibble;
  102. }
  103.  
  104. void write_nibble(uint8_t nibble)
  105. {  
  106.   PIN_HIGH(E);
  107.   write_data_pins(nibble & 0x0F);  
  108.   __asm__("nop;\nnop;\n");
  109.   PIN_LOW(E);
  110.   __asm__("nop;\nnop;\n");
  111. }
  112.  
  113.  
  114. uint8_t read_byte(uint8_t is_data)
  115. {
  116.   uint8_t byte;
  117.  
  118.   PIN_LOW(E);
  119.   PIN_HIGH(RnW);
  120.   if (is_data)
  121.     {PIN_HIGH(RS);}
  122.   else
  123.     {PIN_LOW(RS);}
  124.   PIN_INPUT(DATA_PINS);
  125.  
  126.   byte  = read_nibble() << 4;
  127.   byte |= read_nibble();
  128.  
  129.   return byte;
  130. }
  131.  
  132. void write_byte(uint8_t byte, uint8_t is_data)
  133. {
  134.   PIN_LOW(E);
  135.   PIN_LOW(RnW);
  136.   (is_data) ? (PIN_HIGH(RS)) : (PIN_LOW(RS));
  137.   PIN_OUTPUT(DATA_PINS);
  138.  
  139.   write_nibble(byte >> 4);
  140.   write_nibble(byte & 0x0F);
  141. }
  142.  
  143. uint8_t lcd_is_busy()
  144. {
  145. //   _delay_ms(1); // Needed by ST7066 LCD controller
  146.   return (read_byte(LCD_CMD) & 0x80) ? (0xFF) : (0x00);
  147. }
  148.  
  149. void LCD1602_Init()
  150. {
  151.   PIN_LOW(RnW);
  152.   PIN_OUTPUT(DATA_PINS);
  153.   _delay_ms(40);
  154.   write_nibble(0x03);
  155.   _delay_ms(5);
  156.   write_nibble(0x03);
  157.   _delay_ms(5);
  158.   write_nibble(0x03);
  159.   _delay_ms(1);
  160.  
  161.   write_nibble(0x02);// LCD 4bit.
  162.   _delay_ms(1);
  163.   write_nibble(0x02);
  164.   _delay_ms(1);
  165.   write_nibble(0x08);
  166.   _delay_ms(1);
  167.   write_nibble(0x00);
  168.   _delay_ms(1);
  169.   write_nibble(0x08|0x04);
  170.   _delay_ms(1);
  171.   write_nibble(0x00);
  172.   _delay_ms(1);
  173.   write_nibble(0x01);
  174.   _delay_ms(1);
  175.   write_nibble(0x00);
  176.   _delay_ms(1);
  177.   write_nibble(0x06);
  178.   _delay_ms(1);
  179. //   while (lcd_is_busy());
  180. //   write_byte(0x28, LCD_CMD); // LCD 4bit, 2 lines, 5x8 font
  181. //   _delay_ms(1);
  182. //   while (lcd_is_busy());
  183. //   write_byte(0x08, LCD_CMD); // Display ON
  184. //   _delay_ms(1);
  185. //   while (lcd_is_busy());
  186. //   write_byte(0x01, LCD_CMD); // Display Clear
  187. //   _delay_ms(1);
  188. //   while (lcd_is_busy());
  189. //   write_byte(0b0110, LCD_CMD); // Entry Mode Set: increment addr, don't shift display
  190. //   _delay_ms(1);
  191. //   while (lcd_is_busy());
  192. }
  193.  
  194. void LCD_SetXY(uint8_t x, uint8_t y)
  195. {
  196.   while (lcd_is_busy());
  197.   write_byte(0b10000000|((0x40*y) + x), LCD_CMD);
  198. }
  199.  
  200. void LCD_PrintString(char *str)
  201. {
  202.   while(*str != 0)
  203.   {
  204.     while(lcd_is_busy());
  205.     write_byte(*str, LCD_DATA);
  206.     str++;
  207.   }
  208. }
  209.  
  210. void LCD_Clear()
  211. {
  212.     LCD_SetXY(0,0);
  213.     LCD_PrintString("                ");
  214.     LCD_SetXY(0,1);
  215.     LCD_PrintString("                ");
  216. }
  217.  
  218.  
  219. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement