首要咱们有必要必定ST公司的实力,也供认STM32的确是一款十分不错的Cortex-M3核单片机,可是,他的手册实在是让人觉得无法了解,尤其是其间的TIM模块,没有条理可言,看了两天简直仍是不知所云,让人很是抑郁。一起配套的固件库的阐明也很难和手册上的寄存器对应起来,研讨起来十分费力!功用强大却是真的,但至少也应该配套一个让人看的理解的阐明吧~~
两天时刻研讨了STM32定时器的最最根底的部分,把定时器最根底的两个功用完成了,余下的功用有待持续学习。
首要有一点需求留意:FWLib固件库现在的最新版应该是V2.0.x,V1.0.x版别固件库中,TIM1模块被独立出来,调用的函数与其他定时器不同;在V2.0系列版别中,取消了TIM1.h,一切的TIM模块一致调用TIM.h即可。网络上撒播的各种代码有许多是根据v1版别的固件库,在移植到v2版别固件库时,需求做些修正。本文的一切程序都是根据V2.0固件库。
以下是定时器向上溢出示例代码:
C言语: TIM1模块发生向上溢出事情
//Step1.时钟设置:发动TIM1
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
//Step2.中止NVIC设置:答应中止,设置优先级
NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_IRQChannel; //更新事情
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //抢占优先级0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //呼应优先级1
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //答应中止
NVIC_Init(&NVIC_InitStructure); //写入设置
//Step3.TIM1模块设置
void TIM_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_BaseInitStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
//TIM1 运用内部时钟
//TIM_InternalClockConfig(TIM1);
//TIM1根本设置
//设置预分频器分频系数71,即APB2=72M, TIM1_CLK=72/72=1MHz
//TIM_Period(TIM1_ARR)=1000,计数器向上计数到1000后发生更新事情,计数值归零
//向上计数形式
//TIM_RepetitionCounter(TIM1_RCR)=0,每次向上溢出都发生更新事情
TIM_BaseInitStructure.TIM_Period = 1000;
TIM_BaseInitStructure.TIM_Prescaler = 71;
TIM_BaseInitStructure.TIM_ClockDivision = 0;
TIM_BaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_BaseInitStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM1, &TIM_BaseInitStructure);
//清中止,避免一启用中止后当即发生中止
TIM_ClearFlag(TIM1, TIM_FLAG_Update);
//使能TIM1中止源
TIM_ITConfig(TIM1, TIM_IT_Update, ENABLE);
//TIM1总开关:敞开
TIM_Cmd(TIM1, ENABLE);
}
//Step4.中止服务子程序:
void TIM1_UP_IRQHandler(void)
{
GPIOC-》ODR ^= (1《《4); //闪灯
TIM_ClearITPendingBit(TIM1, TIM_FLAG_Update); //清中止
}
下面是输出比较功用完成TIM1_CH1管脚输出指定频率的脉冲:
C言语: TIM1模块完成输出比较,主动翻转并触发中止
//Step1.发动TIM1,一起还要留意给相应功用管脚发动时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
//Step2. PA.8口设置为TIM1的OC1输出口
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);
//Step3.使能TIM1的输出比较匹配中止
NVIC_InitStructure.NVIC_IRQChannel = TIM1_CC_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
//Step4. TIM模块设置
void TIM_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_BaseInitStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
//TIM1根本计数器设置
TIM_BaseInitStructure.TIM_Period = 0xffff; //这儿有必要是65535
TIM_BaseInitStructure.TIM_Prescaler = 71; //预分频71,即72分频,得1M
TIM_BaseInitStructure.TIM_ClockDivision = 0;
TIM_BaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_BaseInitStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM1, &TIM_BaseInitStructure);
//TIM1_OC1模块设置
TIM_OCStructInit(& TIM_OCInitStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle; //管脚输出形式:翻转
TIM_OCInitStructure.TIM_Pulse = 2000; //翻转周期:2000个脉冲
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //使能TIM1_CH1通道
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出为正逻辑
TIM_OC1Init(TIM1, &TIM_OCInitStructure); //写入装备
//清中止
TIM_ClearFlag(TIM1, TIM_FLAG_CC1);
//TIM1中止源设置,敞开相应通道的捕捉比较中止
TIM_ITConfig(TIM1, TIM_IT_CC1, ENABLE);
//TIM1敞开
TIM_Cmd(TIM1, ENABLE);
//通道输出使能
TIM_CtrlPWMOutputs(TIM1, ENABLE);
}
Step5.中止服务子程序
void TIM1_CC_IRQHandler(void)
{
u16 capture;
if(TIM_GetITStatus(TIM1, TIM_IT_CC1) == SET)
{
TIM_ClearITPendingBit(TIM1, TIM_IT_CC1 );
capture = TIM_GetCapture1(TIM1);
TIM_SetCompare1(TIM1, capture + 2000);
//这儿解说下:
//将TIM1_CCR1的值添加2000,使得下一个TIM事情也需求2000个脉冲,
//另一种方法是清零脉冲计数器
//TIM_SetCounter(TIM2,0x0000);
}
}
关于TIM的操作,要留意的是STM32处理器由于低功耗的需求,各模块需求别离独立敞开时钟,所以,必定不要忘掉给用到的模块和管脚使能时钟,由于这个原因,浪费了我很多时刻阿~~!
九九的STM32笔记(二)TIM模块发生PWM
这个是STM32的PWM输出形式,STM32的TIM1模块是增强型的定时器模块,天然生成便是为电机操控而生,能够发生3组6路PWM,一起每组2路PWM为互补,并能够带有死区,能够用来驱动H桥。
下面的代码,是使用TIM1模块的1、2通道发生总共4路PWM的代码比如,相似代码也能够参阅ST的固件库中相应example
C言语: TIM1模块发生PWM,带死区
//Step1.敞开TIM和相应端口时钟
//发动GPIO
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | \
RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD,\
ENABLE);
//发动AFIO
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
//发动TIM1
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
//Step2. GPIO做相应设置,为AF输出
//PA.8/9口设置为TIM1的OC1输出口
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//PB.13/14口设置为TIM1_CH1N和TIM1_CH2N输出口
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
//Step3. TIM模块初始化
void TIM_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_BaseInitStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_BDTRInitTypeDef TIM_BDTRInitStructure;
//TIM1根本计数器设置(设置PWM频率)
//频率=TIM1_CLK/(ARR+1)
TIM_BaseInitStructure.TIM_Period = 1000-1;
TIM_BaseInitStructure.TIM_Prescal