Advertisement
Guest User

cpp

a guest
Nov 28th, 2019
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.14 KB | None | 0 0
  1. #include "MyLiquidCrystal.h"
  2.  
  3.  
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <inttypes.h>
  7. #include "Arduino.h"
  8.  
  9.  
  10. MyLiquidCrystal::MyLiquidCrystal(uint8_t rs, uint8_t rw, uint8_t enable,
  11.                  uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3,
  12.                  uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7)
  13. {
  14.   init(0, rs, rw, enable, d0, d1, d2, d3, d4, d5, d6, d7);
  15. }
  16.  
  17. MyLiquidCrystal::MyLiquidCrystal(uint8_t rs, uint8_t enable,
  18.                  uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3,
  19.                  uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7)
  20. {
  21.   init(0, rs, 255, enable, d0, d1, d2, d3, d4, d5, d6, d7);
  22. }
  23.  
  24. MyLiquidCrystal::MyLiquidCrystal(uint8_t rs, uint8_t rw, uint8_t enable,
  25.                  uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3)
  26. {
  27.   init(1, rs, rw, enable, d0, d1, d2, d3, 0, 0, 0, 0);
  28. }
  29.  
  30.  
  31.  
  32. MyLiquidCrystal::MyLiquidCrystal(uint8_t rs,  uint8_t enable,
  33.                  uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3)
  34. {
  35.   init(1, rs, 255, enable, d0, d1, d2, d3, 0, 0, 0, 0);
  36. }
  37.  
  38.  
  39.  
  40. void MyLiquidCrystal::init(uint8_t fourbitmode, uint8_t rs, uint8_t rw, uint8_t enable,
  41.              uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3,
  42.              uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7)
  43. {
  44.   _register_select_pin = rs;
  45.   _read_write_pin = rw;
  46.   _enable_pin = enable;
  47.  
  48.   _data_pins[0] = d0;
  49.   _data_pins[1] = d1;
  50.   _data_pins[2] = d2;
  51.   _data_pins[3] = d3;
  52.   _data_pins[4] = d4;
  53.   _data_pins[5] = d5;
  54.   _data_pins[6] = d6;
  55.   _data_pins[7] = d7;
  56.  
  57.   if (fourbitmode)
  58.     _displayfunction = LCD_4BITMODE | LCD_1LINE | LCD_5x8DOTS;
  59.   else
  60.     _displayfunction = LCD_8BITMODE | LCD_1LINE | LCD_5x8DOTS;
  61.  
  62.  
  63.   begin(16, 1);  
  64. }
  65.  
  66.  
  67. //MODIFYYYYYYYYYYYYYYYYY!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  68.  
  69. void MyLiquidCrystal::begin(uint8_t cols, uint8_t lines, uint8_t dotsize) {
  70.   if (lines > 1) {
  71.     _displayfunction |= LCD_2LINE;
  72.   }
  73.   _numlines = lines;
  74.  
  75.   setRowOffsets(0x00, 0x40, 0x00 + cols, 0x40 + cols);  
  76.  
  77.   // for some 1 line displays you can select a 10 pixel high font
  78.   if ((dotsize != LCD_5x8DOTS) && (lines == 1)) {
  79.     _displayfunction |= LCD_5x10DOTS;
  80.   }
  81.  
  82.   pinMode(_register_select_pin, OUTPUT);
  83.   // we can save 1 pin by not using RW. Indicate by passing 255 instead of pin#
  84.   if (_read_write_pin != 255) {
  85.     pinMode(_read_write_pin, OUTPUT);
  86.   }
  87.   pinMode(_enable_pin, OUTPUT);
  88.  
  89.   // Do these once, instead of every time a character is drawn for speed reasons.
  90.   for (int i=0; i<((_displayfunction & LCD_8BITMODE) ? 8 : 4); ++i)
  91.   {
  92.     pinMode(_data_pins[i], OUTPUT);
  93.    }
  94.  
  95.   // SEE PAGE 45/46 FOR INITIALIZATION SPECIFICATION!
  96.   // according to datasheet, we need at least 40ms after power rises above 2.7V
  97.   // before sending commands. Arduino can turn on way before 4.5V so we'll wait 50
  98.   delayMicroseconds(50000);
  99.   // Now we pull both RS and R/W low to begin commands
  100.   digitalWrite(_register_select_pin, LOW);
  101.   digitalWrite(_enable_pin, LOW);
  102.   if (_read_write_pin != 255) {
  103.     digitalWrite(_read_write_pin, LOW);
  104.   }
  105.  
  106.   //put the LCD into 4 bit or 8 bit mode
  107.   if (! (_displayfunction & LCD_8BITMODE)) {
  108.     // this is according to the hitachi HD44780 datasheet
  109.     // figure 24, pg 46
  110.  
  111.     // we start in 8bit mode, try to set 4 bit mode
  112.     write4bits(0x03);
  113.     delayMicroseconds(4500); // wait min 4.1ms
  114.  
  115.     // second try
  116.     write4bits(0x03);
  117.     delayMicroseconds(4500); // wait min 4.1ms
  118.    
  119.     // third go!
  120.     write4bits(0x03);
  121.     delayMicroseconds(150);
  122.  
  123.     // finally, set to 4-bit interface
  124.     write4bits(0x02);
  125.   } else {
  126.     // this is according to the hitachi HD44780 datasheet
  127.     // page 45 figure 23
  128.  
  129.     // Send function set command sequence
  130.     command(LCD_FUNCTIONSET | _displayfunction);
  131.     delayMicroseconds(4500);  // wait more than 4.1ms
  132.  
  133.     // second try
  134.     command(LCD_FUNCTIONSET | _displayfunction);
  135.     delayMicroseconds(150);
  136.  
  137.     // third go
  138.     command(LCD_FUNCTIONSET | _displayfunction);
  139.   }
  140.  
  141.   // finally, set # lines, font size, etc.
  142.   command(LCD_FUNCTIONSET | _displayfunction);  
  143.  
  144.   // turn the display on with no cursor or blinking default
  145.   _displaycontrol = LCD_DISPLAYON | LCD_CURSOROFF | LCD_BLINKOFF;  
  146.   display();
  147.  
  148.   // clear it off
  149.   //clear();
  150.  
  151.   // Initialize to default text direction (for romance languages)
  152.   _displaymode = LCD_ENTRYLEFT | LCD_ENTRYSHIFTDECREMENT;
  153.   // set the entry mode
  154.   command(LCD_ENTRYMODESET | _displaymode);
  155.  
  156. }
  157.  
  158.  
  159.  
  160. void MyLiquidCrystal::setCursor(uint8_t col, uint8_t row)
  161. {
  162.   const size_t max_lines = sizeof(_row_offsets) / sizeof(*_row_offsets);
  163.   if ( row >= max_lines ) {
  164.     row = max_lines - 1;    // we count rows starting w/0
  165.   }
  166.   if ( row >= _numlines ) {
  167.     row = _numlines - 1;    // we count rows starting w/0
  168.   }
  169.  
  170.   command(LCD_SETDDRAMADDR | (col + _row_offsets[row]));
  171. }
  172.  
  173.  
  174. void MyLiquidCrystal::setRowOffsets(int row0, int row1, int row2, int row3)
  175. {
  176.   _row_offsets[0] = row0;
  177.   _row_offsets[1] = row1;
  178.   _row_offsets[2] = row2;
  179.   _row_offsets[3] = row3;
  180. }
  181.  
  182. void MyLiquidCrystal::write4bits(uint8_t value) {
  183.   for (int i = 0; i < 4; i++) {
  184.     digitalWrite(_data_pins[i], (value >> i) & 0x01);
  185.   }
  186.  
  187.   pulseEnable();
  188. }
  189.  
  190. inline void MyLiquidCrystal::command(uint8_t value) {
  191.   send(value, LOW);
  192. }
  193.  
  194. void MyLiquidCrystal::display() {
  195.   _displaycontrol |= LCD_DISPLAYON;
  196.   command(LCD_DISPLAYCONTROL | _displaycontrol);
  197. }
  198.  
  199. void MyLiquidCrystal::pulseEnable(void) {
  200.   digitalWrite(_enable_pin, LOW);
  201.   delayMicroseconds(1);    
  202.   digitalWrite(_enable_pin, HIGH);
  203.   delayMicroseconds(1);    // enable pulse must be >450ns
  204.   digitalWrite(_enable_pin, LOW);
  205.   delayMicroseconds(100);   // commands need > 37us to settle
  206. }
  207.  
  208.  
  209. void MyLiquidCrystal::send(uint8_t value, uint8_t mode) {
  210.   digitalWrite(_register_select_pin, mode);
  211.  
  212.   // if there is a RW pin indicated, set it low to Write
  213.   if (_read_write_pin != 255) {
  214.     digitalWrite(_read_write_pin, LOW);
  215.   }
  216.  
  217.   if (_displayfunction & LCD_8BITMODE) {
  218.     write8bits(value);
  219.   } else {
  220.     write4bits(value>>4);
  221.     write4bits(value);
  222.   }
  223. }
  224.  
  225. void MyLiquidCrystal::write8bits(uint8_t value) {
  226.   for (int i = 0; i < 8; i++) {
  227.     digitalWrite(_data_pins[i], (value >> i) & 0x01);
  228.   }
  229.  
  230.   pulseEnable();
  231. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement