51单片机,L298N模块,按钮按一下电机快速转5秒然后再慢速转5秒最后停止,程序怎么写用(中断、定时器)

51单片机,L298N模块,按钮按一下电机快速转5秒然后再慢速转5秒最后停止,程序怎么写用(中断、定时器)

#include<reg51.h>
#define uchar unsigned char
uchar stepd[]={0x01,0x03,0x02,0x06,0x04,0xc0,0x08,0x09};
uchar num=0,fx=0;
sbit key1=P3^0;
sbit key2=P3^1;
sbit key3=P3^2;
unsigned int fcnt,scnt,ft,st;
void t0isr() interrupt 1
{
if(ft>0)
{
TH0=(65536-fcnt)/256;
TL0=(65536-fcnt)%256;
ft--;
}
else
{
TH0=(65536-fcnt)/256;
TL0=(65536-fcnt)%256;
st--;
}
if(ft==0 && st==0)TR0=0;
P2=stepd[num];
if(fx==0){num++;num%=8;}
if(fx==1)
{
if(num==0)num=7;
else num--;
}
}
main()
{
TMOD=0x01;
fcnt=5000;
scnt=10000;
TH0=(65536-fcnt)/256;
TL0=(65536-fcnt)%256;
TR0=1;
ET0=1;
EA=1;
while(1)
{
if(key1==0)
{
while(key1==0);
TR0=~TR0;
ft=1000;
st=500;
}
if(key2==0)
{
while(key2==0);
fx=0;
}
if(key3==0)
{
while(key3==0);
fx=1;
}
}
}
温馨提示:答案为网友推荐,仅供参考