Purpose of use: use the TIM timer to make the small light turn on and off every 0.5 seconds
bsp_led.h
#ifndef __LED_H #define __LED_H #include "stm32f10x.h" /* Define the GPIO port that the LED is connected to, the user only needs to modify the following code to change the controlled LED pin*/ #define LED1_GPIO_PORT GPIOC /* GPIO port*/ #define LED1_GPIO_CLK RCC_APB2Periph_GPIOC /* GPIO port clock*/ #define LED1_GPIO_PIN GPIO_Pin_2 #define LED2_GPIO_PORT GPIOC /* GPIO port*/ #define LED2_GPIO_CLK RCC_APB2Periph_GPIOC /* GPIO port clock*/ #define LED2_GPIO_PIN GPIO_Pin_3 /** the macro definition to trigger the led on or off * 1 - off *0 - on */ #define ON 0 #define OFF 1 /* Use standard firmware library to control IO*/ #define LED1(a) if (a) \ GPIO_SetBits(LED1_GPIO_PORT,LED1_GPIO_PIN);\ else \ GPIO_ResetBits(LED1_GPIO_PORT,LED1_GPIO_PIN) #define LED2(a) if (a) \ GPIO_SetBits(LED2_GPIO_PORT,LED2_GPIO_PIN);\ else \ GPIO_ResetBits(LED2_GPIO_PORT,LED2_GPIO_PIN) /* Control IO by directly operating the register */ #define digitalHi(p,i) {p->BSRR=i;} //The output is high level #define digitalLo(p,i) {p->BRR=i ;} //output low level #define digitalToggle(p,i) {p->ODR ^=i;} //output inverted state /* Define macros that control IO */ #define LED1_TOGGLE digitalToggle(LED1_GPIO_PORT,LED1_GPIO_PIN) #define LED1_OFF digitalHi(LED1_GPIO_PORT,LED1_GPIO_PIN) #define LED1_ON digitalLo(LED1_GPIO_PORT,LED1_GPIO_PIN) #define LED2_TOGGLE digitalToggle(LED2_GPIO_PORT,LED2_GPIO_PIN) #define LED2_OFF digitalHi(LED2_GPIO_PORT,LED2_GPIO_PIN) #define LED2_ON digitalLo(LED2_GPIO_PORT,LED2_GPIO_PIN) void LED_GPIO_Config(void); #endif /* __LED_H */
bsp_led.c
/** ****************************************************************************** * @file bsp_led.c * @author fire * @version V1.0 * @date 2013-xx-xx * @brief led application function interface ****************************************************************************** **/ #include "bsp_led.h" /** * @brief initializes the IO that controls the LED * @param none * @retval None */ void LED_GPIO_Config(void) { /*Define a structure of type GPIO_InitTypeDef*/ GPIO_InitTypeDef GPIO_InitStructure; /*Enable LED related GPIO peripheral clock*/ RCC_APB2PeriphClockCmd( LED1_GPIO_CLK | LED2_GPIO_CLK , ENABLE); /*Select the GPIO pin to control*/ GPIO_InitStructure.GPIO_Pin = LED1_GPIO_PIN; /* Set the pin mode to general push-pull output */ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; /* Set pin rate to 50MHz */ GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; /* Call library function to initialize GPIO*/ GPIO_Init(LED1_GPIO_PORT, &GPIO_InitStructure); /*Select the GPIO pin to control*/ GPIO_InitStructure.GPIO_Pin = LED2_GPIO_PIN; /* Call library function to initialize GPIO*/ GPIO_Init(LED2_GPIO_PORT, &GPIO_InitStructure); /* turn off all leds */ GPIO_SetBits(LED1_GPIO_PORT, LED1_GPIO_PIN); /* turn off all leds */ GPIO_SetBits(LED2_GPIO_PORT, LED2_GPIO_PIN); } /*********************************************END OF FILE**********************/
Basic Timer Configuration
bsp_TiMbase.h
#ifndef __BSP_TIMEBASE_H #define __BSP_TIMEBASE_H #include "stm32f10x.h" /********************Basic timer TIM parameter definition, only TIM6, 7************/ #define BASIC_TIM6 / / If using TIM7, just comment out this macro #ifdef BASIC_TIM6 // use the basic timer TIM6 #define BASIC_TIM TIM6 #define BASIC_TIM_APBxClock_FUN RCC_APB1PeriphClockCmd #define BASIC_TIM_CLK RCC_APB1Periph_TIM6 #define BASIC_TIM_Period 1000-1 #define BASIC_TIM_Prescaler 71 #define BASIC_TIM_IRQ TIM6_IRQn #define BASIC_TIM_IRQHandler TIM6_IRQHandler # else // use basic timer TIM7 #define BASIC_TIM TIM7 #define BASIC_TIM_APBxClock_FUN RCC_APB1PeriphClockCmd #define BASIC_TIM_CLK RCC_APB1Periph_TIM7 #define BASIC_TIM_Period 1000-1 #define BASIC_TIM_Prescaler 71 #define BASIC_TIM_IRQ TIM7_IRQn #define BASIC_TIM_IRQHandler TIM7_IRQHandler #endif /****************************Function declaration******************** ************/ void BASIC_TIM_Init(void); #endif /* __BSP_TIMEBASE_H */
bsp_TiMbase.c
// Basic timer TIMx,x[6,7] timing initialization function #include "bsp_TiMbase.h" // Interrupt priority configuration static void BASIC_TIM_NVIC_Config( void ) { NVIC_InitTypeDef NVIC_InitStructure; // set interrupt group to 0 NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); // set interrupt source NVIC_InitStructure.NVIC_IRQChannel = BASIC_TIM_IRQ ; // set primary priority to 0 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; // Set the preemption priority to 3 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); } /* * Note: There are 5 members in the TIM_TimeBaseInitTypeDef structure, and there are only 5 members in the registers of TIM6 and TIM7 * TIM_Prescaler and TIM_Period, so when using TIM6 and TIM7, you only need to initialize these two members, * The other three members are common timers and advanced timers only. *----------------------------------------------------------------------------- *typedef struct *{ TIM_Prescaler has both * TIM_CounterMode TIMx, x[6,7] not, others have * TIM_Period has both * TIM_ClockDivision TIMx, x[6,7] no, others have * TIM_RepetitionCounter TIMx, x[1,8,15,16,17] only *}TIM_TimeBaseInitTypeDef; *----------------------------------------------------------------------------- */ static void BASIC_TIM_Mode_Config(void) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; // Turn on the timer clock, that is, the internal clock CK_INT=72M BASIC_TIM_APBxClock_FUN(BASIC_TIM_CLK, ENABLE); // Automatically reload the value of the register, and generate an update or interrupt after accumulating TIM_Period+1 frequency TIM_TimeBaseStructure.TIM_Period = BASIC_TIM_Period; // The clock prescaler is TIM_TimeBaseStructure.TIM_Prescaler= BASIC_TIM_Prescaler; // Clock division factor, no basic timer, don't care //TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1; // Counter counting mode, the basic timer can only count up, there is no setting for counting mode //TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; // The value of the repetition counter, the basic timer does not have it, don't care //TIM_TimeBaseStructure.TIM_RepetitionCounter=0; // initialize the timer TIM_TimeBaseInit(BASIC_TIM, &TIM_TimeBaseStructure); // clear counter interrupt flag TIM_ClearFlag(BASIC_TIM, TIM_FLAG_Update); // enable counter interrupt TIM_ITConfig(BASIC_TIM,TIM_IT_Update,ENABLE); // enable counter TIM_Cmd(BASIC_TIM, ENABLE); } void BASIC_TIM_Init(void) { BASIC_TIM_NVIC_Config(); BASIC_TIM_Mode_Config(); } /*********************************************END OF FILE**********************/
main
// Basic timer TIMx,x[6,7] timing application #include " stm32f10x.h " #include "bsp_led.h" #include "bsp_TiMbase.h" volatile uint32_t time = 0; // ms timing variable /** * @brief main function * @param none * @retval None */ int main(void) { /* led port configuration */ LED_GPIO_Config(); BASIC_TIM_Init(); while(1) { if ( time == 1000 ) /* 1000 * 1 ms = 1s time to */ { time = 0; /* Invert LED1*/ LED1_TOGGLE; } } } /*********************************************END OF FILE**********************/
interrupt function
void BASIC_TIM_IRQHandler (void) { if ( TIM_GetITStatus( BASIC_TIM, TIM_IT_Update) != RESET ) { time++; TIM_ClearITPendingBit(BASIC_TIM , TIM_FLAG_Update); } }