TivaC Lcd Driver for 1602A LCD controller
A c++ driver for interfacing the TivaC with the 1602A LCD controller
|
The main class for the LcdDriver, all interactions with the 1602 lcd controller will be through this class. More...
#include <lcd_driver.hpp>
Public Member Functions | |
LcdDriver (const LcdConfig &lcdConfig) | |
Construct a new Lcd Driver object, doesn't initiate any hardware, just compute data to be ready for future operations. More... | |
void | init (void) |
Initialize the lcd driver, by turning on all gpio clocks and set the gpio mode to be ready to drive the lcd. | |
void | enable (void) |
Start communicating with the lcd and write settings to the lcd controller, this might take more than other operations since it will wait the recommended amount of time in the manual for the lcd to warm up. | |
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 the most as it offers the most straightforward interface to writing to the LCD. More... | |
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 modify the deeper level API Note that during other operation, the cursor might have been moved, if that happens, it's best to use the cursorPositionChange to get back to the desired position and continue printing text. More... | |
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 lcd controller, the 1602 can store 8 5x8 or 4 5x10 pattern. More... | |
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 controller register to set the settings. More... | |
void | lcdReset (void) |
reset the LCD and erase all RAM, also reset cursor to (0,0) | |
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 so future texts will be printed at the cursor. More... | |
void | backLedSwitch (const bool &isBackLedOn) |
Turn on or off the back light LED The TivaC itself probably doesn't have the current to turn on or off the backLight alone so probably use a relay or a transistor. More... | |
Private Member Functions | |
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 outlined in the datasheet to intiate and read data from the controller, if using 4 pins it will take two transfers to deliver 8 bits instead of 1 if using 8 pins More... | |
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 and then a proper setup and hold time is followed to make sure that the controller receives the data, the method will follow the correct procedure to intitiate connection with the controller More... | |
void | parallelDataWriteSingle (const uint8_t &data, const bool &isDataReg) |
used for writing a single byte to the lcd controller More... | |
void | parallelModeSwitch (const bool &isInput) |
switch all the data pins(like D0-D7) to input mode or output mode Used for quickly switching between receiving and sending data More... | |
void | pinModeSwitch (const uint32_t pinDesc[PIN_DESCRIPTION_LEN], const bool &isInput) |
switch a single pin to input/output More... | |
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 during communcication More... | |
bool | pinRead (const uint32_t pinDesc[PIN_DESCRIPTION_LEN]) |
Read whehther a pin is high or low. More... | |
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 More... | |
void | pinDescCheck (uint32_t pinDesc[PIN_DESCRIPTION_LEN]) |
check whether the array describing a pin is valid, will raise assert if not More... | |
void | comSetup (const bool &isDataReg, const bool &isReadMode) |
Utility function used to generate signal to start the communication with the lcd controller The method will put the RS, RW line in the correct mode(depending on other params passed to this function), pull high enable pin and then wait for the signal to become stable then exit. More... | |
void | comStop (void) |
stop the communcation by waiting for the current data to be finished transacting and then deassert the enable line | |
void | comMaintain (const bool &isReadMode) |
temporarily deassert the enable line, wait for the current data to be finished transacting and then asser the enable line again to continue communication More... | |
void | registerSelect (const bool &isDataReg) |
select data(like RAM) or program register be switching the RS line More... | |
void | configWrite (void) |
used for writing configs to controller, uses other functions like functionSetCommandCreate to get the command then push it to the lcd controller | |
void | comSwitch (const bool &iscomEnabled) |
turn on or off communication by switching the EN pin More... | |
void | comModeSwitch (const bool &isReadMode) |
switch to read/write mode by switching RW pin More... | |
uint8_t | entryModeCommandCreate (const bool &cursorRightDir, const bool &displayShiftEnabled) |
Create a command of entry mode category. More... | |
uint8_t | displayCommandCreate (const bool &displayOn, const bool &cursorOn, const bool &isCursorBlink) |
Create a command of display category. More... | |
uint8_t | functionSetCommandCreate (const bool &is8BitDataLen, const bool &is2Lines, const bool &is5x10Font) |
Create a command of function set category. More... | |
uint8_t | cursorDisplayShiftCommandCreate (const bool &isShiftDisplay, const bool &isRight) |
create a command to shift the cursor and maybe the display with it More... | |
bool | lcdIsBusy (void) |
Read lcd controller memory and see if the lcd is busy with an operation. More... | |
uint8_t | addrCounterGet (void) |
Read lcd controller memory to retrieve the 7 bits address counter This command can be helpful during debug to probe what memories are being read. More... | |
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 address counter to be ready for the read/write operation. More... | |
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 communication where all data is sent using 4 pins only once instead of 4 pins twice like the rest of the communication. More... | |
uint8_t | instructionDataRead (void) |
Read the lcd controller data to get the busy status and current address counter. More... | |
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. More... | |
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 displayed or the character generator ram, storing custom pattern The reading operation may result in the cursor moving. More... | |
Private Attributes | |
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 has to do 2. | |
LcdConfig | _lcdConfig |
The LcdDriver copy of the user config received at constructor. | |
GeneralTimer | _generalTimer |
Instance of general timer used for all timing purposes. | |
The main class for the LcdDriver, all interactions with the 1602 lcd controller will be through this class.
lcddriver::LcdDriver::LcdDriver | ( | const LcdConfig & | lcdConfig | ) |
Construct a new Lcd Driver object, doesn't initiate any hardware, just compute data to be ready for future operations.
lcdConfig | struct storing settings for lcd driver such as pin allocations |
|
private |
Change the address counter on the lcd controller This command is used a lot since it changes the address counter to be ready for the read/write operation.
addr | address to change to |
isDataRam | if true then the address to change to is part of the dataRAM, otherwise part of CGRAM |
|
private |
Read lcd controller memory to retrieve the 7 bits address counter This command can be helpful during debug to probe what memories are being read.
void lcddriver::LcdDriver::backLedSwitch | ( | const bool & | isBackLedOn | ) |
Turn on or off the back light LED The TivaC itself probably doesn't have the current to turn on or off the backLight alone so probably use a relay or a transistor.
isBackLedOn | turn on LED if true, off otherwise |
|
private |
temporarily deassert the enable line, wait for the current data to be finished transacting and then asser the enable line again to continue communication
isReadMode | is this transaction a read one |
|
private |
switch to read/write mode by switching RW pin
isReadMode | if true then RW is pulled high to indicate the next transaction to be a read one |
|
private |
Utility function used to generate signal to start the communication with the lcd controller The method will put the RS, RW line in the correct mode(depending on other params passed to this function), pull high enable pin and then wait for the signal to become stable then exit.
isDataReg | true if the target of the communication is the data section of the lcd controller(like DDRAM) |
isReadMode | if true then the transaction is a read one |
|
private |
turn on or off communication by switching the EN pin
iscomEnabled | true then EN pin pulled high, pulled low otherwise |
|
private |
create a command to shift the cursor and maybe the display with it
isShiftDisplay | if true allow display to shift if required |
isRight | if true, set the movement direction to right |
void lcddriver::LcdDriver::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 so future texts will be printed at the cursor.
cursorX | x coordinate to set, max at 15 for 16x2 lcd |
cursorY | y coordinate to set, max at 1 for 16x2 lcd |
|
private |
Used to write only 4 bits using D4-D7 This command is used mainly during the beginning of the communication where all data is sent using 4 pins only once instead of 4 pins twice like the rest of the communication.
dataToWrite | data to be written, most likely a command to setup communication |
stopAfterWrite | if true terminate connection after write, otherwise maintain the connection with the lcd controller |
void lcddriver::LcdDriver::displayAppend | ( | const char * | dataToAppend | ) |
Append text to existing text onscreen, this method will also be used the most if there is no need to modify the deeper level API Note that during other operation, the cursor might have been moved, if that happens, it's best to use the cursorPositionChange to get back to the desired position and continue printing text.
dataToAppend | character array reprenting string to print, limited at 32 |
|
private |
Create a command of display category.
displayOn | if true, turn on display, otherwise off |
cursorOn | if true, turn on cursor, otherwise off |
isCursorBlink | if true, blink cursor, otherwise no blink |
void lcddriver::LcdDriver::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 the most as it offers the most straightforward interface to writing to the LCD.
dataToWrite | character array reprenting string to print, limited at 32 |
|
private |
Create a command of entry mode category.
cursorRightDir | if true, set the direction of movement to the right |
displayShiftEnabled | if true, allow the display to be shifted when necessary(check the manual for this) |
|
private |
Create a command of function set category.
is8BitDataLen | if true, then send data over 8 pins, otherwise, send data using 4 pins(D4-D7) |
is2Lines | if true, display 2 lines on lcd, display 1 otherwise |
is5x10Font | if true, use 5x10 font, use 5x8 font otherwise |
|
private |
Read the lcd controller data to get the busy status and current address counter.
|
private |
Read lcd controller memory and see if the lcd is busy with an operation.
void lcddriver::LcdDriver::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 controller register to set the settings.
displayOn | display on if true, off otherwise |
cursorOn | cursor on if true, off otherwise |
cursorBlinkOn | cursor blinking on if true, off otherwise |
void lcddriver::LcdDriver::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 lcd controller, the 1602 can store 8 5x8 or 4 5x10 pattern.
charPattern | array storing the byte patterns, generate them using the link in the README |
customCharSlot | what slot to store the new pattern at, there should be 8 slot if 5x8 font is used |
|
private |
used for reading the data from the controller RAM/program memory The method will follow procedures outlined in the datasheet to intiate and read data from the controller, if using 4 pins it will take two transfers to deliver 8 bits instead of 1 if using 8 pins
isDataReg | true if reading from the data memory(like RAM), false if reading from lcd controller register(where it has things like the busy status) |
readDataBuf | buffer for storing the data, array of byte |
totalReadData | how many bytes to read |
|
private |
used for sending data to the data pins(D0-D7) connected to the LCD The bits are shifted to each pin and then a proper setup and hold time is followed to make sure that the controller receives the data, the method will follow the correct procedure to intitiate connection with the controller
dataList | array of data to be sent |
dataLen | how many byte to send |
isDataReg | is the destination data memory(like RAM) or lcd controller memory |
|
private |
used for writing a single byte to the lcd controller
data | the byte to be written |
isDataReg | is the destination data memory(like RAM) or lcd controller memory |
|
private |
switch all the data pins(like D0-D7) to input mode or output mode Used for quickly switching between receiving and sending data
isInput | if true then all data pin becomes input else become output |
|
private |
check whether the array describing a pin is valid, will raise assert if not
pinDesc | array describing the pin |
|
private |
switch a single pin to input/output
pinDesc | array describing the pin |
isInput | if true, set pin to input, otherwise, set pin to false |
|
private |
configure the pins on things like drive strength(affect rise/fall time of signal), push/pull mode
pinDesc | array describing the pin |
|
private |
Read whehther a pin is high or low.
pinDesc | array describing the pin |
|
private |
switch on/off a pin This is the lowest level function, used for toggling control pin for bitbanging during communcication
pinDesc | array describing the pin |
output | if true, set pin to high, otherwise, set to low |
|
private |
Used for reading the RAM of the lcd controller, it can either be the data ram storing data to be displayed or the character generator ram, storing custom pattern The reading operation may result in the cursor moving.
returnData | the buffer to store received data |
totalDataRead | how many bytes to read from the RAM |
startingRamAddr | starting address of the RAM, depending on the type of memory selected the method will append the correct bits to indicate the correct memory region, for example, for DDRAM, bit 7 is set |
isDataRam | true if reading from data RAM(DDRAM), false if from character generator RAM(CGRAM) |
|
private |
Write to the RAM of the lcd controller, can be data ram or character generator RAM.
data | arrays of data to be written |
dataLen | len of the array of data |
isTextMode | true if in text mode, text mode is helpful for writing to data RAM to be displayed, text mode will interpret special character like or $ |
|
private |
select data(like RAM) or program register be switching the RS line
isDataReg | if true then the RS line is pulled high to indicate that transaction will target RAM region, otherwise target the lcd controller program memory |