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.

205 lines
6.6 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
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
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. #if 0
  7. #define PWM_FREQHZ 25000 //硬件pwm频率
  8. /***********************************************************************************************************************
  9. * =========================================================================================================== *
  10. ***********************************************************************************************************************/
  11. void onkey(zkey_t* key, zkey_state_t key_state);
  12. /***********************************************************************************************************************
  13. * =========================================================================================================== *
  14. ***********************************************************************************************************************/
  15. static zkey_t s_keys[] = {
  16. ZKEY_INIT("timerkey", port_gpio_get_timer_key_state), //左2
  17. ZKEY_INIT("levelkey", port_gpio_get_level_key_state), //左1
  18. ZKEY_INIT("intervalkey", port_gpio_get_interval_key_state), //左3
  19. ZKEY_INIT("powerkey", port_gpio_get_power_key_state),
  20. };
  21. zkey_module_t key_module = ZMODULE_INIT(s_keys, onkey);
  22. void onkey(zkey_t* key, zkey_state_t key_state) {
  23. if (key == &s_keys[2] /*左3*/ && zks_falling_edge == key_state) {
  24. ozone_pwm_control_update();
  25. printf("onkey2\n");
  26. }
  27. if (key == &s_keys[0] /*左2*/ && zks_falling_edge == key_state) {
  28. ozone_pwm_control_update();
  29. printf("onkey0\n");
  30. }
  31. if (key == &s_keys[1] /*左2*/ && zks_falling_edge == key_state) {
  32. }
  33. if (key == &s_keys[3] /*左2*/ && zks_falling_edge == key_state) {
  34. printf("onkey3\n");
  35. ozone_pwm_control_update();
  36. }
  37. }
  38. /***********************************************************************************************************************
  39. * =================================================INTERNAL_FUNCTION================================================= *
  40. ***********************************************************************************************************************/
  41. void UART0_IRQHandler(void) {
  42. /**
  43. * @brief
  44. *
  45. */
  46. // rx_data = UART_RecByte(UART0);
  47. // if (!s_rxbuf_is_ready) {
  48. // if (rx_count < sizeof(rx_buffer) - 1) {
  49. // rx_buffer[rx_count++] = rx_data;
  50. // uart0_lastreceive_ticket = get_sys_ticket();
  51. // }
  52. // }
  53. UART_ClearITPendingBit(UART0, UART_FLAG_TB);
  54. UART_ClearITPendingBit(UART0, UART_FLAG_RB);
  55. UART_ClearITPendingBit(UART0, UART_FLAG_FE);
  56. }
  57. /**
  58. * @brief
  59. *
  60. * @param freq
  61. * @param us
  62. */
  63. float get_duty_by_freq_and_valid_time(uint32_t freq, uint32_t us) {
  64. float period = 1000000.0 / freq;
  65. float duty = us * 100 / period;
  66. return duty;
  67. }
  68. void ozone_pwm_control_update() {
  69. printf("freqhz:%d duty:%d us\n", freqhz, dutyus);
  70. set_pwm_modbul_freq_duty2(freqhz, get_duty_by_freq_and_valid_time(freqhz, dutyus));
  71. }
  72. #define DO_IT_EACH_MS(time) \
  73. { \
  74. static uint32_t __ticket = 0; \
  75. if (port_haspassedms(__ticket) > (time)) { \
  76. __ticket = get_sys_ticket();
  77. #define END() \
  78. } \
  79. }
  80. /***********************************************************************************************************************
  81. * =============================================================================================================
  82. ***********************************************************************************************************************/
  83. bool strcontains(const char* sonstr, const char* fatherstr) { return (strncmp(sonstr, fatherstr, strlen(sonstr)) == 0); }
  84. void process_rx_order() {
  85. if (strcontains("AT+PWM_FREQ_UP_FINE_TUNING", rx_buffer)) {
  86. freqhz += 50;
  87. ozone_pwm_control_update();
  88. } else if (strcontains("AT+PWM_FREQ_DOWN_FINE_TUNING", rx_buffer)) {
  89. freqhz -= 50;
  90. if (freqhz <= 0) freqhz = 1;
  91. ozone_pwm_control_update();
  92. } else if (strcontains("AT+PWM_FREQ_UP", rx_buffer)) {
  93. freqhz += 1000;
  94. ozone_pwm_control_update();
  95. } else if (strcontains("AT+PWM_FREQ_DOWN", rx_buffer)) {
  96. freqhz -= 1000;
  97. if (freqhz <= 0) freqhz = 1;
  98. ozone_pwm_control_update();
  99. } else if (strcontains("AT+PWM_DUTY_UP", rx_buffer)) {
  100. dutyus += 1;
  101. ozone_pwm_control_update();
  102. } else if (strcontains("AT+PWM_DUTY_DOWN", rx_buffer)) {
  103. dutyus -= 1;
  104. if (dutyus < 0) dutyus = 0;
  105. ozone_pwm_control_update();
  106. } else if (strcontains("AT+PWM=", rx_buffer)) {
  107. /**
  108. * AT+PWM=25000,8
  109. */
  110. int _dutyus = 0;
  111. int _freq = 0;
  112. sscanf(rx_buffer, "AT+PWM=%d,%d", &_freq, &_dutyus);
  113. if (_dutyus < 0 || _freq < 10000) {
  114. printf("Error\n");
  115. return;
  116. }
  117. dutyus = _dutyus;
  118. freqhz = _freq;
  119. ozone_pwm_control_update();
  120. } else if (strcontains("AT+MAT2=", rx_buffer)) {
  121. /**
  122. * AT+PWM=25000,8
  123. */
  124. int mat2 = 0;
  125. sscanf(rx_buffer, "AT+MAT2=%d", &mat2);
  126. set_pwm_mat2(mat2);
  127. } else if (strcontains("AT+FAN=", rx_buffer)) {
  128. /**
  129. * AT+PWM=25000,8
  130. */
  131. int mat2 = 0;
  132. sscanf(rx_buffer, "AT+FAN=%d", &mat2);
  133. if (mat2 > 0) {
  134. port_fan_set(true);
  135. } else {
  136. port_fan_set(false);
  137. }
  138. }
  139. }
  140. void try_process_rx_order() {
  141. if (port_haspassedms(uart0_lastreceive_ticket) > 100) {
  142. if (rx_count != 0) {
  143. s_rxbuf_is_ready = true;
  144. process_rx_order();
  145. rx_count = 0;
  146. memset(rx_buffer, 0, sizeof(rx_buffer));
  147. s_rxbuf_is_ready = false;
  148. }
  149. }
  150. }
  151. float power_filter(float nowpower, float factor) {
  152. static float last = 0;
  153. float now_raw = nowpower;
  154. float now = ((float)last * (1.0f - factor)) + ((float)now_raw * factor);
  155. last = now;
  156. return now;
  157. }
  158. uint16_t capture(int timeoff);
  159. #endif
  160. int main(void) {
  161. HRC_Config(Enable, SCU_HRC_48M, Enable); //时钟源SCU_CLK_HRC
  162. SystemInit(); //
  163. DeviceClockAllEnable(); //打开所有外设时钟
  164. #if 0
  165. //硬件初始化
  166. unused_gpio_init();
  167. gpio_init();
  168. uart0_init();
  169. t16_pa4_init();
  170. //模块初始化
  171. zkey_init(&key_module);
  172. //上电默认开机并高档工作
  173. port_fan_set(true);
  174. //启动结束
  175. printf("Initialization completed \r\n");
  176. printf("version:%s\r\n", VERSION);
  177. ozone_pwm_control_update();
  178. // stop_pwm_output();
  179. while (true) {
  180. // End..................................
  181. }
  182. #endif
  183. }