/********************************************************************************
 * @file    TIM_CounterBase.c
 * @author  SOC AE Team
 * @version V1.1
 * @date    2025-06-27
 * @brief   TIM作为计数器，TxEN每计数10次触发一次中断。
 *
 *******************************************************************************/
#include "sc32_conf.h"
#include "Function_Init.h"
#include "stdio.h"


void TIM_CounterBase_Function(void)
{
  TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
  
  /* 使能TIM1时钟 */
  RCC_APB0Cmd(ENABLE);
  RCC_APB0PeriphClockCmd(RCC_APB0Periph_TIM1,ENABLE);
  
  /* TIM配置结构体初始化 */
  TIM_TimeBaseStructInit(&TIM_TimeBaseInitStruct);
  
  /* 设置TIM配置结构体 */
  TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;	//TIM工作在向上计数模式
  TIM_TimeBaseInitStruct.TIM_EXENX = TIM_EXENX_Disable;	//外部重加载管脚失能
  TIM_TimeBaseInitStruct.TIM_Preload = 65535-10;	//每计数10次触发一次TIM溢出中断
  TIM_TimeBaseInitStruct.TIM_Prescaler = TIM_PRESCALER_1;	//TIM分频为1分频
  TIM_TimeBaseInitStruct.TIM_WorkMode = TIM_WorkMode_Counter;	//TIM工作在计数器模式
  
  /* TIM时基配置 */
  TIM_TIMBaseInit(TIM1,&TIM_TimeBaseInitStruct);
  
  /* 使能TIM1中断 */
  TIM_ITConfig(TIM1,TIM_IT_TI | TIM_IT_INTEN,ENABLE);
  NVIC_EnableIRQ(TIMER1_IRQn);
  
  /* TIM外设初始化 */
  TIM_Cmd(TIM1,ENABLE);
  
  while(1);
}

void TIM_CounterBase_TIM1Handler(void)
{
  /* 判断TIM1中断是否置起 */
  if(TIM_GetFlagStatus(TIM1,TIM_Flag_TI) == SET)
  {
    TIM_ClearFlag(TIM1,TIM_Flag_TI);	//清除TIM1中断
    printf("TIM In\r\n");
  }
}
