Stm32 timer interrupt makes LED light blink

Publisher:温暖阳光Latest update time:2018-06-18 Source: eefocusKeywords:Stm32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Knowledge points:


Stm32 has a total of 11 timers:


1. Two advanced timers: TIM1, TIM8-------------------------APB2


2. Four general timers: TIM2~TIM5-------------------------APB1


3. Two basic timers: TIM6, TIM7-------------------------APB1


4. Two watchdogs


5. A SysTick timer




Main program main.c:


/* 

*illustrate: 

*PA0:KEY1;PA1:KEY2; 

*PA2:LED1;PA3:LED2; 

*PA9:USART1_TX;PA10:USART1_RX 

*/  

#include "stm32f10x.h"  

#include "stm32f10x_rcc.h"  

#include "stm32f10x_gpio.h"  

#include "stm32f10x_tim.h"  

#include "stm32f10x_exti.h"  

#include "system_stm32f10x.h"  

#include "misc.h"  

  

void RCC_Configuration(void);  

void GPIO_Configuration(void);   

void TIM3_Configuration(void);  

void NVIC_Configuration(void);  

  

int main()  

{  

    SystemInit();  

      

    RCC_Configuration();  

    GPIO_Configuration();   

    TIM3_Configuration();  

    NVIC_Configuration();  

                                                              

    while(1);  

}  

  

void RCC_Configuration(void)    

{     

  ////USART2 and USART3 are both on APB1 and USART1 is on APB2  

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);     

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);     

}   

  

void GPIO_Configuration(void)    

{    

  GPIO_InitTypeDef GPIO_InitStructure;    

    

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3;    

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;  

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;  

  GPIO_Init(GPIOA, &GPIO_InitStructure);   

}  

  

void TIM3_Configuration(void)    

{  

  TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;  

    

  TIM_ClearITPendingBit(TIM3, TIM_IT_Update);  

    

  TIM_TimeBaseStructure.TIM_Period = 2000; //Frequency: 72MHz 72000000/36000=2000  

  TIM_TimeBaseStructure.TIM_Prescaler = 35999;//36000-1=35999  

  TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;  

  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  

  TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);  

    

  TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE );  

  TIM_Cmd(TIM3,ENABLE);  

}  

  

void NVIC_Configuration(void)//Configure interrupt priority    

{    

  NVIC_InitTypeDef NVIC_InitStructure;    

      

  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);    

      

  NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;    

  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;    

  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;    

  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;    

  NVIC_Init(&NVIC_InitStructure);    

}    

Interrupt function stm32f10x_it.c


#include "..\include\stm32f10x.h"   

#include "..\include\stm32f10x_it.h"  

#include "..\include\stm32f10x_gpio.h"  

#include "..\include\stm32f10x_tim.h"  

  

void TIM3_IRQHandler(void)  

{  

  TIM_ClearITPendingBit(TIM3, TIM_IT_Update);  

    

  if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_2)==Bit_RESET)  

  {  

    GPIO_SetBits(GPIOA,GPIO_Pin_2);  

  }else{  

    GPIO_ResetBits(GPIOA,GPIO_Pin_2);  

  }  

}  

Interrupt function declaration header file stm32f10x_it.h:


#ifndef __STM32F10x_IT_H  

#define __STM32F10x_IT_H  

  

/* Includes ------------------------------------------------------------------*/  

/* Exported types ------------------------------------------------------------*/  

/* Exported constants --------------------------------------------------------*/  

/* Exported macro ------------------------------------------------------------*/  

/* Exported functions ------------------------------------------------------- */    

void NMIException(void);  

void HardFaultException(void);  

void MemManageException(void);  

void BusFaultException(void);  

void UsageFaultException(void);  

void DebugMonitor(void);  

void SVCHandler(void);  

void PendSVC(void);  

void SysTickHandler(void);  

void WWDG_IRQHandler(void);  

void PVD_IRQHandler(void);  

void TAMPER_IRQHandler(void);  

void RTC_IRQHandler(void);  

void FLASH_IRQHandler(void);  

void RCC_IRQHandler(void);  

void EXTI0_IRQHandler(void);  

void EXTI1_IRQHandler(void);  

void EXTI2_IRQHandler(void);  

void EXTI3_IRQHandler(void);  

void EXTI4_IRQHandler(void);  

void DMA1_Channel1_IRQHandler(void);  

void DMA1_Channel2_IRQHandler(void);  

void DMA1_Channel3_IRQHandler(void);  

void DMA1_Channel4_IRQHandler(void);  

void DMA1_Channel5_IRQHandler(void);  

void DMA1_Channel6_IRQHandler(void);  

void DMA1_Channel7_IRQHandler(void);  

void ADC1_2_IRQHandler(void);  

void USB_HP_CAN_TX_IRQHandler(void);  

void USB_LP_CAN_RX0_IRQHandler(void);  

void CAN_RX1_IRQHandler(void);  

void CAN_SCE_IRQHandler(void);  

void EXTI9_5_IRQHandler(void);  

void TIM1_BRK_IRQHandler(void);  

void TIM1_UP_IRQHandler(void);  

void TIM1_TRG_COM_IRQHandler(void);  

void TIM1_CC_IRQHandler(void);  

void TIM2_IRQHandler(void);  

void TIM3_IRQHandler(void);  

void TIM4_IRQHandler(void);  

void I2C1_EV_IRQHandler(void);  

void I2C1_ER_IRQHandler(void);  

void I2C2_EV_IRQHandler(void);  

void I2C2_ER_IRQHandler(void);  

void SPI1_IRQHandler(void);  

void SPI2_IRQHandler(void);  

void USART1_IRQHandler(void);  

void USART2_IRQHandler(void);  

void USART3_IRQHandler(void);  

void EXTI15_10_IRQHandler(void);  

void RTCAlarm_IRQHandler(void);  

void USBWakeUp_IRQHandler(void);  

void TIM8_BRK_IRQHandler(void);  

void TIM8_UP_IRQHandler(void);  

void TIM8_TRG_COM_IRQHandler(void);  

void TIM8_CC_IRQHandler(void);  

void ADC3_IRQHandler(void);  

void FSMC_IRQHandler(void);  

void SDIO_IRQHandler(void);  

void TIM5_IRQHandler(void);  

void SPI3_IRQHandler(void);  

void UART4_IRQHandler(void);  

void UART5_IRQHandler(void);  

void TIM6_IRQHandler(void);  

void TIM7_IRQHandler(void);  

void DMA2_Channel1_IRQHandler(void);  

void DMA2_Channel2_IRQHandler(void);  

void DMA2_Channel3_IRQHandler(void);  

void DMA2_Channel4_5_IRQHandler(void);  

                       

#endif /* __STM32F10x_IT_H */  

  

/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/  


Keywords:Stm32 Reference address:Stm32 timer interrupt makes LED light blink

Previous article:STM32 example of LED light flashing control and related precautions
Next article:Learn CubeMX with a limited understanding——TIM: Timer interrupt to realize LED flashing

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号