STM32F103 TIM1 PWM
使用不同版本的标准库导致了不同的现象,内部晶振最大64M所以分频是64'000'000/64 = 1'000'000;周期定为1000;则可以生成1kz的pwm,占空比0‰到1000‰
void TIM1_PWM(void) { GPIO_InitTypeDef GPIO_InitStructure; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure; /*开时钟PWM的 与GPIO的*/ RCC_APB2PeriphClockCmd (RCC_APB2Periph_TIM1,ENABLE); RCC_APB2PeriphClockCmd (RCC_APB2Periph_GPIOA, ENABLE); /* PA8设置为功能脚(PWM) */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init (GPIOA, &GPIO_InitStructure); TIM_DeInit (TIM1);//重设为缺省值 /*TIM1时钟配置*/ TIM_TimeBaseStructure.TIM_Prescaler = 64;//预分频(时钟分频)64M/4000=18K TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//向上计数 TIM_TimeBaseStructure.TIM_Period = 1000-1;//装载值 TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;//设置了时钟分割 TIM_TimeBaseStructure.TIM_RepetitionCounter = 0x0;//周期计数器值 TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);//初始化TIMx的时间基数单位 /* Channel 1 Configuration in PWM mode 通道一的PWM*/ TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //PWM模式2 TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;//正向通道PA8 TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable;//反向通道PB13 TIM_OCInitStructure.TIM_Pulse = 500; //占空时间 1000 中有500的时间为高,互补的输出正好相反 TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性 TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low; //互补端的极性 TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset;//空闲状态下的非工作状态 TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset; TIM_OC1Init(TIM1, &TIM_OCInitStructure); /* TIM1 counter enable开定时器 */ TIM_Cmd (TIM1, ENABLE); /* TIM1 Main Output Enable 使能TIM1外设的主输出*/ TIM_CtrlPWMOutputs (TIM1, ENABLE); }
使用uCOS的时候由于是修改官方uCOS例程文件,其使用的是2008年的 V2.0的库
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset; TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
这两句代码貌似必须要加上
而使用2011年的V3.5.0的库就不用加
TIM_SetCompare1(TIM1, 0);//使用这个修改占空比
做个记录
附带TIM3 PWM设置 TIM4和TIM3类似
/***************************************************************Author:Liming*** * @brief TIM3_GPIO_Config * @param None * @retval None *****************************************************************0X49E7FC7B****/ static void TIM3_GPIO_Config(void) { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE); //使能GPIO外设和AFIO复用功能模块时钟使能 //TIM3_CH1-TIM3_CH2的PWM的GPIO GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 ; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //50MHZ GPIO_Init(GPIOA, &GPIO_InitStructure); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE); //使能GPIO外设和AFIO复用功能模块时钟使能 //TIM3_CH3-TIM3_CH4的PWM的GPIO GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 ; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //50MHZ GPIO_Init(GPIOB, &GPIO_InitStructure); } /***************************************************************Author:Liming*** * @brief TIM3_Mode_Config * @param None * @retval None *****************************************************************0X49E7FC7B****/ static void TIM3_Mode_Config(void) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure; /* PWM信号电平跳变值 */ u16 CCR1_Val=0; u16 CCR2_Val=0; u16 CCR3_Val=0; u16 CCR4_Val=0; /* ----------------------------------------------------------------------- TIM3 Configuration: generate 4 PWM signals with 4 different duty cycles: TIM3CLK = 64 MHz, Prescaler = 0x0, TIM3 counter clock = 64 MHz TIM3 ARR Register = 999 => TIM3 Frequency = TIM3 counter clock/(ARR + 1) TIM3 Frequency = 64 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% ----------------------------------------------------------------------- */ /* Time base configuration */ TIM_TimeBaseStructure.TIM_Period = TIM_PERIOD; //当定时器从0计数到999,即为1000次,为一个定时周期 TIM_TimeBaseStructure.TIM_Prescaler = 5; //设置预分频:不预分频,即为64MHz TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1 ; //设置时钟分频系数:不分频 TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数模式 TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); /* PWM1 Mode configuration: Channel1 */ TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //配置为PWM模式1 TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = CCR1_Val; //设置跳变值,当计数器计数到这个值时,电平发生跳变 TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //当定时器计数值小于CCR1_Val时为高电平 TIM_OC1Init(TIM3, &TIM_OCInitStructure); //使能通道1 TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable); /* PWM1 Mode configuration: Channel2 */ TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = CCR2_Val; //设置通道2的电平跳变值,输出另外一个占空比的PWM TIM_OC2Init(TIM3, &TIM_OCInitStructure); //使能通道2 TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable); /* PWM1 Mode configuration: Channel3 */ TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = CCR3_Val; //设置通道3的电平跳变值,输出另外一个占空比的PWM TIM_OC3Init(TIM3, &TIM_OCInitStructure); //使能通道3 TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable); /* PWM1 Mode configuration: Channel4 */ TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = CCR4_Val; //设置通道4的电平跳变值,输出另外一个占空比的PWM TIM_OC4Init(TIM3, &TIM_OCInitStructure); //使能通道4 TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable); TIM_ARRPreloadConfig(TIM3, ENABLE); // 使能TIM3重载寄存器ARR /* TIM3 enable counter */ TIM_Cmd(TIM3, ENABLE); //使能定时器3 }
修改占空比方式跟TIM1一样