/********************************************************************************
* @file    LEDPWM_Example.c
* @author  SOC AE Team
* @version V1.1
* @date    2025-06-27
* @brief   使用LEDPWM输出可以调节LED亮度波形,观察COM0（PA9）和SEG4（PA10）可以观察到波形，其中SEG1波形的占空比是10%。
*
*******************************************************************************/
#if defined(SC32f10xx)||defined(SC32f12xx)
#include "sc32_conf.h"
#include "Function_Init.h"
#include "stdio.h"
#if defined(SC32f12xx)
const GPIO_Pin_TypeDef Com_GPIO_Pin[] = {GPIO_Pin_13, GPIO_Pin_12, GPIO_Pin_11, GPIO_Pin_10, GPIO_Pin_9, GPIO_Pin_8, GPIO_Pin_7, GPIO_Pin_6};
#elif defined(SC32f10xx)
const GPIO_Pin_TypeDef Com_GPIO_Pin[] = {GPIO_Pin_9, GPIO_Pin_8, GPIO_Pin_7, GPIO_Pin_6, GPIO_Pin_5, GPIO_Pin_4, GPIO_Pin_3, GPIO_Pin_2};
#endif
//定义Com当前值，扫描最小值和最大值
uint8_t ScanCom = 0,ComMin,ComMax;;
uint8_t Seg_Duty[8][28];

uint8_t ScanCnt;	//扫描切换Com所用计数器

void LEDPWM_Example_Init(LED_InitTypeDef* LED_BaseInitStruct);

void LEDPWM_Example_Function(void)
{
  LED_InitTypeDef LED_InitStruct; //定义LED初始化结构体变量
  
  /* 使能PWM0时钟 */
  RCC_APB2Cmd(ENABLE);
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_LCD_LED, ENABLE);
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_LEDPWM, ENABLE);
  
  /* 结构体成员初始化 */
  LED_StructInit(&LED_InitStruct);
  LED_InitStruct.LED_Duty = LED_Duty_1_4_SEG4_27COM0_3;	//配置占空比为1/4，并且开启COM0~3输出
  LED_InitStruct.LED_FrameFre = LED_FrameFre_B64Hz;	//帧频为B类型32帧
  /* 使能COM管脚输出通道，每个位对应一个通道 */
  LED_InitStruct.LED_ComPin = 0x0F;
  /* 使能Sega管脚输出通道，每个位对应一个通道 */
  LED_InitStruct.LED_SegPin =  0x00000F00 | LEDPWM_Channel_31;
  
  LEDPWM_Example_Init(&LED_InitStruct);
  
  /* 使能PWM0中断 */
  LEDPWM_ITConfig(PWM_IT_INTEN, ENABLE);
  NVIC_EnableIRQ(LEDPWM_IRQn);
  
  /* PWM外设初始化 */
  LEDPWM_Cmd(ENABLE);
  printf("\r\n LEDPWM_CON:0x%x \r\n",LEDPWM->LEDPWM_CON);
  printf("\r\n LEDPWM_CHN0:0x%x \r\n",LEDPWM->LEDPWM_CHN);

  /* 设置占空比 */
  Seg_Duty[0][8] = (uint8_t)(93.75 * 0.1);	//10%的亮度
  Seg_Duty[0][9] = (uint8_t)(93.75 * 0.3);	//30%的亮度
  Seg_Duty[0][10] = (uint8_t)(93.75 * 0.6);	//60%的亮度
  Seg_Duty[0][11] = (uint8_t)(93.75 * 0.8);	//80%的亮度
  
  while(1);
}

void LEDPWM_Example_Init(LED_InitTypeDef* LED_InitStruct)
{
  LEDPWM_InitTypeDef LEDPWM_InitStruct;
  GPIO_InitTypeDef GPIO_InitStrcut;
  /* PWM配置结构体初始化 */
  LEDPWM_StructInit(&LEDPWM_InitStruct);
#if defined(SC32f12xx)  
  /* 初始化COM管脚 */
  switch((uint16_t)LED_InitStruct->LED_Duty)
  {
  case LED_Duty_1_8:
    {
      GPIO_InitStrcut.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9
        | GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13;
      ComMin = 0,ComMax = 7;
      break;
    }
  case LED_Duty_1_6:
    {
      GPIO_InitStrcut.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8| GPIO_Pin_9
        | GPIO_Pin_10 | GPIO_Pin_11;
      ComMin = 0,ComMax = 5;
      break;
    }
  case LED_Duty_1_5:
    {
      GPIO_InitStrcut.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9
        | GPIO_Pin_10;
      ComMin = 0,ComMax = 4;
      break;
    }
  case LED_Duty_1_4_SEG0_27COM4_7:
    {
      GPIO_InitStrcut.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9;
      ComMin = 4,ComMax = 7;
      break;
    }
  case LED_Duty_1_4_SEG4_27COM0_3:
    {
      GPIO_InitStrcut.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_12 | GPIO_Pin_11 | GPIO_Pin_10;
      ComMin = 0,ComMax = 3;
      break;
    }
  default:
    break;
  }
  
  GPIO_InitStrcut.GPIO_Mode = GPIO_Mode_OUT_PP;
  GPIO_Init(GPIOA, &GPIO_InitStrcut);
  GPIO_SetBits(GPIOA,GPIO_InitStrcut.GPIO_Pin);
#elif  defined(SC32f10xx) 
  /* 初始化COM管脚 */
  switch((uint16_t)LED_InitStruct->LED_Duty)
  {
  case LED_Duty_1_8:
    {
      GPIO_InitStrcut.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9
        | GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13;
      ComMin = 0,ComMax = 7;
      break;
    }
  case LED_Duty_1_6:
    {
      GPIO_InitStrcut.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8| GPIO_Pin_9
        | GPIO_Pin_10 | GPIO_Pin_11;
      ComMin = 0,ComMax = 5;
      break;
    }
  case LED_Duty_1_5:
    {
      GPIO_InitStrcut.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9
        | GPIO_Pin_10;
      ComMin = 0,ComMax = 4;
      break;
    }
  case LED_Duty_1_4_SEG0_27COM4_7:
    {
      GPIO_InitStrcut.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9;
      ComMin = 4,ComMax = 7;
      break;
    }
  case LED_Duty_1_4_SEG4_27COM0_3:
    {
      GPIO_InitStrcut.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_8 | GPIO_Pin_7 | GPIO_Pin_6;
      ComMin = 0,ComMax = 3;
      break;
    }
  default:
    break;
  }
  
  GPIO_InitStrcut.GPIO_Mode = GPIO_Mode_OUT_PP;
  GPIO_Init(GPIOA, &GPIO_InitStrcut);
  GPIO_SetBits(GPIOA,GPIO_InitStrcut.GPIO_Pin);
#endif
  /* 设置PWM配置结构体 */
  LEDPWM_InitStruct.LEDPWM_AlignedMode = PWM_AlignmentMode_Edge;	//PWM对齐模式为边沿对齐
  LEDPWM_InitStruct.LEDPWM_Cycle = (uint16_t)(32000000 / 256 / 1000);	//PWM的频率为1000Hz
  LEDPWM_InitStruct.LEDPWM_OutputChannel = LED_InitStruct->LED_SegPin;	//使能PWM00输出
  LEDPWM_InitStruct.LEDPWM_Prescaler = LEDPWM_PRESCALER_DIV256;	        //LEDPWM0时钟分频为1分配
  LEDPWM_Init(&LEDPWM_InitStruct);	                                //配置LEDPWM0
  
  LEDPWM_SetDuty(LEDPWM_Channel_0, LEDPWM_InitStruct.LEDPWM_Cycle / 2);	//配置占空比为50%
}

//LED SEG切换
void LEDPWM_SegScan(void)
{
  uint8_t i,j = 1;
  for(i = 0; i < 28; i++)
  {
    LEDPWM_SetDuty((LEDPWM_Channel_Typedef)(j << i),Seg_Duty[ScanCom][i]);
  }
}

//LED COM切换
void LEDPWM_ComScan(void)
{
  if(ScanCom == 0)
    GPIO_SetBits(GPIOA, Com_GPIO_Pin[ComMax]); //SCAN COM
  else
    GPIO_SetBits(GPIOA, Com_GPIO_Pin[ScanCom-1]); //SCAN COM
  
  GPIO_ResetBits(GPIOA, Com_GPIO_Pin[ScanCom]); //SCAN COM
}

void LEDPWM_Example_LEDPWMHandler(void)
{
  
  /* 判断PWM0中断是否置起 */
  if(LEDPWM_GetFlagStatus(PWM_Flag_PWMIF) == SET)
  {
    LEDPWM_ClearFlag(PWM_Flag_PWMIF);	//清除PWM0中断
    
    if(ScanCnt ++ > 5)	//帧频为 1000/5/4=50Hz
    {
      ScanCom ++;
      if(ScanCom > ComMax)
        ScanCom = ComMin;
      LEDPWM_SegScan();
      LEDPWM_ComScan();
      ScanCnt = 0;
    }
  }
}
#endif
