Skip to content

Instantly share code, notes, and snippets.

@ajarmst
Created April 8, 2022 19:30
Show Gist options
  • Save ajarmst/762bcf6701cd2fbc5d69b2978f633013 to your computer and use it in GitHub Desktop.
Save ajarmst/762bcf6701cd2fbc5d69b2978f633013 to your computer and use it in GitHub Desktop.
Basic Hitachi LCD Demo
/////////////////////////////////////////////////////////////////////////////
// Basic Hitachi LCD Demo
/////////////////////////////////////////////////////////////////////////////
#include <hidef.h> /* common defines and macros */
#include "derivative.h" /* derivative-specific definitions */
#include "pll.h"
#include "swled.h"
#include "timer.h"
#include "lcd.h"
#include "segs.h"
/////////////////////////////////////////////////////////////////////////////
// Main Entry
/////////////////////////////////////////////////////////////////////////////
void main(void)
{
int i = 0;
char szName[] = "AJ Armstrong";
char szProgram[] = "Computer Engineering Technology";
char * p = 0;
// main entry point - these two lines must appear first
_DISABLE_COP();
EnableInterrupts;
/////////////////////////////////////////////////////////////////////////////
// one-time initializations
/////////////////////////////////////////////////////////////////////////////
PLL_To20MHz();
(void) SWL_Init();
(void) TMR_Init(eTMR_Prescale_128, 0, 0, eTMR_Pin_Toggle);
(void) LCD_Init();
(void) SEG_Init();
//LCD now initialized and I have LCD_Inst() and LCD_Data()
//Clear the LCD
LCD_Inst(0x01);
//Move cursor home
LCD_Inst(0x02);
//As currently configured, the LCD cursor will advance after
//printing a character, so I don't have to move it manually
//Let's write some characters:
LCD_Data('C');
LCD_Data('M');
LCD_Data('P');
LCD_Data('E');
LCD_Data('1');
LCD_Data('2');
LCD_Data('5');
LCD_Data('0');
//But that is really tedious. Can I just use a string?
//Yep. First, let's move the cursor to the next row:
LCD_Inst(0x80 | 0x14); // 0x80 = command prefix to move cursor! 14 is row 3
//Using array syntax:
for (i = 0 ; szName[i] != 0 ; ++i)
LCD_Data(szName[i]);
//Move to row 2:
LCD_Inst(0x80 | 0x40);
//Using pointer syntax
p = szProgram; // Pointer to first character
while(*p) // while the character pointed to is not 0 (NUL)
LCD_Data(*(p++)); //write character pointed to and increment pointer to next char
//Let's try something fancier:
//Turn on shifting: 0x04 is entry mode; 0x02 is l to r; 0x01 is shift enabled
LCD_Inst(0x04 | 0x02 | 0x01);
//Main loop
for (i = 0 ; ; ++i)
{
TMR_Sleep(500);
// Set up a loop with 8 possibilities
switch(i % 16) // will iterate from 0 to 15 then restart
{
//Move right for first 8
case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
//0x10 = Cursor shift; 0x08 is Shift display; 0x04 is RtoL
LCD_Inst(0x10 | 0x08 | 0x04);
break;
//Move left for the others
default:
//0x10 = Cursor shift; 0x08 is Shift display;
LCD_Inst(0x10 | 0x08);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment