Вы находитесь на странице: 1из 9

#include <hidef.

h> /* for EnableInterrupts macro */


#include "derivative.h" /* include peripheral declarations */
#define XTAL_FREQ
8
#define KeyAddr PTCE4
#define KeyInPort PTCE4
#define SCROLLDELAY 100
#define KEYDELAY
100

//-- ms Delay
//-- us Delay between addressing and reading

//********* LCD CONTROL COMMANDS *********


#define LCD_CLS 0x10
//-- Clear Screen
#define LCD_CR
0x11
//-- Carrage Return
#define LCD_LF
0x12
//-- New Line
#define LCD_CRLF 0x13
//-- CR and NL
#define LCD_BEEP 0x14
//-- Makes a beep!!
#define LCD_L0
0x15
//-- Goes to start of Line 0
#define LCD_L1
0x16
//-- Goes to start of Line 1
#define LCD_PUTCH 0x1D
//-- Puts next char direct to LCD bypass Buff
#define LCD_GOTO 0x1E
//-- Moves Cursor to next byte location
#define LCD_SHOW 0x1F
//-- Update the display
//****************************************
#define BITNUM(adr, bit)
((unsigned)(&adr)*8+(bit))
static ChipSel ; //- Port Select Pin
static TChipSel;
//------- FLAGS ---------int NewData;
int BeepNow;
int GotoCommand;
int GotoNew;
int PutchCommand;
int PutchNew;
int LCD_RS;
//------- Global Variables, Buffers etc ---------unsigned char Buffer[41]="Waiting for data.\0
unsigned char Head,count,GotoData,PutchData;
unsigned char KeyPressed,LastKeyPressed;
//------- Functions Used in Program -------void interrupt GlobalInterrupt(void);
unsigned char KeyRead(void);
char DecodeKey(char keycode);
void DisplayData(void);
//void RunGame(void);
void ScrollMessage(unsigned char row,const char Message[]);
void main(void) {
EnableInterrupts;
/* include your code here */
void lcd_init(void){
LCD_RS = 0;
/* write control bytes */
DelayMs(15);
// power on delay
PTBD = 0x3;
// attention!
DelayMs(5);

\0";

DelayUs(100);
PTBD = 0x2;
DelayUs(40);
lcd_write(0x28);
lcd_write(0x08);
lcd_write(0x0F);
lcd_write(0x06);

// set 4 bit mode


/* 4 bit mode, 1/16 duty, 5x8 font */
/* display off */
/*display on, blink curson on */
/* entry mode*/

}
for(;;) {
__RESET_WATCHDOG(); /* feeds the dog */
PTDD = 0x00;
DelayMs(6);
PTED = 0x4F; /*O */
PTDD = 0x03;
DelayUs(40);
PTED = 0x6D; /*M*/
PTDD = 0x00;
DelayMs(6);
PTED = 0x61; /*A*/
PTDD = 0x03;
DelayUs(40);
PTED = 0x72; /*R*/
PTDD = 0x03;
DelayMs(6);
PTED = 0x20; /*SPACE*/
PTDD = 0x00;
DelayUs(40);
PTED = 0x4D; /*M*/
PTDD = 0x03;
DelayMs(6);
PTED = 0x61; /*A*/
PTDD = 0x03;
DelayUs(40);
PTED = 0x72; /*R*/
PTDD = 0x03;
DelayMs(6);
PTED = 0x74; /*T*/
PTDD = 0x03;
DelayUs(40);
PTED = 0x69; /*I*/
PTDD = 0x03;
DelayMs(6);
PTED = 0x6E; /*N*/
PTDD = 0x03;
DelayUs(40);
PTED = 0x65; /*E*/
PTDD = 0x03;
DelayMs(6);
PTED = 0x7A; /*Z*/
PTDD = 0x03;
}/* loop forever */
/* please make sure that you never leave main */
//-- Setup Variables -NewData=0;
Head=0;
BeepNow=0;
GotoCommand=0;

GotoNew=0;
GotoData=21;
//-- Set up Ports -PTGD_PTGD5=0;
PTED_PTED5=0;
PTHD_PTHD3=0;

//-- Set Chip Sel Pin High to indicate Busy to the master
ChipSel=1;
TChipSel=0;
//-- Set Up Interrupts -TPM1CH2=0;
//-- Enable the IRQ Mode
PTCE4 = 0xFF;
//-- IRQ configured as input
PTCE4 = 0x07;
//-- IRQ Controls as input
PTCE4 = 0x00;
//-- NULL Output to start with
//-- Initialize the LCD -lcd_init();
lcd_clear();
// ScrollMessage(0,"
// ScrollMessage(1,"

LCD & Keypad Driver");


Firmware Version 2.01

");

lcd_puts("LCD & Keypad Driver");


lcd_goto(40);
lcd_puts("Version 2.01.1");
DelayMs(200);
DisplayData();
//-- Signal to the Master controller to tell it that ready to recieve!!
ChipSel=0;
//- Take Pin Low For 10ms
DelayMs(20);
TPM1CH2=1;
//-- Enable PSP Interrupts
TChipSel=1;
//- Back into High Impedance State
IRQSC = 0;
//-- Main Program Loop -while(1)
{
if(NewData==1)
{
NewData=0;
DisplayData();
}
if(GotoNew==1)
//-- Move Cursor to selected Position
{
GotoNew=0;
lcd_goto(GotoData);
}
if(PutchNew==1)
//-- Put Character Directly to LCD Bypassing Buffer
{
PutchNew=0;
putch(PutchData);

}
KeyPressed=KeyRead();
if(KeyPressed != 0)
{
KeyPressed=DecodeKey(KeyPressed);
if(KeyPressed != LastKeyPressed)
{
LastKeyPressed=KeyPressed;
PTBD= KeyPressed;
}
}
}
}
void DelayMs(unsigned char cnt){
while (cnt < 150000){
cnt = cnt + 1 ;
}
}
void DelayUs(unsigned char _cnt){
while(_cnt < 3000000){
_cnt = (_cnt + 1);
}
}
void lcd_write(unsigned char c){
PTBD = c >> 4;
PTBD = c;
DelayUs(40);
}
void lcd_puts(const char *s){
LCD_RS = 1;
// write characters
while(*s){
lcd_write(*s++);
}
}
//**********************************************************************
//GlobalInterrupt - exactly what it says!!
//**********************************************************************
void interrupt GlobalInterrupt(void)
{
char Icount,TempD;
if(IRQSC)
{
if(IBF)
// Input Buffer Full
{
TempD=PTBD;
switch(TempD)
{
default:
if(GotoCommand==1)
{
GotoData=TempD;
GotoCommand=0;
GotoNew=1;
break;

}
if(PutchCommand==1)
{
PutchData=TempD;
PutchNew=1;
PutchCommand=0;
break;
}
Buffer[Head++]=TempD;
//-- Add Data to the Buffer and point to next
if(Head < 40) break;
//-- If not off the end then exit ...
Head=20;
//-- ... else CR!
break;
//**************************************
case LCD_CRLF:
//-- Combination of CR and LF
if(Head < 20)
{
Head=0;
}
else
{
Head=20;
}
//**************************************
case LCD_LF:
//-- Line Feed
if(Head >=20)
{
for(Icount=0;Icount<20;Icount++)
{
Buffer[Icount]=Buffer[Icount+20];
Buffer[Icount+20]=0; //-- Clear the data out
}
}
else
{
Head+=20;
}
break;
//**************************************
case LCD_CR:
//-- Carrage Return
if(Head < 20)
{
Head=0;
}
else
{
Head=20;
}
break;
//**************************************
case LCD_CLS:
//-- Clear the buffer
for(Icount=0;Icount<40;Icount++)
{
Buffer[Icount]=0;
}

//**************************************
case LCD_L0:
//-- Go to Start of 1st Line
Head=0;
//-- Also Clear the Line
for(Icount=0;Icount<20;Icount++) Buffer[Icount]=0;
break;
//**************************************
case LCD_L1:
//-- Go to Start of 2nd Line
Head=20;
//-- Also Clear the Line
for(Icount=20;Icount<40;Icount++) Buffer[Icount]=0;
break;
//**************************************
case LCD_SHOW:
//-- Update the Display
NewData=1;
break;
//**************************************
case LCD_GOTO:
//-- Put Cursor to location
GotoCommand=1;
//-- Indicated by next byte
break;
//**************************************
case LCD_PUTCH:
//-- Put next character directly to LCD
PutchCommand=1;
//-- By Passing the Buffer
break;
}
}
if(!OBF)
// Output Buffer has been read
{
PTBD=0x00;
// Put Null into buffer to indicate no key pressed
//
LastKeyPressed=0; // Allow Beep again on same key
}
TPM1CH2 = 0;
}
}
//******************* END OF GlobalInterrupt
//**********************************************************************
//KeyRead
//**********************************************************************
unsigned char KeyRead(void)
{
unsigned char line,data,result=0;
for(line=0;line <8;line++)
{
KeyAddr= line;
//-- Set Row To Read
DelayUs(KEYDELAY);
data = KeyInPort; //-- Read in the data
data = data >> 4; //-- shift to lower nibble
data
|= 0xF0;
//-- set upper nibble to 1s
data ^= 0xFF;
//-- invert everything (XOR)
if(data !=0)
{
result=line<<4; //-- Put line number in upper nibble
result+=data;
//-- Put Row Bit pattern in lower nibb
le
line=10;
//-- Terminate the loop
}

}
DelayUs(KEYDELAY); //-- We Bit More Delay
return(result);
//-- Return the result
}
//******************* END OF KeyRead
//**********************************************************************
//DecodeKey
//**********************************************************************
char DecodeKey(char keycode)
{
switch(keycode)
{
case 1:
return('D');
case 2:
return('E');
case 4:
return('0');
case 8:
return('C');
case 17:
return('R');
case 18:
return('3');
case 20:
return('2');
case 24:
return('1');
case 33:
return('L');
case 34:
return('6');
case 36:
return('5');
case 40:
return('4');
case 49:
return('U');
case 50:
return('9');
case 52:
return('8');
case 56:
return('7');
case 81:
return('M');
case 82:
return('a');
case 84:
return('b');
case 88:
return('c');
case 95:
//-- all function keys hit.
return(0xFF);

default:
break;
}
return(0);
}
//******************* END OF DecodeKey
//**********************************************************************
//DisplayData - Displays the Buffer on the LCD Display
//**********************************************************************
void DisplayData(void)
{
// unsigned char count;
lcd_clear();
//-- Clear the LCD
lcd_goto(0);
for(count=0;count<20;count++)
//-- Display the first line
{
if(Buffer[count]==0)
{
count=19;
//-- Check for end of string character
}
else
{
putch(Buffer[count]);
//-- Display Character on screen
}
}
lcd_goto(40);
//-- Move Cursor to second Line
for(;count<40;count++)
//-- Display the second line
{
if(Buffer[count]==0)
{
count=40;
//-- Check for end of string character
}
else
{
putch(Buffer[count]);
//-- Display Character on screen
}
}
// lcd_goto(21);
//-- Put cursor off the screen
}
//******************* END OF DisplayData
//**********************************************************************
//ScrollMessage
//**********************************************************************
void ScrollMessage(unsigned char row,const char Message[])
{
char TempS[21];
unsigned int MHead=0,Done=0;//,count;
if(row >1) row=1;
row=row*40;
while(Done==0)
{
for(count=0;count<20;count++)
{
TempS[count]=Message[MHead+count];
if(Message[MHead+count+1]==0) Done=1;
}

MHead++;
lcd_goto(row);
lcd_puts(TempS);
DelayMs(SCROLLDELAY);
}
}
//******************* END OF ScrollMessage

Вам также может понравиться