为什么0-150就是pwm_t==0的时候是低电平,170-255是高电平?
//PWM电机调速
#include <reg52.h>
#include <..CONFIGQXA51.h>
unsigned char pwm_left_val = 170;//左电机占空比值 取值范围0-170,0最快
unsigned char pwm_right_val = 170;//右电机占空比值取值范围0-170 ,0最快
unsigned char pwm_t;//周期
/*void delay(unsigned int z)//毫秒级延时
{
unsigned int x,y;
for(x = z; x > 0; x--)
for(y = 114; y > 0 ; y--);
}*/
/*小车前进*/
void forward()
{
left_motor_go; //左电机前进
right_motor_go; //右电机前进
}
//定时器0中断
void timer0() interrupt 1
{
pwm_t++;
if(pwm_t == 255) // 0-170 低电平
pwm_t = EN1 = EN2 = 0;
if(pwm_left_val == pwm_t) // 170-255 高电平
EN1 = 1;
if(pwm_right_val == pwm_t)
EN2 = 1;
}
void main()
{
TMOD |= 0x02;//8位自动重装模块
TH0 = 220;
TL0 = 220;//11.0592M晶振下占空比最大比值是256,输出100HZ
TR0 = 1;//启动定时器0
ET0 = 1;//允许定时器0中断
EA = 1;//总中断允许
while(1)
{
forward();
}
}
一周热门 更多>