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.

293 lines
8.9 KiB

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