LiquidCrystal_I2C.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. // Based on the work by DFRobot
  2. #include "LiquidCrystal_I2C.h"
  3. #include <inttypes.h>
  4. #if defined(ARDUINO) && ARDUINO >= 100
  5. #include "Arduino.h"
  6. #define printIIC(args) Wire.write(args)
  7. inline size_t LiquidCrystal_I2C::write(uint8_t value) {
  8. send(value, Rs);
  9. return 1;
  10. }
  11. #else
  12. #include "WProgram.h"
  13. #define printIIC(args) Wire.send(args)
  14. inline void LiquidCrystal_I2C::write(uint8_t value) {
  15. send(value, Rs);
  16. }
  17. #endif
  18. #include "Wire.h"
  19. // When the display powers up, it is configured as follows:
  20. //
  21. // 1. Display clear
  22. // 2. Function set:
  23. // DL = 1; 8-bit interface data
  24. // N = 0; 1-line display
  25. // F = 0; 5x8 dot character font
  26. // 3. Display on/off control:
  27. // D = 0; Display off
  28. // C = 0; Cursor off
  29. // B = 0; Blinking off
  30. // 4. Entry mode set:
  31. // I/D = 1; Increment by 1
  32. // S = 0; No shift
  33. //
  34. // Note, however, that resetting the Arduino doesn't reset the LCD, so we
  35. // can't assume that its in that state when a sketch starts (and the
  36. // LiquidCrystal constructor is called).
  37. LiquidCrystal_I2C::LiquidCrystal_I2C(uint8_t lcd_Addr,uint8_t lcd_cols,uint8_t lcd_rows)
  38. {
  39. _Addr = lcd_Addr;
  40. _cols = lcd_cols;
  41. _rows = lcd_rows;
  42. _backlightval = LCD_NOBACKLIGHT;
  43. }
  44. void LiquidCrystal_I2C::init(){
  45. init_priv();
  46. }
  47. void LiquidCrystal_I2C::init_priv()
  48. {
  49. Wire.begin();
  50. _displayfunction = LCD_4BITMODE | LCD_1LINE | LCD_5x8DOTS;
  51. begin(_cols, _rows);
  52. }
  53. void LiquidCrystal_I2C::begin(uint8_t cols, uint8_t lines, uint8_t dotsize) {
  54. if (lines > 1) {
  55. _displayfunction |= LCD_2LINE;
  56. }
  57. _numlines = lines;
  58. // for some 1 line displays you can select a 10 pixel high font
  59. if ((dotsize != 0) && (lines == 1)) {
  60. _displayfunction |= LCD_5x10DOTS;
  61. }
  62. // SEE PAGE 45/46 FOR INITIALIZATION SPECIFICATION!
  63. // according to datasheet, we need at least 40ms after power rises above 2.7V
  64. // before sending commands. Arduino can turn on way befer 4.5V so we'll wait 50
  65. delay(50);
  66. // Now we pull both RS and R/W low to begin commands
  67. expanderWrite(_backlightval); // reset expanderand turn backlight off (Bit 8 =1)
  68. delay(1000);
  69. //put the LCD into 4 bit mode
  70. // this is according to the hitachi HD44780 datasheet
  71. // figure 24, pg 46
  72. // we start in 8bit mode, try to set 4 bit mode
  73. write4bits(0x03 << 4);
  74. delayMicroseconds(4500); // wait min 4.1ms
  75. // second try
  76. write4bits(0x03 << 4);
  77. delayMicroseconds(4500); // wait min 4.1ms
  78. // third go!
  79. write4bits(0x03 << 4);
  80. delayMicroseconds(150);
  81. // finally, set to 4-bit interface
  82. write4bits(0x02 << 4);
  83. // set # lines, font size, etc.
  84. command(LCD_FUNCTIONSET | _displayfunction);
  85. // turn the display on with no cursor or blinking default
  86. _displaycontrol = LCD_DISPLAYON | LCD_CURSOROFF | LCD_BLINKOFF;
  87. display();
  88. // clear it off
  89. clear();
  90. // Initialize to default text direction (for roman languages)
  91. _displaymode = LCD_ENTRYLEFT | LCD_ENTRYSHIFTDECREMENT;
  92. // set the entry mode
  93. command(LCD_ENTRYMODESET | _displaymode);
  94. home();
  95. }
  96. /********** high level commands, for the user! */
  97. void LiquidCrystal_I2C::clear(){
  98. command(LCD_CLEARDISPLAY);// clear display, set cursor position to zero
  99. delayMicroseconds(2000); // this command takes a long time!
  100. }
  101. void LiquidCrystal_I2C::home(){
  102. command(LCD_RETURNHOME); // set cursor position to zero
  103. delayMicroseconds(2000); // this command takes a long time!
  104. }
  105. void LiquidCrystal_I2C::setCursor(uint8_t col, uint8_t row){
  106. int row_offsets[] = { 0x00, 0x40, 0x14, 0x54 };
  107. if ( row > _numlines ) {
  108. row = _numlines-1; // we count rows starting w/0
  109. }
  110. command(LCD_SETDDRAMADDR | (col + row_offsets[row]));
  111. }
  112. // Turn the display on/off (quickly)
  113. void LiquidCrystal_I2C::noDisplay() {
  114. _displaycontrol &= ~LCD_DISPLAYON;
  115. command(LCD_DISPLAYCONTROL | _displaycontrol);
  116. }
  117. void LiquidCrystal_I2C::display() {
  118. _displaycontrol |= LCD_DISPLAYON;
  119. command(LCD_DISPLAYCONTROL | _displaycontrol);
  120. }
  121. // Turns the underline cursor on/off
  122. void LiquidCrystal_I2C::noCursor() {
  123. _displaycontrol &= ~LCD_CURSORON;
  124. command(LCD_DISPLAYCONTROL | _displaycontrol);
  125. }
  126. void LiquidCrystal_I2C::cursor() {
  127. _displaycontrol |= LCD_CURSORON;
  128. command(LCD_DISPLAYCONTROL | _displaycontrol);
  129. }
  130. // Turn on and off the blinking cursor
  131. void LiquidCrystal_I2C::noBlink() {
  132. _displaycontrol &= ~LCD_BLINKON;
  133. command(LCD_DISPLAYCONTROL | _displaycontrol);
  134. }
  135. void LiquidCrystal_I2C::blink() {
  136. _displaycontrol |= LCD_BLINKON;
  137. command(LCD_DISPLAYCONTROL | _displaycontrol);
  138. }
  139. // These commands scroll the display without changing the RAM
  140. void LiquidCrystal_I2C::scrollDisplayLeft(void) {
  141. command(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVELEFT);
  142. }
  143. void LiquidCrystal_I2C::scrollDisplayRight(void) {
  144. command(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVERIGHT);
  145. }
  146. // This is for text that flows Left to Right
  147. void LiquidCrystal_I2C::leftToRight(void) {
  148. _displaymode |= LCD_ENTRYLEFT;
  149. command(LCD_ENTRYMODESET | _displaymode);
  150. }
  151. // This is for text that flows Right to Left
  152. void LiquidCrystal_I2C::rightToLeft(void) {
  153. _displaymode &= ~LCD_ENTRYLEFT;
  154. command(LCD_ENTRYMODESET | _displaymode);
  155. }
  156. // This will 'right justify' text from the cursor
  157. void LiquidCrystal_I2C::autoscroll(void) {
  158. _displaymode |= LCD_ENTRYSHIFTINCREMENT;
  159. command(LCD_ENTRYMODESET | _displaymode);
  160. }
  161. // This will 'left justify' text from the cursor
  162. void LiquidCrystal_I2C::noAutoscroll(void) {
  163. _displaymode &= ~LCD_ENTRYSHIFTINCREMENT;
  164. command(LCD_ENTRYMODESET | _displaymode);
  165. }
  166. // Allows us to fill the first 8 CGRAM locations
  167. // with custom characters
  168. void LiquidCrystal_I2C::createChar(uint8_t location, uint8_t charmap[]) {
  169. location &= 0x7; // we only have 8 locations 0-7
  170. command(LCD_SETCGRAMADDR | (location << 3));
  171. for (int i=0; i<8; i++) {
  172. write(charmap[i]);
  173. }
  174. }
  175. // Turn the (optional) backlight off/on
  176. void LiquidCrystal_I2C::noBacklight(void) {
  177. _backlightval=LCD_NOBACKLIGHT;
  178. expanderWrite(0);
  179. }
  180. void LiquidCrystal_I2C::backlight(void) {
  181. _backlightval=LCD_BACKLIGHT;
  182. expanderWrite(0);
  183. }
  184. /*********** mid level commands, for sending data/cmds */
  185. inline void LiquidCrystal_I2C::command(uint8_t value) {
  186. send(value, 0);
  187. }
  188. /************ low level data pushing commands **********/
  189. // write either command or data
  190. void LiquidCrystal_I2C::send(uint8_t value, uint8_t mode) {
  191. uint8_t highnib=value&0xf0;
  192. uint8_t lownib=(value<<4)&0xf0;
  193. write4bits((highnib)|mode);
  194. write4bits((lownib)|mode);
  195. }
  196. void LiquidCrystal_I2C::write4bits(uint8_t value) {
  197. expanderWrite(value);
  198. pulseEnable(value);
  199. }
  200. void LiquidCrystal_I2C::expanderWrite(uint8_t _data){
  201. Wire.beginTransmission(_Addr);
  202. printIIC((int)(_data) | _backlightval);
  203. Wire.endTransmission();
  204. }
  205. void LiquidCrystal_I2C::pulseEnable(uint8_t _data){
  206. expanderWrite(_data | En); // En high
  207. delayMicroseconds(1); // enable pulse must be >450ns
  208. expanderWrite(_data & ~En); // En low
  209. delayMicroseconds(50); // commands need > 37us to settle
  210. }
  211. // Alias functions
  212. void LiquidCrystal_I2C::cursor_on(){
  213. cursor();
  214. }
  215. void LiquidCrystal_I2C::cursor_off(){
  216. noCursor();
  217. }
  218. void LiquidCrystal_I2C::blink_on(){
  219. blink();
  220. }
  221. void LiquidCrystal_I2C::blink_off(){
  222. noBlink();
  223. }
  224. void LiquidCrystal_I2C::load_custom_character(uint8_t char_num, uint8_t *rows){
  225. createChar(char_num, rows);
  226. }
  227. void LiquidCrystal_I2C::setBacklight(uint8_t new_val){
  228. if(new_val){
  229. backlight(); // turn backlight on
  230. }else{
  231. noBacklight(); // turn backlight off
  232. }
  233. }
  234. void LiquidCrystal_I2C::printstr(const char c[]){
  235. //This function is not identical to the function used for "real" I2C displays
  236. //it's here so the user sketch doesn't have to be changed
  237. print(c);
  238. }
  239. // unsupported API functions
  240. void LiquidCrystal_I2C::off(){}
  241. void LiquidCrystal_I2C::on(){}
  242. void LiquidCrystal_I2C::setDelay (int cmdDelay,int charDelay) {}
  243. uint8_t LiquidCrystal_I2C::status(){return 0;}
  244. uint8_t LiquidCrystal_I2C::keypad (){return 0;}
  245. uint8_t LiquidCrystal_I2C::init_bargraph(uint8_t graphtype){return 0;}
  246. void LiquidCrystal_I2C::draw_horizontal_graph(uint8_t row, uint8_t column, uint8_t len, uint8_t pixel_col_end){}
  247. void LiquidCrystal_I2C::draw_vertical_graph(uint8_t row, uint8_t column, uint8_t len, uint8_t pixel_row_end){}
  248. void LiquidCrystal_I2C::setContrast(uint8_t new_val){}