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.

297 lines
10 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
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
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), //左2
  16. ZKEY_INIT("gearskey", port_gpio_get_gears_key_state), //左1
  17. ZKEY_INIT("intervalkey", port_gpio_get_interval_key_state), //左3
  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. // int freqhz = 29300;
  41. int freqhz = 34450;
  42. int dutyus = 4;
  43. void ozone_pwm_control_update();
  44. /***********************************************************************************************************************
  45. * =======================================================HOOK======================================================== *
  46. ***********************************************************************************************************************/
  47. bool hook_get_ozone_generator_working_flag() { return ozone_pwm_control_hardware_is_enable(); }
  48. int hook_get_autoshutdown_timecount() { return g_auto_shutdown_countdown_s; }
  49. void onkey(zkey_t* key, zkey_state_t key_state) {
  50. if (key == &s_keys[2] /*左3*/ && zks_falling_edge == key_state) {
  51. freqhz += 50;
  52. ozone_pwm_control_update();
  53. }
  54. if (key == &s_keys[0] /*左2*/ && zks_falling_edge == key_state) {
  55. freqhz -= 50;
  56. ozone_pwm_control_update();
  57. }
  58. }
  59. /***********************************************************************************************************************
  60. * =================================================INTERNAL_FUNCTION================================================= *
  61. ***********************************************************************************************************************/
  62. void UART0_IRQHandler(void) {
  63. /**
  64. * @brief
  65. *
  66. */
  67. rx_data = UART_RecByte(UART0);
  68. if (!s_rxbuf_is_ready) {
  69. if (rx_count < sizeof(rx_buffer) - 1) {
  70. rx_buffer[rx_count++] = rx_data;
  71. uart0_lastreceive_ticket = get_sys_ticket();
  72. }
  73. }
  74. UART_ClearITPendingBit(UART0, UART_FLAG_TB);
  75. UART_ClearITPendingBit(UART0, UART_FLAG_RB);
  76. UART_ClearITPendingBit(UART0, UART_FLAG_FE);
  77. }
  78. /**
  79. * @brief
  80. *
  81. * @param freq
  82. * @param us
  83. */
  84. float get_duty_by_freq_and_valid_time(uint32_t freq, uint32_t us) {
  85. float period = 1000000.0 / freq;
  86. float duty = us * 100 / period;
  87. return duty;
  88. }
  89. void ozone_pwm_control_update() {
  90. printf("freqhz:%d duty:%d us\n", freqhz, dutyus);
  91. set_pwm_modbul_freq_duty2(freqhz, get_duty_by_freq_and_valid_time(freqhz, dutyus));
  92. }
  93. #define DO_IT_EACH_MS(time) \
  94. { \
  95. static uint32_t __ticket = 0; \
  96. if (port_haspassedms(__ticket) > (time)) { \
  97. __ticket = get_sys_ticket();
  98. #define END() \
  99. } \
  100. }
  101. /***********************************************************************************************************************
  102. * =============================================================================================================
  103. ***********************************************************************************************************************/
  104. bool strcontains(const char* sonstr, const char* fatherstr) { return (strncmp(sonstr, fatherstr, strlen(sonstr)) == 0); }
  105. void process_rx_order() {
  106. if (strcontains("AT+PWM_FREQ_UP_FINE_TUNING", rx_buffer)) {
  107. freqhz += 50;
  108. ozone_pwm_control_update();
  109. } else if (strcontains("AT+PWM_FREQ_DOWN_FINE_TUNING", rx_buffer)) {
  110. freqhz -= 50;
  111. if (freqhz <= 0) freqhz = 1;
  112. ozone_pwm_control_update();
  113. } else if (strcontains("AT+PWM_FREQ_UP", rx_buffer)) {
  114. freqhz += 1000;
  115. ozone_pwm_control_update();
  116. } else if (strcontains("AT+PWM_FREQ_DOWN", rx_buffer)) {
  117. freqhz -= 1000;
  118. if (freqhz <= 0) freqhz = 1;
  119. ozone_pwm_control_update();
  120. } else if (strcontains("AT+PWM_DUTY_UP", rx_buffer)) {
  121. dutyus += 1;
  122. ozone_pwm_control_update();
  123. } else if (strcontains("AT+PWM_DUTY_DOWN", rx_buffer)) {
  124. dutyus -= 1;
  125. if (dutyus < 0) dutyus = 0;
  126. ozone_pwm_control_update();
  127. } else if (strcontains("AT+PWM=", rx_buffer)) {
  128. /**
  129. * AT+PWM=25000,8
  130. */
  131. int _dutyus = 0;
  132. int _freq = 0;
  133. sscanf(rx_buffer, "AT+PWM=%d,%d", &_freq, &_dutyus);
  134. if (_dutyus < 0 || _freq < 10000) {
  135. printf("Error\n");
  136. return;
  137. }
  138. dutyus = _dutyus;
  139. freqhz = _freq;
  140. ozone_pwm_control_update();
  141. } else if (strcontains("AT+MAT2=", rx_buffer)) {
  142. /**
  143. * AT+PWM=25000,8
  144. */
  145. int mat2 = 0;
  146. sscanf(rx_buffer, "AT+MAT2=%d", &mat2);
  147. set_pwm_mat2(mat2);
  148. } else if (strcontains("AT+FAN=", rx_buffer)) {
  149. /**
  150. * AT+PWM=25000,8
  151. */
  152. int mat2 = 0;
  153. sscanf(rx_buffer, "AT+FAN=%d", &mat2);
  154. if (mat2 > 0) {
  155. port_fan_set(true);
  156. } else {
  157. port_fan_set(false);
  158. }
  159. }
  160. }
  161. void try_process_rx_order() {
  162. if (port_haspassedms(uart0_lastreceive_ticket) > 100) {
  163. if (rx_count != 0) {
  164. s_rxbuf_is_ready = true;
  165. process_rx_order();
  166. rx_count = 0;
  167. memset(rx_buffer, 0, sizeof(rx_buffer));
  168. s_rxbuf_is_ready = false;
  169. }
  170. }
  171. }
  172. float power_filter(float nowpower, float factor) {
  173. static float last = 0;
  174. float now_raw = nowpower;
  175. float now = ((float)last * (1.0f - factor)) + ((float)now_raw * factor);
  176. last = now;
  177. return now;
  178. }
  179. uint16_t capture(int timeoff);
  180. int main(void) {
  181. HRC_Config(Enable, SCU_HRC_48M, Enable); //时钟源SCU_CLK_HRC
  182. SystemInit();
  183. DeviceClockAllEnable(); //打开所有外设时钟
  184. User_SysTickInit(); //滴答定时器初始化为(配置为1ms中断)
  185. SysTick_Enable();
  186. //硬件初始化
  187. unused_gpio_init();
  188. gpio_init();
  189. uart0_init();
  190. t16_pa4_init();
  191. //模块初始化
  192. zkey_init(&key_module);
  193. //上电默认开机并高档工作
  194. port_fan_set(true);
  195. //启动结束
  196. printf("Initialization completed \r\n");
  197. printf("version:%s\r\n", VERSION);
  198. ozone_pwm_control_update();
  199. // stop_pwm_output();
  200. while (true) {
  201. /*******************************************************************************************************************
  202. * ===================================================================================================== *
  203. *******************************************************************************************************************/
  204. DO_IT_EACH_MS(200) {
  205. static uint8_t debug_led_state = 1;
  206. debug_led_state = !debug_led_state;
  207. port_debug_set(debug_led_state);
  208. port_led_r_set(debug_led_state);
  209. port_led_g_set(debug_led_state);
  210. port_led_b_set(debug_led_state);
  211. }
  212. END()
  213. static float fan_w = 0;
  214. static float ozone_w = 0;
  215. DO_IT_EACH_MS(1 * 5) { //
  216. float pa2_now = adc_get_value_pa2();
  217. float pb8_now = adc_get_value_pb8();
  218. float pa2 = adc_get_value_pa2_filter(0.005);
  219. float pb8 = adc_get_value_pb8_filter(0.005);
  220. fan_w = 12 * pa2 / 1; //功率
  221. ozone_w = 12 * pb8 / 0.5; //功率
  222. }
  223. END()
  224. DO_IT_EACH_MS(3000) { //
  225. printf("{plotter:%f %f}\r\n", fan_w, ozone_w);
  226. }
  227. END()
  228. try_process_rx_order();
  229. /***********************************************************************************************************************
  230. * =================================================================================================== *
  231. ***********************************************************************************************************************/
  232. DO_IT_EACH_MS(KEY_PERIOD) { zkey_do_loop_in_each_period(NULL); }
  233. END();
  234. ozone_pwm_control_module_loop();
  235. // DO_IT_EACH_MS(5 * 1000) {
  236. // for (size_t i = 0; i < 95; i++) {
  237. // uint16_t now = capture(i);
  238. // printf("{p:%f}\r\n", now*1.0);
  239. // }
  240. // for (size_t i = 0; i < 100; i++) {
  241. // printf("{p:2.0}\r\n");
  242. // }
  243. // }
  244. // END();
  245. #if 0
  246. DO_IT_EACH_MS(1) {
  247. static int count = 0;
  248. count++;
  249. if (count % 25 == 12) {
  250. set_pwm_modbul_freq_duty2(freqhz / 2, 0);
  251. } else if (count % 25 == 0) {
  252. ozone_pwm_control_update();
  253. }
  254. }
  255. END();
  256. #endif
  257. // End..................................
  258. }
  259. }