This commit is contained in:
moyuhai
2026-06-09 16:39:17 +08:00
commit 41a602f89c
3057 changed files with 771495 additions and 0 deletions
@@ -0,0 +1,217 @@
#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 = 10;//初始化暖光
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=10;
// 切换 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;
}
float brightness_table[101] = {
0.006f, 0.006f, 0.006f, 0.006f, 0.006f,
0.006f, 0.006f, 0.006f, 0.007f, 0.008f,
0.009f, 0.010f, 0.011f, 0.013f, 0.015f,
0.018f, 0.020f, 0.023f, 0.026f, 0.029f,
0.032f, 0.036f, 0.039f, 0.043f, 0.047f,
0.052f, 0.056f, 0.061f, 0.066f, 0.071f,
0.076f, 0.082f, 0.087f, 0.093f, 0.099f,
0.106f, 0.112f, 0.119f, 0.126f, 0.133f,
0.141f, 0.148f, 0.156f, 0.164f, 0.173f,
0.181f, 0.190f, 0.199f, 0.208f, 0.218f,
0.227f, 0.237f, 0.247f, 0.258f, 0.268f,
0.279f, 0.290f, 0.302f, 0.313f, 0.325f,
0.337f, 0.349f, 0.362f, 0.375f, 0.388f,
0.401f, 0.414f, 0.428f, 0.442f, 0.456f,
0.471f, 0.485f, 0.500f, 0.516f, 0.531f,
0.547f, 0.563f, 0.579f, 0.595f, 0.612f,
0.629f, 0.646f, 0.664f, 0.681f, 0.699f,
0.718f, 0.736f, 0.755f, 0.774f, 0.793f,
0.813f, 0.832f, 0.852f, 0.873f, 0.893f,
0.914f, 0.935f, 0.957f, 0.978f, 1.000f,1.000f
};
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;
// }
}
void UpdateDisplay(void) //静态更新 可以调节亮度
{
W_PWM_duty = W_PWM * Brightness;
C_PWM_duty = C_PWM * Brightness;
}
void UpdateDisplay_1(void) //动态更新 不可以调节亮度,只能调节速度
{
W_PWM_duty = W_PWM;
C_PWM_duty = C_PWM;
// printf("dd=%d--%d--%d-%d-%d\r\n",choose_mode_falsg,W_PWM,C_PWM,driveMode,Brightness);
}
void temperature(uint8_t data) //更新
{
driveMode=0;
if(data<57&&data>=16){C_PWM= 0;W_PWM = 10;driveMode=1;}
else if(data<77&&data>=57){C_PWM= 1;W_PWM = 7;}
else if(data<101&&data>=77){C_PWM= 2;W_PWM = 6;}
else if(data<123&&data>=101){C_PWM= 3;W_PWM = 5;}
else if(data<145&&data>=123){C_PWM= 4;W_PWM = 4;}
else if(data<167&&data>=145){C_PWM= 5;W_PWM = 3;}
else if(data<194&&data>=167){C_PWM= 6;W_PWM = 2;}
else if(data<210&&data>=194){C_PWM= 7;W_PWM = 1;}
else if(data>=210){C_PWM= 10;W_PWM = 0;driveMode=2;}
//printf("datat=%d-%d-%d\r\n",data,C_PWM,W_PWM);
//data = 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();
}
}