/*! * \file pwm.c * * \brief Target pwm implementation * * \copyright Revised BSD License, see section \ref LICENSE. * * \code * * _ __ _ ________ _ * | |/ /(_)___ / ____/ /_ (_)___ * | // / __ \/ / / __ \/ / __ \ * / |/ / / / / /___/ / / / / /_/ / * /_/|_/_/_/ /_/\____/_/ /_/_/ .___/ * /_/ * (C) 2022-2025 XinChip * * \endcode * * \author ( XinChip ) Alex-J * * \author ( XinChip ) */ /*------------------------------------------------------------------------------------ INCLUDE HEADE FILES --------------------------------------------------------------------------------------*/ #include "pwm.h" #include "xc_drv_pwm.h" #include "rgblight.h" /*------------------------------------------------------------------------------------ Macros -------------------------------------------------------------------------------------*/ /*------------------------------------------------------------------------------------ Global Variables -------------------------------------------------------------------------------------*/ /*------------------------------------------------------------------------------------ TypeDef -------------------------------------------------------------------------------------*/ /*------------------------------------------------------------------------------------ Local Variables -------------------------------------------------------------------------------------*/ uint16_t ch0_capture_data[CAPTURE_DATA_MAX]; uint16_t ch1_capture_data[CAPTURE_DATA_MAX]; uint16_t ch2_capture_data[CAPTURE_DATA_MAX]; PWM_CAP_STA_TypeDef pwm_cap_state = PWM_CAP_IDLE; PWM_BRK_STA_TypeDef pwm_brk_state = PWM_BRK_IDLE; PWM_BRK_CAP_STA_TypeDef pwm_brk_cap_state = PWM_BRK_CAP_VALID; pwm_ch_cap_dutycycle_t pwm_ch_cap_dutycycle; pwm_ch_cap_freq_t pwm_ch_cap_freq; pwm_ch_cap_type_t pwm_ch_cap_type; /*------------------------------------------------------------------------------------ Func Prototype -------------------------------------------------------------------------------------*/ /*------------------------------------------------------------------------------------ Functions -------------------------------------------------------------------------------------*/ void UpdateDisplay(void) { xc_pwm_dutycycle_set(PWM2_IDX,256,r_DutyCycle*Brightness/10); xc_pwm_dutycycle_set(PWM3_IDX,256,g_DutyCycle*Brightness/10); xc_pwm_dutycycle_set(PWM5_IDX,256,b_DutyCycle*Brightness/10); } /** * @brief app_pwm_init * @details * @param void * @retval void */ void app_pwm_init() { // PWM0 and PWM1 can be mapped to other pins, PWM0 and PWM1 have inverted output. // PWM(HZ) = PWMCLK(16000000)/(DutyCycleAcc*(period+1))/2 PWM_InitCfg_t pwm_cfg; pwm_cfg.DutyCycleAcc = 256; pwm_cfg.Period = 29; pwm_cfg.SrcClk = PWM_CLK_SRC_32M_DIV; pwm_cfg.SrcEnable = PWM_EN_SEL_ALL; pwm_cfg.DutyCycle =240; //r_DutyCycle*Brightness/10; pwm_cfg.OutputPin = GPIO_12; xc_pwm_init(PWM2_IDX, &pwm_cfg);//R pwm_cfg.DutyCycle = g_DutyCycle*Brightness/10; pwm_cfg.OutputPin = GPIO_13; // The pin cannot be changed xc_pwm_init(PWM3_IDX, &pwm_cfg);//G pwm_cfg.DutyCycle = b_DutyCycle*Brightness/10; pwm_cfg.OutputPin = GPIO_1; // The pin cannot be changed xc_pwm_init(PWM5_IDX, &pwm_cfg);//B xc_pwm_start_all(); }