Advertisement
miszczo

[C] AVR HD44780 LIBRARY

Dec 4th, 2014
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.54 KB | None | 0 0
  1. /*
  2.  * hd44780.h
  3.  *
  4.  *  Created on: 20 lis 2014
  5.  *      Author: Administrator
  6.  */
  7.  
  8. #ifndef HD44780_H_
  9. #define HD44780_H_
  10.  
  11. // TO:DO RW PIN , 8BIT MODE , USER DEFCHAR
  12.  
  13. //*******************************************************************************************//
  14. //                      SET LCD CONNECTIONS TO MICROCONTROLLER                               //
  15. //                      AND COLUMNS AND ROWS VALUES                                          //
  16. //   ______________________________________________________________                          //
  17. //  |   1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16                     |    1. GND      9.  D2   //
  18. //  |    ______________________________________________________    |    2. VCC      10. D3   //
  19. //  |   |                                                      |   |    3. BIAS     11. D4   //
  20. //  |   |                 TYPICAL LCD HD44780                  |   |    4. RS       12. D5   //
  21. //  |   |                                                      |   |    5. RW       13. D6   //
  22. //  |   |                 DISPLAY CONNECTIONS                  |   |    6. E        14. D7   //
  23. //  |   |______________________________________________________|   |    7. D0       15. LED+ //
  24. //  |______________________________________________________________|    8. D1       16. LED- //
  25. //                                                                                           //
  26. //*******************************************************************************************//
  27. // COLUMNS AND ROWS
  28. #define HD_COLUMNS 16
  29. #define HD_ROWS 2
  30. // CONNECTION LCD TO UC         __
  31. #define LCD_E_PORT B        // |__
  32. #define LCD_E_PIN 0         // |__
  33.                             //  __   __
  34. #define LCD_RS_PORT B       // |__| |__
  35. #define LCD_RS_PIN 1        // | \   __|
  36.                             //  _
  37. #define LCD_D4_PORT B       // | \ /_|
  38. #define LCD_D4_PIN 2        // |_/   |
  39.                             //  _   __
  40. #define LCD_D5_PORT B       // | \ |__
  41. #define LCD_D5_PIN 3        // |_/  __|
  42.                             //  _   __
  43. #define LCD_D6_PORT B       // | \ |__
  44. #define LCD_D6_PIN 4        // |_/ |__|
  45.                             //  _  ___
  46. #define LCD_D7_PORT B       // | \   /
  47. #define LCD_D7_PIN 5        // |_/  /
  48. //*******************************************************************************************//
  49.  
  50.  
  51. //*******************************************************************************************//
  52. //                              DEFINE USES FUNCTION                                         //
  53. //*******************************************************************************************//
  54. #define USE_HD_CHAR             1       //
  55. #define USE_HD_STRING           1       //  from ram
  56. #define USE_HD_FLASH_STRING     0       //  from flash
  57. #define USE_HD_EEPROM_STRING    1       //  from eeprom
  58.  
  59. #define USE_HD_INT              1
  60. #define USE_HD_HEX              0
  61. #define USE_HD_BIN              0
  62.  
  63.  
  64.  
  65. //*******************************************************************************************//
  66. //                              OTHER DEFINITION , DONT TOUCH                                //
  67. //*******************************************************************************************//
  68. #if USE_HD_INT == 1 || USE_HD_HEX == 1 || USE_HD_BIN == 1
  69. #define USE_HD_STRING 1
  70. #endif
  71.  
  72.  
  73. #if USE_HD_STRING == 1  || USE_HD_EEPROM_STRING == 1 || USE_HD_FLASH_STRING == 1
  74. #define USE_HD_CHAR 1
  75. #endif
  76.  
  77. #define GLUE(a , b) a##b
  78.  
  79. //E wire
  80. //DDR
  81. #define LCD_DDR_E_S(s)          GLUE(DDR,s)
  82. #define LCD_DDR_E               LCD_DDR_E_S(LCD_E_PORT)         // DDRX in LCD_DDR_E definition
  83. //PORT
  84. #define LCD_PORT_E_S(s)         GLUE(PORT,s)
  85. #define LCD_PORT_E              LCD_PORT_E_S(LCD_E_PORT)        // PORTX in LCD_PORT_E definition
  86. //PIN
  87. #define LCD_PIN_E_S(s)          GLUE(PIN,s)
  88. #define LCD_PIN_E               LCD_PIN_E_S(LCD_E_PORT)         // PORTX in LCD_PORT_E definition
  89. //other for E wire
  90. #define E_HIGH                  LCD_PORT_E|=(1<<LCD_E_PIN)      // set E pin high
  91. #define E_LOW                   LCD_PORT_E&=~(1<<LCD_E_PIN)     // set E pin low
  92. #define E_IN                    LCD_DDR_E&=~(1<<LCD_E_PIN)      // set E pin as input
  93. #define E_OUT                   LCD_DDR_E|=(1<<LCD_E_PIN)       // set E pin as output
  94. #define E_STATE                 (LCD_PIN_E|(1<<LCD_E_PIN)       // get state E pin
  95.  
  96. //RS wire (comments like E wire)
  97. #define LCD_DDR_RS_S(s)         GLUE(DDR,s)
  98. #define LCD_DDR_RS              LCD_DDR_RS_S(LCD_RS_PORT)
  99.  
  100. #define LCD_PORT_RS_S(s)        GLUE(PORT,s)
  101. #define LCD_PORT_RS             LCD_PORT_RS_S(LCD_RS_PORT)
  102.  
  103. #define LCD_PIN_RS_S(s)         GLUE(PIN,s)
  104. #define LCD_PIN_RS              LCD_PIN_RS_S(LCD_RS_PORT)
  105.  
  106. #define RS_HIGH                 LCD_PORT_RS|=(1<<LCD_RS_PIN)
  107. #define RS_LOW                  LCD_PORT_RS&=~(1<<LCD_RS_PIN)
  108. #define RS_IN                   LCD_DDR_RS&=~(1<<LCD_RS_PIN)
  109. #define RS_OUT                  LCD_DDR_RS|=(1<<LCD_RS_PIN)
  110. #define RS_STATE                (LCD_PIN_RS|(1<<LCD_RS_PIN)
  111.  
  112. //RW wire (comments like E wire)
  113. #define LCD_DDR_RW_S(s)         GLUE(DDR,s)
  114. #define LCD_DDR_RW              LCD_DDR_RW_S(LCD_RW_PORT)
  115.  
  116. #define LCD_PORT_RW_S(s)        GLUE(PORT,s)
  117. #define LCD_PORT_RW             LCD_PORT_RW_S(LCD_RW_PORT)
  118.  
  119. #define LCD_PIN_RW_S(s)         GLUE(PIN,s)
  120. #define LCD_PIN_RW              LCD_PIN_RW_S(LCD_RW_PORT)
  121.  
  122. #define RW_HIGH                 LCD_PORT_RW|=(1<<LCD_RW_PIN)
  123. #define RW_LOW                  LCD_PORT_RW&=~(1<<LCD_RW_PIN)
  124. #define RW_IN                   LCD_DDR_RW&=~(1<<LCD_RW_PIN)
  125. #define RW_OUT                  LCD_DDR_RW|=(1<<LCD_RW_PIN)
  126. #define RW_STATE                (LCD_PIN_RW|(1<<LCD_RW_PIN)
  127.  
  128. //D4 wire (comments like E wire)
  129. #define LCD_DDR_D4_S(s)         GLUE(DDR,s)
  130. #define LCD_DDR_D4              LCD_DDR_D4_S(LCD_D4_PORT)
  131.  
  132. #define LCD_PORT_D4_S(s)        GLUE(PORT,s)
  133. #define LCD_PORT_D4             LCD_PORT_D4_S(LCD_D4_PORT)
  134.  
  135. #define LCD_PIN_D4_S(s)         GLUE(PIN,s)
  136. #define LCD_PIN_D4              LCD_PIN_D4_S(LCD_D4_PORT)
  137.  
  138. #define D4_HIGH                 LCD_PORT_D4|=(1<<LCD_D4_PIN)
  139. #define D4_LOW                  LCD_PORT_D4&=~(1<<LCD_D4_PIN)
  140. #define D4_IN                   LCD_DDR_D4&=~(1<<LCD_D4_PIN)
  141. #define D4_OUT                  LCD_DDR_D4|=(1<<LCD_D4_PIN)
  142. #define D4_STATE                (LCD_PIN_D4|(1<<LCD_D4_PIN)
  143.  
  144. //D5 wire (comments like E wire)
  145. #define LCD_DDR_D5_S(s)         GLUE(DDR,s)
  146. #define LCD_DDR_D5              LCD_DDR_D5_S(LCD_D5_PORT)
  147.  
  148. #define LCD_PORT_D5_S(s)        GLUE(PORT,s)
  149. #define LCD_PORT_D5             LCD_PORT_D5_S(LCD_D5_PORT)
  150.  
  151. #define LCD_PIN_D5_S(s)         GLUE(PIN,s)
  152. #define LCD_PIN_D5              LCD_PIN_D5_S(LCD_D5_PORT)
  153.  
  154. #define D5_HIGH                 LCD_PORT_D5|=(1<<LCD_D5_PIN)
  155. #define D5_LOW                  LCD_PORT_D5&=~(1<<LCD_D5_PIN)
  156. #define D5_IN                   LCD_DDR_D5&=~(1<<LCD_D5_PIN)
  157. #define D5_OUT                  LCD_DDR_D5|=(1<<LCD_D5_PIN)
  158. #define D5_STATE                (LCD_PIN_D5|(1<<LCD_D5_PIN)
  159.  
  160. //D6 wire (comments like E wire)
  161. #define LCD_DDR_D6_S(s)         GLUE(DDR,s)
  162. #define LCD_DDR_D6              LCD_DDR_D6_S(LCD_D6_PORT)
  163.  
  164. #define LCD_PORT_D6_S(s)        GLUE(PORT,s)
  165. #define LCD_PORT_D6             LCD_PORT_D6_S(LCD_D6_PORT)
  166.  
  167. #define LCD_PIN_D6_S(s)         GLUE(PIN,s)
  168. #define LCD_PIN_D6              LCD_PIN_D6_S(LCD_D6_PORT)
  169.  
  170. #define D6_HIGH                 LCD_PORT_D6|=(1<<LCD_D6_PIN)
  171. #define D6_LOW                  LCD_PORT_D6&=~(1<<LCD_D6_PIN)
  172. #define D6_IN                   LCD_DDR_D6&=~(1<<LCD_D6_PIN)
  173. #define D6_OUT                  LCD_DDR_D6|=(1<<LCD_D6_PIN)
  174. #define D6_STATE                (LCD_PIN_D6|(1<<LCD_D6_PIN)
  175.  
  176. //D7 wire (comments like E wire)
  177. #define LCD_DDR_D7_S(s)         GLUE(DDR,s)
  178. #define LCD_DDR_D7              LCD_DDR_D7_S(LCD_D7_PORT)
  179.  
  180. #define LCD_PORT_D7_S(s)        GLUE(PORT,s)
  181. #define LCD_PORT_D7             LCD_PORT_D7_S(LCD_D7_PORT)
  182.  
  183. #define LCD_PIN_D7_S(s)         GLUE(PIN,s)
  184. #define LCD_PIN_D7              LCD_PIN_D7_S(LCD_D7_PORT)
  185.  
  186. #define D7_HIGH                 LCD_PORT_D7|=(1<<LCD_D7_PIN)
  187. #define D7_LOW                  LCD_PORT_D7&=~(1<<LCD_D7_PIN)
  188. #define D7_IN                   LCD_DDR_D7&=~(1<<LCD_D7_PIN)
  189. #define D7_OUT                  LCD_DDR_D7|=(1<<LCD_D7_PIN)
  190. #define D7_STATE                (LCD_PIN_D7|(1<<LCD_D7_PIN)
  191.  
  192.  
  193. // definitions to LCD
  194. #define LCD_DAT 0x00
  195. #define LCD_CMD 0x01
  196. #define LCD_CLR 0x01
  197. //*******************************************************************************************//
  198.  
  199. //*******************************************************************************************//
  200. //                              AVILABLE FUNCTION                                            //
  201. //*******************************************************************************************//
  202. void hd_write_data(uint8_t mode , uint8_t byte);
  203. void hd_init(void);
  204. void hd_clear(void);
  205.  
  206. void hd_char(char character);
  207. void hd_string(char *string);
  208. void hd_flash_string(const char *string);
  209. void hd_eeprom_string(char *string);
  210.  
  211. void hd_int(int integer);
  212. void hd_hex(int hexadecimal);
  213. void hd_bin(int binary);
  214. //*******************************************************************************************//
  215. #endif /* HD44780_H_ */
  216.  
  217.  
  218.  
  219.  
  220.  
  221.  
  222. /*
  223.  * hd44780.c
  224.  *
  225.  *  Created on: 20 lis 2014
  226.  *      Author: Administrator
  227.  */
  228.  
  229.  
  230. #include <avr/io.h>
  231. #include <util/delay.h>
  232. #include <avr/pgmspace.h>
  233. #include <avr/eeprom.h>
  234. #include "hd44780.h"
  235. #include <stdlib.h>
  236.  
  237.  
  238. static inline void hd_dir_out(void);
  239. static void hd_nibble(uint8_t nibble);
  240.  
  241. static inline void hd_dir_out(void){
  242.     D4_OUT;
  243.     D5_OUT;
  244.     D6_OUT;
  245.     D7_OUT;
  246. }
  247.  
  248.  
  249. // prepare data pins to send nibble(4bits)
  250. static void hd_nibble(uint8_t nibble){
  251.     (nibble & (1<<0)) ? (D4_HIGH) : (D4_LOW);
  252.     (nibble & (1<<1)) ? (D5_HIGH) : (D5_LOW);
  253.     (nibble & (1<<2)) ? (D6_HIGH) : (D6_LOW);
  254.     (nibble & (1<<3)) ? (D7_HIGH) : (D7_LOW);
  255. }
  256.  
  257. // writing data to lcd
  258. void hd_write_data(uint8_t mode , uint8_t byte){
  259.     // outs
  260.     hd_dir_out();
  261.     // mode commend or data
  262.     mode ? (RS_LOW) : (RS_HIGH);
  263.     // send older nibble
  264.     E_HIGH;
  265.     hd_nibble(byte>>4);
  266.     E_LOW;
  267.     // send younger nibble
  268.     E_HIGH;
  269.     hd_nibble(byte);
  270.     E_LOW;
  271.     mode ? (_delay_ms(2)) : (_delay_us(40)); //delay
  272. }
  273.  
  274. // function to init lcd
  275. void hd_init(void){
  276.     // pins as outs and low state
  277.     E_OUT;
  278.     E_LOW;
  279.     RS_OUT;
  280.     RS_LOW;
  281.     _delay_ms(15); //wake to power up
  282.     //init sequence for 4bit bus
  283.     E_HIGH;
  284.     hd_nibble(3); // 8 bit mode
  285.     E_LOW;
  286.     _delay_ms(5);
  287.     E_HIGH;         // one more time
  288.     E_LOW;
  289.     _delay_us(100);
  290.     E_HIGH;         // second time
  291.     E_LOW;
  292.     _delay_us(100);
  293.     E_HIGH;
  294.     hd_nibble(2);   //4bit mode
  295.     E_LOW;
  296.     _delay_us(100);
  297.     // default config display
  298.     hd_write_data(LCD_CMD , 0b00101000); // 4bit mode, 5x7 font
  299.     hd_write_data(LCD_CMD , 0b00001100); // turn on display
  300.     hd_write_data(LCD_CMD , 0b00000110); // shift cursor
  301.  
  302.     hd_clear(); // clear display
  303. }
  304.  
  305. void hd_clear(void){
  306.     hd_write_data(LCD_CMD , LCD_CLR);
  307. }
  308.  
  309. #if USE_HD_CHAR ==1
  310. void hd_char(char character) {
  311.     hd_write_data(LCD_DAT , character );
  312. }
  313. #endif
  314.  
  315. #if USE_HD_STRING
  316. void hd_string(char *string){
  317.     while(*string) hd_char(*string++);
  318. }
  319. #endif
  320.  
  321.  
  322. #if USE_HD_FLASH_STRING == 1
  323. void hd_flash_string(const char *string){
  324.     register char buffer;
  325.     while((buffer = pgm_read_byte(string++))) hd_char(buffer);
  326. }
  327. #endif
  328.  
  329. #if USE_HD_EEPROM_STRING == 1
  330. void hd_eeprom_string(char *string){
  331.     register char buffer = 1;
  332.     while(1){
  333.         buffer = eeprom_read_byte((uint8_t*)string++);
  334.         //((buffer == 0x00) || (buffer == 0xFF)) ? break : hd_char(buffer);
  335.         if((buffer == 0x00) || (buffer == 0xFF)) break;
  336.         hd_char(buffer);
  337.     }
  338. }
  339. #endif
  340.  
  341.  
  342. #if USE_HD_INT == 1
  343. void hd_int(int integer){
  344.     char buffer[7];
  345.     itoa(integer , buffer , 10); // data , destination , system
  346.     hd_string(buffer);
  347. }
  348. #endif
  349.  
  350. #if USE_HD_HEX == 1
  351. void hd_hex(int hexadecimal)
  352. {
  353.     char buffer[5];
  354.     itoa(hexadecimal , buffer , 16); // data , destination , system
  355.     hd_string(buffer);
  356. }
  357. #endif
  358.  
  359. #if USE_HD_BIN == 1
  360. void hd_bin(int binary){
  361.     char buffer[17];
  362.     itoa(binary , buffer , 2); // data , destination , system
  363.     hd_string(buffer);
  364. }
  365. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement