外部中断测脉冲数,当触发了外部中断后,定时器0开始计时2秒,2s完成后将脉冲数通过串口发送电脑。那三个中断程序该怎么写?用的是STC89C52
#include <reg52.h>
typedef unsigned char uint8;
typedef unsigned int uint16;
uint8 getdat;
uint16 time,counter=0;
void init()
{
TMOD = 0x21;
TH1 = 0xfd;
TL1 = 0xfd;
SCON=0x50;
TR1 = 1;
EA=1;
ES=1;
TH0=0xfc;
TL0=0x67;
TR0=1;
ET0=1;
EX1=1;
IT1=1;
}
main()
{
init();
while (1);
}
void ex1_interrupt() interrupt 2
{
counter++;
}
void t0_interrupt() interrupt 1
{
TH0=0xfc;
TL0=0x67;
time++;
if(time==2000)
{
time=0;
ES=0;
SBUF=counter;
while(!TI);
TI=0;
ES=1;
counter=0;
}
}
void UART_interrupt() interrupt 4
{
if(RI)
{
RI=0;
getdat=SBUF;
}
}
这个是我编的,请问有没有问题?旋转编码器的A相接外部中断1,测出的数据是否准确?我的旋转编码器有点问题。测不了
那在我的程序上应如何修改?
追答1如果想采集快些,将你的编码器倍频,然后采集时可以用100ms采集,这样一般工业上要求采集响应还是够的,这个你要作下硬件处理,软件将定时改为100ms,进行数据分析发送。
2发送可以做个子程序。然后分解发送。
void send_data(uint8 ch)
{
ES=0;
TI=0;
SBUF=ch;
while(!TI);
TI=0;
ES=1;
}
这里改成
if(time==100)//100ms采集,1秒10次,你现在可以用2000实验。因为硬件问题。
{
time=0;
send_data(counter/256);//发送高位
send_data(counter%256);//发送低位
counter=0;
}