/********************************************************************************
 * @file    PWM_Independent.c
 * @author  SOC AE Team
 * @version V1.1
 * @date    2025-06-27
 * @brief   PWM00输出频率为1000Hz@PWMClk=32Mhz，占空比为50%的PWM。
 *
 *******************************************************************************/
 #if defined(SC32f10xx)||defined(SC32f12xx)
#include "sc32_conf.h"
#include "Function_Init.h"
#include "stdio.h"


void PWM_Independent_Function(void)
{
  PWM_InitTypeDef PWM_InitStruct;
  
  /* 使能PWM0时钟 */
  RCC_APB0Cmd(ENABLE);
  RCC_APB0PeriphClockCmd(RCC_APB0Periph_PWM0,ENABLE);
  
  /* PWM配置结构体初始化 */
  PWM_StructInit(&PWM_InitStruct);
  
  /* 设置PWM配置结构体 */
  PWM_InitStruct.PWM_AlignedMode = PWM_AlignmentMode_Edge;//PWM对齐模式为边沿对齐
  PWM_InitStruct.PWM_Cycle = (uint16_t)(32000000/1000);	//PWM的频率为1000Hz
  PWM_InitStruct.PWM_OutputChannel = PWM_Channel_0;	//使能PWM00输出
  PWM_InitStruct.PWM_Prescaler = PWM_PRESCALER_DIV1;	//PWM0时钟分频为1分配
  PWM_InitStruct.PWM_WorkMode = PWM_WorkMode_Independent;	//PWM0工作模式为独立模式
  
  PWM_Init(PWM0,&PWM_InitStruct);	//配置PWM0
  
  PWM_SetDuty(PWM0,PWM_Channel_0,PWM_InitStruct.PWM_Cycle / 2);	//配置占空比为50%
  
  /* 使能PWM0中断 */
  PWM_ITConfig(PWM0,PWM_IT_INTEN,ENABLE);
  NVIC_EnableIRQ(PWM0_IRQn);
  
  /* PWM外设初始化 */
  PWM_Cmd(PWM0,ENABLE);
  
  while(1);
}

void PWM_Independent_PWMHandler(void)
{
  /* 判断PWM0中断是否置起 */
  if(PWM_GetFlagStatus(PWM0,PWM_Flag_PWMIF) == SET)
  {
    PWM_ClearFlag(PWM0,PWM_Flag_PWMIF);	//清除PWM0中断
  }
}
#endif
