Chapter 10-Glass Bliss

Download Report

Transcript Chapter 10-Glass Bliss

Chapter 10
Glass Bliss
Using the Parallel Master Port to
communicate with Alphanumeric
LCD displays
Alphanumeric LCD Modules
Di Jasio - Programming 32-bit Microcontrollers in C
HD44780 Instruction Set
Instruction
Clear display
RS
0
R/W
0
DB7
0
DB6
0
DB5
0
Code
DB4 DB3
0
0
DB2
0
Description / Execution time
*
DB1
0
DB0
1
Cursor home
0
0
0
0
0
0
0
1.64mS
0
1
Entry mode set
0
0
0
0
0
0
0
1
I/D
S
Display On/Off
0
0
0
0
0
0
1
D
C
B
Cursor/display shift 0
0
0
0
0
1
S/C
R/L
*
*
DL
N
F
*
*
Clears display and returns cursor to
the home position (address 0).
Returns cursor to home position (address 0).
Also returns display being shifted to the original
position. DDRAM contents remains unchanged.
1.64mS
Sets cursor move direction (I/D), specifies to shift
the display (S). These operations are performed
during data read/write.
40uS
Sets On/Off of all display (D), cursor On/Off (C)
and blink of cursor position character (B).
40uS
Sets cursor-move or display-shift (S/C), shift
direction (R/L). DDRAM contents remains
unchanged.
Function set
0
0
0
0
1
Set CGRAM addr
0
0
0
1
CGRAM address
Set DDRAM addr
0
0
1
DDRAM address
Read busy-flag
and address ctr
0
1
BF
CGRAM / DDRAM address
1
0
write data
Write to CGRAM
(or DDRAM)
Read
CGRAM
1 32-bit
1 Microcontrollers
read data in C
Di
Jasio
- Programming
(or DDRAM)
40uS
Sets interface data length (DL), number of
display line (N) and character font(F).
40uS
Sets the CGRAM address. CGRAM data is sent
and received after this setting.
40uS
Sets the DDRAM address. DDRAM data is sent
and received after this setting.
40uS
Reads Busy-flag (BF) indicating internal
operation is being performed and reads CGRAM
or DDRAM address counter contents (depending
on previous instruction). 0uS
Writes data to CGRAM or DDRAM.
40uS
Reads data from CGRAM or DDRAM.
40uS
HD44780 Instruction Set (cont.)
Bit name
I/D
S
D
C
B
S/C
R/L
DL
N
F
BF
Setting / Status
0 = Decrement cursor position 1 = Increment cursor position
0 = No display shift
1 = Display shift
0 = Display off
1 = Display on
0 = Cursor off
1 = Cursor on
0 = Cursor blink off
1 = Cursor blink on
0 = Move cursor 1 = Shift display
0 = Shift left 1 = Shift right
0 = 4-bit interface 1 = 8-bit interface
0 = 1/8 or 1/11 Duty (1 line)
1 = 1/16 Duty (2 lines)
0 = 5x7 dots 1 = 5x10 dots
0 = Can accept instruction 1 = Internal operation in progress
Di Jasio - Programming 32-bit Microcontrollers in C
Character Generator Table
Di Jasio - Programming 32-bit Microcontrollers in C
Parallel Master Port
Di Jasio - Programming 32-bit Microcontrollers in C
PMCON
PMCON Register 20-1 (DS61143)
Di Jasio - Programming 32-bit Microcontrollers in C
LCD Initialization
#define LCDDATA 1
#define LCDCMD 0
#define PMDATA PMDIN1
// RS = 1 ; access data register
// RS = 0 ; access command register
// PMP data buffer
void LCDinit( void)
{
// PMP initialization
PMCON = 0x83BF;
PMMODE = 0x3FF;
PMPEN = 0x0001;
// Enable the PMP, long waits
// Master Mode 1
// PMA0 enabled
PMADDR = LCDCMD;
PMDATA = 0x38;
TMR1 = 0; while(
PMDATA = 0x0c;
TMR1 = 0; while(
PMDATA = 0x01;
TMR1 = 0; while(
PMDATA = 0x06;
TMR1 = 0; while(
} // LCDinit
//
//
TMR1<8); //
//
TMR1<8); //
TMR1<300);
TMR1<300);
Di Jasio - Programming 32-bit Microcontrollers in C
command register (ADDR = 0)
set: 8-bit interface, 2 lines, 5x7
8 x 6us = 48us
ON, no cursor, no blink
8 x 6us = 48us
// clear display
// 300 x 6us = 1.8ms
// increment cursor, no shift
// 300 x 6us = 1.8ms
Reading the LCD
char readLCD( int addr)
{
int dummy;
while( PMMODEbits.BUSY);
PMADDR = addr;
dummy = PMDATA;
while( PMMODEbits.BUSY);
return( PMDATA);
//
//
//
//
//
wait for PMP to be available
select the command address
init read cycle, dummy read
wait for PMP to be available
read the status register
} // readLCD
#define busyLCD() readLCD( LCDCMD) & 0x80
#define addrLCD() readLCD( LCDCMD) & 0x7F
#define getLCD() readLCD( LCDDATA)
Di Jasio - Programming 32-bit Microcontrollers in C
Writing to the LCD
void writeLCD( int addr, char c)
{
while( busyLCD());
while( PMMODEbits.BUSY);
// wait for PMP to be available
PMADDR = addr;
PMDATA = c;
} // writeLCD
#define busyLCD() readLCD( LCDCMD) & 0x80
#define addrLCD() readLCD( LCDCMD) & 0x7F
#define getLCD() readLCD( LCDDATA)
Di Jasio - Programming 32-bit Microcontrollers in C
Extending the “Include Search Path”
Di Jasio - Programming 32-bit Microcontrollers in C
LCD Control Using the peripheral library
void initLCD( void)
{
// PMP initialization
mPMPOpen( PMP_ON | PMP_READ_WRITE_EN | 3,
PMP_DATA_BUS_8 | PMP_MODE_MASTER1 |
PMP_WAIT_BEG_4 | PMP_WAIT_MID_15 |
PMP_WAIT_END_4,
0x0001,
// only PMA0 enabled
PMP_INT_OFF);
// no interrupts used
// wait for >30ms
Delayms( 30);
//initiate the HD44780 display
PMPSetAddress( LCDCMD);
//
PMPMasterWrite( 0x38);
//
Delayms( 1);
//
8-bit init sequence
select command register
8-bit int, 2 lines, 5x7
> 48 us
PMPMasterWrite( 0x0c);
Delayms( 1);
// ON, no cursor, no blink
// > 48 us
PMPMasterWrite( 0x01);
Delayms( 2);
// clear display
// > 1.6ms
PMPMasterWrite( 0x06);
Delayms( 2);
} // initLCD
Di Jasio - Programming 32-bit Microcontrollers in C
// increment cursor, no shift
// > 1.6ms
LCD Control Using the peripheral library
char readLCD( int addr)
{
PMPSetAddress( addr);
// select register
mPMPMasterReadByte();
// initiate read sequence
return mPMPMasterReadByte();// read actual data
} // readLCD
void writeLCD( int addr, char c)
{
while( busyLCD());
PMPSetAddress( addr);
// select register
PMPMasterWrite( c);
// initiate write sequence
} // writeLCD
Di Jasio - Programming 32-bit Microcontrollers in C
putsLCD()
void putsLCD( char *s)
{
char c;
while( *s)
{
switch (*s)
{
case '\n':
// point to second line
setLCDC( 0x40);
break;
case '\r':
// home, point to first line
setLCDC( 0);
break;
case '\t':
// advance next tab (8) positions
c = addrLCD();
while( c & 7)
{
putLCD( ' ');
c++;
}
if ( c > 15)
// if necessary move to second line
setLCDC( 0x40);
break;
default:
// print character
putLCD( *s);
break;
} //switch
s++;
} //while
} //putsLCD
Di Jasio - Programming 32-bit Microcontrollers in C
Advanced LCD Control
#define setLCDG( a) writeLCD( LCDCMD, (a & 0x3F) | 0x40)
Di Jasio - Programming 32-bit Microcontrollers in C
Progress Bar
void newBarTip( int i, int width)
{
char bar;
int pos;
// save cursor position
while( busyLCD());
pos = addrLCD();
// generate a new character at position i
// set the data pointer to the LCD CGRAM buffer
setLCDG( i*8);
// as a horizontal bar (0-4)x thick moving left to right
// 7 pixel tall
if ( width > 4)
width = 0;
else
width = 4 - width;
for( bar=0xff; width > 0; width--)
bar<<=1;
// bar >>= 1; if right to left
// fill
putLCD(
putLCD(
putLCD(
putLCD(
putLCD(
putLCD(
putLCD(
putLCD(
each row (8) with the same pattern
bar);
bar);
bar);
bar);
bar);
bar);
bar);
bar);
// restore cursor position
setLCDC( pos);
} // newBarTip
Di Jasio - Programming 32-bit Microcontrollers in C
Progress Bar (cont.)
void drawProgressBar( int index, int imax, int size)
{
// index is the current progress value
// imax is the maximum value
// size is the number of character positions available
int i;
// scale the input values in the available space
int width = index * (size*5) / imax;
// generate a character to represent the tip
newBarTip( TIP, width % 5);
// user defined character 0
// draw a bar of solid blocks
for ( i=width/5; i>0; i--)
putLCD( BRICK);
// filled block character
// draw the tip of the bar
putLCD( TIP);
} // drawProgressBar
Di Jasio - Programming 32-bit Microcontrollers in C
// use character 0