11.如何举行小车PWM调速及其平滑转向

打印 上一主题 下一主题

主题 1691|帖子 1691|积分 5073

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
原理: 全速进步是wheel_left_A1A= 0; wheel_left_A1B= 1;完全制止是wheel_left_A1A= 0;wheel_left_A1B= 0;那么单元时 间内,比如20ms,   有  15ms  是全速进步,  5ms  是完全制止,     速率就会比  5ms  全速进步,  15ms  完全制止获得的功率多,相应的速率更快!     文件夹main.c    #include "system.h"
  sbit D1 = P3^5;
sbit D2 = P3^6;
sbit D3 = P3^7;
  extern u8 speed_right;
extern u8 speed_left;
  void main(void)
{
    D1 = 1;
    D2 = 1;
    D3 = 1;
    Timer0_Init();
    Timer1_Init();
    Uart1_Init();
    Delayms(500);
    
    while(1)
    {
        speed_right = 10;
        speed_left  = 40;
        Delayms(2000);
        speed_right = 40;
        speed_left  = 10;
        Delayms(2000);
        D1 = 0;
        
    }
}
  文件夹time.c
  u8 speed_right;
u8 cnt_time0_right;
u8 speed_left;
u8 cnt_time1_left;
  void Timer0_Init(void)        //500微秒@11.0592MHz
{
    AUXR &= 0x7F;            //定时器时钟12T模式
    TMOD &= 0xF0;            //设置定时器模式
    TMOD |= 0x01;
    TL0 = 0x33;                //设置定时初始值
    TH0 = 0xFE;                //设置定时初始值
    TF0 = 0;                //清除TF0标记
    TR0 = 1;                //定时器0开始计时
    ET0 = 1;                //使能定时器0中断
    EA = 1;
}
  
void Timer0_Isr(void) interrupt 1
{
    cnt_time0_right++;
    
    /* 重新装入初值确保每个周期一样 */
    TL0 = 0x33;
    TH0 = 0xFE;
    
    if(cnt_time0_right < speed_right)
    {
        Goto_Forward_Right();
    }
    else
    {
        Goto_Stop();
    }
    
    if(cnt_time0_right == 40) cnt_time0_right = 0;
}
  
void Timer1_Init(void)        //500微秒@11.0592MHz
{
    AUXR &= 0x7F;            //定时器时钟12T模式
    TMOD &= 0x0F;            //设置定时器模式
    TMOD |= 0x10;
    TL1 = 0x33;                //设置定时初始值
    TH1 = 0xFE;                //设置定时初始值
    TF1 = 0;                //清除TF0标记
    TR1 = 1;                //定时器0开始计时
    ET1 = 1;                //使能定时器0中断
    EA = 1;
}
  
void Timer1_Isr(void) interrupt 3
{
    cnt_time1_left++;
    
    /* 重新装入初值确保每个周期一样 */
    TL0 = 0x33;
    TH0 = 0xFE;
    
    if(cnt_time1_left < speed_left)
    {
        Goto_Forward_Left();
    }
    else
    {
        Goto_Stop();
    }
    
    if(cnt_time1_left == 40) cnt_time1_left = 0;
}
  文件夹motor.c
  sbit wheel_left_A1A = P3^4;
sbit wheel_left_A1B = P3^5;
sbit wheel_right_B1A = P3^2;
sbit wheel_right_B1B = P3^3;
  void Goto_Back(void)
{
    wheel_left_A1A = 1;
    wheel_left_A1B = 0;
    
    wheel_right_B1A = 1;
    wheel_right_B1B = 0;
}
  void Goto_TurnLeft(void)
{
    wheel_left_A1A = 0;
    wheel_left_A1B = 0;
    
    wheel_right_B1A = 0;
    wheel_right_B1B = 1;
}
  void Goto_TurnRight(void)
{
    wheel_left_A1A = 0;
    wheel_left_A1B = 1;
    
    wheel_right_B1A = 0;
    wheel_right_B1B = 0;
}
  void Goto_Forward(void)
{
    wheel_left_A1A = 0;
    wheel_left_A1B = 1;
    
    wheel_right_B1A = 0;
    wheel_right_B1B = 1;
}
  void Goto_Forward_Left(void)
{
    wheel_left_A1A = 0;
    wheel_left_A1B = 1;
}
  void Goto_Forward_Right(void)
{
    wheel_right_B1A = 0;
    wheel_right_B1B = 1;
}
  void Goto_Stop(void)
{
    wheel_left_A1A = 0;
    wheel_left_A1B = 0;
    
    wheel_right_B1A = 0;
    wheel_right_B1B = 0;
}
 

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

圆咕噜咕噜

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表