目的:利用通用定时器产生PWM波
配置文件:#include "stm32f10x_gpio.h"
#include "stm32f10x_rcc.h"
#include "stm32f10x_tim.h" 由于程序中没有用到中断,就不必加入msic.h了
寄存器:参阅stm32中文参考手册
结构体定义:
typedef struct
{
uint16_t TIM_Prescaler;
uint16_t TIM_CounterMode;
uint16_t TIM_Period;
uint16_t TIM_ClockDivision;
uint8_t TIM_RepetitionCounter;
} TIM_TimeBaseInitTypeDef
详细介绍参阅定时器1
typedef struct
{
uint16_t TIM_OCMode; //pwm模式
uint16_t TIM_OutputState; //比较输出状态
uint16_t TIM_OutputNState; //
uint16_t TIM_Pulse; //设置CCRx的值
uint16_t TIM_OCPolarity; //设置PWM的极性
uint16_t TIM_OCNPolarity;
uint16_t TIM_OCIdleState;
uint16_t TIM_OCNIdleState;
} TIM_OCInitTypeDef;
其中pwm模式有宏定义如下:
#define TIM_OCMode_Timing ((uint16_t)0x0000)
#define TIM_OCMode_Active ((uint16_t)0x0010)
#define TIM_OCMode_Inactive ((uint16_t)0x0020)
#define TIM_OCMode_Toggle ((uint16_t)0x0030)
#define TIM_OCMode_PWM1 ((uint16_t)0x0060)
#define TIM_OCMode_PWM2 ((uint16_t)0x0070)
比较输出状态有宏定义如下:
#define TIM_OutputState_Disable ((uint16_t)0x0000)
#define TIM_OutputState_Enable ((uint16_t)0x0001)
输出极性有宏定义如下:
#define TIM_OCPolarity_High ((uint16_t)0x0000)
#define TIM_OCPolarity_Low ((uint16_t)0x0002)
库函数:
TIM_OC1Init(TIM_TypeDef* TIMx, TIM_OCInitTypeDef* TIM_OCInitStruct) 初始化TIM_OCInitTypeDef;结构体函数,参数:定时器号,TIM_OCInitTypeDef;结构体定义变量
TIM_OC1PreloadConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCPreload)
TIM_OC2PreloadConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCPreload),TIM_OC3PreloadConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCPreload)
TIM_OC4PreloadConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCPreload)
使能对应的PWM输出通道,参数,定时器号,预装使能,其中预装使能有宏定义如下:
#define TIM_OCPreload_Enable ((uint16_t)0x0008)
#define TIM_OCPreload_Disable ((uint16_t)0x0000)
TIM_ARRPreloadConfig(TIM_TypeDef* TIMx, FunctionalState NewState)使能ARR预装,参数:定时器号,使能与否。
TIM_Cmd(TIM_TypeDef* TIMx, FunctionalState NewState) 定时器使能,见定时器1.
程序代码分析:(程序出自FIRE)
Main.c中:
int main(void)
{
TIM3_PWM_Init(); //直接初始化,进入大循环
while (1)
{
}
}
配置文件中:
*/
static void TIM3_GPIO_Config(void) //gpio配置,这里需要注意TM3对应的是那几个输出口,需要在引脚图上查阅,并选用的是复用推挽输出模式。这里PA6 PA7 PB0 PB1分别是对应的TM3的四个输出通道。
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //开定时器时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE); //开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;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
static void TIM3_Mode_Config(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
u16 CCR1_Val = 500; //预设CCRx的值
u16 CCR2_Val = 375;
u16 CCR3_Val = 250;
u16 CCR4_Val = 125;
/* -----------------------------------------------------------------------
TIM3 Channel1 duty cycle = (TIM3_CCR1/ TIM3_ARR+1)* 100% = 50%
TIM3 Channel2 duty cycle = (TIM3_CCR2/ TIM3_ARR+1)* 100% = 37.5%
TIM3 Channel3 duty cycle = (TIM3_CCR3/ TIM3_ARR+1)* 100% = 25%
TIM3 Channel4 duty cycle = (TIM3_CCR4/ TIM3_ARR+1)* 100% = 12.5%
----------------------------------------------------------------------- */
//先配置好时基
TIM_TimeBaseStructure.TIM_Period = 999; //设定周期指999
TIM_TimeBaseStructure.TIM_Prescaler = 0; //预分频设为0,72MHz直接给到定时器 TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1 ; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数模式 TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
/* 配置PWM输出通道1 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //选定pwm模式1 TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //使能输出
TIM_OCInitStructure.TIM_Pulse = CCR1_Val; //装在CCRx的值 TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //选定极性
TIM_OC1Init(TIM3, &TIM_OCInitStructure); //初始化
TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);//使能通道1
接下来的四个通道配置完全一样
/* PWM1 Mode configuration: Channel2 */
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR2_Val;
TIM_OC2Init(TIM3, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);
/* PWM1 Mode configuration: Channel3 */
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR3_Val;
TIM_OC3Init(TIM3, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable);
/* PWM1 Mode configuration: Channel4 */
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR4_Val;
TIM_OC4Init(TIM3, &TIM_OCInitStructure);
TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM3, ENABLE); // 使能装在ARR的值
TIM_Cmd(TIM3, ENABLE);
void TIM3_PWM_Init(void) //将两个初始化函数封装入一个函数当中
{
TIM3_GPIO_Config();
TIM3_Mode_Config();
}
Pwm产生的基本原理是,定时器从0开始按照时基计数,一直计数到ARR预装的值而后清零重新计数。此过程中如果计数器的值达到了CCRx的值,那么IO口输出就会取反。这样就完成了pwm的输出。其中周期计算公式类似于时基的计算,占空比有如下公式:
占空比 = TIMx_CCR/(TIMx_ARR +1)
由此看出,调节CCRx的值即可调节占空比。
PWM在控制直流电机转速和调节舵机中有着重要应用。
另外,pwm两种模式有一些不同,pwm1向上计数时,没有达到CCRx预设值时,输出为有效电平,否则输出无效电平。向下计数时相反。PWM2模式与之整个相反。其中电平有效无效不是指高低,而是需要在极性之中设定。