求大神帮忙,帮忙添加一个0.5倍速和1倍速切换的功能,89c51rc单片机
时间:10-02
整理:3721RD
点击:
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
#define FOSC 12000000L
#define TSTEP0 (65536-FOSC*16875/12000000)//16875*1us计时
sbit KEY0=P3^2;//反向加速按键
//sbit KEY2=P3^3;
sbit POUT=P0^4;
sbit POUT1=P0^5;
uchar Count0 = 0;
uint s;
uint s1;
//uchar Count1 = 0;
void delay500ms(void)
{
uchar i,j,k;
for(i=15;i>0;i--)
for(j=202;j>0;j--)
for(k=81;k>0;k--);
}
void delay1(uint del1)//延时del1X0.1ms 函数
{
uint i,j;
for(i=0;i<del1;i++)
{
for(j=0;j<10;j++);
}
}
/*void Time0_Init()
{
TMOD = 0x01; //定时器0选用方式1
TH0 = TSTEP0; //初值的计算
TL0 = TSTEP0>>8;
TR0 = 1; //启动定时器
ET0 = 1; //打开定时器中断允许位
}
void Time0_Int() interrupt 1
{
TH0 = TSTEP0;
TL0 = TSTEP0>>8;
Count0++; //长度加1
}*/
void Time1_Init()
{
// TMOD = 0x10; //定时器1选用方式1
TH1 = TSTEP0; //初值的计算
TL1 = TSTEP0>>8;
TR1 = 1; //启动定时器
ET1 = 1; //打开定时器中断允许位
}
void Time1_Int() interrupt 3
{
TH1 = TSTEP0;
TL1 = TSTEP0>>8;
Count0++; //长度加1
}
void Outside_Init0(void)
{
IT0 = 0; //低电平触发
EX0 = 1; //外部中断0允许
// IT1 = 0; //低电平触发
// EX1 = 1; //外部中断1允许
}
void Outside_Int0(void) interrupt 0 //高速反转中断函数
{
delay500ms();//延时500ms给步进电机反向旋转缓冲
POUT1=0;//反向
s=250;
s1=1;//改s1可以修改反转速率
while(1)
{
if(KEY0==0&&25000/s>s1)
{
delay1(25000/s);//缓慢加速至高速
s++;
POUT=!POUT;
}
else if (KEY0==0&&(25000/s<=s1))
{
delay1(s1);
POUT=!POUT;
}
else
{
delay500ms();
POUT1=1;
return;
}
}
}
void Main(void)
{
Time1_Init();
Outside_Init0();
EA = 1; //打开总中断
POUT1=1;//正向
while(1)
{
if(Count0==10)
{
Count0=0;
POUT=!POUT;
}
}
}
![](../imgqa/ele/mcu/mcu-3721rd.com-30885eudrrmg3vw0.jpg)
#define uchar unsigned char
#define uint unsigned int
#define FOSC 12000000L
#define TSTEP0 (65536-FOSC*16875/12000000)//16875*1us计时
sbit KEY0=P3^2;//反向加速按键
//sbit KEY2=P3^3;
sbit POUT=P0^4;
sbit POUT1=P0^5;
uchar Count0 = 0;
uint s;
uint s1;
//uchar Count1 = 0;
void delay500ms(void)
{
uchar i,j,k;
for(i=15;i>0;i--)
for(j=202;j>0;j--)
for(k=81;k>0;k--);
}
void delay1(uint del1)//延时del1X0.1ms 函数
{
uint i,j;
for(i=0;i<del1;i++)
{
for(j=0;j<10;j++);
}
}
/*void Time0_Init()
{
TMOD = 0x01; //定时器0选用方式1
TH0 = TSTEP0; //初值的计算
TL0 = TSTEP0>>8;
TR0 = 1; //启动定时器
ET0 = 1; //打开定时器中断允许位
}
void Time0_Int() interrupt 1
{
TH0 = TSTEP0;
TL0 = TSTEP0>>8;
Count0++; //长度加1
}*/
void Time1_Init()
{
// TMOD = 0x10; //定时器1选用方式1
TH1 = TSTEP0; //初值的计算
TL1 = TSTEP0>>8;
TR1 = 1; //启动定时器
ET1 = 1; //打开定时器中断允许位
}
void Time1_Int() interrupt 3
{
TH1 = TSTEP0;
TL1 = TSTEP0>>8;
Count0++; //长度加1
}
void Outside_Init0(void)
{
IT0 = 0; //低电平触发
EX0 = 1; //外部中断0允许
// IT1 = 0; //低电平触发
// EX1 = 1; //外部中断1允许
}
void Outside_Int0(void) interrupt 0 //高速反转中断函数
{
delay500ms();//延时500ms给步进电机反向旋转缓冲
POUT1=0;//反向
s=250;
s1=1;//改s1可以修改反转速率
while(1)
{
if(KEY0==0&&25000/s>s1)
{
delay1(25000/s);//缓慢加速至高速
s++;
POUT=!POUT;
}
else if (KEY0==0&&(25000/s<=s1))
{
delay1(s1);
POUT=!POUT;
}
else
{
delay500ms();
POUT1=1;
return;
}
}
}
void Main(void)
{
Time1_Init();
Outside_Init0();
EA = 1; //打开总中断
POUT1=1;//正向
while(1)
{
if(Count0==10)
{
Count0=0;
POUT=!POUT;
}
}
}
![](../imgqa/ele/mcu/mcu-3721rd.com-30885eudrrmg3vw0.jpg)
加速什么,频率输出吗?