/********************************************************************************
* @file    TIM_PWMOutput.c
* @author  SOC AE Team
* @version V1.1
* @date    2025-06-27
* @brief   TIM0 PWMB输出频率为1000Hz@APB0Clock=32M，占空比为50%的PWM波。
*
*******************************************************************************/
#include "sc32_conf.h"
#include "Function_Init.h"
#include "stdio.h"

void TIM_PWMOutput_Function(void)
{
  TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
  TIM_PWM_InitTypeDef TIM_PWM_InitStruct;
  /* 使能TIM0时钟 */
  RCC_APB0Cmd(ENABLE);
  RCC_APB0PeriphClockCmd(RCC_APB0Periph_TIM0, ENABLE);
  
  /* TIM配置结构体初始化 */
  TIM_TimeBaseStructInit(&TIM_TimeBaseInitStruct);
  TIM_PWMStructInit(&TIM_PWM_InitStruct);
  
  /* 设置TIM时基配置结构体 */
  TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;	//TIM工作在向上计数模式
  TIM_TimeBaseInitStruct.TIM_EXENX = TIM_EXENX_Disable;	//外部重加载管脚失能
#if defined(SC32f10xx)||defined(SC32f12xx)
  TIM_TimeBaseInitStruct.TIM_Preload = (32000000 / 1000);	//设置TIM的定时器评论为1000Hz@APB0Clock=32M
#endif
  TIM_TimeBaseInitStruct.TIM_Prescaler = TIM_PRESCALER_1;	//TIM分频为1分频
  TIM_TimeBaseInitStruct.TIM_WorkMode = TIM_WorkMode_Timer;	//TIM工作在定时器模式，时钟源为APB0时钟
  
  /* TIM时基配置 */
  TIM_TIMBaseInit(TIM0, &TIM_TimeBaseInitStruct);
  
  /* 设置TIM PWM配置结构体 */
  TIM_PWM_InitStruct.TIM_PWMOutputChannl = TIM_PWMChannel_PWMB;	//TIM0 PWMB输出使能
  
  /* TIM PWM配置 */
  TIM_PWMInit(TIM0,&TIM_PWM_InitStruct);

  TIM_PWMSetDuty(TIM0,TIM_PWMChannel_PWMB,TIM_TimeBaseInitStruct.TIM_Preload/2);//占空比设置为50%
  
  /* TIM外设初始化 */
  TIM_Cmd(TIM0, ENABLE);
  while(1)
  {
  }
}
