STM32-Timer simple application

// TIM_TimeBaseInitTypeDef defined in the document "stm32f10x_tim.h":
/ typedef struct
{ U16 TIM_Period; U16 TIM_Prescaler; U8 TIM_ClockDivision; U16 TIM_CounterMode; U8 TIM_RepetitionCounter; } TIM_TimeBaseInitTypeDef;





/
// TIM_Period: timer period, when the value of the counter register is incremented to When equal to this value, set the relevant event flag. The range is 0~65535.
//TIM_Prescaler: Timer prescaler setting. The clock source is the timer clock through the prescaler. The range is 0~65535.
//TIM_ClockDivision: Clock division. The basic timer does not have this function and does not need to be set.
//TIM_CounterMode: Timer counting mode setting. The basic timer can only count up and does not need to be set.
//TIM_RepetitionCounter: Repetition counter, basic timer does not have this function, no need to set.
/ typedef struct
{ __IO uint32_t CRL; __IO uint32_t CRH; __IO uint32_t IDR; __IO uint32_t ODR;




__IO uint32_t BSRR;
__IO uint32_t BRR;
__IO uint32_t LCKR;
} GPIO_TypeDef;
/
/************************** TIMER_BASE.H /
#ifndef __TIMER_BASE_H
#define __TIMER_BASE_H
#include “stm32f10x.h”
void TIME_NVIC_Configuration(u8 timeNo);
void TIME_Configuration(TIM_TypeDef * TIMx,u16 arr, u16 psc);
void GPIO_Config(void);
#endif
/
TIMER_BASE.c******* *******************/
#include “TIME_base.h”
void TIME_NVIC_Configuration(u8 timeNo)//If the system generates multiple interrupts, then there is priority for interrupt response Level
{ NVIC_InitTypeDef NVIC_InitStructure; switch (timeNo) {



case 2: NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn; break; //Specify IRQ channel
case 3: NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; break; //Specify IRQ channel
case 4: NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;
case 5 : NVIC_InitStructure.NVIC_IRQChannel = TIM5_IRQn; break; // specified IRQ channel
}
NVIC_PriorityGroupConfig (NVIC_PriorityGroup_2); // set the priority packets, are higher priority
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; // specified preemptive priority
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3 ; //Response priority
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //Whether the defined IRQ is enabled or disabled
NVIC_Init(&NVIC_InitStructure);
}

void TIME_Configuration(TIM_TypeDef * TIMx,u16 arr , u16 psc)//配置TIMX
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
if (TIMx == TIM2)
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
else if (TIMx == TIM3)
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
else if (TIMx == TIM4)
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE);
else if (TIMx == TIM5)
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5,ENABLE);
else return 0;
//TIM_DeInit(TIMx)
//TIM_InternalClockConfig(TIMx)

TIM_TimeBaseStructure.TIM_Period = arr - 1; //设置了在下一个更新事件装入活动的自动重装载寄存器周期的值 范围是0x0000-0xFFFF
TIM_TimeBaseStructure.TIM_Prescaler = psc - 1; //设置了用来作为 TIMx 时钟频率除数的预分频值
TIM_TimeBaseInit(TIMx, &TIM_TimeBaseStructure); 
TIM_ITConfig(TIMx,TIM_IT_Update|TIM_IT_Trigger,ENABLE);//使能或者失能 TIM 的中断,详见附录图1
TIM_Cmd(TIMx, ENABLE); //使能TIMX外设 

}

void GPIO_Config (void) // Configure an IO port:
{
/ definition of a type of structure GPIO_InitTypeDef /
GPIO_InitTypeDef GPIO_InitStructure;
/ Open GPIOB peripheral clock /
RCC_APB2PeriphClockCmd (RCC_APB2Periph_GPIOB, the ENABLE);
/ Select control pin GPIOB /
GPIO_InitStructure .GPIO_Pin = GPIO_Pin_0;
/ Set the pin mode to general push-pull output /
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
/ Set the pin speed to 50MHz /
GPIO_InitStructure.GPIO_Speed ​​= GPIO_Speed_50MHz;
/ Call the library function, initialize GPIOB /
GPIO_Init(GPIOB, &GPIO)_InitStructure ;
/ PB.0 output high /
GPIO_SetBits(GPIOB,GPIO_Pin_0);
}
Add timer-triggered interrupt handling function in stm32f10x_it.c
void TIM6_IRQHandler(void)
{ Static vu8 flag = 0; if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET) //Get interrupt status { TIM_ClearITPendingBit(TIM2, TIM_IT_Update ); //Clear the interrupt flag Count ++; If (Count> =50) { If (GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_15) == Reset) { Flag ++; } If(flag %2) { If (GPIO_ReadInputDataBit(GPIOAGPIO_Pin_12) == Reset) GPIO_SetBits(GPIOA,GPIO_Pin_12); Else GPIO_ResetBits(GPIOA,GPIO_Pin_12); } } } /




















************************ main.c ************************ *** /

#include “TIMER_BASE.h”
#include “stm32f10x_it.h”
int main()
{
GPIO_Config();
TIME_Configuration(TIM2,5000,72);
TIME_NVIC_Configuration(2);
while(1)
{
}
}

Guess you like

Origin blog.csdn.net/News53231323/article/details/113506465