小弟最近学单片机,本想实现通过串口来更改定时器的定时时间,来达到P11口的电平变化,然后,上电之后不正常,串口发送了一些数据之后,才正常,不知道是怎么回事,希望各位大神看看我程序哪里错了
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
void serial_time_init(void);
uint dingshi = 40;
uint flag=0;
static uint aaa=0;
sbit P35 = P3^5;
sbit P20 = P2^0;
sbit P21 = P2^1;
sbit P11 = P1^1;
void main()
{
P1 = 0XFF;
P35 = 0;
P21 = 0;
P20 = 0;
serial_time_init();
while(1)
{
if(flag==1)
{
flag = 0;
TR0 = 0;
dingshi = aaa;
TR0 = 1;
}
}
}
void serial_time_init(void)
{
SCON = 0X50;
TMOD |= 0X21;
TH1 = 0XFD;
TL1 = 0XFD;
TR1 = 1;
TH0 = (65536-50000)/256;
TL0 = (65536-50000)%256;
TR0 = 1;
ET0 = 1; //这里也可以写成 SM0 = .................
EA = 1;
ES = 1;
}
void serial() interrupt 4
{
if(RI==1)
{
RI = 0;
aaa = SBUF;
flag = 1;
}
}
void time() interrupt 1
{
static uchar a = 0;
TH0 = (65536-50000)/256;
TL0 = (65536-50000)%256;
a++;
if(a==dingshi)
{
a=0;
P11 =0;
}
else
P11 = 1;
}
|