中级会员
- 积分
- 208
- 金钱
- 208
- 注册时间
- 2018-4-9
- 在线时间
- 111 小时
|
发表于 2018-7-11 09:39:49
|
显示全部楼层
请问楼主用定时器1为什么输出不了PWM呢
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); //使能引脚复用时钟
RCC_APB1PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); //使能定时器1时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //使能GPIO外设时钟
GPIO_PinRemapConfig(GPIO_PartialRemap_TIM1, ENABLE); //定时器1部分重映射
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; //TIM1_CH1
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //引脚速率
GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化 GPIO
//初始化 TIM1
TIM_TimeBaseStructure.TIM_Period = arr; //设置在自动重装载周期值
TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置预分频值
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM 向上计数模式
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); //初始化 TIMx
//初始化 TIM1 Channel2 PWM 模式
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //选择 PWM 模式 2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
// TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable; //互补输出失能,可不写
TIM_OCInitStructure.TIM_Pulse = 5000;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //输出极性低
TIM_OC1Init(TIM1, &TIM_OCInitStructure); //使能预装载定时器
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_Cmd(TIM1, ENABLE); //是能定时器
TIM_CtrlPWMOutputs(TIM1, ENABLE); //使能定时器输出PWM |
|