You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

268 lines
9.8 KiB

4 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
4 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. #include "main.h"
  2. #include <stdbool.h> //定义布尔
  3. #include <string.h>
  4. #include "adc.h"
  5. #include "board.h"
  6. #define PWM_FREQHZ 25000 //硬件pwm频率
  7. /***********************************************************************************************************************
  8. * =========================================================================================================== *
  9. ***********************************************************************************************************************/
  10. void onkey(zkey_t* key, zkey_state_t key_state){}
  11. /***********************************************************************************************************************
  12. * =========================================================================================================== *
  13. ***********************************************************************************************************************/
  14. static zkey_t s_keys[] = {
  15. ZKEY_INIT("timerkey", port_gpio_get_timer_key_state),
  16. ZKEY_INIT("gearskey", port_gpio_get_gears_key_state),
  17. ZKEY_INIT("intervalkey", port_gpio_get_interval_key_state),
  18. ZKEY_INIT("switchkey", port_gpio_get_switch_key_state),
  19. };
  20. zkey_module_t key_module = ZMODULE_INIT(s_keys, onkey);
  21. extern uint32_t target_frequencyhz;
  22. extern uint32_t target_duty;
  23. /***********************************************************************************************************************
  24. * =========================================================================================================== *
  25. ***********************************************************************************************************************/
  26. static bool error_even_trigger_after_stop_ozone_work_state; //错误事件触发后停止臭氧工作标志位
  27. // 设置
  28. bool g_setting_interval_work_flag = false; //现在间歇工作状态
  29. work_level_t g_setting_level = kwork_level_hight; //关机前的档位
  30. //标志位
  31. bool g_power_flag = false; //开关机标志位
  32. static int g_error_num;
  33. bool g_auto_shutdown_flag = false; //定时功能使能标志位
  34. static uint32_t g_auto_shutdown_countdown_s; //定时时间
  35. bool s_rxbuf_is_ready = false;
  36. char rx_buffer[50]; //接收数据
  37. uint8_t rx_data; //接收中断缓冲
  38. uint8_t rx_count = 0; //接收缓冲计数
  39. uint32_t uart0_lastreceive_ticket = 0;
  40. /***********************************************************************************************************************
  41. * =======================================================HOOK======================================================== *
  42. ***********************************************************************************************************************/
  43. bool hook_get_ozone_generator_working_flag() { return ozone_pwm_control_hardware_is_enable(); }
  44. int hook_get_autoshutdown_timecount() { return g_auto_shutdown_countdown_s; }
  45. /***********************************************************************************************************************
  46. * =================================================INTERNAL_FUNCTION================================================= *
  47. ***********************************************************************************************************************/
  48. void UART0_IRQHandler(void) {
  49. /**
  50. * @brief
  51. *
  52. */
  53. rx_data = UART_RecByte(UART0);
  54. if (!s_rxbuf_is_ready) {
  55. if (rx_count < sizeof(rx_buffer) - 1) {
  56. rx_buffer[rx_count++] = rx_data;
  57. uart0_lastreceive_ticket = get_sys_ticket();
  58. }
  59. }
  60. UART_ClearITPendingBit(UART0, UART_FLAG_TB);
  61. UART_ClearITPendingBit(UART0, UART_FLAG_RB);
  62. UART_ClearITPendingBit(UART0, UART_FLAG_FE);
  63. }
  64. /**
  65. * @brief
  66. *
  67. * @param freq
  68. * @param us
  69. */
  70. float get_duty_by_freq_and_valid_time(uint32_t freq, uint32_t us) {
  71. float period = 1000000.0 / freq;
  72. float duty = us * 100 / period;
  73. return duty;
  74. }
  75. //int freqhz = 29300;
  76. int freqhz = 29200;
  77. int dutyus = 6;
  78. void ozone_pwm_control_update() {
  79. printf("freqhz:%d duty:%d us\n", freqhz, dutyus);
  80. set_pwm_modbul_freq_duty2(freqhz, get_duty_by_freq_and_valid_time(freqhz, dutyus));
  81. }
  82. #define DO_IT_EACH_MS(time) \
  83. { \
  84. static uint32_t __ticket = 0; \
  85. if (port_haspassedms(__ticket) > (time)) { \
  86. __ticket = get_sys_ticket();
  87. #define END() \
  88. } \
  89. }
  90. /***********************************************************************************************************************
  91. * =============================================================================================================
  92. ***********************************************************************************************************************/
  93. bool strcontains(const char* sonstr, const char* fatherstr) { return (strncmp(sonstr, fatherstr, strlen(sonstr)) == 0); }
  94. void process_rx_order() {
  95. if (strcontains("AT+PWM_FREQ_UP_FINE_TUNING", rx_buffer)) {
  96. freqhz += 50;
  97. ozone_pwm_control_update();
  98. } else if (strcontains("AT+PWM_FREQ_DOWN_FINE_TUNING", rx_buffer)) {
  99. freqhz -= 50;
  100. if (freqhz <= 0) freqhz = 1;
  101. ozone_pwm_control_update();
  102. } else if (strcontains("AT+PWM_FREQ_UP", rx_buffer)) {
  103. freqhz += 1000;
  104. ozone_pwm_control_update();
  105. } else if (strcontains("AT+PWM_FREQ_DOWN", rx_buffer)) {
  106. freqhz -= 1000;
  107. if (freqhz <= 0) freqhz = 1;
  108. ozone_pwm_control_update();
  109. } else if (strcontains("AT+PWM_DUTY_UP", rx_buffer)) {
  110. dutyus += 1;
  111. ozone_pwm_control_update();
  112. } else if (strcontains("AT+PWM_DUTY_DOWN", rx_buffer)) {
  113. dutyus -= 1;
  114. if (dutyus < 0) dutyus = 0;
  115. ozone_pwm_control_update();
  116. } else if (strcontains("AT+PWM=", rx_buffer)) {
  117. /**
  118. * AT+PWM=25000,8
  119. */
  120. int _dutyus = 0;
  121. int _freq = 0;
  122. sscanf(rx_buffer, "AT+PWM=%d,%d", &_freq, &_dutyus);
  123. if (_dutyus < 0 || _freq < 10000) {
  124. printf("Error\n");
  125. return;
  126. }
  127. dutyus = _dutyus;
  128. freqhz = _freq;
  129. ozone_pwm_control_update();
  130. } else if (strcontains("AT+MAT2=", rx_buffer)) {
  131. /**
  132. * AT+PWM=25000,8
  133. */
  134. int mat2 = 0;
  135. sscanf(rx_buffer, "AT+MAT2=%d", &mat2);
  136. set_pwm_mat2(mat2);
  137. } else if (strcontains("AT+FAN=", rx_buffer)) {
  138. /**
  139. * AT+PWM=25000,8
  140. */
  141. int mat2 = 0;
  142. sscanf(rx_buffer, "AT+FAN=%d", &mat2);
  143. if (mat2 > 0) {
  144. port_fan_set(true);
  145. } else {
  146. port_fan_set(false);
  147. }
  148. }
  149. }
  150. void try_process_rx_order() {
  151. if (port_haspassedms(uart0_lastreceive_ticket) > 100) {
  152. if (rx_count != 0) {
  153. s_rxbuf_is_ready = true;
  154. process_rx_order();
  155. rx_count = 0;
  156. memset(rx_buffer, 0, sizeof(rx_buffer));
  157. s_rxbuf_is_ready = false;
  158. }
  159. }
  160. }
  161. float power_filter(float nowpower, float factor) {
  162. static float last = 0;
  163. float now_raw = nowpower;
  164. float now = ((float)last * (1.0f - factor)) + ((float)now_raw * factor);
  165. last = now;
  166. return now;
  167. }
  168. int main(void) {
  169. HRC_Config(Enable, SCU_HRC_48M, Enable); //时钟源SCU_CLK_HRC
  170. SystemInit();
  171. DeviceClockAllEnable(); //打开所有外设时钟
  172. User_SysTickInit(); //滴答定时器初始化为(配置为1ms中断)
  173. SysTick_Enable();
  174. //硬件初始化
  175. unused_gpio_init();
  176. gpio_init();
  177. uart0_init();
  178. t16_pa4_init();
  179. //模块初始化
  180. zkey_init(&key_module);
  181. //上电默认开机并高档工作
  182. port_fan_set(true);
  183. //启动结束
  184. printf("Initialization completed \r\n");
  185. printf("version:%s\r\n", VERSION);
  186. ozone_pwm_control_update();
  187. // stop_pwm_output();
  188. while (true) {
  189. /*******************************************************************************************************************
  190. * ===================================================================================================== *
  191. *******************************************************************************************************************/
  192. DO_IT_EACH_MS(200) {
  193. static uint8_t debug_led_state = 1;
  194. debug_led_state = !debug_led_state;
  195. port_debug_set(debug_led_state);
  196. port_led_r_set(debug_led_state);
  197. port_led_g_set(debug_led_state);
  198. port_led_b_set(debug_led_state);
  199. }
  200. END()
  201. // DO_IT_EACH_MS(1 * 5) { //
  202. // float pa2_now = adc_get_value_pa2();
  203. // float pb8_now = adc_get_value_pb8();
  204. // float pb9_now = adc_get_value_pb9();
  205. // float pa2 = adc_get_value_pa2_filter(0.01);
  206. // float pb8 = adc_get_value_pb8_filter(0.01);
  207. // float pb9 = adc_get_value_pb9_filter(0.001);
  208. // static int count = 0;
  209. // static float sumpower = 0;
  210. // float device_total_power1 = (pb9) / 0.2 /*采样电阻*/ * 12;
  211. // printf("{plotter:%f}\r\n", device_total_power1);
  212. // // sumpower += device_total_power1;
  213. // // count++;
  214. // // if (count >= 500) {
  215. // // count = 0;
  216. // // sumpower = 0;
  217. // // }
  218. // // printf("{plotter:%f}\r\n", power_filter(device_total_power, 0.03));
  219. // // float device_total_power2 = (pb8 * 6) / 0.5 /*采样电阻*/ * 12;
  220. // // printf("{plotter:%f}\r\n", device_total_power1);
  221. // }
  222. // END()
  223. try_process_rx_order();
  224. /***********************************************************************************************************************
  225. * =================================================================================================== *
  226. ***********************************************************************************************************************/
  227. DO_IT_EACH_MS(KEY_PERIOD) { zkey_do_loop_in_each_period(NULL); }
  228. END();
  229. ozone_pwm_control_module_loop();
  230. // End..................................
  231. }
  232. }