/******************************************************************
硬件調(diào)試通過(guò)
*******************************************************************/
#include"reg52.h"
#include "intrins.h"
#include "string.h"
#include "key.h" //按鍵通碼與ascii對(duì)照表
#define uchar unsigned char
#define uint unsigned int
#define BOOL bit
#define cmd_word 0xff //復(fù)位命令
sbit Clk_Line = P3^3; //時(shí)鐘線
sbit Data_Line=P3^4; //鍵盤數(shù)據(jù)線
uchar key=0; //按鍵最終值
uint Key_Value=0;
uchar Int_Time=0;
uchar save=0;
uchar display[40];
uchar scancode[40];
uint num = 0;
uchar blank;
uchar flag;
uchar comf = 0x01;
//**************************************************************************
void init(void);
void delay(uint us); //us=12大約100us
void command(uchar cmd);
void interrupt1_receive(void);
void getkey(void) ;////內(nèi)部中斷0 用來(lái)處理緩沖區(qū)里的數(shù)據(jù)
void keyprocess(void);
void lcdclr(void);
//**************************************************************************
//Port Definitions**********************************************************
sbit rs= P2^0;//
sbit rw = P2^1;
sbit ep = P2^2;
#define LCDPORT P0
sbit LcdLight = LCDPORT^4;
void Delay1ms(unsigned int count)
{
unsigned int i,j;
for(i=0;i
}
//向LCD寫入命令或數(shù)據(jù)************************************************************
#define LCD_COMMAND 0x00 // Command
#define LCD_DATA 0x01 // Data
#define LCD_CLEAR_SCREEN 0x01 // 清屏
#define LCD_HOMING 0x02 // 光標(biāo)返回原點(diǎn)
//設(shè)置顯示模式************************************************************
#define LCD_SHOW 0x04 //顯示開
#define LCD_HIDE 0x00 //顯示關(guān)
#define LCD_CURSOR 0x02 //顯示光標(biāo)
#define LCD_NO_CURSOR 0x00 //無(wú)光標(biāo)
#define LCD_FLASH 0x01 //光標(biāo)閃動(dòng)
#define LCD_NO_FLASH 0x00 //光標(biāo)不閃動(dòng)
//設(shè)置輸入模式************************************************************
#define LCD_AC_UP 0x02
#define LCD_AC_DOWN 0x00 // default
#define LCD_MOVE 0x01 // 畫面可平移
#define LCD_NO_MOVE 0x00 //default
//移動(dòng)光標(biāo)或屏幕************************************************************
#define LCD_CURSOR 0x02
#define LCD_SCREEN 0x08
#define LCD_LEFT 0x00
#define LCD_RIGHT 0x04
//************************************************************************
delayms(uchar ms)
{// 延時(shí)子程序
uchar i;
while(ms--)
{
for(i = 0; i< 250; i++)
{
_nop_();
_nop_();
_nop_();
_nop_();
}
}
}
void command(uchar cmd)
{
uchar parity;
uchar i;
comf = 0x01;
parity=0;
Clk_Line=1;
Data_Line=1;
delay(100);
Clk_Line=0;
delay(20);
Data_Line=0;
Clk_Line = 1;
for(i=0;i<8;i++)
{
Clk_Line = 1;
while(Clk_Line);
Data_Line = 0;
if(cmd & (comf<<1))
{
Data_Line = 1;
parity++;
}
}
while(!Clk_Line) ;
while(Clk_Line) ;
if((parity&0x01))
Data_Line=0;
else Data_Line=1;
while(!Clk_Line) ;
while(Clk_Line) ;
Data_Line=1;
while(!Clk_Line);
while(Data_Line) ;
while(Clk_Line) ;
while(!Clk_Line) ;
while(!Data_Line);
}
BOOL lcd_bz()
{// 測(cè)試LCD忙碌狀態(tài)
BOOL result;
rs = 0;
rw = 1;
ep = 1;
_nop_();
_nop_();
_nop_();
_nop_();
result = (BOOL)(LCDPORT & 0x80);
ep = 0;
return result;
}
lcd_wcmd(uchar cmd)
{// 寫入指令數(shù)據(jù)到LCD
while(lcd_bz());
rs = 0;
rw = 0;
ep = 0;
_nop_();
_nop_();
LCDPORT = cmd;
_nop_();
_nop_();
_nop_();
_nop_();
ep = 1;
_nop_();
_nop_();
_nop_();
_nop_();
ep = 0;
}
lcd_pos(uchar pos)
{//設(shè)定顯示位置
lcd_wcmd(pos | 0x80);
}
lcd_wdat(uchar dat)
{//寫入字符顯示數(shù)據(jù)到LCD
while(lcd_bz());
rs = 1;
rw = 0;
ep = 0;
LCDPORT = dat;
_nop_();
_nop_();
_nop_();
_nop_();
ep = 1;
_nop_();
_nop_();
_nop_();
_nop_();
ep = 0;
}
lcd_init()
{//LCD初始化設(shè)定
lcd_wcmd(0x38);//
delayms(1);
lcd_wcmd(0x0c);//
delayms(1);
lcd_wcmd(0x06);//
delayms(1);
lcd_wcmd(0x01);//清除LCD的顯示內(nèi)容
delayms(1);
}
void init(void)
{
EA = 1;
IT1 = 1; //外部中斷1是下降沿觸發(fā)方式
}
void getkey(void)
{
uchar i=0;
//static uint num = 0;
save = Key_Value;
for(i=0;i<49;i++)
{
if(noshift[i][0] == save) //普按鍵的通碼在緩沖區(qū)的第一位
{
key = noshift[i][1]; //搜索到字母后進(jìn)行相應(yīng)的處理
if(num == 32)
{
//num = 0;
while(num)
{
display[--num] = 0;
}
blank = 0;
lcdclr();
}
display[num++] = key;
flag = 0;
break;
}
}
}
void delay(uint us) //us=12大約100us
{
while(us--) ;
}
void interrupt1_receive(void) interrupt 2
{
_nop_();
if(Data_Line)
Key_Value|=0x8000;
Int_Time++;
if(Int_Time==11)
{
Key_Value>>=6;
Int_Time=0;
EX1=0;
}
Key_Value>>=1;
}
void main(void)
{
uchar i=0;
uchar thir=2;
init(); //初始化
lcd_init();// 初始化LCD
delay(10);
command(cmd_word);
delay(150);
while(1)
{
EX1=1;
while(EX1) ;
getkey();
lcd_pos(0XC0);
lcd_wdat(key);
_nop_();
}
}