LiquidCrystal_I2C.cpp 8.6 KB

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