TivaC Lcd Driver for 1602A LCD controller
A c++ driver for interfacing the TivaC with the 1602A LCD controller
lcd_driver.hpp
Go to the documentation of this file.
1 
9 #ifndef _LCD_DRIVER_HPP
10 #define _LCD_DRIVER_HPP
11 
12 #include <cstdint>
13 
14 #include "general_timer/general_timer.hpp"
15 
19 namespace lcddriver {
20 
24 static const uint32_t LCD_MAX_PRINT_STRING = 32;
25 
30 static const uint32_t TOTAL_PARALLEL_PIN = 4;
31 
37 static const uint32_t PIN_DESCRIPTION_LEN = 3;
38 
42 static const uint32_t PIN_DESC_CLOCK_INDEX = 0;
46 static const uint32_t PIN_DESC_PORT_INDEX = 1;
47 
51 static const uint32_t PIN_DESC_PIN_INDEX = 2;
52 
57 static const uint32_t MAX_TOTAL_CUSTOM_PATTERN = 8;
61 static const uint32_t CUSTOM_CHAR_PATTERN_LEN = 8;
62 
69 typedef struct {
70  bool useBacklight;
71  uint32_t regSelectPin[PIN_DESCRIPTION_LEN];
72  uint32_t readWritePin[PIN_DESCRIPTION_LEN];
73  uint32_t enablePin[PIN_DESCRIPTION_LEN];
74  uint32_t backLightPin[PIN_DESCRIPTION_LEN];
75 
81  uint32_t parallelPinList[TOTAL_PARALLEL_PIN][PIN_DESCRIPTION_LEN];
82 } LcdConfig;
83 
89 class LcdDriver {
90  private:
95  uint32_t _totalBitPerPin;
96 
101 
105  GeneralTimer _generalTimer;
106 
117  void parallelDataRead(const bool &isDataReg, uint8_t *readDataBuf, const uint32_t &totalReadData);
118 
128  void parallelDataWrite(const uint8_t *dataList, const uint32_t &dataLen, const bool &isDataReg);
129 
136  void parallelDataWriteSingle(const uint8_t &data, const bool &isDataReg);
137 
143  void parallelModeSwitch(const bool &isInput);
144 
150  void pinModeSwitch(const uint32_t pinDesc[PIN_DESCRIPTION_LEN], const bool &isInput);
151 
159  void pinWrite(const uint32_t pinDesc[PIN_DESCRIPTION_LEN], const bool &output);
160 
167  bool pinRead(const uint32_t pinDesc[PIN_DESCRIPTION_LEN]);
168 
174  void pinPadConfig(const uint32_t pinDesc[PIN_DESCRIPTION_LEN]);
175 
180  void pinDescCheck(uint32_t pinDesc[PIN_DESCRIPTION_LEN]);
181 
191  void comSetup(const bool &isDataReg, const bool &isReadMode);
192 
197  void comStop(void);
198 
204  void comMaintain(const bool &isReadMode);
205 
211  void registerSelect(const bool &isDataReg);
212 
217  void configWrite(void);
218 
223  void comSwitch(const bool &iscomEnabled);
224 
230  void comModeSwitch(const bool &isReadMode);
231 
240  uint8_t entryModeCommandCreate(const bool &cursorRightDir, const bool &displayShiftEnabled);
241 
250  uint8_t displayCommandCreate(const bool &displayOn,
251  const bool &cursorOn,
252  const bool &isCursorBlink);
253 
263  uint8_t functionSetCommandCreate(const bool &is8BitDataLen,
264  const bool &is2Lines,
265  const bool &is5x10Font);
266 
274  uint8_t cursorDisplayShiftCommandCreate(const bool &isShiftDisplay, const bool &isRight);
275 
281  bool lcdIsBusy(void);
287  uint8_t addrCounterGet(void);
288 
297  void addrCounterChange(const uint8_t &addr, const bool &isDataRam);
298 
307  void dataWrite4Bit(const uint32_t &dataToWrite, const bool &stopAfterWrite);
308 
313  uint8_t instructionDataRead(void);
314 
323  void ramDataWrite(const uint8_t *data, const uint32_t dataLen, const bool &isTextMode);
324 
337  void ramDataRead(uint8_t * returnData,
338  const uint32_t &totalDataRead,
339  const uint8_t & startingRamAddr,
340  const bool & isDataRam);
341 
342  public:
348  LcdDriver(const LcdConfig &lcdConfig);
349 
354  void init(void);
355 
361  void enable(void);
362 
368  void displayWrite(const char *dataToWrite);
369 
378  void displayAppend(const char *dataToAppend);
379 
388  void newCustomCharAdd(const uint8_t charPattern[CUSTOM_CHAR_PATTERN_LEN],
389  const uint32_t &customCharSlot);
390 
398  void lcdSettingSwitch(const bool &displayOn, const bool &cursorOn, const bool &cursorBlinkOn);
399 
403  void lcdReset(void);
411  void cursorPositionChange(const uint8_t &cursorX, const uint8_t &cursorY);
412 
420  void backLedSwitch(const bool &isBackLedOn);
421 };
422 } // namespace lcddriver
423 #endif
void ramDataWrite(const uint8_t *data, const uint32_t dataLen, const bool &isTextMode)
Write to the RAM of the lcd controller, can be data ram or character generator RAM.
Definition: lcd_utils.cpp:230
void newCustomCharAdd(const uint8_t charPattern[CUSTOM_CHAR_PATTERN_LEN], const uint32_t &customCharSlot)
Add new custom character pattern The new pattern will be stored in the custom generator RAM of the lc...
Definition: lcd_driver.cpp:177
the structure used for carrying the lcd controller settings Each pin description is an array of 3 mem...
Definition: lcd_driver.hpp:69
void configWrite(void)
used for writing configs to controller, uses other functions like functionSetCommandCreate to get the...
Definition: lcd_driver.cpp:125
void parallelDataRead(const bool &isDataReg, uint8_t *readDataBuf, const uint32_t &totalReadData)
used for reading the data from the controller RAM/program memory The method will follow procedures ou...
Definition: lcd_utils.cpp:174
uint8_t entryModeCommandCreate(const bool &cursorRightDir, const bool &displayShiftEnabled)
Create a command of entry mode category.
Definition: lcd_utils.cpp:99
void ramDataRead(uint8_t *returnData, const uint32_t &totalDataRead, const uint8_t &startingRamAddr, const bool &isDataRam)
Used for reading the RAM of the lcd controller, it can either be the data ram storing data to be disp...
Definition: lcd_utils.cpp:217
void parallelDataWrite(const uint8_t *dataList, const uint32_t &dataLen, const bool &isDataReg)
used for sending data to the data pins(D0-D7) connected to the LCD The bits are shifted to each pin a...
Definition: lcd_utils.cpp:156
void enable(void)
Start communicating with the lcd and write settings to the lcd controller, this might take more than ...
Definition: lcd_driver.cpp:136
void parallelDataWriteSingle(const uint8_t &data, const bool &isDataReg)
used for writing a single byte to the lcd controller
Definition: lcd_utils.cpp:144
void displayWrite(const char *dataToWrite)
Erase the display and add new text to it starting at position (0,0), this method will be the one used...
Definition: lcd_driver.cpp:157
void lcdReset(void)
reset the LCD and erase all RAM, also reset cursor to (0,0)
Definition: lcd_driver.cpp:192
void parallelModeSwitch(const bool &isInput)
switch all the data pins(like D0-D7) to input mode or output mode Used for quickly switching between ...
Definition: lcd_utils.cpp:138
void lcdSettingSwitch(const bool &displayOn, const bool &cursorOn, const bool &cursorBlinkOn)
Change lcd settings like on/off display, cursor, or blinking mode This method writes to the lcd contr...
Definition: lcd_driver.cpp:186
bool useBacklight
whether the backlight can be turned on/off using pin
Definition: lcd_driver.hpp:70
void registerSelect(const bool &isDataReg)
select data(like RAM) or program register be switching the RS line
Definition: lcd_driver.cpp:88
void pinWrite(const uint32_t pinDesc[PIN_DESCRIPTION_LEN], const bool &output)
switch on/off a pin This is the lowest level function, used for toggling control pin for bitbanging d...
Definition: lcd_utils.cpp:81
uint8_t functionSetCommandCreate(const bool &is8BitDataLen, const bool &is2Lines, const bool &is5x10Font)
Create a command of function set category.
Definition: lcd_utils.cpp:117
void pinPadConfig(const uint32_t pinDesc[PIN_DESCRIPTION_LEN])
configure the pins on things like drive strength(affect rise/fall time of signal), push/pull mode
Definition: lcd_utils.cpp:90
void comModeSwitch(const bool &isReadMode)
switch to read/write mode by switching RW pin
Definition: lcd_driver.cpp:96
void displayAppend(const char *dataToAppend)
Append text to existing text onscreen, this method will also be used the most if there is no need to ...
Definition: lcd_driver.cpp:170
uint8_t cursorDisplayShiftCommandCreate(const bool &isShiftDisplay, const bool &isRight)
create a command to shift the cursor and maybe the display with it
Definition: lcd_utils.cpp:128
bool lcdIsBusy(void)
Read lcd controller memory and see if the lcd is busy with an operation.
Definition: lcd_utils.cpp:268
uint8_t displayCommandCreate(const bool &displayOn, const bool &cursorOn, const bool &isCursorBlink)
Create a command of display category.
Definition: lcd_utils.cpp:107
void pinDescCheck(uint32_t pinDesc[PIN_DESCRIPTION_LEN])
check whether the array describing a pin is valid, will raise assert if not
Definition: lcd_utils.cpp:35
void comStop(void)
stop the communcation by waiting for the current data to be finished transacting and then deassert th...
Definition: lcd_driver.cpp:110
void init(void)
Initialize the lcd driver, by turning on all gpio clocks and set the gpio mode to be ready to drive t...
Definition: lcd_driver.cpp:53
the namespace of the LcdDriver, all lcd controller related files are under this namespace ...
Definition: lcd_driver.cpp:33
LcdConfig _lcdConfig
The LcdDriver copy of the user config received at constructor.
Definition: lcd_driver.hpp:100
void comMaintain(const bool &isReadMode)
temporarily deassert the enable line, wait for the current data to be finished transacting and then a...
Definition: lcd_driver.cpp:116
void comSetup(const bool &isDataReg, const bool &isReadMode)
Utility function used to generate signal to start the communication with the lcd controller The metho...
Definition: lcd_driver.cpp:100
uint32_t _totalBitPerPin
How many bits that each pin has to send, if using 8 then each only has to send 1, but if 4 then each ...
Definition: lcd_driver.hpp:95
void cursorPositionChange(const uint8_t &cursorX, const uint8_t &cursorY)
Change cursor position on an x-y scale This method also changes the data ram position with the cursor...
Definition: lcd_driver.cpp:165
bool pinRead(const uint32_t pinDesc[PIN_DESCRIPTION_LEN])
Read whehther a pin is high or low.
Definition: lcd_utils.cpp:86
uint8_t addrCounterGet(void)
Read lcd controller memory to retrieve the 7 bits address counter This command can be helpful during ...
Definition: lcd_utils.cpp:258
void pinModeSwitch(const uint32_t pinDesc[PIN_DESCRIPTION_LEN], const bool &isInput)
switch a single pin to input/output
Definition: lcd_utils.cpp:73
The main class for the LcdDriver, all interactions with the 1602 lcd controller will be through this ...
Definition: lcd_driver.hpp:89
uint8_t instructionDataRead(void)
Read the lcd controller data to get the busy status and current address counter.
Definition: lcd_utils.cpp:262
void comSwitch(const bool &iscomEnabled)
turn on or off communication by switching the EN pin
Definition: lcd_driver.cpp:92
void addrCounterChange(const uint8_t &addr, const bool &isDataRam)
Change the address counter on the lcd controller This command is used a lot since it changes the addr...
Definition: lcd_utils.cpp:211
GeneralTimer _generalTimer
Instance of general timer used for all timing purposes.
Definition: lcd_driver.hpp:105
void backLedSwitch(const bool &isBackLedOn)
Turn on or off the back light LED The TivaC itself probably doesn&#39;t have the current to turn on or of...
Definition: lcd_driver.cpp:152
LcdDriver(const LcdConfig &lcdConfig)
Construct a new Lcd Driver object, doesn&#39;t initiate any hardware, just compute data to be ready for f...
Definition: lcd_driver.cpp:48
void dataWrite4Bit(const uint32_t &dataToWrite, const bool &stopAfterWrite)
Used to write only 4 bits using D4-D7 This command is used mainly during the beginning of the communi...
Definition: lcd_utils.cpp:198