K60笔记-PWM控制电机

本程序使用山外库:http://git.oschina.net/chenxuuu/vcan-K60-V5.3

首先初始化

    ftm_pwm_init(FTM0, FTM_CH2,10*1000,0);
    //初始化 FTM PWM ,使用 FTM0_CH3,频率为10k ,占空比为 100 / FTM0_PRECISON
    ftm_pwm_init(FTM0, FTM_CH3,10*1000,0);
    ftm_pwm_init(FTM0, FTM_CH4,10*1000,0);
    ftm_pwm_init(FTM0, FTM_CH5,10*1000,0);

PWM驱动

    ftm_pwm_duty(FTM0, FTM_CH6, 10);
    //设置 FTM0_CH6 占空比为 10/FTM0_PRECISON

示例:

/*!
 *     COPYRIGHT NOTICE
 *     Copyright (c) 2013,山外科技
 *     All rights reserved.
 *     技术讨论:山外论坛 http://www.vcan123.com
 *
 *     除注明出处外,以下所有内容版权均属山外科技所有,未经允许,不得用于商业用途,
 *     修改内容时必须保留山外科技的版权声明。
 *
 * @file       main.c
 * @brief      山外K60 平台主程序
 * @author     山外科技
 * @version    v5.2
 * @date       2014-11-03
 */

#include "common.h"
#include "include.h"

/*!
 *  @brief      main函数
 *  @since      v5.2
 *  @note       FTM PWM 电机驱动测试
 */
void main(void)
{
    uint8 i = 0;
    printf("\n*****FTM PWM 电机测试*****\n");

    ftm_pwm_init(FTM0, FTM_CH3,10*1000,80);         //初始化 FTM PWM ,使用 FTM0_CH3,频率为10k ,占空比为 100 / FTM0_PRECISON
                                                    // FTM0_PRECISON 配置 为 100 ,即占空比 为 100%
                                                    // port_cfg.h 里 配置 FTM0_CH3 对应为 PTA6
    gpio_init(PTD15,GPO,0);                         // 使能端 输入为 0

    //山外的电机驱动模块,经过 MOS 管 反相隔离。
    //K60 输出 PWM 为 100% ,实际接入 电机驱动就是 0%
    //K60 输出 使能端 为 低电平,实际接入 电机驱动 使能端就是 高电平

    while(1)
    {
        for(i= 0;i<=100;i+=10)
        {
            ftm_pwm_duty(FTM0,FTM_CH3,i);       //改变 占空比 ,K60 输出 PWM 占空比 逐渐增大,电机逐渐 降速
            DELAY_MS(500);
        }
    }
}

然而感觉这样子好麻烦诶qwq

所以翻出了原来的一个函数

/************电机输出函数*************/
void SetMotorVoltage(float fLeftVoltage,float fRightVoltage)
{
    int nOut;
    if(fLeftVoltage>0)
    {
	ftm_pwm_duty(FTM0,FTM_CH2,0);//左轮正向运动PWM占空比为0
	nOut=(int)(fLeftVoltage*PERIOD);//
	if(nOut>1000)
	{
		nOut=1000;
	}
	ftm_pwm_duty(FTM0,FTM_CH3,nOut);//左轮反向运动PWM占空比为nOut
    }                                                   //左电机正转
    else
    {
	ftm_pwm_duty(FTM0,FTM_CH3,0);//左轮反向运动PWM占空比为0
	fLeftVoltage=-fLeftVoltage;
	nOut=(int)(fLeftVoltage*PERIOD);
	if(nOut>1000)
	{
		nOut=1000;
	}
	ftm_pwm_duty(FTM0,FTM_CH2,nOut);//左轮正向运动PWM占空比为nOut
    }                                                    //左电机反转

    if(fRightVoltage>0)
    {
	ftm_pwm_duty(FTM0,FTM_CH4,0);//右轮正向运动PWM占空比为0
	nOut=(int)(fRightVoltage*PERIOD);
	if(nOut>1000)
	{
		nOut=1000;
	}
	ftm_pwm_duty(FTM0,FTM_CH5,nOut);//右轮反向运动PWM占空比为nOut
    }                                                     //右电机正转
    else
    {
	ftm_pwm_duty(FTM0,FTM_CH5,0);//右轮反向运动PWM占空比为0
	fRightVoltage=-fRightVoltage;
	nOut=(int)(fRightVoltage*PERIOD);
	if(nOut>1000)
	{
		nOut=1000;
	}
	ftm_pwm_duty(FTM0,FTM_CH4,nOut);//右轮正向运动PWM占空比为nOut
    }                                                     //右电机反转
}

这个函数可以直接让电机转

调用时可以这样:

SetMotorVoltage(x,y);
//正值正转,负值反转,xy均为float类型

示例

/*!
 *     COPYRIGHT NOTICE
 *     Copyright (c) 2013,山外科技
 *     All rights reserved.
 *     技术讨论:山外论坛 http://www.vcan123.com
 *
 *     除注明出处外,以下所有内容版权均属山外科技所有,未经允许,不得用于商业用途,
 *     修改内容时必须保留山外科技的版权声明。
 *
 * @file       main.c
 * @brief      山外K60 平台主程序
 * @author     山外科技
 * @version    v5.2
 * @date       2014-11-03
 */

#include "common.h"
#include "include.h"
#include "MK60_adc.h"
#include  "OLED.h"
#define	PERIOD				100				//电压转换PWM比例    待定

/************电机输出函数*************/
void SetMotorVoltage(float fLeftVoltage,float fRightVoltage)
{
    int nOut;
    if(fLeftVoltage>0)
    {
	ftm_pwm_duty(FTM0,FTM_CH2,0);//左轮正向运动PWM占空比为0
	nOut=(int)(fLeftVoltage*PERIOD);//
	if(nOut>1000)
	{
		nOut=1000;
	}
	ftm_pwm_duty(FTM0,FTM_CH3,nOut);//左轮反向运动PWM占空比为nOut
    }                                                   //左电机正转
    else
    {
	ftm_pwm_duty(FTM0,FTM_CH3,0);//左轮反向运动PWM占空比为0
	fLeftVoltage=-fLeftVoltage;
	nOut=(int)(fLeftVoltage*PERIOD);
	if(nOut>1000)
	{
		nOut=1000;
	}
	ftm_pwm_duty(FTM0,FTM_CH2,nOut);//左轮正向运动PWM占空比为nOut
    }                                                    //左电机反转

    if(fRightVoltage>0)
    {
	ftm_pwm_duty(FTM0,FTM_CH4,0);//右轮正向运动PWM占空比为0
	nOut=(int)(fRightVoltage*PERIOD);
	if(nOut>1000)
	{
		nOut=1000;
	}
	ftm_pwm_duty(FTM0,FTM_CH5,nOut);//右轮反向运动PWM占空比为nOut
    }                                                     //右电机正转
    else
    {
	ftm_pwm_duty(FTM0,FTM_CH5,0);//右轮反向运动PWM占空比为0
	fRightVoltage=-fRightVoltage;
	nOut=(int)(fRightVoltage*PERIOD);
	if(nOut>1000)
	{
		nOut=1000;
	}
	ftm_pwm_duty(FTM0,FTM_CH4,nOut);//右轮正向运动PWM占空比为nOut
    }                                                     //右电机反转
}

/*!
 *  @brief      main函数
 *  @since      v5.2
 *  @note       FTM PWM 电机驱动测试
 */
void main(void)
{
    float pwmc=0;
    printf("\n*****FTM PWM 电机测试*****\n");

    ftm_pwm_init(FTM0, FTM_CH2,10*1000,0);         //初始化 FTM PWM ,使用 FTM0_CH3,频率为10k ,占空比为 100 / FTM0_PRECISON
    ftm_pwm_init(FTM0, FTM_CH3,10*1000,0);
    ftm_pwm_init(FTM0, FTM_CH4,10*1000,0);
    ftm_pwm_init(FTM0, FTM_CH5,10*1000,0);
                                                    // FTM0_PRECISON 配置 为 100 ,即占空比 为 100%
                                                    // port_cfg.h 里 配置 FTM0_CH3 对应为 PTA6
//    gpio_init(PTD15,GPO,0);                         // 使能端 输入为 0
    //山外的电机驱动模块,经过 MOS 管 反相隔离。
    //K60 输出 PWM 为 100% ,实际接入 电机驱动就是 0%
    //K60 输出 使能端 为 低电平,实际接入 电机驱动 使能端就是 高电平

    while(1)
    {
        for(pwmc=0;pwmc<1;pwmc+=0.1)
        {
          SetMotorVoltage(pwmc,pwmc);
          led (LED1,LED_ON);
          lptmr_delay_ms(1000);
          led (LED1,LED_OFF);
          lptmr_delay_ms(100);
        }
        for(pwmc=0;pwmc>-1;pwmc-=0.1)
        {
          SetMotorVoltage(pwmc,pwmc);
          led (LED1,LED_ON);
          lptmr_delay_ms(1000);
          led (LED1,LED_OFF);
          lptmr_delay_ms(100);
        }
    }
}

本文的工程文件:http://git.oschina.net/chenxuuu/K60-test/tree/master/FTM_PWM_电机驱动

“K60笔记-PWM控制电机”的2个回复

  1. Google Chrome 52.0.2743.116 Google Chrome 52.0.2743.116 Windows 7 x64 Edition Windows 7 x64 Edition
    Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36

    谢谢分享,

    1. Google Chrome 52.0.2743.98 Google Chrome 52.0.2743.98 Android 4.4.4 Android 4.4.4
      Mozilla/5.0 (Linux; Android 4.4.4; OPPO R7 Build/KTU84P) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.98 Mobile Safari/537.36

      站内有完整源码

发表评论

电子邮件地址不会被公开。 必填项已用*标注

*(必填)