Skip to content

Instantly share code, notes, and snippets.

@michaellin
Created December 1, 2015 04:37
Show Gist options
  • Save michaellin/9072db15473c9de3e98f to your computer and use it in GitHub Desktop.
Save michaellin/9072db15473c9de3e98f to your computer and use it in GitHub Desktop.
/***Helper module for writing to LCD inputs and controlling RGB LED strip from shift register***/
// the common headers for C99 types
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
// the headers to access the GPIO subsystem
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "inc/hw_gpio.h"
#include "inc/hw_sysctl.h"
// the headers to access the TivaWare Library
#include "driverlib/sysctl.h"
#include "driverlib/pin_map.h"
#include "driverlib/gpio.h"
#include "driverlib/timer.h"
#include "driverlib/interrupt.h"
#include "BITDEFS.H"
#include "termio.h"
// headers for user-defined modules
#include "SR_for_LCD.h"
#include "LCDmod.h"
// module level defines
#define LCD_COMMAND 0
#define LCD_DATA 1
// A variable for the last 8 bits written to the shift register
static uint8_t ShiftRegisterImage;
// place to keep track of which register we are writing to
static uint8_t RegisterSelect;
/**************************************************************
Function: LCD_HWInit
Takes: nothing
Returns: nothing
Purpose: initializes shift register connected to LCD
/*************************************************************/
void LCD_HWInit(void){
SR_Init_LCD();
}
/**************************************************************
Function: LCD_RegisterSelect
Takes: 8 bit integer
Returns: nothing
Purpose: passes input parameter to RegisterSelect variable
/*************************************************************/
static void LCD_RegisterSelect(uint8_t WhichReg){ //WhichReg is either LCD_COMMAND or LCD_DATA
RegisterSelect = WhichReg ;
}
/**************************************************************
Function: LCD_SetData4
Takes: 8 bit integer
Returns: nothing
Purpose: sets 4 bits of data at the input to the LCD
/*************************************************************/
static void LCD_SetData4(uint8_t NewData){
// get the current value of the port so that we can preserve the other bit states
uint8_t CurrentValue;
CurrentValue = SR_GetCurrentRegister_LCD();
// insert the current state of RegisterSelect into bit 6
if (RegisterSelect == LCD_COMMAND){
CurrentValue &= BIT6LO;
}
else {
CurrentValue |= BIT6HI;
}
// clear 4 LSB, while preserving 4 MSB
CurrentValue &= (BIT7HI|BIT6HI|BIT5HI|BIT4HI);
// OR currentvalue with 4LSB of NewData
CurrentValue |= (NewData & (BIT0HI|BIT1HI|BIT2HI|BIT3HI));
// write data into register
SR_Write_LCD(CurrentValue);
}
/**************************************************************
Function: LCD_PulseEnable
Takes: nothing
Returns: nothing
Purpose: pulses enable clock on LCD
/*************************************************************/
static void LCD_PulseEnable(void){
uint8_t ThisValue;
// get the current value of the port so that we can preserve the other bit states
ThisValue = SR_GetCurrentRegister_LCD();
// pulse enable line high
SR_Write_LCD(ThisValue | BIT7HI);
// pulse enable line low
SR_Write_LCD(ThisValue & BIT7LO);
}
/**************************************************************
Function: LCD_Write4
Takes: 8 bit integer
Returns: nothing
Purpose: Writes a 4 bit nibble to LCD
/*************************************************************/
static void LCD_Write4(uint8_t NewData){
// put the 4 bits of data onto the LCD data lines
LCD_SetData4(NewData);
// pulse the enable line to complete the write
LCD_PulseEnable();
}
/**************************************************************
Function: LCD_Write8v2 (version 2 of LCD_Write8)
Takes: 8 bit integer
Returns: nothing
Purpose: Writes an 8 bit nibble of data to LCD.
/*************************************************************/
static void LCD_Write8v2(uint8_t NewData){
// Shift 4MSB into 4LSB positions
uint8_t NewDataMSB = NewData>>4;
// Write nibble to LCD
LCD_Write4(NewDataMSB);
// Mask NewData to get 4LSB
uint8_t NewDataLSB = NewData&0xff; // Mask for 4 LSB
// Write nibble to LCD
LCD_Write4(NewDataLSB);
}
/**************************************************************
Function: LCD_WriteCommand4
Takes: 8 bit integer
Returns: nothing
Purpose: Writes a 4 bit nibble to the LCD in the form of a command.
/*************************************************************/
void LCD_WriteCommand4(uint8_t NewData){
// Clear the register select bit
LCD_RegisterSelect(LCD_COMMAND);
// Write the 4LSB to the shift register
LCD_Write4(NewData);
}
/**************************************************************
Function: LCD_WriteCommand8
Takes: 8 bit integer
Returns: nothing
Purpose: Writes an 8 bit command to the LCD via two 4 bit nibbles.
/*************************************************************/
void LCD_WriteCommand8(uint8_t NewData){
// Clear the register select bit
LCD_RegisterSelect(LCD_COMMAND);
// Write 8 bit command to LCD
LCD_Write8v2(NewData);
}
/**************************************************************
Function: LCD_WriteData8
Takes: 8 bit integer
Returns: nothing
Purpose: Writes 8 bits of data to the LCD via two 4 bit nibbles.
/*************************************************************/
void LCD_WriteData8(uint8_t NewData){
// Set the register select bit high
LCD_RegisterSelect(LCD_DATA);
// Write 8 bits of data to the shift register via two 4 bit nibbles
LCD_Write8v2(NewData);
}
/******** THE FOLLOWING FUNCTIONS SUPPORT THE RGB LED STRIP USED IN CELEBRATION MODE********/
/**************************************************************
Function: LCD_RedLEDOn
Takes: nothing
Returns: nothing
Purpose: Turn on red LEDs on RGB LED strip.
/*************************************************************/
void LCD_RedLEDOn( void ) { //Using Bit 4 i.e. Q3 or QD
// Initialize CurrentValue variable
uint8_t CurrentValue;
// Get current image of shift register data lines
ShiftRegisterImage = SR_GetCurrentRegister_LCD();
// Set bit 4 high on shift register without altering LCD inputs
CurrentValue = ShiftRegisterImage | 0x10;
// Write this to shift register outputs
SR_Write_LCD(CurrentValue);
}
/**************************************************************
Function: LCD_RedLEDOff
Takes: nothing
Returns: nothing
Purpose: Turn off red LEDs on RGB LED strip.
/*************************************************************/
void LCD_RedLEDOff( void ) { //Using Bit 4 i.e. Q3 or QD
// Initialize CurrentValue variable
uint8_t CurrentValue;
// Get current image of shift register data lines
ShiftRegisterImage = SR_GetCurrentRegister_LCD();
// Set bit 4 low on shift register without altering LCD inputs
CurrentValue = ShiftRegisterImage & ~0x10;
// Write this to shift register outputs
SR_Write_LCD(CurrentValue);
}
/**************************************************************
Function: LCD_BlueLEDOn
Takes: nothing
Returns: nothing
Purpose: Turn on blue LEDs on RGB LED strip.
/*************************************************************/
void LCD_BlueLEDOn( void ) { //Using Bit 5 i.e. Q2 or QC
// Initialize CurrentValue variable
uint8_t CurrentValue;
// Get current image of shift register data lines
ShiftRegisterImage = SR_GetCurrentRegister_LCD();
// Set bit 5 high on shift register without altering LCD inputs
CurrentValue = ShiftRegisterImage | 0x20;
// Write this to shift register outputs
SR_Write_LCD(CurrentValue);
}
/**************************************************************
Function: LCD_BlueLEDOff
Takes: nothing
Returns: nothing
Purpose: Turn off blue LEDs on RGB LED strip.
/*************************************************************/
void LCD_BlueLEDOff( void ) { //Using Bit 5 i.e. Q2 or QC
// Initialize CurrentValue variable
uint8_t CurrentValue;
// Get current image of shift register data lines
ShiftRegisterImage = SR_GetCurrentRegister_LCD();
// Set bit 5 low on shift register without altering LCD inputs
CurrentValue = ShiftRegisterImage & ~0x20;
// Write this to shift register outputs
SR_Write_LCD(CurrentValue);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment