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
8.9 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
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
  1. #include "main.h"
  2. #include <stdbool.h> //定义布尔
  3. #include <string.h>
  4. #include "board.h"
  5. //
  6. #include "../zes8p5066lib/basic.h"
  7. #include "../zes8p5066lib/gpio.h"
  8. #include "../zes8p5066lib/key.h"
  9. #include "../zes8p5066lib/systicket.h"
  10. #include "../zes8p5066lib/uart0.h"
  11. #include "test.h"
  12. #if 0
  13. #define PWM_FREQHZ 25000 //硬件pwm频率
  14. /***********************************************************************************************************************
  15. * =========================================================================================================== *
  16. ***********************************************************************************************************************/
  17. void onkey(zkey_t* key, zkey_state_t key_state);
  18. /***********************************************************************************************************************
  19. * =========================================================================================================== *
  20. ***********************************************************************************************************************/
  21. void onkey(zkey_t* key, zkey_state_t key_state) {
  22. if (key == &s_keys[2] /*左3*/ && zks_falling_edge == key_state) {
  23. ozone_pwm_control_update();
  24. printf("onkey2\n");
  25. }
  26. if (key == &s_keys[0] /*左2*/ && zks_falling_edge == key_state) {
  27. ozone_pwm_control_update();
  28. printf("onkey0\n");
  29. }
  30. if (key == &s_keys[1] /*左2*/ && zks_falling_edge == key_state) {
  31. }
  32. if (key == &s_keys[3] /*左2*/ && zks_falling_edge == key_state) {
  33. printf("onkey3\n");
  34. ozone_pwm_control_update();
  35. }
  36. }
  37. /***********************************************************************************************************************
  38. * =================================================INTERNAL_FUNCTION================================================= *
  39. ***********************************************************************************************************************/
  40. void UART0_IRQHandler(void) {
  41. /**
  42. * @brief
  43. *
  44. */
  45. // rx_data = UART_RecByte(UART0);
  46. // if (!s_rxbuf_is_ready) {
  47. // if (rx_count < sizeof(rx_buffer) - 1) {
  48. // rx_buffer[rx_count++] = rx_data;
  49. // uart0_lastreceive_ticket = get_sys_ticket();
  50. // }
  51. // }
  52. UART_ClearITPendingBit(UART0, UART_FLAG_TB);
  53. UART_ClearITPendingBit(UART0, UART_FLAG_RB);
  54. UART_ClearITPendingBit(UART0, UART_FLAG_FE);
  55. }
  56. /**
  57. * @brief
  58. *
  59. * @param freq
  60. * @param us
  61. */
  62. float get_duty_by_freq_and_valid_time(uint32_t freq, uint32_t us) {
  63. float period = 1000000.0 / freq;
  64. float duty = us * 100 / period;
  65. return duty;
  66. }
  67. void ozone_pwm_control_update() {
  68. printf("freqhz:%d duty:%d us\n", freqhz, dutyus);
  69. set_pwm_modbul_freq_duty2(freqhz, get_duty_by_freq_and_valid_time(freqhz, dutyus));
  70. }
  71. #define DO_IT_EACH_MS(time) \
  72. { \
  73. static uint32_t __ticket = 0; \
  74. if (port_haspassedms(__ticket) > (time)) { \
  75. __ticket = get_sys_ticket();
  76. #define END() \
  77. } \
  78. }
  79. /***********************************************************************************************************************
  80. * =============================================================================================================
  81. ***********************************************************************************************************************/
  82. bool strcontains(const char* sonstr, const char* fatherstr) { return (strncmp(sonstr, fatherstr, strlen(sonstr)) == 0); }
  83. void process_rx_order() {
  84. if (strcontains("AT+PWM_FREQ_UP_FINE_TUNING", rx_buffer)) {
  85. freqhz += 50;
  86. ozone_pwm_control_update();
  87. } else if (strcontains("AT+PWM_FREQ_DOWN_FINE_TUNING", rx_buffer)) {
  88. freqhz -= 50;
  89. if (freqhz <= 0) freqhz = 1;
  90. ozone_pwm_control_update();
  91. } else if (strcontains("AT+PWM_FREQ_UP", rx_buffer)) {
  92. freqhz += 1000;
  93. ozone_pwm_control_update();
  94. } else if (strcontains("AT+PWM_FREQ_DOWN", rx_buffer)) {
  95. freqhz -= 1000;
  96. if (freqhz <= 0) freqhz = 1;
  97. ozone_pwm_control_update();
  98. } else if (strcontains("AT+PWM_DUTY_UP", rx_buffer)) {
  99. dutyus += 1;
  100. ozone_pwm_control_update();
  101. } else if (strcontains("AT+PWM_DUTY_DOWN", rx_buffer)) {
  102. dutyus -= 1;
  103. if (dutyus < 0) dutyus = 0;
  104. ozone_pwm_control_update();
  105. } else if (strcontains("AT+PWM=", rx_buffer)) {
  106. /**
  107. * AT+PWM=25000,8
  108. */
  109. int _dutyus = 0;
  110. int _freq = 0;
  111. sscanf(rx_buffer, "AT+PWM=%d,%d", &_freq, &_dutyus);
  112. if (_dutyus < 0 || _freq < 10000) {
  113. printf("Error\n");
  114. return;
  115. }
  116. dutyus = _dutyus;
  117. freqhz = _freq;
  118. ozone_pwm_control_update();
  119. } else if (strcontains("AT+MAT2=", rx_buffer)) {
  120. /**
  121. * AT+PWM=25000,8
  122. */
  123. int mat2 = 0;
  124. sscanf(rx_buffer, "AT+MAT2=%d", &mat2);
  125. set_pwm_mat2(mat2);
  126. } else if (strcontains("AT+FAN=", rx_buffer)) {
  127. /**
  128. * AT+PWM=25000,8
  129. */
  130. int mat2 = 0;
  131. sscanf(rx_buffer, "AT+FAN=%d", &mat2);
  132. if (mat2 > 0) {
  133. port_fan_set(true);
  134. } else {
  135. port_fan_set(false);
  136. }
  137. }
  138. }
  139. void try_process_rx_order() {
  140. if (port_haspassedms(uart0_lastreceive_ticket) > 100) {
  141. if (rx_count != 0) {
  142. s_rxbuf_is_ready = true;
  143. process_rx_order();
  144. rx_count = 0;
  145. memset(rx_buffer, 0, sizeof(rx_buffer));
  146. s_rxbuf_is_ready = false;
  147. }
  148. }
  149. }
  150. float power_filter(float nowpower, float factor) {
  151. static float last = 0;
  152. float now_raw = nowpower;
  153. float now = ((float)last * (1.0f - factor)) + ((float)now_raw * factor);
  154. last = now;
  155. return now;
  156. }
  157. uint16_t capture(int timeoff);
  158. #endif
  159. void onkey(zkey_t* key, zkey_state_t key_state);
  160. int a = 34;
  161. static zkey_t s_keys[] = {
  162. ZKEY_INIT("powerkey", port_gpio_get_power_key_state), //电源按键
  163. ZKEY_INIT("levelkey", port_gpio_get_level_key_state), //左1
  164. ZKEY_INIT("timerkey", port_gpio_get_timer_key_state), //左2
  165. ZKEY_INIT("intervalkey", port_gpio_get_interval_key_state), //左3
  166. };
  167. zkey_module_t key_module = ZMODULE_INIT(s_keys, onkey);
  168. void onkey(zkey_t* key, zkey_state_t key_state) {
  169. if /* */ (strcmp(key->name, "powerkey") == 0 && zks_rising_edge == key_state) {
  170. printf("on %s \n", key->name);
  171. } else if (strcmp(key->name, "levelkey") == 0 && zks_rising_edge == key_state) {
  172. printf("on %s \n", key->name);
  173. } else if (strcmp(key->name, "timerkey") == 0 && zks_rising_edge == key_state) {
  174. printf("on %s \n", key->name);
  175. } else if (strcmp(key->name, "intervalkey") == 0 && zks_rising_edge == key_state) {
  176. printf("on %s \n", key->name);
  177. }
  178. }
  179. void systicket_do_debug_light_state(void) {
  180. static uint32_t lastprocess = 0;
  181. static uint8_t debug_led_state = 1;
  182. if (systicket_haspassedms(lastprocess) > 300) {
  183. lastprocess = systicket_get_now_ms();
  184. debug_led_state = !debug_led_state;
  185. port_debug_set(debug_led_state);
  186. }
  187. }
  188. int main(void) {
  189. SystemInit(); //配置系统时钟
  190. DeviceClockAllEnable(); //打开所有外设时钟
  191. systicket_init();
  192. //
  193. zgpio_init_all_gpio();
  194. port_init();
  195. printf("==========OZONE_GENERATOR==========\n");
  196. printf("= manufactor: iflytop\n");
  197. printf("= version : %s\n", VERSION);
  198. printf("=\n");
  199. /**
  200. * @brief
  201. */
  202. zkey_init(&key_module);
  203. /**
  204. * @brief
  205. */
  206. port_debug_set(false);
  207. port_fan_set(false);
  208. port_led0_set(false);
  209. port_led1_set(false);
  210. port_led2_set(false);
  211. port_led3_set(false);
  212. port_led_r_set(false);
  213. port_led_g_set(false);
  214. port_led_b_set(false);
  215. //
  216. port_fan_set(true);
  217. while (true) {
  218. //调试指示灯逻辑
  219. systicket_do_debug_light_state();
  220. //按键扫描逻辑
  221. DO_IT_EACH_MS(KEY_PERIOD) { zkey_do_loop_in_each_period(NULL); }
  222. END();
  223. test_all_light();
  224. #if 1 //打印功率
  225. DO_IT_EACH_MS(300) { //
  226. // printf("v :fan:%f,ozone:%f\n", port_adc_get_fan_voltage(), port_adc_get_ozone_generator_voltage());
  227. printf("power:fan:%f,ozone:%f\n", port_adc_get_fan_power(), port_adc_get_ozone_generator_power());
  228. }
  229. END();
  230. #endif
  231. // port_adc_get_fan_voltage
  232. // port_adc_get_ozone_generator_voltage
  233. // test_all_light();
  234. }
  235. /**
  236. * @brief
  237. */
  238. // keil勾选Use MicroLIB 2、IAR/keil #define __PRINTF_USE_UART0__
  239. #if 0
  240. //硬件初始化
  241. unused_gpio_init();
  242. gpio_init();
  243. uart0_init();
  244. t16_pa4_init();
  245. //模块初始化
  246. zkey_init(&key_module);
  247. //上电默认开机并高档工作
  248. port_fan_set(true);
  249. //启动结束
  250. printf("Initialization completed \r\n");
  251. printf("version:%s\r\n", VERSION);
  252. ozone_pwm_control_update();
  253. // stop_pwm_output();
  254. while (true) {
  255. // End..................................
  256. }
  257. #endif
  258. }