TivaC Lcd Driver for 1602A LCD controller
A c++ driver for interfacing the TivaC with the 1602A LCD controller
Public Member Functions | Private Member Functions | Private Attributes | List of all members
lcddriver::LcdDriver Class Reference

The main class for the LcdDriver, all interactions with the 1602 lcd controller will be through this class. More...

#include <lcd_driver.hpp>

Collaboration diagram for lcddriver::LcdDriver:
Collaboration graph
[legend]

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.
 

Detailed Description

The main class for the LcdDriver, all interactions with the 1602 lcd controller will be through this class.

Constructor & Destructor Documentation

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.

Parameters
lcdConfigstruct storing settings for lcd driver such as pin allocations

Member Function Documentation

void lcddriver::LcdDriver::addrCounterChange ( const uint8_t &  addr,
const bool &  isDataRam 
)
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.

Parameters
addraddress to change to
isDataRamif true then the address to change to is part of the dataRAM, otherwise part of CGRAM
uint8_t lcddriver::LcdDriver::addrCounterGet ( void  )
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.

Returns
uint8_t the 7 bits address counter
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.

Parameters
isBackLedOnturn on LED if true, off otherwise
void lcddriver::LcdDriver::comMaintain ( const bool &  isReadMode)
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

Parameters
isReadModeis this transaction a read one
void lcddriver::LcdDriver::comModeSwitch ( const bool &  isReadMode)
private

switch to read/write mode by switching RW pin

Parameters
isReadModeif true then RW is pulled high to indicate the next transaction to be a read one
void lcddriver::LcdDriver::comSetup ( const bool &  isDataReg,
const bool &  isReadMode 
)
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.

Parameters
isDataRegtrue if the target of the communication is the data section of the lcd controller(like DDRAM)
isReadModeif true then the transaction is a read one
void lcddriver::LcdDriver::comSwitch ( const bool &  iscomEnabled)
private

turn on or off communication by switching the EN pin

Parameters
iscomEnabledtrue then EN pin pulled high, pulled low otherwise
uint8_t lcddriver::LcdDriver::cursorDisplayShiftCommandCreate ( const bool &  isShiftDisplay,
const bool &  isRight 
)
private

create a command to shift the cursor and maybe the display with it

Parameters
isShiftDisplayif true allow display to shift if required
isRightif true, set the movement direction to right
Returns
uint8_t a command that can be sent to the lcd controller to control cursor movement
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.

Parameters
cursorXx coordinate to set, max at 15 for 16x2 lcd
cursorYy coordinate to set, max at 1 for 16x2 lcd
void lcddriver::LcdDriver::dataWrite4Bit ( const uint32_t &  dataToWrite,
const bool &  stopAfterWrite 
)
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.

Parameters
dataToWritedata to be written, most likely a command to setup communication
stopAfterWriteif 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.

Parameters
dataToAppendcharacter array reprenting string to print, limited at 32
uint8_t lcddriver::LcdDriver::displayCommandCreate ( const bool &  displayOn,
const bool &  cursorOn,
const bool &  isCursorBlink 
)
private

Create a command of display category.

Parameters
displayOnif true, turn on display, otherwise off
cursorOnif true, turn on cursor, otherwise off
isCursorBlinkif true, blink cursor, otherwise no blink
Returns
uint8_t a display command that can be sent to the lcd controller
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.

Parameters
dataToWritecharacter array reprenting string to print, limited at 32
uint8_t lcddriver::LcdDriver::entryModeCommandCreate ( const bool &  cursorRightDir,
const bool &  displayShiftEnabled 
)
private

Create a command of entry mode category.

Parameters
cursorRightDirif true, set the direction of movement to the right
displayShiftEnabledif true, allow the display to be shifted when necessary(check the manual for this)
Returns
uint8_t an entry command that can be sent to the lcd controller
uint8_t lcddriver::LcdDriver::functionSetCommandCreate ( const bool &  is8BitDataLen,
const bool &  is2Lines,
const bool &  is5x10Font 
)
private

Create a command of function set category.

Parameters
is8BitDataLenif true, then send data over 8 pins, otherwise, send data using 4 pins(D4-D7)
is2Linesif true, display 2 lines on lcd, display 1 otherwise
is5x10Fontif true, use 5x10 font, use 5x8 font otherwise
Returns
uint8_t a function set command that can be sent to the lcd controller
uint8_t lcddriver::LcdDriver::instructionDataRead ( void  )
private

Read the lcd controller data to get the busy status and current address counter.

Returns
uint8_t the busy bit(bit 7) and the address counter(bit 0-6)
bool lcddriver::LcdDriver::lcdIsBusy ( void  )
private

Read lcd controller memory and see if the lcd is busy with an operation.

Returns
true lcd is doing an operation
false lcd is idle
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.

Parameters
displayOndisplay on if true, off otherwise
cursorOncursor on if true, off otherwise
cursorBlinkOncursor 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.

Parameters
charPatternarray storing the byte patterns, generate them using the link in the README
customCharSlotwhat slot to store the new pattern at, there should be 8 slot if 5x8 font is used
void lcddriver::LcdDriver::parallelDataRead ( const bool &  isDataReg,
uint8_t *  readDataBuf,
const uint32_t &  totalReadData 
)
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

Parameters
isDataRegtrue if reading from the data memory(like RAM), false if reading from lcd controller register(where it has things like the busy status)
readDataBufbuffer for storing the data, array of byte
totalReadDatahow many bytes to read
void lcddriver::LcdDriver::parallelDataWrite ( const uint8_t *  dataList,
const uint32_t &  dataLen,
const bool &  isDataReg 
)
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

Parameters
dataListarray of data to be sent
dataLenhow many byte to send
isDataRegis the destination data memory(like RAM) or lcd controller memory
void lcddriver::LcdDriver::parallelDataWriteSingle ( const uint8_t &  data,
const bool &  isDataReg 
)
private

used for writing a single byte to the lcd controller

Parameters
datathe byte to be written
isDataRegis the destination data memory(like RAM) or lcd controller memory
void lcddriver::LcdDriver::parallelModeSwitch ( const bool &  isInput)
private

switch all the data pins(like D0-D7) to input mode or output mode Used for quickly switching between receiving and sending data

Parameters
isInputif true then all data pin becomes input else become output
void lcddriver::LcdDriver::pinDescCheck ( uint32_t  pinDesc[PIN_DESCRIPTION_LEN])
private

check whether the array describing a pin is valid, will raise assert if not

Parameters
pinDescarray describing the pin
void lcddriver::LcdDriver::pinModeSwitch ( const uint32_t  pinDesc[PIN_DESCRIPTION_LEN],
const bool &  isInput 
)
private

switch a single pin to input/output

Parameters
pinDescarray describing the pin
isInputif true, set pin to input, otherwise, set pin to false
void lcddriver::LcdDriver::pinPadConfig ( const uint32_t  pinDesc[PIN_DESCRIPTION_LEN])
private

configure the pins on things like drive strength(affect rise/fall time of signal), push/pull mode

Parameters
pinDescarray describing the pin
bool lcddriver::LcdDriver::pinRead ( const uint32_t  pinDesc[PIN_DESCRIPTION_LEN])
private

Read whehther a pin is high or low.

Parameters
pinDescarray describing the pin
Returns
true pin is high
false pin is low
void lcddriver::LcdDriver::pinWrite ( const uint32_t  pinDesc[PIN_DESCRIPTION_LEN],
const bool &  output 
)
private

switch on/off a pin This is the lowest level function, used for toggling control pin for bitbanging during communcication

Parameters
pinDescarray describing the pin
outputif true, set pin to high, otherwise, set to low
void lcddriver::LcdDriver::ramDataRead ( uint8_t *  returnData,
const uint32_t &  totalDataRead,
const uint8_t &  startingRamAddr,
const bool &  isDataRam 
)
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.

Parameters
returnDatathe buffer to store received data
totalDataReadhow many bytes to read from the RAM
startingRamAddrstarting 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
isDataRamtrue if reading from data RAM(DDRAM), false if from character generator RAM(CGRAM)
void lcddriver::LcdDriver::ramDataWrite ( const uint8_t *  data,
const uint32_t  dataLen,
const bool &  isTextMode 
)
private

Write to the RAM of the lcd controller, can be data ram or character generator RAM.

Parameters
dataarrays of data to be written
dataLenlen of the array of data
isTextModetrue if in text mode, text mode is helpful for writing to data RAM to be displayed, text mode will interpret special character like
or $
void lcddriver::LcdDriver::registerSelect ( const bool &  isDataReg)
private

select data(like RAM) or program register be switching the RS line

Parameters
isDataRegif true then the RS line is pulled high to indicate that transaction will target RAM region, otherwise target the lcd controller program memory

The documentation for this class was generated from the following files: