#include <stc12c5a60s2.h>
#define uint unsigned int
#define uchar unsigned char
#define ulong unsigned long
sbit k1=P2^6;
sbit k2=P2^7;
uchar s; //s是字符串UartBuffer[5]={0}变量
unsigned char code a[]={0x01,0x02,0x03,0x04,0x05};
uchar UartBuffer[5]={0};
uchar code b = 0x02;
uint q;
uint haomiao ;
/************************************************
延时20ms
************************************************/
void Delay20ms() //@11.0592MHz
{
unsigned char i, j;
i = 36;
j = 217;
do
{
while (--j);
} while (--i);
}
/************************************************
延时10us
************************************************/
void Delay10us() //@11.0592MHz
{
unsigned char i;
i = 2;
while (--i);
}
/************************************************
波特率
************************************************/
void UartInit(void) //9600bps@11.0592MHz
{
PCON &= 0x3F; //波特率不倍速
SCON = 0x50; //8位数据,可变波特率 0101 0000
AUXR &= 0x00; //定时器1时钟为Fosc/12,即12T
TMOD &= 0x0F; //清除定时器1模式位
TMOD |= 0x20; //设定定时器1为8位自动重装方式
TL1 = 0xFD; //设定定时初值
TH1 = 0xFD; //设定定时器重装值
ET1 = 0; //禁止定时器1中断
TR1 = 1; //启动定时器1
}
/************************************************
定时器1
************************************************/
void Timer0Init(void) //100微秒@11.0592MHz
{
AUXR &= 0x7F; //定时器时钟12T模式
TMOD &= 0xF0; //设置定时器模式
TMOD |= 0x01; //设置定时器模式
TL0 = 0xA4; //设置定时初值
TH0 = 0xFF; //设置定时初值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
ET0 = 1;
EA=1;
}
/************************************************
定时器1中断
************************************************/
void Timer0 () interrupt 1
{
uint count;
TL0 = 0xA4; //设置定时初值
TH0 = 0xFF; //设置定时初值
count++;
if(count==1000) // 10是1ms 100是10ms 1000是100ms
{
haomiao++;
count=0;
}
if (haomiao==259)
{
haomiao =0;
}
}
/************************************************
串口中断 TI发送 RI接收
************************************************/
void UART_Routine(void) interrupt 4
{
if(RI==1)RI = 0;
if(TI==1)TI = 0;
}
/************************************************
串口发送字符
************************************************/
void Send_zifu(unsigned char zifu)//串口发送字符
{
SBUF=zifu;
while(TI==0);
TI=0;
}
/************************************************
串口发送字串
************************************************/
void Send_string(unsigned char string)//串口发送字符串
{
SBUF=string;
while(TI==0);
Delay10us();
TI=0;
}
/************************************************
按键扫描
************************************************/
void key() //
{
uchar counts;
uchar i;
if(k1==0)
{
Delay10us();
if(k1==0)
{
for (i=0;i<7;i++)
{
Send_zifu (a[i]) ;
}
counts++;
}
}
if(k2==0)
{
Delay20ms();
if(k2==0)
{
Send_string (b) ;
counts++;
}
}
}
/************************************************
主函数
************************************************/
void main(void)
{
Timer0Init();
P3 = 0xff;
UartInit();
while(1)
{
key();
}
}
/*
*/