#include <reg51.h>
#define uchar unsigned char
#define uint unsigned int
#define T 10000
uchar i;
sbit CLK1=P2^0;
sbit CWB1=P2^1;
sbit CLK2=P2^2;
sbit CWB2=P2^3;
sbit K1=P1^0;
sbit K2=P1^1;
sbit K3=P1^2;
sbit K4=P1^3;
/* 定时器0服务子程序 */
void
time0() interrupt 1 // 用定时器0中断来产生CLK1时钟
{
unsigned char a;
TH0 = -T/256;
TL0 = -T%256;
a++;
if(a=100)
{
CLK1 = ~CLK1;
}
}
/* 定时器1服务子程序 */
void tim1() interrupt 3 // 用定时器1中断来产生CLK2时钟
{
unsigned char b;
TH1 = -T/256;
TL1 = -T%256;
b++;
if(b=100)
{
CLK2= ~CLK2;
}
}
/*串口初始化*/
void init_uart(void)
{
TMOD=0x21;
TH1=0xfd;
TL1=0xfd;
SCON=0x50;
PCON=0x00;
TR1=1;
ES=1;
}
/* 主程序 */
void main()
{
EA=1;
ET0=1;
ET1=1;
init_uart();
while(1)
{
if(RI==1) //查看是否接收到数据
{
i=SBUF; //读取数据
RI=0; } //清除标志位
switch(i)
{
case 0x01:
TH0 = -T/256;
TL0 = -T%256;
TR0=1;
break;
case 0x02:
TR0=0;
break;
case 0x03:
CWB1=0;
break;
case 0x04:
CWB1=1;
break;
case 0x05:
TR1=0;
TMOD=0x10;
TH1=-T/256;
TL1=-T%256;
TR0=1;
break;
case 0x06:
TR1=0;
break;
case 0x07:
CWB2=0;
break;
case 0x08:
CWB2=1;
break;
default:break;
}
}
}
串口接收程序 发送03 04是对的 01 02不对
一周热门 更多>