⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pwm.c

📁 这是一个正确的飞思卡尔智能车程序,希望对比赛有帮助!!!
💻 C
字号:
/**********************************************************************

 * 文 件 名:pwm.c
 
 * 功    能:脉冲输出模块,控制电机速度和舵机转向
 
 * 日    期:2007年4月26日
 
 **********************************************************************/
#include <hidef.h>      /* common defines and macros */
#include <mc9s12dg128.h>     /* derivative information */ 
#define Mid_PWM 2932
#define Right_Max 3465
#define Left_Max 2399
//PWM初始化
//设置舵机频率为50HZ,PWM7输出;设置电机频率为1kHZ,PWM0 1输出
void InitPWM(void)
{  
    PWME = 0; //关闭所有通道
    PWMCTL_CON01 = 1;//0,1通道级联组合成一个16bit通道
    PWMPOL = 0xff;//设置0 1 67通道为正极性方波
    PWMCAE = 0;//设置0 1 67通道为左对齐方式
    PWMCLK = 0xc2;//SA作为0 1通道的时钟源,SB作为67通道的时钟源
    
    //PWMSCLA = 80;//CLOCK SA = CLOCK A /(2*80)=200K   32M
    PWMSCLB = 100;//CLOCK SA = CLOCK A /(2*100)=200K    40M
    PWMPER6 = 200;//6通道周期计数器设置         周期1ms
    PWMPER7= 200;//7通道周期计数器设置         周期1ms    
    
    //PWMSCLB = 8;//CLOCK SB = CLOCK B /(2*8)=2M           32M
    PWMSCLA = 10;//CLOCK SB = CLOCK B /(2*10)=2M            40M
    //40000
    PWMPER0 = 0x9C;
    PWMPER1 = 0x40;//01通道周期计数器设置       周期20ms
   
    PWMDTY6 = 0;//6通道占空比初始化,速度为0     低电平
    PWMDTY7 = 0;//7通道占空比初始化,速度为0     低电平
    
    // 3000
    PWMDTY0 = 0x0B;
    PWMDTY1 = 0xB8;//0 1通道占空比初始化,角度为正前方90度    1.5ms高电平
    
    PWME = 0xc2;//打开0 1 67通道,驱动电机和舵机使能  
}

//设置舵机,a为0到180度的角度,小车正前方为90度
void SetAngle(signed char a)
{
    word Dutycycle,Highbyte;
    if(a >= 30) a = 30;
    if(a <= -30) a = -30;
    Dutycycle = a*800/45+2932;//-30度-30度
    Highbyte = Dutycycle & 0xFF00;
    PWMDTY0 = Highbyte>>8;
    PWMDTY1 = Dutycycle&0x00FF;
}

void SetDutycycle(word temp_Duty)
  {    word High;
       if(temp_Duty>Right_Max) temp_Duty=Right_Max;
       if(temp_Duty<Left_Max) temp_Duty=Left_Max;
       High = temp_Duty & 0xFF00;
       PWMDTY0 = High>>8;
       PWMDTY1 = temp_Duty&0x00FF;
  }
//设置电机速度
//1通道正转,0通道反转,v为0-200的整数,v/200为1通道的占空比
void SetSpeed(byte v)
{    
    static byte tempspeed = 0;     //静态局部变量
    PWMDTY7 = 0;
    if (tempspeed < v)
    {
        for (;tempspeed <= v; tempspeed+=5)
            PWMDTY6 = tempspeed;
    }
    else
    {
        tempspeed = v;
        PWMDTY6 = tempspeed;
    }
    return ;
}

//反转,0通道反转
void SetRevspeed(byte v)
{
    PWMDTY7 = v;
    PWMDTY6 = 0;
} 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -