429 views|0 replies

410

Posts

3

Resources
The OP
 

[ACM32G103RCT6 development board review] + Timer interrupt test [Copy link]

 

Test the timer interrupt function.

1. System Block Diagram

Timer 2 uses the APB1 peripheral clock source.

If the input PCLKDIV frequency division value is set to 1, TIMCLK = PCLK1, otherwise TIMCLK = PCLK1*2

2. Procedure

2.1、hours.c

#include "main.h"

#define TIM_CLOCK_FREQ            (10000) 

TIM_HandleTypeDef tim2_handler;   

volatile uint32_t Timer_Update_Flag; 

//定时器2初始化
void init_tim2(void)
{
	uint32_t timer_clock; 
	timer_clock = HAL_RCC_GetPCLK1Freq();
	
	if (HAL_RCC_GetHCLKFreq() != timer_clock)  // if hclk/pclk != 1, then timer clk = pclk * 2  
	{
		 timer_clock =  timer_clock << 1;    
	}
	
	tim2_handler.Instance = TIM2;
	tim2_handler.Init.ARRPreLoadEn = TIM_ARR_PRELOAD_ENABLE;    
	tim2_handler.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; 
	tim2_handler.Init.CounterMode = TIM_COUNTERMODE_UP; 
	tim2_handler.Init.RepetitionCounter = 0;  
	tim2_handler.Init.Prescaler = (timer_clock/TIM_CLOCK_FREQ) - 1;  
	tim2_handler.Init.Period = (TIM_CLOCK_FREQ/1000)*100 - 1;  // 100ms 
	
	__HAL_RCC_TIM2_RESET();   
	__HAL_RCC_TIM2_CLK_ENABLE(); 
	
	NVIC_ClearPendingIRQ(TIM2_IRQn);   
  NVIC_EnableIRQ(TIM2_IRQn); 
	
	HAL_TIMER_MSP_Init(&tim2_handler);     
	HAL_TIMER_Base_Init(&tim2_handler);    
	HAL_TIM_ENABLE_IT(&tim2_handler, TIMER_INT_EN_UPD);  
	HAL_TIMER_Base_Start(tim2_handler.Instance); 
																				
}

//中断回调函数 
void HAL_TIM_Updeate_Event_Callback(TIM_HandleTypeDef *htim)
{
    Timer_Update_Flag = 1; 
		led1_tog();
}

2.2、main.c

#include "main.h" 

int main(void)
{
	HAL_Init();  
	SystemClock_Config();    
	usart_init(115200); 
   
	init_led();
	init_tim2();
	while(1)
	{
		HAL_Delay(500);
		//led1_tog();
		printf("https://en.eeworld.com/bbs/\r\n");
	}
}

3. Operation Results

Test waveform of PF3 port

100ms timing period

This post is from Domestic Chip Exchange
 
 

Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list