c8051f005键盘测试程序
扫描二维码
随时随地手机看文章
c8051f005键盘测试程序
//-----------------------------------------------------------------------------
//Includes
//-----------------------------------------------------------------------------
#include
#include
#include
//-----------------------------------------------------------------------------
//FunctionPROTOTYPES
//-----------------------------------------------------------------------------
#defineucharunsignedchar
#defineuintunsignedint
#defineBAUDRATE115200//BaudrateofUARTinbps
#defineSYSCLK11059200//SYSCLKFrequencyinHz
voidSYSCLK_Init(void);
voiddelaynus(unsignedintq);//Nus延时函数
voidPORT_Init(void);
voidSPI0_Init(void);
voidLCD_Init(void);
voidSenDSPIByte(unsignedcharch);
voiddelaynms(unsignedintj);
voidwritecom(unsignedcharcom);
voidwritEDAta(unsignedchard);
voidwritechar(unsignedcharua);
voidWrite_COM(ucharins);
voidlcden(datad);
voidLCD_set_xy(unsignedcharx,unsignedchary);
voidLCD_write_string(unsignedcharn);
voidlcd_key1(void);
voidlcd_key2(void);
voidlcd_key3(void);
voidlcd_key4(void);
voidUART0_Init(void);
voidpresskey(void);
//-----------------------------------------------------------------------------
//GlobalCONSTANTS
//-----------------------------------------------------------------------------
sbitS3=P1^0;
sbitS4=P1^1;
sbitS5=P1^2;
sbitS6=P1^3;
sbitlcdcs=P3^0;
unsignedchardatad,comd,kk,sdf,ppca;
unsignedcharvirt_port,v,b,m;
unsignedcharlcd_data_count;
unsignedchar*lcdpoint;
unsignedcharqqq;
unsignedchardata8;
unsignedinti;
//-----------------------------------------------------------------------------
//MAINRoutine
//-----------------------------------------------------------------------------
voidmain(void){
WDTCN=0xde;//dISAblewatchdogtimer
WDTCN=0xad;
SYSCLK_Init();
PORT_Init();
UART0_Init();//initializecrossbarandGPIO
SPI0_Init();
LCD_Init();
delaynms(20);
delaynus(100);
LCD_set_xy(0X00,0);
delaynus(200);
presskey();
delaynus(200);
LCD_write_string(16);
delaynus(200);
while(1)
{
if(S3==0)
{
Write_COM(0X01);
delaynms(200);
LCD_set_xy(0X00,0);
delaynus(200);
lcd_key1();
delaynus(200);
LCD_write_string(6);
delaynus(200);
}
elseif(S4==0)
{
Write_COM(0X01);
delaynms(200);
LCD_set_xy(0X00,0);
delaynus(200);
lcd_key2();
delaynus(200);
LCD_write_string(6);
delaynus(200);
}
elseif(S5==0)
{
Write_COM(0X01);
delaynms(200);
LCD_set_xy(0X00,0);
delaynus(200);
lcd_key3();
delaynus(200);
LCD_write_string(6);
delaynus(200);
}
elseif(S6==0)
{
Write_COM(0X01);
delaynms(200);
LCD_set_xy(0X00,0);
delaynus(200);
lcd_key4();
delaynus(200);
LCD_write_string(6);
delaynus(200);
}
else
{
}
}
}
//-----------------------------------------------------------------------------
//InitializationSubroutines
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//PORT_Init
//-----------------------------------------------------------------------------
voidPORT_Init(void)
{
XBR0=0x27;//XBAR0:InitialResetValue
XBR1=0x00;//XBAR1:InitialResetValue
XBR2=0x5c;//XBAR2:InitialResetValue
PRT0CF=0x14;//OutputconfigurationforP0
PRT1CF=0x10;//OutputconfigurationforP3
PRT3CF=0x01;//OutputconfigurationforP3
}
//-----------------------------------------------------------------------------
//SYSCLK_Init
//-----------------------------------------------------------------------------
voidSYSCLK_Init(void)
{
OSCXCN=0x67;//startexternaloscillatorwith
for(i=0;i<256;i++);//XTLVLDblankinginterval(>1ms)
while(!(OSCXCN&0x80));//Waitforcrystalosc.tosettle
OSCICN=0x88;//selectexternaloscillatorasSYSCLK
}
//-----------------------------------------------------------------------------
//SPI0_Init
//-----------------------------------------------------------------------------
voidSPI0_Init(void)
{
SPI0CFG=0x07;//datasampLEDon1stSCKrisingedge
SPI0CFG|=0xC0;//CKPOL=1;
SPI0CN=0x03;//Mastermode;SPIenabled;flags
SPI0CKR=SYSCLK/2/2000000-1;//SPIcLOCk<=8MHz(limitedby
//EEPROMspec.)
}
//-----------------------------------------------------------------------------
//UART0_Init
//-----------------------------------------------------------------------------
voidUART0_Init(void)
{
SCON=0x50;//SCON:mode1,8-bitUART,enableRX
TMOD=0x20;//TMOD:timer1,mode2,8-bitreload
TH1=-(SYSCLK/BAUDRATE/16);//setTimer1reloadvalueforbaudrate
TR1=1;//startTimer1
CKCON|=0x10;//Timer1usesSYSCLKastimebase
PCON|=0x80;//SMOD=1
TI=1;//InDICateTXready
}
//-----------------------------------------------------------------------------
//LCD_Init
//-----------------------------------------------------------------------------
voidLCD_Init(void)//向LCD送命令
{
//unsignedintxdatax;
delaynms(100);
datad=0x00;
SendSPIByte(datad);
delaynms(10);
Write_COM(0x30);
delaynms(10);
Write_COM(0x30);
delaynms(10);
Write_COM(0x30);
delaynms(10);
Write_COM(0x28);
delaynms(100);
virt_port=0;
SendSPIByte(virt_port);
lcden(virt_port);
Write_COM(0x01);
delaynms(100);
Write_COM(0x06);
delaynms(10);
Write_COM(0x0C);
delaynms(500);
}
//-----------------------------------------------------------------------------
//SendSPIByte
//-----------------------------------------------------------------------------
voidSendSPIByte(unsignedcharch)
{lcdcs=1;
delaynus(100);
SPIF=0;
SPI0DAT=ch;
while(SPIF==0);
delaynus(100);
lcdcs=0;
delaynus(100);
_nop_();//等待写结束
}
//-----------------------------------------------------------------------------
//lcden
//-----------------------------------------------------------------------------
voidlcden(datad)
{
datad|=0x08;
SendSPIByte(datad);
datad&=0xf7;
SendSPIByte(datad);
}
//-----------------------------------------------------------------------------
//delaynms
//-----------------------------------------------------------------------------
voiddelaynms(unsignedintuu)
{
unsignedintoo,ll;
for(oo=0;oo
for(ll=0;ll<1140;ll++);
}
}
//-----------------------------------------------------------------------------
//writechar
//-----------------------------------------------------------------------------
voidwritechar(unsignedcharua)
{
uintj;
uchart,x;
for(j=0;j<500;j++);
datad|=0x02;
SendSPIByte(datad);
datad|=ua&0xf0;
SendSPIByte(datad);
datad|=0x08;
SendSPIByte(datad);
for(x=0;x<3;x++);
datad&=0xf7;
SendSPIByte(datad);
for(x=0;x<3;x++);
datad&=0x07;
delaynus(100);
SendSPIByte(virt_port);
t|=ua&0x0f;
datad|=t<<4;
SendSPIByte(datad);
for(x=0;x<3;x++);
datad|=0x08;
SendSPIByte(datad);
for(x=0;x<3;x++);
datad&=0xf7;
SendSPIByte(datad);
for(x=0;x<3;x++);
datad=0x00;
t=0x00;
SendSPIByte(datad);
}
//-----------------------------------------------------------------------------
//Write_COM
//-----------------------------------------------------------------------------
voidWrite_COM(ucharins)
{
uchart;
uintj;
for(j=0;j<5000;j++);//用延时代替查询
virt_port|=ins&0xf0;
SendSPIByte(virt_port);
//LCDE=1;
virt_port|=0x08;
SendSPIByte(virt_port);
for(i=3;i>0;i--);
//LCDE=0;
virt_port&=~0x08;
SendSPIByte(virt_port);
virt_port&=0x07;
SendSPIByte(virt_port);
t=ins<<4;
virt_port|=t&0xf0;
SendSPIByte(virt_port);
//LCDE=1;
virt_port|=0x08;
SendSPIByte(virt_port);
for(i=3;i>0;i--);
//LCDE=0;
virt_port&=~0x08;
SendSPIByte(virt_port);
virt_port=0;
SendSPIByte(virt_port);
}
//-----------------------------------------------------------------------------
//LCD_set_xy
//-----------------------------------------------------------------------------
voidLCD_set_xy(unsignedcharx,unsignedchary)
{
unsignedcharaddress;
if(y==0)address=0x80+x;
else
address=0xc0+x;
Write_COM(address);
}
//-----------------------------------------------------------------------------
//LCD_write_string
//-----------------------------------------------------------------------------
voidLCD_write_string(unsignedcharn)
{
unsignedchardata1;
for(n;n>0;n--)
{
data1=*lcdpoint;
writechar(data1);
delaynms(100);
lcdpoint++;
delaynus(10);
}
}
//-----------------------------------------------------------------------------
//presskey
//-----------------------------------------------------------------------------
voidpresskey(void)
{
unsignedcharxdataDDCdata[16]={0x50,0x4c,0x45,0x41,0x53,0x45,0x20,0x50,0x52,0x45,0x53,0x53,0x20,0x4b,0x45,0x59};
lcdpoint=&DDCdata;
}
//-----------------------------------------------------------------------------
//lcd_key1
//-----------------------------------------------------------------------------
voidlcd_key1(void)
{
unsignedcharxdatakey1ok[6]={0x53,0x33,0x20,0x4f,0x4b,0x21};
lcdpoint=&key1ok;
}
//-----------------------------------------------------------------------------
//lcd_key2
//-----------------------------------------------------------------------------
voidlcd_key2(void)
{
unsignedcharxdatakey2ok[6]={0x53,0x34,0x20,0x4f,0x4b,0x21};
lcdpoint=&key2ok;
}
//-----------------------------------------------------------------------------
//lcd_key3
//-----------------------------------------------------------------------------
voidlcd_key3(void)
{
unsignedcharxdatakey3ok[6]={0x53,0x35,0x20,0x4f,0x4b,0x21};
lcdpoint=&key3ok;
}
//-----------------------------------------------------------------------------
//lcd_key4
//-----------------------------------------------------------------------------
voidlcd_key4(void)
{
unsignedcharxdatakey4ok[6]={0x53,0x36,0x20,0x4f,0x4b,0x21};
lcdpoint=&key4ok;
}
//-----------------------------------------------------------------------------
//delaynus
//-----------------------------------------------------------------------------
voiddelaynus(unsignedintq)//Nus延时函数
{
for(i=0;i{
_nop_();
}
}