#include "reg52.h"
#define DISTIME_DELAY 500000
#define LZ_WIRELESS_DEBUG 0
unsigned char in_buff;
char flag_send;
char timer0_count;
char code str[]=
{
"\r\n\
##########################################\r\n\
hardware is stc89c52\r\n\
software is keil c code\r\n\
SC2272-M4 TEST!\r\n\
write by:Mr Lee\r\n\
##########################################\r\n"
};
void uart_init()
{
TMOD=0x20; //TMOD=0
TH1=0xf3; //12MHZ ,BPS:4800,N,8,1,0xf3=243
TL1=0xf3;
PCON=0x80; //方式一,8位数据位,一位起始位和一位结束位
TR1=1; //
SCON=0x40; //串口通信控制寄存器 模式一
}
void timer0_init(void)
{
//TMOD=0x01;//选定时器0,工作方式1,16位计数模式
TMOD|=0x01;
TH0=(65535-DISTIME_DELAY)/255;//设置初始值
TL0=(65535-DISTIME_DELAY)%255;
EA=1;//打开总中断
ET0=1;//打开定时器0中断
TR0=1;//定时器零开始计数
}
void uart_putchar(unsigned char dat)
{
SBUF=dat; //把数据送给sbuf缓存器中
while(TI!=1);//发送标志位 TI如果发送了为1,没发送为0,没发送等待,到了退出循环
TI=0; //到了,TI清为0
}
void uart_printf(unsigned char *buff)
{
while(*buff)
uart_putchar(*buff++);
}
void delay(unsigned int k)
{
unsigned int i,j;
for(i=0;i
{
for(j=0;j<121;j++)
{;}
}
}
void main()
{
uart_init();
timer0_init();
uart_printf(str);
while(1)
{
if((P1&0x10)==0x10)
{
in_buff=(P1&0x0f);
if(flag_send==1)
{
flag_send=0;
switch(in_buff)
{
case 1:{uart_printf("key B is pressed\r\n");}break;
case 2:{uart_printf("key D is pressed\r\n");}break;
case 4:{uart_printf("key A is pressed\r\n");}break;
case 8:{uart_printf("key C is pressed\r\n");}break;
default:
{
uart_printf("wireless is error!\r\n");
}break;
}
#if LZ_WIRELESS_DEBUG==1
uart_putchar(in_buff);
uart_printf("\r\n");
#endif
}
}
else
{
if(flag_send==1)
{
flag_send=0;
uart_printf("no key is pressed\r\n");
}
}
}
}
void timer0() interrupt 1
{
TH0=(65535-DISTIME_DELAY)/255;
TL0=(65535-DISTIME_DELAY)%255;
timer0_count++;
if(timer0_count>=20)
{
timer0_count=0;
flag_send=1;
}
}