timer driven

foreword

(1) This series is based on STM32 project notes, covering the use of various STM32 peripherals, from shallow to deep.

(2) The MCU used by the editor is STM32F105RCT6. The project notes are based on the actual project of the editor, but the content in the blog is suitable for the students who develop various MCUs to learn and use.

learning target

  1. Master the STM32 microcontroller general-purpose timer initialization.
  2. Complete the timing function of the timer 1ms.
  3. Experimental effect: LED light flickers for 1 second.

main content

Timer of STM32 MCU

STM32 has a total of 8 timers, which are 2 advanced timers (TIM1, TIM8), 4 general-purpose timers (TIM2, TIM3, TIM4, TIM5) and 2 basic timers (TIM6, TIM7)

Timer initialization, divided into 3 parts

(1) Turn on the timer clock

(2) Configure timer parameters

(3) Configure the timer interrupt and turn on the timer interrupt function

In this section, we use timer 4, so timer 4 needs to be initialized.

open timer clock
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
Configure timer parameters
	TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;

	TIM_DeInit(TIM4); 
	TIM_TimeBaseStructure.TIM_Period = 1000;   			//1ms 
	TIM_TimeBaseStructure.TIM_Prescaler = SystemCoreClock/1000000 - 1;              
	TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;     
	TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; 
	TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);	
	
	TIM_ClearFlag(TIM4, TIM_FLAG_Update);					  
	TIM_ITConfig(TIM4, TIM_IT_Update, ENABLE); 
					 
	TIM_Cmd(TIM4, ENABLE); 

Relevant parameter description:

TIM_Period: Set the value of the auto-reload register period

TIM_Prescaler: Set the prescaler value of the clock frequency divisor 1M // 0.000 001 seconds 1uS

Why -1; because counting from 0 means 1 assigns 1 means 2 assigns.../

72000 000 / 1000 000 = 72; //71

72M is divided by 72 to get 1M, 1M is the clock frequency, 1/1M=1us is a counting cycle of the timer. To accumulate period=1000, a timer interrupt will be generated, that is, the period of timer interrupt is 1us*1000=1ms.

TIM_ClockDivision: //Set the clock division, which has nothing to do with the timer timing time

TIM_CounterMode: //TIM counts up

After configuration, the timing time of the timer is 1s, here is the formula for calculating the timing time of the timer:

① Timer clock frequency = 72M/(Prescaler+1);

② A counting period = 1/timer clock frequency;

③ Timing time = Period * timer clock time;

Configure timer interrupt
      NVIC_InitTypeDef NVIC_InitStructure;

	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置NVIC中断分组2

	NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;///先占优先级
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;        ///代优先级
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

Configure NVIC interrupt group 2
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
Timer interrupt service function
void TIM4_IRQHandler(void)///定时器4 中断服务函数
{
	hal_LedProc();
	TIM_ClearFlag(TIM4, TIM_FLAG_Update);
}

hal_timer.h code

#ifndef _HAL_TIMER_H_
#define _HAL_TIMER_H_


void hal_timerInit(void);

#endif

hal_timer.c code

#include "stm32F10x.h"
#include "hal_timer.h"
#include "hal_led.h"
#include "string.h"

/*******************************************************************************
* Function Name  : static void hal_timer4Config(void)
* Description    : 定时器硬件配置函数
* Input          : None
* Output         : None
* Return         : None
* Attention		 	 : None
*******************************************************************************/
static void hal_timer4Config(void)
{
	TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
	NVIC_InitTypeDef NVIC_InitStructure;
	
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
	
	TIM_DeInit(TIM4); 
	TIM_TimeBaseStructure.TIM_Period = 1000; 			//1ms  
	TIM_TimeBaseStructure.TIM_Prescaler = SystemCoreClock/1000000 - 1;              
	TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;     
	TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; 
	TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);	
	
	TIM_ITConfig(TIM4, TIM_IT_Update, ENABLE); ///打开定时器4中断				 
	TIM_Cmd(TIM4, ENABLE);   ///打开定时器4

	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置NVIC中断分组2
	
	NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;///先占优先级
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;        ///从优先级
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);
}


/*******************************************************************************
* Function Name  : void hal_timerInit(void)
* Description    : 定时器初始化
* Input          : None
* Output         : None
* Return         : None
* Attention		 	 : None
*******************************************************************************/
void hal_timerInit(void)
{
	hal_timer4Config();
}
/
void TIM4_IRQHandler(void)
{
	hal_LedProc();
	TIM_ClearFlag(TIM4, TIM_FLAG_Update);
}
//



hal_led.c code

#include "stm32F10x.h"
#include "hal_timer.h"
#include "hal_led.h"

static void hal_LedConfig(void);
static void hal_Led1Turn(void);

void hal_LedInit(void)
{
	hal_LedConfig();
}
/
/
void delay_1sTest(void)
{
	unsigned int i=0;
	i = 7200000;
	while(i--);   
}

static void hal_LedHandle(void)
{
//	delay_1sTest();
//	GPIO_ResetBits(LED8_PORT,LED8_PIN);
//	GPIO_ResetBits(LED7_PORT,LED7_PIN);
//	delay_1sTest();
//	GPIO_SetBits(LED8_PORT,LED8_PIN);
//	GPIO_SetBits(LED7_PORT,LED7_PIN);
  static unsigned short delayt =0;
	delayt ++;
  if(delayt > 999)
	{1秒钟
		hal_Led1Turn();
		delayt = 0;
	}	
}
//


void hal_LedProc(void)
{
	hal_LedHandle();
}

static void hal_LedConfig(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;	
	RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOC,ENABLE); 						 
	GPIO_InitStructure.GPIO_Pin = LED8_PIN | LED7_PIN;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; ; 
	GPIO_Init(LED8_PORT, &GPIO_InitStructure);
	
	GPIO_ResetBits(LED8_PORT,LED8_PIN);
	GPIO_SetBits(LED7_PORT,LED7_PIN);
}


static void hal_Led1Turn(void)
{/1-》0    0-》1
	GPIO_WriteBit(LED8_PORT,LED8_PIN,(BitAction)(1-GPIO_ReadOutputDataBit(LED8_PORT,LED8_PIN)));
	GPIO_WriteBit(LED7_PORT,LED7_PIN,(BitAction)(1-GPIO_ReadOutputDataBit(LED7_PORT,LED7_PIN)));
}
/

main.c code

#include "stm32f10x.h"
#include "hal_timer.h"
#include "hal_led.h"
#include "hal_gpio.h"


int main(void)
{
	hal_LedInit();
	hal_GpioConfig_init();	
	hal_timerInit();
  while (1)
  {	
//		if(hal_Gpio_AcStateCheck() == STA_AC_BREAK)
//		{
//			GPIO_ResetBits(LED7_PORT,LED7_PIN);
//		}
//		else
//		{	
//			GPIO_SetBits(LED7_PORT,LED7_PIN);			
//		}		
//		hal_LedProc();
  }
}

Guess you like

Origin blog.csdn.net/weixin_62261692/article/details/132500698