MOTO.c(参考匿名的)

#include "MOTO.h"

int16_t MOTO1_PWM = 0;
int16_t MOTO2_PWM = 0;
int16_t MOTO3_PWM = 0;
int16_t MOTO4_PWM = 0;

void Moto_PwmRflash(int16_t MOTO1_PWM,int16_t MOTO2_PWM,int16_t MOTO3_PWM,int16_t MOTO4_PWM)
{       
    if(MOTO1_PWM>Moto_PwmMax)   MOTO1_PWM = Moto_PwmMax;
    if(MOTO2_PWM>Moto_PwmMax)   MOTO2_PWM = Moto_PwmMax;
    if(MOTO3_PWM>Moto_PwmMax)   MOTO3_PWM = Moto_PwmMax;
    if(MOTO4_PWM>Moto_PwmMax)   MOTO4_PWM = Moto_PwmMax;
    if(MOTO1_PWM<0) MOTO1_PWM = 0;
    if(MOTO2_PWM<0) MOTO2_PWM = 0;
    if(MOTO3_PWM<0) MOTO3_PWM = 0;
    if(MOTO4_PWM<0) MOTO4_PWM = 0;

    TIM4->CCR1 =MOTO1_PWM;
    TIM4->CCR2 = MOTO2_PWM;
    TIM4->CCR3 = MOTO3_PWM;
    TIM4->CCR4 = MOTO4_PWM;
}

void Tim4_init(void)
{
    TIM_TimeBaseInitTypeDef     TIM_TimeBaseStructure;
    TIM_OCInitTypeDef               TIM_OCInitStructure;
    uint16_t PrescalerValue = 0;
    /* -----------------------------------------------------------------------
    TIM3 Configuration(配置): generate 4 PWM signals with 4 different duty cycles(占空比):
    The TIM3CLK frequency(频率) is set to SystemCoreClock (Hz), to get TIM3 counter
    clock at 24 MHz the Prescaler is computed(计算) as following:
    - Prescaler = (TIM3CLK / TIM3 counter clock) - 1
    SystemCoreClock is set to 72 MHz for Low-density(密度), Medium-density, High-density
    and Connectivity(连通性) line devices and to 24 MHz for Low-Density Value line and
    Medium-Density Value line devices

    The TIM3 is running at 36 KHz: TIM3 Frequency = TIM3 counter clock/(ARR + 1)
    = 24 MHz / 1000 = 24 KHz
    TIM3 Channel1 duty cycle = (TIM3_CCR1/ TIM3_ARR)* 100 = 50%
    TIM3 Channel2 duty cycle = (TIM3_CCR2/ TIM3_ARR)* 100 = 37.5%
    TIM3 Channel3 duty cycle = (TIM3_CCR3/ TIM3_ARR)* 100 = 25%
    TIM3 Channel4 duty cycle = (TIM3_CCR4/ TIM3_ARR)* 100 = 12.5%
    ----------------------------------------------------------------------- */
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);//TIM3和TIM4同在APB1时钟下
    /* Compute the prescaler value */
    PrescalerValue = (uint16_t) (SystemCoreClock / 24000000) - 1;
    /* Time base configuration */
    TIM_TimeBaseStructure.TIM_Period = 999;     //计数上线  ,CNT=999时中断计数器并结束一个周期,进行下一个从零开始的周期
    TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;   //pwm时钟分频 72MHz多大分频分成24MHz
    TIM_TimeBaseStructure.TIM_ClockDivision = 0;    
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;     //向上计数
    TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);

    /* PWM1 Mode configuration: Channel */
    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;//CNT<CCR1时为有效电平
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
    TIM_OCInitStructure.TIM_Pulse = 0;//初始占空比为0
    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;//低电平有效
    TIM_OC1Init(TIM4, &TIM_OCInitStructure);
    TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable);
    TIM_OC2Init(TIM4, &TIM_OCInitStructure);
    TIM_OC2PreloadConfig(TIM4, TIM_OCPreload_Enable);
    TIM_OC3Init(TIM4, &TIM_OCInitStructure);
    TIM_OC3PreloadConfig(TIM4, TIM_OCPreload_Enable);
    TIM_OC4Init(TIM4, &TIM_OCInitStructure);
    TIM_OC4PreloadConfig(TIM4, TIM_OCPreload_Enable);
    TIM_ARRPreloadConfig(TIM4, ENABLE);
    TIM_Cmd(TIM4, ENABLE);
}


void Moto_Init(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    //使能电机用的时钟
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB , ENABLE); 
    //设置电机使用到得管脚
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 ; 
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; 
    GPIO_Init(GPIOB, &GPIO_InitStructure);

    Tim4_init();    
}

发布了58 篇原创文章 · 获赞 4 · 访问量 2万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览