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.

152 lines
4.1 KiB

3 years ago
  1. #include "pwm.h"
  2. #define TIMING_TICK 1800000
  3. #define INTERVAL_TICK 30000
  4. #define CLCK 48
  5. uint32_t s_hardware_period; //��λms
  6. uint32_t s_hardware_duty; //��λms
  7. /**
  8. * @brief s_large_dutys_large_periodINTERVAL_TIME
  9. * s_large_duty = INTERVAL_TIME/s_large_period
  10. * s_large_period=1800000
  11. * s_large_duty = (30000/1800000)*100
  12. *
  13. */
  14. uint32_t s_large_period; //��λms
  15. uint32_t s_large_duty = 100; //��λms
  16. uint32_t begin_ticket;
  17. uint8_t update_large_period_count;
  18. bool enableFlag = false;
  19. void HOOK_pwm_module_set_pwm_duty(uint32_t frequency, uint32_t duty) {
  20. ozone_pwm_control_set_pwm_duty(frequency, duty);
  21. }
  22. void HOOK_pwm_stop(void) {
  23. HOOK_pwm_module_set_pwm_duty(0, 0); //�ر�PWM����
  24. port_fan_set(false); //���Ƚ�������
  25. }
  26. bool HOOK_pwm_is_enable(void) { return enableFlag; }
  27. void update_large_period(void) {
  28. uint32_t large_period;
  29. update_large_period_count++;
  30. switch (update_large_period_count) {
  31. case 1:
  32. large_period = 1 * TIMING_TICK;
  33. break;
  34. case 2:
  35. large_period = 2 * TIMING_TICK;
  36. break;
  37. case 3:
  38. large_period = 3 * TIMING_TICK;
  39. break;
  40. case 4:
  41. large_period = 4 * TIMING_TICK;
  42. break;
  43. case 5:
  44. update_large_period_count = 0;
  45. large_period = 0 * TIMING_TICK;
  46. break;
  47. }
  48. pwm_module_set_pwm_output_1(
  49. large_period, s_large_duty); //�����º��������Լ����ڵ�ռ�ձȴ���ȥ
  50. }
  51. /**
  52. * @brief һֱʱʱռձΪ100
  53. * Ъʱ300000
  54. * @param freq
  55. * @param duty
  56. */
  57. void pwm_module_set_pwm_output_1(uint32_t large_period, uint32_t large_duty) {
  58. pwm_module_set_pwm_output_2(s_hardware_period, s_hardware_duty, large_period,
  59. large_duty);
  60. }
  61. /**
  62. * @brief ʱPWM
  63. *
  64. * @param hardware_period pwmƵ
  65. * @param hardware_duty pwmռձ
  66. * @param large_period ʱ
  67. * @param large_duty ʱռձ
  68. */
  69. void pwm_module_set_pwm_output_2(uint32_t hardware_period,
  70. uint32_t hardware_duty, uint32_t large_period,
  71. uint32_t large_duty) {
  72. s_hardware_period = hardware_period;
  73. s_hardware_duty = hardware_duty;
  74. s_large_period = large_period;
  75. s_large_duty = large_duty;
  76. begin_ticket = get_sys_ticket();
  77. enableFlag = true;
  78. HOOK_pwm_module_set_pwm_duty(s_hardware_period, s_hardware_duty);
  79. }
  80. void pwm_module_stop_pwm(void) { enableFlag = false; }
  81. void pwm_module_loop(void) {
  82. if (!enableFlag) {
  83. HOOK_pwm_stop();
  84. return;
  85. }
  86. if (port_haspassedms(begin_ticket) % s_large_period <=
  87. s_large_period * s_large_duty / 100) {
  88. if (!HOOK_pwm_is_enable()) {
  89. HOOK_pwm_module_set_pwm_duty(s_hardware_period, s_hardware_duty);
  90. }
  91. } else {
  92. enableFlag = false;
  93. HOOK_pwm_stop();
  94. }
  95. }
  96. //######################################################
  97. double calculate_top(double target_frequency) {
  98. int clck = 0;
  99. int target_frequency_hz = 0;
  100. int top = 0;
  101. clck = CLCK * 1000 * 1000;
  102. target_frequency_hz = (int)target_frequency * 1000;
  103. // printf("target_frequency_hz%d\n", target_frequency_hz);
  104. top = clck / target_frequency_hz;
  105. // printf("top%d\t\n", top);
  106. return top;
  107. }
  108. double calculate_MAT2(double top, double duty) {
  109. double percentage_duty = 0;
  110. double Mat2 = 0;
  111. percentage_duty = duty / 100;
  112. Mat2 = top * percentage_duty;
  113. return Mat2;
  114. }
  115. void ozone_pwm_control_set_pwm_duty(uint32_t frequency, uint32_t duty) {
  116. double top = 0;
  117. double Mat2 = 0;
  118. if (frequency > CLCK * 1000) {
  119. printf("out of range\n");
  120. return;
  121. }
  122. top = calculate_top(frequency); //������Ҫ��Ƶ�ʼ�����TOP���Զ���װ��ֵ��
  123. if (top == 0) {
  124. return;
  125. }
  126. Mat2 = calculate_MAT2(top, duty);
  127. printf("top:\t%2.lf\n", top);
  128. printf("MAT2:\t%2.lf\n", Mat2);
  129. printf("MAT3:\t%2.lf\n", top);
  130. T16Nx_Disable(T16N0);
  131. t16n0_1_init();
  132. T16Nx_SetMAT2(T16N0, Mat2);
  133. if (duty == 100) {
  134. T16Nx_SetMAT3(T16N0, top - 1);
  135. } else {
  136. T16Nx_SetMAT3(T16N0, top);
  137. }
  138. T16Nx_SetTOP1(T16N0, top);
  139. }
  140. //######################################################