#include<reg52.h>
sbit KEY4 = P3^6; //按键判断端口
unsigned char KeySta = 1;
unsigned char T0RH;
unsigned char T0RL;
void ConfigUART(unsigned int baud);
void ConfigTimer0(unsigned int ms);
void main()
{
unsigned char backup = 1;
unsigned char i;
ConfigTimer0(1);
ConfigUART(9600);
P3 = 0xf7;
PS = 1;
EA = 1;
while(1)
{
// SBUF = 0x01; //若在主函数发送
// while(!TI);
// TI = 0;
P1 = i;
if(KeySta!=backup)
{
if(backup==0)
{
i++;
i&=7;
TI = 0;
SBUF = 0x01; //按键发送
while(!TI);
TI = 0;
}
backup = KeySta;
}
}
}
void ConfigTimer0(unsigned int ms)
{
unsigned long tmp; //临时变量(需开中断EA = 1;和定义T0RH,T0RL
tmp = 11059200 / 12; //定时器计数频率
tmp = (tmp * ms) / 1000; //计算所需的计数值
tmp = 65536 - tmp; //计算定时器重载值
tmp = tmp + 18; //补偿中断响应延时造成的误差
T0RH = (unsigned char)(tmp>>8); //定时器重载值拆分为高低字节
T0RL = (unsigned char)tmp;
TMOD &= 0xF0; //清零T0的控制位
TMOD |= 0x01; //配置T0为模式1
TH0 = T0RH; //加载T0重载值
TL0 = T0RL;
ET0 = 1; //使能T0中断
TR0 = 1; //启动T0
}
void ConfigUART (unsigned int baud) //开串口
{
SCON = 0x50;
TMOD &= 0x0f;
TMOD |= 0x20;
TH1 = 256-11059200/12/32/baud;
TL1 = TH1;
ET1 = 0;
ES = 1;
TR1 = 1;
IP = 0x10;
}
void Interrupt_Time0() interrupt 1 using 2 //按键判断
{
static unsigned char keybuf = 0xff;
TH0 = T0RH;
TL0 = T0RL;
keybuf = (keybuf<<1)|(KEY4);
if(keybuf == 0)
KeySta = 0;
if(keybuf == 0xff)
KeySta = 1;
}
3 个解决方案
#1
应该能发送,在按键程序里改变一个io口看看
#2
开了串口中断,中断程序在哪?
#3
LZ还得想想该如何编程!
SBUF = 0x01; //按键发送
while(!TI);//这句就不要了
中断4里,加入
if(TI) TI=0;
SBUF = 0x01; //按键发送
while(!TI);//这句就不要了
中断4里,加入
if(TI) TI=0;
#1
应该能发送,在按键程序里改变一个io口看看
#2
开了串口中断,中断程序在哪?
#3
LZ还得想想该如何编程!
SBUF = 0x01; //按键发送
while(!TI);//这句就不要了
中断4里,加入
if(TI) TI=0;
SBUF = 0x01; //按键发送
while(!TI);//这句就不要了
中断4里,加入
if(TI) TI=0;