Skip to content

Instantly share code, notes, and snippets.

@michaellin
Created December 1, 2015 04:52
Show Gist options
  • Save michaellin/5e7c617f4ff94f3e8c94 to your computer and use it in GitHub Desktop.
Save michaellin/5e7c617f4ff94f3e8c94 to your computer and use it in GitHub Desktop.
/***Service module that implements a state machine for writing individual characters and commands to LCD***/
/*----------------------------- Include Files -----------------------------*/
#include "ES_Configure.h"
#include "ES_Framework.h"
#include "ES_DeferRecall.h"
#include "ES_ShortTimer.h"
#include "LCDService.h"
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "inc/hw_gpio.h"
#include "inc/hw_sysctl.h"
#include "driverlib/sysctl.h"
#include "driverlib/pin_map.h"
#include "driverlib/gpio.h"
#include "SR_for_LCD.h"
#include "LCDmod.h"
#include "LCDService.h"
/*---------------------------- Module Variables ---------------------------*/
static uint8_t MyPriority;
static LCDState_t CurrentState = InitPState;
static uint8_t InitCounter = 0;
static uint8_t CharToPut;
/**************************************************************
Function: InitLCDService
Takes: 8 bit integer (priority number)
Returns: true or false
Purpose: Stores priority number of this service inside the module and puts state machine into initial pseudo-state.
/*************************************************************/
bool InitLCDService ( uint8_t Priority )
{
ES_Event ThisEvent;
// Store priority number in a stati variable
MyPriority = Priority;
// Put us into the initial pseudo-state to set up for the initial transition
CurrentState = InitPState;
// Set up the short timer for inter-command timings
ES_ShortTimerInit(MyPriority, SHORT_TIMER_UNUSED );
// Post the initial transition event
ThisEvent.EventType = ES_INIT;
if (ES_PostToService( MyPriority, ThisEvent) == true){
return true;
}
else{
return false;
}
}
/**************************************************************
Function: PostLCDService
Takes: ES_Event
Returns: true or false
Purpose: Posts ES_Event to LCDService queue.
/*************************************************************/
bool PostLCDService( ES_Event ThisEvent )
{
return ES_PostToService( MyPriority, ThisEvent);
}
/**************************************************************
Function: RunLCDService
Takes: ES_Event
Returns: ES_Event (ES_NO_EVENT)
Purpose: Runs the LCDService state machine.
/*************************************************************/
ES_Event RunLCDService( ES_Event ThisEvent )
{
ES_Event ReturnEvent;
ReturnEvent.EventType = ES_NO_EVENT; // assume no errors
// Switch
switch (CurrentState){
case InitPState :
if ( ThisEvent.EventType == ES_INIT ){
// Initialize the necessary Tiva pins
LCD_HWInit();
// Increment initialization counter
InitCounter++;
// Start the timer for max short timer delay
ES_ShortTimerStart(TIMER_A, 65535);
// move to the Initializing state
CurrentState = Initializing ;
}
break;
case Initializing :
if( ThisEvent.EventType == ES_SHORT_TIMEOUT ){
// Take appropriate initialization step, then set short timer to delay
if (InitCounter==1){
LCD_WriteCommand4(0x2);
ES_ShortTimerStart(TIMER_A, 450);
}
else if (InitCounter==2){
LCD_WriteCommand4(0x2);
ES_ShortTimerStart(TIMER_A, 450);
}
else if (InitCounter==3){
LCD_WriteCommand4(0x2);
ES_ShortTimerStart(TIMER_A, 450);
}
else if (InitCounter==4){
LCD_WriteCommand4(0x8);
ES_ShortTimerStart(TIMER_A, 450);
}
else if (InitCounter==5){
LCD_WriteCommand8(0x08);
ES_ShortTimerStart(TIMER_A, 450);
}
else if (InitCounter==6){
LCD_WriteCommand8(0x01);
ES_ShortTimerStart(TIMER_A, 4500);
}
else if (InitCounter==7){
LCD_WriteCommand8(0x06);
ES_ShortTimerStart(TIMER_A, 450);
}
else if (InitCounter==8){
LCD_WriteCommand8(0x0f);
ES_ShortTimerStart(TIMER_A, 450);
}
else if (InitCounter==9){
LCD_WriteCommand8(0x80);
ES_ShortTimerStart(TIMER_A, 450);
}
else if (InitCounter==10){
CurrentState = Waiting2Write;
}
// Increment initialization counter
InitCounter++;
}
break;
case Waiting2Write :
if (ThisEvent.EventType == ES_LCD_PUTCHAR ){
// Write character to LCD
LCD_WriteData8(ThisEvent.EventParam) ;
// Start the inter-character timer (Inter char delay = 53 us)
ES_ShortTimerStart(TIMER_A,53);
// Move to the PausingBetweenWrites state
CurrentState = PausingBetweenWrites;
}
else if (ThisEvent.EventType==ES_LCD_COMMAND) {
// Write command to LCD
LCD_WriteCommand8(ThisEvent.EventParam);
// Start the inter-command timer
ES_ShortTimerStart(TIMER_A,53);
// Move to the PausingBetweenWrites state
CurrentState = PausingBetweenWrites;
}
else if (ThisEvent.EventType==ES_LCD_RESET){
// Move to initializing state
CurrentState = Initializing;
// Reset initialization counter to 1
InitCounter = 1;
// Delay 4500 us before taking first initialization step
ES_ShortTimerStart(TIMER_A, 4500);
}
break;
case PausingBetweenWrites :
if( ThisEvent.EventType == ES_SHORT_TIMEOUT ){
// Move to Waiting2Write state
CurrentState = Waiting2Write;
}
else if (ThisEvent.EventType==ES_LCD_RESET){
// Move to initializing state
CurrentState = Initializing;
// Reset initialization counter to 1
InitCounter = 1;
// Delay 4500 us before taking first initialization step
ES_ShortTimerStart(TIMER_A, 4500);
}
break;
}
// Return ES_NO_EVENT
return ReturnEvent;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment