Advertisement
Guest User

LCD Lib Fail1

a guest
Dec 28th, 2013
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 13.84 KB | None | 0 0
  1. /* ================== .h ================*/
  2.  
  3. #ifndef LiquidCrystal_h
  4. #define LiquidCrystal_h
  5.  
  6. #include <inttypes.h>
  7. //#include "Print.h"
  8.  
  9. // https://community.sparkdevices.com/t/fix-for-include-arduino-h/953
  10.  
  11.     #define ARDUINO_H
  12.     #include <stdint.h>
  13.     #include <stddef.h>
  14.     #include <stdlib.h>
  15.    
  16. // to make it compile (by Frido)
  17.  
  18.     #define boolean bool
  19.     #pragma GCC diagnostic ignored "-Wwrite-strings"
  20.     #pragma GCC diagnostic ignored "-Wconversion-null"
  21.     #pragma GCC diagnostic ignored "-Wpointer-arith"
  22.     #pragma GCC diagnostic ignored "-Wsign-compare"
  23.     #pragma GCC diagnostic ignored "-Wreturn-type"
  24.  
  25.  
  26.  
  27. // commands
  28. #define LCD_CLEARDISPLAY 0x01
  29. #define LCD_RETURNHOME 0x02
  30. #define LCD_ENTRYMODESET 0x04
  31. #define LCD_DISPLAYCONTROL 0x08
  32. #define LCD_CURSORSHIFT 0x10
  33. #define LCD_FUNCTIONSET 0x20
  34. #define LCD_SETCGRAMADDR 0x40
  35. #define LCD_SETDDRAMADDR 0x80
  36.  
  37. // flags for display entry mode
  38. #define LCD_ENTRYRIGHT 0x00
  39. #define LCD_ENTRYLEFT 0x02
  40. #define LCD_ENTRYSHIFTINCREMENT 0x01
  41. #define LCD_ENTRYSHIFTDECREMENT 0x00
  42.  
  43. // flags for display on/off control
  44. #define LCD_DISPLAYON 0x04
  45. #define LCD_DISPLAYOFF 0x00
  46. #define LCD_CURSORON 0x02
  47. #define LCD_CURSOROFF 0x00
  48. #define LCD_BLINKON 0x01
  49. #define LCD_BLINKOFF 0x00
  50.  
  51. // flags for display/cursor shift
  52. #define LCD_DISPLAYMOVE 0x08
  53. #define LCD_CURSORMOVE 0x00
  54. #define LCD_MOVERIGHT 0x04
  55. #define LCD_MOVELEFT 0x00
  56.  
  57. // flags for function set
  58. #define LCD_8BITMODE 0x10
  59. #define LCD_4BITMODE 0x00
  60. #define LCD_2LINE 0x08
  61. #define LCD_1LINE 0x00
  62. #define LCD_5x10DOTS 0x04
  63. #define LCD_5x8DOTS 0x00
  64.  
  65. class LiquidCrystal : public Print {
  66. public:
  67.   LiquidCrystal(uint8_t rs, uint8_t enable,
  68.                 uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3,
  69.                 uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7);
  70.   LiquidCrystal(uint8_t rs, uint8_t rw, uint8_t enable,
  71.                 uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3,
  72.                 uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7);
  73.   LiquidCrystal(uint8_t rs, uint8_t rw, uint8_t enable,
  74.                 uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3);
  75.   LiquidCrystal(uint8_t rs, uint8_t enable,
  76.                 uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3);
  77.  
  78.   void init(uint8_t fourbitmode, uint8_t rs, uint8_t rw, uint8_t enable,
  79.             uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3,
  80.             uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7);
  81.    
  82.   void begin(uint8_t cols, uint8_t rows, uint8_t charsize = LCD_5x8DOTS);
  83.  
  84.   void clear();
  85.   void home();
  86.  
  87.   void noDisplay();
  88.   void display();
  89.   void noBlink();
  90.   void blink();
  91.   void noCursor();
  92.   void cursor();
  93.   void scrollDisplayLeft();
  94.   void scrollDisplayRight();
  95.   void leftToRight();
  96.   void rightToLeft();
  97.   void autoscroll();
  98.   void noAutoscroll();
  99.  
  100.   void createChar(uint8_t, uint8_t[]);
  101.   void setCursor(uint8_t, uint8_t);
  102.   virtual void write(uint8_t);
  103.   void command(uint8_t);
  104. private:
  105.   void send(uint8_t, uint8_t);
  106.   void write4bits(uint8_t);
  107.   void write8bits(uint8_t);
  108.   void pulseEnable();
  109.  
  110.   uint8_t _rs_pin; // LOW: command.  HIGH: character.
  111.   uint8_t _rw_pin; // LOW: write to LCD.  HIGH: read from LCD.
  112.   uint8_t _enable_pin; // activated by a HIGH pulse.
  113.   uint8_t _data_pins[8];
  114.  
  115.   uint8_t _displayfunction;
  116.   uint8_t _displaycontrol;
  117.   uint8_t _displaymode;
  118.  
  119.   uint8_t _initialized;
  120.  
  121.   uint8_t _numlines,_currline;
  122. };
  123.  
  124. #endif
  125.  
  126. /* ================== .cpp ==================*/
  127.  
  128. /*
  129.  
  130. #include "LiquidCrystal.h"
  131.  
  132. #include <stdio.h>
  133. #include <string.h>
  134. #include <inttypes.h>
  135. #include "WProgram.h"
  136.  
  137. */
  138.  
  139. // When the display powers up, it is configured as follows:
  140. //
  141. // 1. Display clear
  142. // 2. Function set:
  143. //    DL = 1; 8-bit interface data
  144. //    N = 0; 1-line display
  145. //    F = 0; 5x8 dot character font
  146. // 3. Display on/off control:
  147. //    D = 0; Display off
  148. //    C = 0; Cursor off
  149. //    B = 0; Blinking off
  150. // 4. Entry mode set:
  151. //    I/D = 1; Increment by 1
  152. //    S = 0; No shift
  153. //
  154. // Note, however, that resetting the Arduino doesn't reset the LCD, so we
  155. // can't assume that its in that state when a sketch starts (and the
  156. // LiquidCrystal constructor is called).
  157.  
  158. LiquidCrystal::LiquidCrystal(uint8_t rs, uint8_t rw, uint8_t enable,
  159.                              uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3,
  160.                              uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7)
  161. {
  162.   init(0, rs, rw, enable, d0, d1, d2, d3, d4, d5, d6, d7);
  163. }
  164.  
  165. LiquidCrystal::LiquidCrystal(uint8_t rs, uint8_t enable,
  166.                              uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3,
  167.                              uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7)
  168. {
  169.   init(0, rs, 255, enable, d0, d1, d2, d3, d4, d5, d6, d7);
  170. }
  171.  
  172. LiquidCrystal::LiquidCrystal(uint8_t rs, uint8_t rw, uint8_t enable,
  173.                              uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3)
  174. {
  175.   init(1, rs, rw, enable, d0, d1, d2, d3, 0, 0, 0, 0);
  176. }
  177.  
  178. LiquidCrystal::LiquidCrystal(uint8_t rs,  uint8_t enable,
  179.                              uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3)
  180. {
  181.   init(1, rs, 255, enable, d0, d1, d2, d3, 0, 0, 0, 0);
  182. }
  183.  
  184. void LiquidCrystal::init(uint8_t fourbitmode, uint8_t rs, uint8_t rw, uint8_t enable,
  185.                          uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3,
  186.                          uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7)
  187. {
  188.   _rs_pin = rs;
  189.   _rw_pin = rw;
  190.   _enable_pin = enable;
  191.  
  192.   _data_pins[0] = d0;
  193.   _data_pins[1] = d1;
  194.   _data_pins[2] = d2;
  195.   _data_pins[3] = d3;
  196.   _data_pins[4] = d4;
  197.   _data_pins[5] = d5;
  198.   _data_pins[6] = d6;
  199.   _data_pins[7] = d7;
  200.  
  201.   pinMode(_rs_pin, OUTPUT);
  202.   // we can save 1 pin by not using RW. Indicate by passing 255 instead of pin#
  203.   if (_rw_pin != 255) {
  204.     pinMode(_rw_pin, OUTPUT);
  205.   }
  206.   pinMode(_enable_pin, OUTPUT);
  207.  
  208.   if (fourbitmode)
  209.     _displayfunction = LCD_4BITMODE | LCD_1LINE | LCD_5x8DOTS;
  210.   else
  211.     _displayfunction = LCD_8BITMODE | LCD_1LINE | LCD_5x8DOTS;
  212.  
  213.   begin(16, 1);  
  214. }
  215.  
  216. void LiquidCrystal::begin(uint8_t cols, uint8_t lines, uint8_t dotsize) {
  217.   if (lines > 1) {
  218.     _displayfunction |= LCD_2LINE;
  219.   }
  220.   _numlines = lines;
  221.   _currline = 0;
  222.  
  223.   // for some 1 line displays you can select a 10 pixel high font
  224.   if ((dotsize != 0) && (lines == 1)) {
  225.     _displayfunction |= LCD_5x10DOTS;
  226.   }
  227.  
  228.   // SEE PAGE 45/46 FOR INITIALIZATION SPECIFICATION!
  229.   // according to datasheet, we need at least 40ms after power rises above 2.7V
  230.   // before sending commands. Arduino can turn on way befer 4.5V so we'll wait 50
  231.   delayMicroseconds(50000);
  232.   // Now we pull both RS and R/W low to begin commands
  233.   digitalWrite(_rs_pin, LOW);
  234.   digitalWrite(_enable_pin, LOW);
  235.   if (_rw_pin != 255) {
  236.     digitalWrite(_rw_pin, LOW);
  237.   }
  238.  
  239.   //put the LCD into 4 bit or 8 bit mode
  240.   if (! (_displayfunction & LCD_8BITMODE)) {
  241.     // this is according to the hitachi HD44780 datasheet
  242.     // figure 24, pg 46
  243.  
  244.     // we start in 8bit mode, try to set 4 bit mode
  245.     write4bits(0x03);
  246.     delayMicroseconds(4500); // wait min 4.1ms
  247.  
  248.     // second try
  249.     write4bits(0x03);
  250.     delayMicroseconds(4500); // wait min 4.1ms
  251.    
  252.     // third go!
  253.     write4bits(0x03);
  254.     delayMicroseconds(150);
  255.  
  256.     // finally, set to 8-bit interface
  257.     write4bits(0x02);
  258.   } else {
  259.     // this is according to the hitachi HD44780 datasheet
  260.     // page 45 figure 23
  261.  
  262.     // Send function set command sequence
  263.     command(LCD_FUNCTIONSET | _displayfunction);
  264.     delayMicroseconds(4500);  // wait more than 4.1ms
  265.  
  266.     // second try
  267.     command(LCD_FUNCTIONSET | _displayfunction);
  268.     delayMicroseconds(150);
  269.  
  270.     // third go
  271.     command(LCD_FUNCTIONSET | _displayfunction);
  272.   }
  273.  
  274.   // finally, set # lines, font size, etc.
  275.   command(LCD_FUNCTIONSET | _displayfunction);  
  276.  
  277.   // turn the display on with no cursor or blinking default
  278.   _displaycontrol = LCD_DISPLAYON | LCD_CURSOROFF | LCD_BLINKOFF;  
  279.   display();
  280.  
  281.   // clear it off
  282.   clear();
  283.  
  284.   // Initialize to default text direction (for romance languages)
  285.   _displaymode = LCD_ENTRYLEFT | LCD_ENTRYSHIFTDECREMENT;
  286.   // set the entry mode
  287.   command(LCD_ENTRYMODESET | _displaymode);
  288.  
  289. }
  290.  
  291. /********** high level commands, for the user! */
  292. void LiquidCrystal::clear()
  293. {
  294.   command(LCD_CLEARDISPLAY);  // clear display, set cursor position to zero
  295.   delayMicroseconds(2000);  // this command takes a long time!
  296. }
  297.  
  298. void LiquidCrystal::home()
  299. {
  300.   command(LCD_RETURNHOME);  // set cursor position to zero
  301.   delayMicroseconds(2000);  // this command takes a long time!
  302. }
  303.  
  304. void LiquidCrystal::setCursor(uint8_t col, uint8_t row)
  305. {
  306.   int row_offsets[] = { 0x00, 0x40, 0x14, 0x54 };
  307.   if ( row > _numlines ) {
  308.     row = _numlines-1;    // we count rows starting w/0
  309.   }
  310.  
  311.   command(LCD_SETDDRAMADDR | (col + row_offsets[row]));
  312. }
  313.  
  314. // Turn the display on/off (quickly)
  315. void LiquidCrystal::noDisplay() {
  316.   _displaycontrol &= ~LCD_DISPLAYON;
  317.   command(LCD_DISPLAYCONTROL | _displaycontrol);
  318. }
  319. void LiquidCrystal::display() {
  320.   _displaycontrol |= LCD_DISPLAYON;
  321.   command(LCD_DISPLAYCONTROL | _displaycontrol);
  322. }
  323.  
  324. // Turns the underline cursor on/off
  325. void LiquidCrystal::noCursor() {
  326.   _displaycontrol &= ~LCD_CURSORON;
  327.   command(LCD_DISPLAYCONTROL | _displaycontrol);
  328. }
  329. void LiquidCrystal::cursor() {
  330.   _displaycontrol |= LCD_CURSORON;
  331.   command(LCD_DISPLAYCONTROL | _displaycontrol);
  332. }
  333.  
  334. // Turn on and off the blinking cursor
  335. void LiquidCrystal::noBlink() {
  336.   _displaycontrol &= ~LCD_BLINKON;
  337.   command(LCD_DISPLAYCONTROL | _displaycontrol);
  338. }
  339. void LiquidCrystal::blink() {
  340.   _displaycontrol |= LCD_BLINKON;
  341.   command(LCD_DISPLAYCONTROL | _displaycontrol);
  342. }
  343.  
  344. // These commands scroll the display without changing the RAM
  345. void LiquidCrystal::scrollDisplayLeft(void) {
  346.   command(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVELEFT);
  347. }
  348. void LiquidCrystal::scrollDisplayRight(void) {
  349.   command(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVERIGHT);
  350. }
  351.  
  352. // This is for text that flows Left to Right
  353. void LiquidCrystal::leftToRight(void) {
  354.   _displaymode |= LCD_ENTRYLEFT;
  355.   command(LCD_ENTRYMODESET | _displaymode);
  356. }
  357.  
  358. // This is for text that flows Right to Left
  359. void LiquidCrystal::rightToLeft(void) {
  360.   _displaymode &= ~LCD_ENTRYLEFT;
  361.   command(LCD_ENTRYMODESET | _displaymode);
  362. }
  363.  
  364. // This will 'right justify' text from the cursor
  365. void LiquidCrystal::autoscroll(void) {
  366.   _displaymode |= LCD_ENTRYSHIFTINCREMENT;
  367.   command(LCD_ENTRYMODESET | _displaymode);
  368. }
  369.  
  370. // This will 'left justify' text from the cursor
  371. void LiquidCrystal::noAutoscroll(void) {
  372.   _displaymode &= ~LCD_ENTRYSHIFTINCREMENT;
  373.   command(LCD_ENTRYMODESET | _displaymode);
  374. }
  375.  
  376. // Allows us to fill the first 8 CGRAM locations
  377. // with custom characters
  378. void LiquidCrystal::createChar(uint8_t location, uint8_t charmap[]) {
  379.   location &= 0x7; // we only have 8 locations 0-7
  380.   command(LCD_SETCGRAMADDR | (location << 3));
  381.   for (int i=0; i<8; i++) {
  382.     write(charmap[i]);
  383.   }
  384. }
  385.  
  386. /*********** mid level commands, for sending data/cmds */
  387.  
  388. inline void LiquidCrystal::command(uint8_t value) {
  389.   send(value, LOW);
  390. }
  391.  
  392. inline void LiquidCrystal::write(uint8_t value) {
  393.   send(value, HIGH);
  394. }
  395.  
  396. /************ low level data pushing commands **********/
  397.  
  398. // write either command or data, with automatic 4/8-bit selection
  399. void LiquidCrystal::send(uint8_t value, uint8_t mode) {
  400.   digitalWrite(_rs_pin, mode);
  401.  
  402.   // if there is a RW pin indicated, set it low to Write
  403.   if (_rw_pin != 255) {
  404.     digitalWrite(_rw_pin, LOW);
  405.   }
  406.  
  407.   if (_displayfunction & LCD_8BITMODE) {
  408.     write8bits(value);
  409.   } else {
  410.     write4bits(value>>4);
  411.     write4bits(value);
  412.   }
  413. }
  414.  
  415. void LiquidCrystal::pulseEnable(void) {
  416.   digitalWrite(_enable_pin, LOW);
  417.   delayMicroseconds(1);    
  418.   digitalWrite(_enable_pin, HIGH);
  419.   delayMicroseconds(1);    // enable pulse must be >450ns
  420.   digitalWrite(_enable_pin, LOW);
  421.   delayMicroseconds(100);   // commands need > 37us to settle
  422. }
  423.  
  424. void LiquidCrystal::write4bits(uint8_t value) {
  425.   for (int i = 0; i < 4; i++) {
  426.     pinMode(_data_pins[i], OUTPUT);
  427.     digitalWrite(_data_pins[i], (value >> i) & 0x01);
  428.   }
  429.  
  430.   pulseEnable();
  431. }
  432.  
  433. void LiquidCrystal::write8bits(uint8_t value) {
  434.   for (int i = 0; i < 8; i++) {
  435.     pinMode(_data_pins[i], OUTPUT);
  436.     digitalWrite(_data_pins[i], (value >> i) & 0x01);
  437.   }
  438.  
  439.   pulseEnable();
  440. }
  441.  
  442. /* ============== MAIN =====================*/
  443.  
  444. /*
  445.   LiquidCrystal Library - Hello World
  446.  
  447.  Demonstrates the use a 16x2 LCD display.  The LiquidCrystal
  448.  library works with all LCD displays that are compatible with the
  449.  Hitachi HD44780 driver. There are many of them out there, and you
  450.  can usually tell them by the 16-pin interface.
  451.  
  452.  This sketch prints "Hello World!" to the LCD
  453.  and shows the time.
  454.  
  455.   The circuit:
  456.  * LCD RS pin to digital pin 12
  457.  * LCD Enable pin to digital pin 11
  458.  * LCD D4 pin to digital pin 5
  459.  * LCD D5 pin to digital pin 4
  460.  * LCD D6 pin to digital pin 3
  461.  * LCD D7 pin to digital pin 2
  462.  * 10K resistor:
  463.  * ends to +5V and ground
  464.  * wiper to LCD VO pin (pin 3)
  465.  
  466.  Library originally added 18 Apr 2008
  467.  by David A. Mellis
  468.  library modified 5 Jul 2009
  469.  by Limor Fried (http://www.ladyada.net)
  470.  example added 9 Jul 2009
  471.  by Tom Igoe
  472.  modified 8 Feb 2010
  473.  by Tom Igoe
  474.  
  475.  This example code is in the public domain.
  476.  
  477.  http://www.arduino.cc/en/Tutorial/LiquidCrystal
  478.  */
  479.  
  480. // include the library code:
  481.  
  482. // #include <LiquidCrystal.h>
  483.  
  484. // initialize the library with the numbers of the interface pins
  485. LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
  486.  
  487. void setup() {
  488.   // set up the LCD's number of columns and rows:
  489.   lcd.begin(16, 2);
  490.   // Print a message to the LCD.
  491.   lcd.print("hello, world!");
  492. }
  493.  
  494. void loop() {
  495.   // set the cursor to column 0, line 1
  496.   // (note: line 1 is the second row, since counting begins with 0):
  497.   lcd.setCursor(0, 1);
  498.   // print the number of seconds since reset:
  499.   lcd.print(millis()/1000);
  500. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement