This module
provides an interface to the SANYO DM2023 Liquid Crystal Dot Matrix Display
Module when using the C32 Microcontroller.
Pin Assignments
C32 Ports |
LCD Ports |
M0 |
DB0 |
M1 |
DB1 |
M2 |
DB2 |
T3 |
DB3 |
T4 |
DB4 |
T5 |
DB5 |
T6 |
DB6 |
T7 |
DB7 |
M4 |
RS |
M5 |
EN |
Here are
codes for this module. Please click on the file names for detail.
- LCD.h
- LCD.c
void
LCDInit(void);
/*
Prototype: void LCDInit(void)
Parameters: None
Returns: None
Usage: LCDInit();
Description
LCDInit(void) initializes the LCD and sest the
cursor movement and display shift.
Characters written to the display will be
placed at the rightmost position on the top line,
and then the line will scroll one position to
the left (like a moving message display).
In this way as new characters are entered,
the whole line will slide to the left,
keeping the rightmost position open.
*/
void
LCDputchar(unsigned char input_char);
/*
Prototype: void LCDputchar(char input_char)
Parameters: char input_char, the letter you want
printed represented as a
constant char (‘A’) on
LCD. Follows ASCII encoding for letters.
Returns: None
Usage: LCDputchar('A');
Description
LCDputchar(char input_char) writes a
character to the display.
The input of the function is a char variable
which will be displayed on the LCD.
The function returns nothing after
displaying the character on the LCD.
*/
void
Control_RS(char input);
/*
Prototype: void Control_RS(char input)
Parameters: char input, either the command ('c') or
data ('d') register to select.
Returns: None
Usage: Control_RS('c');
Description
Control_RS(char input) selects either the
command or data register of the display.
The input of the function is either the
command ('c') or data ('d') register to select.
The
function returns nothing after selecting the register.
*/
void
Pulse_EN(void);
/*
Prototype: void Pulse_EN(void)
Parameters: None
Returns: None
Usage: Pulse_EN();
Description
Pulse_EN(void) pulses the enable line to
perform the data transfer (Use Port M5).
*/
void
Wait(int wait_T);
/*
Prototype: void Wait(int wait_T)
Parameters: int wait_T, the amount of time to wait
for.
Returns: None
Usage: Wait(10);
Description
Wait(int wait_T) waits for the input amount
of time.
The function returns nothing after waiting.
*/
void
ArrangeLCDbits(unsigned char input);
/*
Prototype: void ArrangeLCDbits(unsigned char input)
Parameters: unsigned char input, the 8 bits to write
to bit 0-2 of port M and bit 3-7 of port T.
Returns: None
Usage: Wait(10);
Description
ArrangeLCDbits(unsigned char input) arranges
bits of port T and port M
according to the input for LCD command and
display.
*/
void
Set_cursor(void);
/*
Prototype: void Set_cursor(void)
Parameters: None
Returns: None
Usage: Set_cursor();
Description
Set_cursor(void) sets the cursor movement
and display shift.
This function returns nothing after the set
up.
*/
#include
<stdio.h>
#include
<ME218_C32.h>
#include
<timers12.h>
#include
"PennyDetector.h"
/*****
C32 bit define part *****/
#define
RSbit BIT4HI
#define
ENbit BIT5HI
/*****
function declaration *****/
void
Control_RS(char input);
void
Pulse_EN(void);
void
LCDInit(void);
void
Wait(int wait_T);
void
LCDputchar(unsigned char input_char);
void
Set_cursor(void);
void
ArrangeLCDbits(unsigned char input);
/*****
function implementation *****/
/**
Select either the command or data register of the display.
Obtain input from
the higher-level function to select either the command('c') or data('d')
register. **/
void
Control_RS(char input)
{
/* Set bit 4-5 of
Port M to be outputs */
DDRM = DDRM | RSbit | ENbit;
/* If the input is 'command', then set RS(bit
4 of Port M) = 0 */
if (input=='c') PTM = PTM & (~RSbit);
/* If the input is 'data', then set RS(bit 4
of Port M) = 1 */
if (input=='d') PTM = PTM | RSbit;
}
/**
Pulse the enable line to perform the data transfer (Use Port M5). **/
void
Pulse_EN(void)
{
/* Declare a dummy variable for a pause */
int i;
/* Set bit 4-5 of Port M to be outputs */
DDRM = DDRM | RSbit | ENbit;
/* Pulse the enable
line high by setting EN(bit 5 of Port M) = 1 */
PTM = PTM | ENbit;
/* Make a dummy assignment */
for(i=0;i<2000;i++);
/* Pulse the enable line low by
setting EN(bit 5 of Port M) = 0 */
PTM = PTM & (~ENbit);
/* Make a dummy
assignment */
for(i=0;i<2000;i++);
}
/**
Initialize the LCD and set the cursor movement and display shift **/
void
LCDInit(void)
{
/* Initialize the timer module */
TimerRate_t time_rate;
time_rate = TMRS12_RATE_2MS;
TMRS12_Init(time_rate);
/* Select the
command register */
Control_RS('c');
/* Set outputs */
DDRT |= 0xF8;
DDRM |= 0x07;
/* Write a command of 0x30
Pulse the enable line */
ArrangeLCDbits(0x30);
Pulse_EN();
/* Wait for at
least 4.1 ms */
Wait(4.1+1);
/* Write a command
of 0x30
Pulse the enable line */
ArrangeLCDbits(0x30);
Pulse_EN();
/* Wait at least
100 µs */
Wait(0.1+1);
/* Write a command
of 0x30
Pulse the enable line */
ArrangeLCDbits(0x30);
Pulse_EN();
/* Wait for at least 1.6 ms */
Wait(1.6+1);
/* Write a command
of 0x08
Pulse the enable line */
ArrangeLCDbits(0x80);
Pulse_EN();
/* Wait for at
least 1.6 ms */
Wait(1.6+1);
/* Write a command of 0x01
Pulse the enable line */
ArrangeLCDbits(0x01);
Pulse_EN();
/* Wait for at
least 4.9 ms */
Wait(4.9+1);
/* Write a command
of 0x06
Pulse the enable line */
ArrangeLCDbits(0x06);
Pulse_EN();
/* Wait for at
least 1.6 ms */
Wait(1.6+1);
/* Write a command
of 0x0F
Pulse the enable line */
ArrangeLCDbits(0x0F);
Pulse_EN();
/* set the cursor movement and display shift
*/
Set_cursor();
}
/**
Wait function - Obtain the amount of time to wait for and wait **/
void
Wait(int wait_T)
{
int start_T;
int dT;
int curr_T;
/* Get current time and set it to be the
start time, Set delta-T to be zero */
start_T = TMRS12_GetTime();
dT = 0;
/* REPEAT while
delta-T is smaller than the input time to wait */
while (dT < wait_T)
{
/* Get current time and Calculate time since
the start time*/
curr_T = TMRS12_GetTime();
dT = curr_T - start_T;
}
}
/**
Set the cursor movement and display shift **/
void
Set_cursor(void)
{
/* Select the
command register */
Control_RS('c');
/* Set outputs */
DDRT |= 0xF8;
DDRM |= 0x07;
/* Set the next address to be written to be the
rightmost position on the first line */
ArrangeLCDbits(0x93);
Pulse_EN();
/* Display the
character and cursor (blinking)*/
ArrangeLCDbits(0x0F);
Pulse_EN();
/* Shift the line one position to the left */
ArrangeLCDbits(0x07);
Pulse_EN();
}
/**
Write a character to the display **/
void
LCDputchar(unsigned char input_char)
{
/* Select the data
register */
Control_RS('d');
/* Set outputs */
DDRT |= 0xF8;
DDRM |= 0x07;
/* Write the input
character */
ArrangeLCDbits(input_char);
Pulse_EN();
}
/**
Arrange bits of port T and port M for LCD **/
void
ArrangeLCDbits(unsigned char input)
{
unsigned char input1;
unsigned char input2;
unsigned char input3;
unsigned char input4;
DDRT |= 0xF8;
DDRM |= 0x07;
input1 = input & 0xF8;
PTT |= input1;
input2 = input | 0x07;
PTT &= input2;
input3 = input & 0x07;
PTM |= input3;
input4 = input | 0xF8;
PTM &= input4;
}