开发板:WarShip STM32 BOARD V2.1
CPU:STM32F103ZET6
由上可知,使用STM32的ETR引脚计脉冲数,需将定时器的时钟源选择为外部时钟模式2
选定此模式的方法:令TIMx_SMCR寄存器中的ECE=1
计数器能够在外部触发ETR的每一个上升沿或下降沿计数
例如,要配置在ETR引脚上,每2个上升沿计数一次的向上计数器,使用下列步骤:
1. 本例中不需要滤波器,置TIMx_SMCR寄存器中的ETF[3:0]=0000
2. 设置预分频器,置TIMx_SMCR寄存器中的ETPS[1:0]=01
3. 设置在ETR的上升沿检测,置TIMx_SMCR寄存器中的ETP=0
4. 开启外部时钟模式2,置TIMx_SMCR寄存器中的ECE=1
5. 启动计数器,置TIMx_CR1寄存器中的CEN=1
计数器在每2个ETR上升沿计数一次。
在ETR的上升沿和计数器实际时钟之间的延时取决于在ETRP信号端的重新同步电路
TIMx_SMCR寄存器的位定义,参见《STM32中文参考手册_V10.pdf》14.3.3章节
说明:
1. 将PB5与PD2短接
2. 使用TIM2定时器在引脚PB5上输出方波,周期为500ms(高500ms低500ms)
3. 使用TIM3_ETR计脉冲功能对上面的方波上升沿计数
#include "etr.h"
#include "led.h"
void bsp_timer2_init( u16 arr,u16 psc ){
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
TIM_TimeBaseStructure.TIM_Period = arr;
TIM_TimeBaseStructure.TIM_Prescaler =psc;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE );
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_SetCounter( TIM2,0 );
TIM_Cmd(TIM2, ENABLE);
}
void TIM2_IRQHandler(void){
if (TIM_GetITStatus(TIM2, TIM_IT_Update)!= RESET){
LED0=!LED0;//PB5
}
TIM_ClearITPendingBit(TIM2,TIM_IT_Update);
}
void bsp_timer3_init(u16 arr,u16 psc){
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOD, &GPIO_InitStructure);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
TIM_TimeBaseStructure.TIM_Period = arr;
TIM_TimeBaseStructure.TIM_Prescaler = psc;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
#if 0
TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE );
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
#endif
TIM_ETRClockMode2Config(TIM3,TIM_ExtTRGPSC_OFF,TIM_ExtTRGPolarity_NonInverted,0);
TIM_SetCounter( TIM3,0 );
TIM_Cmd(TIM3, ENABLE);
}
/*
** TIM3中断可以不使用,对ETR计数无影响
** 使用可以通过借助两个LED闪烁情况判断
** ETR是否调试正确,或者直接打印定时器
** 计数寄存器的值也可以,自行选择
*/
void TIM3_IRQHandler(void){
if (TIM_GetITStatus(TIM3, TIM_IT_Update)!=RESET){
LED1=!LED1;
}
TIM_ClearITPendingBit(TIM3,TIM_IT_Update);
}
#include "led.h"
#include "delay.h"
#include "key.h"
#include "sys.h"
#include "lcd.h"
#include "usart.h"
#include "etr.h"
int main(void){
delay_init();
NVIC_Configuration();
uart_init(9600);
LED_Init();
bsp_timer2_init(4999,7199);//500ms
bsp_timer3_init(5,0);
while(1) {
printf("Count=%d\n",TIM_GetCounter(TIM3));
delay_ms(10);
}
}
如果通过LED查看则LED0一秒亮一次,LED1则5秒亮一次
如果通过打印查看则数值从0至5向上循环
TIM2与TIM3初始化时的自动重装载值与预分频读者可根据自己的需要自行设置