STM32F103C8T6
tiM3 CH1可以输出到PA6和PB4
#include "stm32f10x.h"
void pwm_init(){
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef timInitStruct;
TIM_OCInitTypeDef TIM_OCInitStructure;
//设置IO口
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//设置定时器3
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
//重新将Timer设置为缺省值
TIM_DeInit(TIM3);
//采用内部时钟给TIM3提供时钟源
TIM_InternalClockConfig(TIM3);
timInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;
timInitStruct.TIM_Prescaler = 0;
timInitStruct.TIM_CounterMode = TIM_CounterMode_Up; //向上计数
timInitStruct.TIM_RepetitionCounter = 0;
timInitStruct.TIM_Period = 100-1; //这个值实际上就是TIMX->ARR,延时开始时重新设定即可
TIM_TimeBaseInit(TIM3, &timInitStruct);
//设置PWM输出
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 30;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM3, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);
//ARR预装载缓冲器使能
TIM_ARRPreloadConfig(TIM3, ENABLE);
//开启定时器
TIM_Cmd(TIM3, ENABLE);
}
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
GPIO_Init(GPIOA, &GPIO_InitStructure);
用上面这段,在PA6上有PWM输出
//=============================================================================
// PWM-> PB4
//=============================================================================
GPIO_PinRemapConfig(GPIO_Remap_SWJ_NoJTRST, ENABLE);
GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
GPIO_Init(GPIOB, &GPIO_InitStructure);
但这段在PB4上没有输出PWM
谢谢!
改成下面这样还是没有
/=============================================================================
// PWM-> PB4
//=============================================================================
// GPIO_PinRemapConfig(GPIO_Remap_SWJ_NoJTRST, ENABLE);
GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
一周热门 更多>