Files
pwm32e_pro/project/example/ble/ble_peripheral/app/src/PWM.c
T

199 lines
5.3 KiB
C

#include "pwm.h"
#include "xc_drv_pwm.h"
#include "rgblight.h"
#include "mode.h"
#include "math.h"
#include <stdint.h>
#include "timer.h"
#include "fmc_spi.h"
#include "xc_drv_fmc_spi.h"
#include "xc_drv_fmc_spi_dma.h"
#include "ota_flash_interface.h"
// 定义一个变量来保存当前的 PWM 输出管脚设置
uint8_t pwm_red_idx = PWM2_IDX;
uint8_t pwm_green_idx = PWM3_IDX;
uint8_t pwm_blue_idx = PWM5_IDX;
/*------------------------------------------------------------------------------------
TypeDef
-------------------------------------------------------------------------------------*/
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;
uint16_t timeCnt=0;
uint16_t W_PWM = 100;//初始化暖光
uint16_t C_PWM = 0;
uint16_t deadTime = 4; //2*deadTime+deadTime_C+deadTime_W=20;这个是修改死区的时间。这三个变量加起来要是20, 规定好了的,当时用的10的死区,后面修改了,不破坏原来的封装
uint16_t deadTime_C = _deadTime; //
uint16_t deadTime_W = _deadTime;
uint8_t driveMode=1;
uint8_t PWMMAX=100; //最大占空比
uint8_t Brightness=BRIGHTNESS_MAX_PERCENT;
// 切换 PWM 输出管脚,蓝牙调用此方法,切换设备输出线序
uint16_t W_PWM_duty;//初始化暖光
uint16_t C_PWM_duty;
void switch_pwm_pins(uint8_t red_pwm, uint8_t green_pwm, uint8_t blue_pwm)
{
// pwm_red_idx = red_pwm;
// pwm_green_idx = green_pwm;
// pwm_blue_idx = blue_pwm;
}
void _PWM_INIT(){
GPIO_InitCfg_t GPIO_InitCfg = { 0 }; // 清零初始化配置结构体
GPIO_InitCfg.Mux = GPIO_Mux0; // 选择功能复用器0
GPIO_InitCfg.FunSel = GPIO_Dx; // 选择GPIO功能(非特殊功能)
GPIO_InitCfg.Pull = GPIO_PULLDOWN; // 上拉电阻使能
GPIO_InitCfg.Dir = GPIO_DIR_OUTPUT; // 输出方向
GPIO_InitCfg.Int = NOT_INT; // 不使能中断
GPIO_InitCfg.Pin = IO_PWM_W; // 设置LED1引脚
xc_gpio_init(&GPIO_InitCfg); // 初始化GPIO
GPIO_InitCfg.Dir = GPIO_DIR_OUTPUT; // 输出方向
GPIO_InitCfg.Int = NOT_INT; // 不使能中断
GPIO_InitCfg.Pin = IO_PWM_C; // 设置LED1引脚
xc_gpio_init(&GPIO_InitCfg); // 初始化GPIO
xc_gpio_write_pin(IO_PWM_W, GPIO_PIN_RESET); // 主输出低
xc_gpio_write_pin(IO_PWM_C, GPIO_PIN_RESET); // 主输出低
if(deviceStatus==POWERON){
Start_PWM();
}
}
void PWM_SetDuty(uint8_t PWMX ,uint8_t duty) {
// if(PWMX==IO_PWM_W){
// W_PWM=duty;
// }else if(PWMX ==IO_PWM_C){
// C_PWM=duty;
// }
}
static uint16_t scale_display_value(uint16_t value)
{
if(value > 100U) value = 100U;
if(Brightness >= BRIGHTNESS_MAX_PERCENT) return value;
return (uint16_t)(((uint32_t)value * Brightness + 50U) / 100U);
}
void UpdateDisplay(void) // static update with global brightness
{
W_PWM_duty = scale_display_value(W_PWM);
C_PWM_duty = scale_display_value(C_PWM);
}
void UpdateDisplay_1(void) // dynamic update with global brightness
{
W_PWM_duty = scale_display_value(W_PWM);
C_PWM_duty = scale_display_value(C_PWM);
}
void temperature(uint8_t data) // 0=cold, 255=warm
{
W_PWM = (uint16_t)(((uint32_t)data * 100U + 127U) / 255U);
C_PWM = 100U - W_PWM;
if(W_PWM >= 100U)
{
driveMode = 1;
}
else if(C_PWM >= 100U)
{
driveMode = 2;
}
else
{
driveMode = 0;
}
}
/**
* @brief app_pwm_init
* @details
* @param void
* @retval void
*/
void app_pwm_init()
{
}
//ADC
float RefVol;
void adc_Init(void)
{
//DEBUG("__ADC_DEMO__\r");
uint16_t adc_2v48_param;
GPIO_InitCfg_t GPIO_InitCfg = { 0 };
GPIO_InitCfg.Mux = GPIO_Mux0;
GPIO_InitCfg.FunSel = GPIO_Dx;
GPIO_InitCfg.Pull = GPIO_NOPULL;//上拉
GPIO_InitCfg.Dir = GPIO_DIR_INPUT;
GPIO_InitCfg.Int = RIS_FAIL_EDGE_INT;
GPIO_InitCfg.Pin = GPIO_0;
xc_gpio_init(&GPIO_InitCfg);
ADC_InitCfg_t adc_cfg;
/*----- Special Read the 2.48 v calibration value -----*/
/*< If the content of the parameter
storage area is invalid, the default assignment is 10000 >*/
if(false == xc_adc_2v4_calibrate_read(&adc_2v48_param))
{
adc_2v48_param = 10000;
}
/*----- Special Pin -----*/
/*< The SWD and SWCK pins can also be used as ADC pins.
Pay attention to the function remapping of the PIN pins. >*/
adc_cfg.Freq = ADC_FREQ_2M;
adc_cfg.RefVol = ADC_REF_VOL_3_3V;
adc_cfg.SampEdge = ADC_SAMPEDGE_RISE;
adc_cfg.ExtDataMode = ADC_EXT_DATA_MODE_32BIT;
adc_cfg.ExtEdgeSel = ADC_EXT_EDGE_SEL_INTER;
adc_cfg.ExtSampleNum = ADC_EXT_SAMPLE_NUM_8;
adc_cfg.ExtTriggerSel = ADC_EXT_TRIGGER_SEL_PWM0;
xc_adc_init(&adc_cfg);
RefVol = adc_cfg.RefVol;
}
#define ADV_REF_VOLT_3_3V 3.3f
#define ADV_REF_VOLT_2_4_8V 2.48f
uint16_t after_adc_val;
uint8_t adc_new_data_Flag = false;
float ADC_DATA=0;
uint8_t sss=0;
extern uint8_t r_data[256];
void My_ADC_Get_Value(void){ //掉电保存
uint16_t adc_val;
xc_adc_start_get_value( ADC_CH4_PIN0 ,&adc_val);
if(adc_val<=1000&&sss==1){ //3099-3170
sss=0;
app_op_flash_on();
}
}