#include "main.h" #include //定义布尔 #include #include "adc.h" #include "board.h" #define PWM_FREQHZ 25000 //硬件pwm频率 /*********************************************************************************************************************** * =====================================================函数声明====================================================== * ***********************************************************************************************************************/ void onkey(zkey_t* key, zkey_state_t key_state); /*********************************************************************************************************************** * =====================================================全局变量====================================================== * ***********************************************************************************************************************/ static zkey_t s_keys[] = { ZKEY_INIT("timerkey", port_gpio_get_timer_key_state), //左2 ZKEY_INIT("gearskey", port_gpio_get_gears_key_state), //左1 ZKEY_INIT("intervalkey", port_gpio_get_interval_key_state), //左3 ZKEY_INIT("switchkey", port_gpio_get_switch_key_state), }; zkey_module_t key_module = ZMODULE_INIT(s_keys, onkey); extern uint32_t target_frequencyhz; extern uint32_t target_duty; /*********************************************************************************************************************** * =====================================================全局状态====================================================== * ***********************************************************************************************************************/ static bool error_even_trigger_after_stop_ozone_work_state; //错误事件触发后停止臭氧工作标志位 // 设置 bool g_setting_interval_work_flag = false; //现在间歇工作状态 work_level_t g_setting_level = kwork_level_hight; //关机前的档位 //标志位 bool g_power_flag = false; //开关机标志位 static int g_error_num; bool g_auto_shutdown_flag = false; //定时功能使能标志位 static uint32_t g_auto_shutdown_countdown_s; //定时时间 bool s_rxbuf_is_ready = false; char rx_buffer[50]; //接收数据 uint8_t rx_data; //接收中断缓冲 uint8_t rx_count = 0; //接收缓冲计数 uint32_t uart0_lastreceive_ticket = 0; // int freqhz = 29300; int freqhz = 34450; int dutyus = 4; void ozone_pwm_control_update(); /*********************************************************************************************************************** * =======================================================HOOK======================================================== * ***********************************************************************************************************************/ bool hook_get_ozone_generator_working_flag() { return ozone_pwm_control_hardware_is_enable(); } int hook_get_autoshutdown_timecount() { return g_auto_shutdown_countdown_s; } void onkey(zkey_t* key, zkey_state_t key_state) { if (key == &s_keys[2] /*左3*/ && zks_falling_edge == key_state) { freqhz += 50; ozone_pwm_control_update(); } if (key == &s_keys[0] /*左2*/ && zks_falling_edge == key_state) { freqhz -= 50; ozone_pwm_control_update(); } } /*********************************************************************************************************************** * =================================================INTERNAL_FUNCTION================================================= * ***********************************************************************************************************************/ void UART0_IRQHandler(void) { /** * @brief 接收来自串口的数据并做回显 * */ rx_data = UART_RecByte(UART0); if (!s_rxbuf_is_ready) { if (rx_count < sizeof(rx_buffer) - 1) { rx_buffer[rx_count++] = rx_data; uart0_lastreceive_ticket = get_sys_ticket(); } } UART_ClearITPendingBit(UART0, UART_FLAG_TB); UART_ClearITPendingBit(UART0, UART_FLAG_RB); UART_ClearITPendingBit(UART0, UART_FLAG_FE); } /** * @brief * * @param freq * @param us */ float get_duty_by_freq_and_valid_time(uint32_t freq, uint32_t us) { float period = 1000000.0 / freq; float duty = us * 100 / period; return duty; } void ozone_pwm_control_update() { printf("freqhz:%d duty:%d us\n", freqhz, dutyus); set_pwm_modbul_freq_duty2(freqhz, get_duty_by_freq_and_valid_time(freqhz, dutyus)); } #define DO_IT_EACH_MS(time) \ { \ static uint32_t __ticket = 0; \ if (port_haspassedms(__ticket) > (time)) { \ __ticket = get_sys_ticket(); #define END() \ } \ } /*********************************************************************************************************************** * ======================================================主函数======================================================= ***********************************************************************************************************************/ bool strcontains(const char* sonstr, const char* fatherstr) { return (strncmp(sonstr, fatherstr, strlen(sonstr)) == 0); } void process_rx_order() { if (strcontains("AT+PWM_FREQ_UP_FINE_TUNING", rx_buffer)) { freqhz += 50; ozone_pwm_control_update(); } else if (strcontains("AT+PWM_FREQ_DOWN_FINE_TUNING", rx_buffer)) { freqhz -= 50; if (freqhz <= 0) freqhz = 1; ozone_pwm_control_update(); } else if (strcontains("AT+PWM_FREQ_UP", rx_buffer)) { freqhz += 1000; ozone_pwm_control_update(); } else if (strcontains("AT+PWM_FREQ_DOWN", rx_buffer)) { freqhz -= 1000; if (freqhz <= 0) freqhz = 1; ozone_pwm_control_update(); } else if (strcontains("AT+PWM_DUTY_UP", rx_buffer)) { dutyus += 1; ozone_pwm_control_update(); } else if (strcontains("AT+PWM_DUTY_DOWN", rx_buffer)) { dutyus -= 1; if (dutyus < 0) dutyus = 0; ozone_pwm_control_update(); } else if (strcontains("AT+PWM=", rx_buffer)) { /** * AT+PWM=25000,8 */ int _dutyus = 0; int _freq = 0; sscanf(rx_buffer, "AT+PWM=%d,%d", &_freq, &_dutyus); if (_dutyus < 0 || _freq < 10000) { printf("Error\n"); return; } dutyus = _dutyus; freqhz = _freq; ozone_pwm_control_update(); } else if (strcontains("AT+MAT2=", rx_buffer)) { /** * AT+PWM=25000,8 */ int mat2 = 0; sscanf(rx_buffer, "AT+MAT2=%d", &mat2); set_pwm_mat2(mat2); } else if (strcontains("AT+FAN=", rx_buffer)) { /** * AT+PWM=25000,8 */ int mat2 = 0; sscanf(rx_buffer, "AT+FAN=%d", &mat2); if (mat2 > 0) { port_fan_set(true); } else { port_fan_set(false); } } } void try_process_rx_order() { if (port_haspassedms(uart0_lastreceive_ticket) > 100) { if (rx_count != 0) { s_rxbuf_is_ready = true; process_rx_order(); rx_count = 0; memset(rx_buffer, 0, sizeof(rx_buffer)); s_rxbuf_is_ready = false; } } } float power_filter(float nowpower, float factor) { static float last = 0; float now_raw = nowpower; float now = ((float)last * (1.0f - factor)) + ((float)now_raw * factor); last = now; return now; } uint16_t capture(int timeoff); int main(void) { HRC_Config(Enable, SCU_HRC_48M, Enable); //时钟源SCU_CLK_HRC SystemInit(); DeviceClockAllEnable(); //打开所有外设时钟 User_SysTickInit(); //滴答定时器初始化为(配置为1ms中断) SysTick_Enable(); //硬件初始化 unused_gpio_init(); gpio_init(); uart0_init(); t16_pa4_init(); //模块初始化 zkey_init(&key_module); //上电默认开机并高档工作 port_fan_set(true); //启动结束 printf("Initialization completed \r\n"); printf("version:%s\r\n", VERSION); ozone_pwm_control_update(); // stop_pwm_output(); while (true) { /******************************************************************************************************************* * ==================================================调试指示灯=================================================== * *******************************************************************************************************************/ DO_IT_EACH_MS(200) { static uint8_t debug_led_state = 1; debug_led_state = !debug_led_state; port_debug_set(debug_led_state); port_led_r_set(debug_led_state); port_led_g_set(debug_led_state); port_led_b_set(debug_led_state); } END() static float fan_w = 0; static float ozone_w = 0; DO_IT_EACH_MS(1 * 5) { // float pa2_now = adc_get_value_pa2(); float pb8_now = adc_get_value_pb8(); float pa2 = adc_get_value_pa2_filter(0.005); float pb8 = adc_get_value_pb8_filter(0.005); fan_w = 12 * pa2 / 1; //功率 ozone_w = 12 * pb8 / 0.5; //功率 } END() DO_IT_EACH_MS(3000) { // printf("{plotter:%f %f}\r\n", fan_w, ozone_w); } END() try_process_rx_order(); /*********************************************************************************************************************** * =================================================按键模块调度代码================================================== * ***********************************************************************************************************************/ DO_IT_EACH_MS(KEY_PERIOD) { zkey_do_loop_in_each_period(NULL); } END(); ozone_pwm_control_module_loop(); // DO_IT_EACH_MS(5 * 1000) { // for (size_t i = 0; i < 95; i++) { // uint16_t now = capture(i); // printf("{p:%f}\r\n", now*1.0); // } // for (size_t i = 0; i < 100; i++) { // printf("{p:2.0}\r\n"); // } // } // END(); #if 0 DO_IT_EACH_MS(1) { static int count = 0; count++; if (count % 25 == 12) { set_pwm_modbul_freq_duty2(freqhz / 2, 0); } else if (count % 25 == 0) { ozone_pwm_control_update(); } } END(); #endif // End.................................. } }