#include "stm32f10x.h"
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
uint16_t TimerPeriod = 0;
uint16_t Channel1Pulse = 0, Channel2Pulse = 0, Channel3Pulse = 0, Channel4Pulse = 0;
void RCC_Configuration(void);
void GPIO_Configuration(void);
int main(void)
{
RCC_Configuration();
GPIO_Configuration();
TimerPeriod = (SystemCoreClock / 17570 ) - 1;
Channel1Pulse = (uint16_t) (((uint32_t) 5 * (TimerPeriod - 1)) / 10);
Channel2Pulse = (uint16_t) (((uint32_t) 375 * (TimerPeriod - 1)) / 1000);
Channel3Pulse = (uint16_t) (((uint32_t) 25 * (TimerPeriod - 1)) / 100);
Channel4Pulse = (uint16_t) (((uint32_t) 125 * (TimerPeriod- 1)) / 1000);
// //定时器初始化 函数 见库函数 P246页
TIM_TimeBaseStructure.TIM_Prescaler = 0; //设置用来作为TIM 时钟频率除数的预分频值
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //计数器模式
TIM_TimeBaseStructure.TIM_Period = TimerPeriod;
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //时钟分割
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0; //设置 周期 计数值
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
//TIM1 配置 见 库函数 P294 页
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //脉冲宽度调制模式2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //使能输出比较状态
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; //使能 互补 输出状态
TIM_OCInitStructure.TIM_Pulse = Channel1Pulse; //脉冲 值
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //输出比较极性低
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low;//互补输出极性高
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set; //MOE=0 设置 TIM1输出比较空闲状态
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;//MOE=0 重置 TIM1输出比较空闲状态
TIM_OC1Init(TIM1, &TIM_OCInitStructure); //设定好的参数 初始化TIM
TIM_OCInitStructure.TIM_Pulse = Channel2Pulse; //脉宽值
TIM_OC2Init(TIM1, &TIM_OCInitStructure); //设定好的参数 初始化TIM
TIM_OCInitStructure.TIM_Pulse = Channel3Pulse; //脉宽值
TIM_OC3Init(TIM1, &TIM_OCInitStructure); //设定好的参数 初始化TIM
TIM_OCInitStructure.TIM_Pulse = Channel4Pulse; //脉宽值
TIM_OC4Init(TIM1, &TIM_OCInitStructure);//设定好的参数 初始化TIM
TIM_Cmd(TIM1, ENABLE); //使能 TIM1
TIM_CtrlPWMOutputs(TIM1, ENABLE); //使能 TIM1 输出
while (1)
{}
}
void RCC_Configuration(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1
RCC_APB2Periph_GPIOA
RCC_APB2Periph_GPIOE
RCC_APB2Periph_GPIOB
RCC_APB2Periph_AFIO, ENABLE);
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
#ifdef STM32F10X_CL
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9
GPIO_Pin_11
GPIO_Pin_13
GPIO_Pin_14
GPIO_Pin_8
GPIO_Pin_10
GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOE, &GPIO_InitStructure);
GPIO_PinRemapConfig(GPIO_FullRemap_TIM1, ENABLE);
#else
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8
GPIO_Pin_9
GPIO_Pin_10
GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13
GPIO_Pin_14
GPIO_Pin_15;
GPIO_Init(GPIOB, &GPIO_InitStructure);
#endif
}
#ifdef USE_FULL_ASSERT
void assert_failed(uint8_t* file, uint32_t line)
{
while (1)
{}
}
#endif
|