TIM1配置:
void TIM1_TimeBaseInit(uint16_t TIM1_Prescaler,
TIM1_CounterMode_TypeDef TIM1_CounterMode,
uint16_t TIM1_Period,
uint8_t TIM1_RepetitionCounter)输入参数
输入参数 1 uint16_t TIM1_Prescaler: 预分频值(0x0000)
输入参数 2 TIM1_CounterMode_TypeDef TIM1_CounterMode: 计数模式
输入参数 3 uint16_t TIM1_Period: 自动重装载值(0x0000)
输入参数 4 uint8_t TIM1_RepetitionCounter: 重复计数器值(0x00)
系统配置为16MHZ
下面配置的是 7+1分频,
向上计数模
自动重装载值:125
重复计数器值(0x00)
计算公式:16000000hz/8分频=2000000HZ
2000000/125 = 16000HZ
输出PWM的频率是16KHZ
TIM1_DeInit();
TIM1_TimeBaseInit(7, TIM1_COUNTERMODE_UP, 125, 0x00);// 8分频 16K
TIM1_OC1Init( TIM1_OCMODE_PWM1,
TIM1_OUTPUTSTATE_ENABLE,
TIM1_OUTPUTNSTATE_DISABLE,
0x0000,
TIM1_OCPOLARITY_HIGH,
TIM1_OCNPOLARITY_LOW,
TIM1_OCIDLESTATE_SET,
TIM1_OCNIDLESTATE_SET);
TIM1_CCxCmd(TIM1_CHANNEL_1, ENABLE);
TIM1_OC1PreloadConfig(ENABLE);
TIM1_OC2Init( TIM1_OCMODE_PWM1,
TIM1_OUTPUTSTATE_ENABLE,
TIM1_OUTPUTNSTATE_DISABLE,
0x0000,
TIM1_OCPOLARITY_HIGH,
TIM1_OCNPOLARITY_LOW,
TIM1_OCIDLESTATE_SET,
TIM1_OCNIDLESTATE_SET);
TIM1_CCxCmd(TIM1_CHANNEL_2, ENABLE);
TIM1_OC2PreloadConfig(ENABLE);
TIM1_OC3Init( TIM1_OCMODE_PWM1,
TIM1_OUTPUTSTATE_ENABLE,
TIM1_OUTPUTNSTATE_DISABLE,
0x0000,
TIM1_OCPOLARITY_HIGH,
TIM1_OCNPOLARITY_LOW,
TIM1_OCIDLESTATE_SET,
TIM1_OCNIDLESTATE_SET);
TIM1_CCxCmd(TIM1_CHANNEL_3, ENABLE);
TIM1_OC3PreloadConfig(ENABLE);
TIM1_OC4Init( TIM1_OCMODE_PWM1,
TIM1_OUTPUTSTATE_ENABLE,
0x0000,
TIM1_OCPOLARITY_HIGH,
TIM1_OCIDLESTATE_SET);
TIM1_CCxCmd(TIM1_CHANNEL_4, ENABLE);
TIM1_OC4PreloadConfig(ENABLE);
TIM1_CtrlPWMOutputs(ENABLE); //PWM输出使能
TIM1_Cmd(ENABLE); //开启定时器
TIM3配置:
void TIM3_TimeBaseInit( TIM3_Prescaler_TypeDef TIM3_Prescaler,
uint16_t TIM3_Period)
输入参数 1 TIM3_Prescaler_TypeDef TIM3_Prescaler: 预分频器(TIM3_PSCR)
输入参数 2 uint16_t TIM3_Period:16 位自动装载值(TIM3_ARRH,TIM3_ARRL)
TIM3_DeInit();
TIM3_TimeBaseInit(TIM3_PRESCALER_8, 125);// 8分频 16K
TIM3_OC1Init( TIM3_OCMODE_PWM1,
TIM3_OUTPUTSTATE_ENABLE,
0x0000,
TIM3_OCPOLARITY_HIGH);
TIM3_CCxCmd(TIM3_CHANNEL_1, ENABLE);
TIM3_OC1PreloadConfig(ENABLE);
TIM3_OC2Init( TIM3_OCMODE_PWM1,
TIM3_OUTPUTSTATE_ENABLE,
0x0000,
TIM3_OCPOLARITY_HIGH);
TIM3_CCxCmd(TIM3_CHANNEL_2, ENABLE);
TIM3_OC2PreloadConfig(ENABLE);
// TIM3_CtrlPWMOutputs(ENABLE); //PWM输出使能
TIM3_Cmd(ENABLE); //开启定时器