STM32 MCU Learning (5) Timer Interrupt Experiment

Publisher:自由思想Latest update time:2016-06-03 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
/*
*	
* Software function: timer interrupt experiment
*
*/
#include "stm32f10x.h"
#include "delay.h"


void RCC_Configuration(void);
void GPIO_Configuration(void);
void NVIC_Configuration(void);
void TIM3_Configuration(u16 arr,u16 psc);

/*
Function: int main(void)
Function: main function
Parameters: None
Returns: None
/
int main(void)
{
  RCC_Configuration();
  GPIO_Configuration();
  NVIC_Configuration();
  TIM3_Configuration(4999,7199); //10Khz counting frequency, 500ms when counting to 5000  
  //delay_init(72);
  GPIO_ResetBits(GPIOB,GPIO_Pin_0);
  while(1);   
}


/*
Function: void RCC_Configuration(void)
Function: Reset and clock control configuration
Parameters: None
Returns: None
/
void RCC_Configuration(void)
{
  ErrorStatus HSEStartUpStatus; //Define the external high-speed crystal startup status enumeration variable
  RCC_DeInit(); //Reset RCC external device registers to default values
  RCC_HSEConfig(RCC_HSE_ON); //Turn on the external high-speed crystal oscillator
  HSEStartUpStatus = RCC_WaitForHSEStartUp(); //Wait for the external high-speed clock to be ready
  if(HSEStartUpStatus == SUCCESS) //The external high-speed clock is ready
  {
    FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); // Enable the FLASH pre-read buffer function to speed up the reading of FLASH. Required usage in all programs. Location: in the RCC initialization sub-function, after the clock starts
    FLASH_SetLatency(FLASH_Latency_2); //Flash operation delay
      	
    RCC_HCLKConfig(RCC_SYSCLK_Div1); //Configure AHB (HCLK) clock equal to == SYSCLK
    RCC_PCLK2Config(RCC_HCLK_Div1); //Configure APB2(PCLK2) clock==AHB clock
    RCC_PCLK1Config(RCC_HCLK_Div2); //Configure APB1(PCLK1) clock == AHB1/2 clock
         
    RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9); //Configure PLL clock == external high-speed crystal clock * 9 = 72MHz
    RCC_PLLCmd(ENABLE); //Enable PLL clock
   
    while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET) //Wait for PLL clock to be ready
    {
    }
    RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); //Configure system clock = PLL clock
    while(RCC_GetSYSCLKSource() != 0x08) //Check if PLL clock is used as system clock
    {
    }
  }
  
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE); //Enable GPIOB and AFIO clocks
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //Clock enable

}

/*
Function: void GPIO_Configuration(void)
Function: GPIO configuration
Parameters: None
Returns: None
/
void GPIO_Configuration(void)
{
  GPIO_InitTypeDef GPIO_InitStructure; //Define GPIO initialization structure

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
  GPIO_InitStructure.GPIO_Speed ​​= GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_Init(GPIOB, &GPIO_InitStructure); //PB is used to output and control the LED light

}


void NVIC_Configuration(void) //Interrupt grouping and priority configuration For details, see "STM32 Function Description (Chinese).pdf" P165
{
    NVIC_InitTypeDef NVIC_InitStructure;
	NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; //TIM3 interrupt
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //Preempt priority level 0
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //From priority level 3
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ channel is enabled
	NVIC_Init(&NVIC_InitStructure); //Initialize NVIC registers
}

void TIM3_Configuration(u16 arr,u16 psc) //TIM3 timer configuration   
{
	TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
	TIM_TimeBaseStructure.TIM_Period = arr; //Set the value of the auto-reload register period to load the activity at the next update event	
	TIM_TimeBaseStructure.TIM_Prescaler =psc; //Set the prescaler value used as the TIMx clock frequency divisor
	TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //Set clock division: TDTS = Tck_tim
	TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM up counting mode
	TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //Initialize the time base unit of TIMx according to the specified parameters
 	/*((1+7199[TIM_Prescaler] )/72M)*(1+4999[TIM_Period] )=500,000us=500ms */

	TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE); //Enable the specified TIM3 interrupt and allow update interrupt
	TIM_Cmd(TIM3, ENABLE); //Enable TIMx
}

//Timer 3 interrupt service routine
void TIM3_IRQHandler(void) //TIM3 interrupt
{
	if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET) //Check whether TIM3 update interrupt occurs
		{
		TIM_ClearITPendingBit(TIM3, TIM_IT_Update ); //Clear TIMx update interrupt flag
		if(!GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_0)) GPIO_SetBits(GPIOB,GPIO_Pin_0);
		else GPIO_ResetBits(GPIOB,GPIO_Pin_0);
	}
}
Keywords:STM32 Reference address:STM32 MCU Learning (5) Timer Interrupt Experiment

Previous article:STM32 MCU Learning (11) DS18B20 Temperature Sensor Experiment
Next article:STM32 MCU learning (10) Digital tube output experiment supplement static (common anode) + dynamic (common cathode)

Recommended ReadingLatest update time:2024-11-16 19:40

STM32 USB IAP steps
1. Download STM32_USB-FS-Device_Lib_V3.2.1.rar 2. Install DfuSe_Demo_V3.0_Setup.exe. 3.打开...\STM32_USB-FS-Device_Lib_V3.2.1\Project\Device_Firmware_Upgrade\MDK-ARM\DFU.uvproj Modify: //#define ApplicationAddress 0x08003000 to #define ApplicationAddress 0x08004000 (because the program needs to be modified to increase t
[Microcontroller]
STM32 HardFault_Handler failure
There are two main reasons why HardFault_Handler failure occurs in STM32: 1. Memory overflow or out-of-bounds access. This requires standardizing the code when writing the program yourself, and you need to slowly troubleshoot when you encounter it. 2. Stack overflow. Increase the size of the stack. How to troubles
[Microcontroller]
STM32 HardFault_Handler failure
stm32 RTC real-time clock [operation register + library function]
"RTC" is the abbreviation of Real Time Clock, which means real-time clock. STM32 provides a second interrupt source and an alarm interrupt source.   The RTC is a 32-bit counter that uses an external crystal oscillator at 32.768 kHz.   The Year 2038 Problem    In computer applications, the year 2038 problem may cause s
[Microcontroller]
STM32: External crystal clock configuration
void SystemClock_Config(void) { RCC_DeInit(); RCC_HSEConfig(RCC_HSE_ON); //INPUT HSE = 24M ErrorStatus HSEStartUpStatus = RCC_WaitForHSEStartUp(); if(HSEStartUpStatus == SUCCESS)  { FLASH_PrefetchBufferCmd(ENABLE);    //M0 defined  FLASH_SetLatency(FLASH_Latency_1);  //M0 defined  RCC_PREDIV1Config(RCC_PRE
[Microcontroller]
AVR timer1PWM settings
Introduction: What is PWM? I won't explain it in detail. Just search on the Internet. PWM regulation is widely used in control. Using PWM control can make the circuit simple and reliable, and the chip size will be small. Let's take a look at the PWM output control of AVR. void timer1_pwm() { TCCR1A=(1 COM1A1
[Microcontroller]
AVR timer1PWM settings
STM32 controls stepper motor in timer
When writing a WEB program, I found that if I added stepper motor control, there would be problems. The reason is that if the motor control is placed in the uip loop, because controlling the motor involves time issues. So it must be solved, and the method I thought of is the idea of ​​multitasking. Put the processing o
[Microcontroller]
STM32 study notes - real-time clock RTC
  //Through this routine, you can learn how to configure RTC and read count values, as well as how to convert numerical formats  . If you want to control it well, you need to read more materials and programs.             #include  "stm32f10x_lib.h"       vu32  TimeDisplay  =  0; ErrorStatus 
[Microcontroller]
Configuration 1602 program based on STM32
Note: PD_0-7 is used for PO port (LCD data port of MCU board), PA0_2; They have been configured as registers in the function. Note that when reconfiguring PD These ports should not be configured; PD and PA need to be enabled in the main function. Header function: #include "1602.h" Function:
[Microcontroller]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号