STM32 使用TIM2_CH1(PA15) 输出10K PWM信号

PA15-> TIM2 Remap CH1

1.apb_init()
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);

2.gpio_init()
    #define PWM_GPIO_IDX                        8                   //PA15
    #define PWM_GRP                             GPIOA
    #define PWM_PIN                             GPIO_Pin_15
    #define PWM_MODE                            GPIO_Mode_AF_PP

    GPIO_PinRemapConfig(GPIO_Remap_SWJ_Disable, ENABLE);    //disable JTAG
    GPIO_PinRemapConfig(GPIO_FullRemap_TIM2, ENABLE);       //TIM2 Remap CH1->PA15  

3.pwm_init()
    /*
    72M / 10K = 720
    period = 720-1 = 719
    10分频: prescaler = 10-1 = 9
    */
    #define LF_PERIOD                                   719                     //周期
    #define LF_PRESCALER                                9                       //分频
    #define LF_PULSE                                    360                     //占空比

    void lf_init(void){

        TIM_DeInit(TIM2);//将外设 TIMx 寄存器重设为缺省值
        TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
         /* Time base configuration */
        TIM_TimeBaseStructure.TIM_Period = LF_PERIOD;//设置了在下一个更新事件装入活动的自动重装载寄存器周期的值
        TIM_TimeBaseStructure.TIM_Prescaler = LF_PRESCALER;//设置了用来作为 TIM1 时钟频率除数的预分频值
        TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;// 设置了时钟分割
        TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//选择了计数器模式

        TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);//根据 TIM_TimeBaseInitStruct 中指定的参数初始化 TIMx 的时间基数单位

        TIM_OCInitTypeDef TIM_OCInitStructure;
        /* PWM1 Mode configuration: Channel1 */
        TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;//选择定时器模式
        TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;//选择输出比较状态
        TIM_OCInitStructure.TIM_Pulse = LF_PULSE;//设置了待装入捕获比较寄存器的脉冲值
        TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;//输出极性

        TIM_OC1Init(TIM2, &TIM_OCInitStructure);//根据 TIM1_OCInitStruct 中指定的参数初始化 TIM1 通道 1

        TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Enable);//使能或者失能 TIMx 在 CCR1 上的预装载寄存器

        TIM_ARRPreloadConfig(TIM2, ENABLE);//使能或者失能 TIMx 在 ARR 上的预装载寄存器

        TIM_Cmd(TIM2, ENABLE);//使能或者失能 TIMx 外设
    }

STM32 使用TIM2_CH1(PA15) 输出10K PWM信号_第1张图片

/* -----------------------------------------------------------------------
  //对PWM的频率占空比解析
  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 / 666 = 36 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%
    uint16_t CCR1_Val = 333;
    uint16_t CCR2_Val = 249;
    uint16_t CCR3_Val = 166;
    uint16_t CCR4_Val = 83;
----------------------------------------------------------------------- */

//以下为解决停止pwm信号后偶发出现高电平 2018-08-11
void PwmControl(BOOL bCon){
    if(bCon){
        TIM_ForcedOC1Config(TIM2, TIM_ForcedAction_Active);
        TIM_Cmd(TIM2, ENABLE);
    }else{
        TIM_Cmd(TIM2, DISABLE);
        TIM_ForcedOC1Config(TIM2, TIM_ForcedAction_InActive);   //设置为非活动状态,低电平
    }
}

你可能感兴趣的:(单片机,C语言,stm32)