Skip to content

Instantly share code, notes, and snippets.

@ajarmst
Last active December 10, 2023 21:15
Show Gist options
  • Save ajarmst/3100237976e0a19d44d1e768dd615cf1 to your computer and use it in GitHub Desktop.
Save ajarmst/3100237976e0a19d44d1e768dd615cf1 to your computer and use it in GitHub Desktop.
Demo of custom character generation on the Hitachi LCD interface.
/////////////////////////////////////////////////////////////////////////////
// HC12 Program: I2C Demo
// Processor: MC9S12XDP512
// Bus Speed: 20 MHz (Requires Active PLL)
// Author: AJ Armstrong
// Details: Simple Demo of CGRAM on the Hitachi LCD
// Date: Dec 2023
// Revision History :
/////////////////////////////////////////////////////////////////////////////
#include <stdio.h> // For sprintf, etc
#include <ctype.h> // For character utilities
#include <string.h> // Various string utilities
#include <hidef.h> // Common defines and macros
#include "derivative.h" // Derivative-specific definitions
#include "pll.h" // Phase-locked loop clock control
#include "swled.h" // GPIO switches and LEDs
#include "segs.h" // 7-Segment Displays
#include "timer.h" // Enhanced capture timer
#include "portj.h" // Interrupt buttons
#include "pit.h" // Periodic Interrupt timer
#include "rti.h" // Realtime interrupts
#include "lcd.h" // LCD Display
#include "sci.h" // Serial communications interface (EIA/TIA-232)
#include "i2c.h" // Carlos Estay's I2C Driver Libraries
/////////////////////////////////////////////////////////////////////////////
// Constants
/////////////////////////////////////////////////////////////////////////////
// A couple of Custom characters. Note format: an array of 8 bytes with three
// leading zeroes and then a five bit row with zeros for off pixels and 1 for
// on. By convention, the last row is left blank to permit showing the cursor.
const char cgSmiley[] = //Happy face
{
0b00000000, //... .....
0b00000000, //... .....
0b00010001, //... *...*
0b00000000, //... .....
0b00000000, //... .....
0b00010001, //... *...*
0b00001110, //... .***.
0b00000000, //... .....
};
const char cgFrowny[] = //Sad face
{
0b00000000, //... .....
0b00000000, //... .....
0b00010001, //... *...*
0b00000000, //... .....
0b00000000, //... .....
0b00001110, //... .***.
0b00010001, //... *...*
0b00000000, //... .....
};
/////////////////////////////////////////////////////////////////////////////
// Main Entry
/////////////////////////////////////////////////////////////////////////////
void main(void)
{
// Variables
int i = 0; // Loop counter
/////////////////////////////////////////////////////////////////////////////
// One-Time Initializations
/////////////////////////////////////////////////////////////////////////////
_DISABLE_COP(); // No watchdog
PLL_To20MHz(); // Configure the main clock to 20 MHz (implications for timers)
SWL_Init(); // Standard initialization for switches and LEDs
SEG_Init(); // Standard initializaton for 7-seg displays
RTI_Init(); // Initialize RTI timer
/////////////////////////////////////////////////////////////////////////////
// Setup
/////////////////////////////////////////////////////////////////////////////
EnableInterrupts; // Ensure our board will respond to interrupt signals.
LCD_Init(); // My current LCD drivers need RTI and interrupts. //FIXME
LCD_StringRC(0,5,"CGRAM Demo"); //Top line, centered
LCD_StringRC(1,5,"----------"); //Second line, centered
// DDRAM is the display memory: each location corresponds to a character
// on the display. CGRAM is 64 bytes divided up into 8 8-byte blocks---
// each block corresponding to a special character that can be displayed
// as special characters with ASCII values 0 through 7. The two arrays of
// bytes above show the format needed to define the character.
// First, we set our address to location 0 in CGRAM
LCD_Inst(0b01000000); //Set CGRAM command - 0x0100 0xxx where xxx are addresses 0-7
// Now, iterate through my array of rows and write all 8 to the device:
for (i = 0 ; i < 8 ; ++i)
LCD_Data(cgSmiley[i]);
// My custom smiley character will now be printed if I send a zero as my character
// at any location in DDRAM.
// After I used the above code to develop CGRAM support in my library,
// I can simplify this into the below for creating a frowny face.
LCD_CGChar(1,cgFrowny); // Special character will be at ASCII 0x01
//Show them on the LCD
LCD_AddrRC(2,10); // Third line, middle
LCD_Data(0); //Special character 0 is smiley(ASCII 0, not '0')
//Show them on the LCD
LCD_AddrRC(3,10); // Fourth line, middle
LCD_Data(1); //Special character 1 is frowny (ASCII 1, not '1')
/////////////////////////////////////////////////////////////////////////////
// Main Program Loop
/////////////////////////////////////////////////////////////////////////////
for (;;)
{
asm WAI; //Wait for something interesting to happen
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment