首页 > 代码库 > PWM

PWM

#include "sys.h"#include "beep.h"//tim5 定时器  /*tim4 定时器 定时10分钟 控制LED1*/void my_tim5_init(){    TIM_TimeBaseInitTypeDef TIM_TimeBaseStruct;    NVIC_InitTypeDef  NVIC_InitStruct;    //1.打开时钟    RCC_APB1Peri phClockCmd(RCC_APB1Periph_TIM5, ENABLE);        //2.TIM_TimeBaseInit    // 配置定时器/*    void TIM_TimeBaseInit(TIM_TypeDef* TIMx, TIM_TimeBaseInitTypeDef* TIM_TimeBaseInitStruct)    TIM_TypeDef  控制器基地址 TIM1~14         TIM_TimeBaseInitTypeDef    uint16_t TIM_Prescaler;  分频系数    功能:用于分频    范围:数字    寄存器:PSC            uint16_t TIM_CounterMode; 计数模式    范围:TIM_CounterMode_Up TIM_CounterMode_Down    寄存器:CR1  4 5 6 三位        uint32_t TIM_Period;   自动重载寄存器    范围:    寄存器:ARR          uint16_t TIM_ClockDivision;        uint8_t TIM_RepetitionCounter;        2s  42MHz *2 = 84MHz    */    TIM_TimeBaseStruct.TIM_CounterMode = TIM_CounterMode_Up;    //16位 防止溢出    TIM_TimeBaseStruct.TIM_Period = 20000 -1;     TIM_TimeBaseStruct.TIM_Prescaler= 8400-1;    TIM_TimeBaseInit(TIM5, &TIM_TimeBaseStruct);      //打开中断/*void TIM_ITConfig(TIM_TypeDef* TIMx, uint16_t TIM_IT, FunctionalState NewState)TIM_IT  中断的标志TIM_IT_Update  更新中断NewState   状态ENABLE  DISABLE */    TIM_ITConfig(TIM5, TIM_IT_Update, ENABLE);        NVIC_InitStruct.NVIC_IRQChannel = TIM5_IRQn;    NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;    NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0x2;    NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0x2;    NVIC_Init(&NVIC_InitStruct);}extern u32 jfees;//2S 一次 如何产生10分钟的定时void TIM5_IRQHandler(void){    if(TIM_GetITStatus(TIM5,TIM_IT_Update)==SET) {    //    BEEP = !BEEP;        jfees++;                        TIM_ClearITPendingBit(TIM5,TIM_IT_Update);    }}//占空比:一个时钟周期高电平所占的比例//PWM  外设 需要一个特定的波形//外设需要不同的电压来控制//BEEP  PF8 通过production_data.pdf     TIM13_CH1//PF8 可以 TIM13 的第一个通道//tim13  控制器void timer13_init(){    TIM_TimeBaseInitTypeDef TIM_TimeBaseStruct;    GPIO_InitTypeDef GPIO_InitStruct;    TIM_OCInitTypeDef TIM_OCInitStruct;        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM13, ENABLE);        //PF8 配置成复用模式    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;    GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_8;    GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_DOWN;    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_100MHz;    GPIO_Init(GPIOF, &GPIO_InitStruct);    //把PF8 复用为TIM13功能,就可以把PF8作为 TIM13的一个通道    GPIO_PinAFConfig(GPIOF,GPIO_PinSource8,GPIO_AF_TIM13 );    TIM_TimeBaseStruct.TIM_CounterMode = TIM_CounterMode_Up;    //16位 防止 溢出    TIM_TimeBaseStruct.TIM_Period = 300 -1;     TIM_TimeBaseStruct.TIM_Prescaler= 84-1;    TIM_TimeBaseInit(TIM13, &TIM_TimeBaseStruct);        //PWM的配置    //CCMR1  OC1M  4~6    //111 PWM2     CNT > CCR1  输出有效电平    //110 PWM1     CNT < CCR1  输出有效电平        //CCER 1位 配置有效电平/*void TIM_OCnInit(TIM_TypeDef* TIMx, TIM_OCInitTypeDef* TIM_OCInitStruct);使用不同的通道需要调用不同的函数通道1   TIM_OC1Init uint16_t TIM_OCMode;  功能:配置PWM的模式 范围:TIM_OCMode_PWM1  TIM_OCMode_PWM2  uint16_t TIM_OutputState; 功能:PWM通道使能 开关 范围:TIM_OutputState_Disable TIM_OutputState_Enable 寄存器:CCER 0位  uint16_t TIM_OCPolarity; 功能:有效电平 范围:TIM_OCPolarity_High TIM_OCPolarity_Low 寄存器:CCER 1位 */    TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_PWM1;    TIM_OCInitStruct.TIM_OCPolarity = TIM_OCPolarity_High;    TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Enable;    TIM_OC1Init(TIM13, &TIM_OCInitStruct);    /*void TIM_OC1PreloadConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCPreload);x 1~4只有调用该函数 那么才能修改CCR1的值void TIM_ARRPreloadConfig(TIM_TypeDef* TIMx, FunctionalState NewState);*/    TIM_OC1PreloadConfig(TIM13, TIM_OCPreload_Enable);    TIM_ARRPreloadConfig(TIM13, ENABLE);        TIM_Cmd(TIM13, ENABLE);}//tim14  控制器void timer14_init(){    TIM_TimeBaseInitTypeDef TIM_TimeBaseStruct;    GPIO_InitTypeDef GPIO_InitStruct;    TIM_OCInitTypeDef TIM_OCInitStruct;        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM14, ENABLE);        //PF9 配置成复用模式    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;    GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9;    GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_100MHz;        GPIO_Init(GPIOF, &GPIO_InitStruct);    //把PF9 复用为TIM14功能,就可以把PF9 作为TIM14的一个通道    GPIO_PinAFConfig(GPIOF,GPIO_PinSource9,GPIO_AF_TIM14 );    TIM_TimeBaseStruct.TIM_CounterMode = TIM_CounterMode_Up;    //16位 防止 溢出    TIM_TimeBaseStruct.TIM_Period = 300 -1;     TIM_TimeBaseStruct.TIM_Prescaler= 84-1;    TIM_TimeBaseInit(TIM14, &TIM_TimeBaseStruct);        //PWM的配置    //CCMR1  OC1M  4~6    //111 PWM2     CNT > CCR1  输出有效电平    //110 PWM1     CNT < CCR1  输出有效电平        //CCER 1位  有效电平/*void TIM_OCnInit(TIM_TypeDef* TIMx, TIM_OCInitTypeDef* TIM_OCInitStruct);使用不同的通道需要调用不同的函数通道1   TIM_OC1Init uint16_t TIM_OCMode;  功能:配置 PWM的模式 范围TIM_OCMode_PWM1  TIM_OCMode_PWM2  uint16_t TIM_OutputState; 功能:PWM通道使能 开关 范围:TIM_OutputState_Disable TIM_OutputState_Enable 寄存器:CCER 0位  uint16_t TIM_OCPolarity; 功能:有效电平 范围:TIM_OCPolarity_High TIM_OCPolarity_Low 寄存器:CCER 1位 */    TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_PWM1;    TIM_OCInitStruct.TIM_OCPolarity = TIM_OCPolarity_High;    TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Enable;    TIM_OC1Init(TIM14, &TIM_OCInitStruct);    /*void TIM_OC1PreloadConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCPreload);x 1~4只有调用该函数 那么才能修改CCR1的值void TIM_ARRPreloadConfig(TIM_TypeDef* TIMx, FunctionalState NewState);*/    TIM_OC1PreloadConfig(TIM14, TIM_OCPreload_Enable);    TIM_ARRPreloadConfig(TIM14, ENABLE);        TIM_Cmd(TIM14, ENABLE);}

 

    while(1)    {//        if(jfees >=  time + 5)//        {//            BEEP = !BEEP;//            time = jfees;//        }        if(dir==0) val++;        else if(dir==1) val--;                if(val==300) dir=1;        else if(val==0)dir=0;                //TIM_SetComparen        //设置 CCR1 比较寄存器的值        TIM_SetCompare1(TIM14, val);                delay_ms(10);    }

 

PWM