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.

163 lines
5.5 KiB

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 "pwm.h"
  2. #include "board.h"
  3. #define CLCK 48
  4. uint32_t target_frequencyhz;
  5. uint32_t target_duty;
  6. static double calculate_top(double target_frequency_hz) {
  7. int clck = 0;
  8. int top = 0;
  9. clck = CLCK * 1000 * 1000;
  10. top = clck / target_frequency_hz;
  11. return top;
  12. }
  13. void set_pwm_t16_pa4(int freqhz, float duty) {
  14. double top_double = calculate_top(freqhz); //根据需要的频率计算出TOP(自动重装载值)
  15. uint16_t top = (uint16_t)top_double;
  16. uint16_t Mat2 = (uint16_t)top_double * (duty / 100.0);
  17. uint16_t Mat3 = top;
  18. if (Mat2 >= top) Mat2 = top - 1;
  19. printf("Mat2:%d\r\n", Mat2);
  20. printf("Mat3:%d\r\n", Mat3);
  21. printf("top:%d\r\n", top);
  22. /////////////////////////////////////////////////
  23. T16Nx_Disable(T16N0);
  24. // PA4 T16N0_1
  25. T16Nx_BaseInitStruType x;
  26. T16Nx_PWMInitStruType y;
  27. /* 初始化T16Nx定时器*/
  28. x.T16Nx_ClkS = T16Nx_ClkS_PCLK; //时钟源48M
  29. x.T16Nx_SYNC = Disable; //不同步
  30. x.T16Nx_EDGE = T16Nx_EDGE_Rise; //上升沿触发
  31. x.T16Nx_Mode = T16Nx_Mode_PWM; // 选用PWM模式
  32. x.T16Nx_PREMAT = 0x01; /* 预分频比1:1 */
  33. T16Nx_BaseInit(T16N0, &x);
  34. /* 配置T16N0通道1输出 */
  35. y.T16Nx_MOE0 = Disable;
  36. y.T16Nx_MOE1 = Enable;
  37. y.T16Nx_POL0 = POSITIVE; //在串口发送的时候,正极性代表发送的数据与接受的数据相同,负极性代表与发送的数据相反,在这么不知道有没有作用
  38. y.T16Nx_POL1 = POSITIVE;
  39. y.T16Nx_PWMMODE = T16Nx_PWMMode_INDEP; //选择独立模式
  40. y.PWMDZE = Disable; // PWM互补模式死区使能
  41. y.REGBUFEN = Enable; //缓冲寄存器使能 (REGBUFEN目前不知道干什么用的)
  42. T16Nx_PMWOutInit(T16N0, &y);
  43. /* 配置T16N0 通道1输出 */
  44. /*MAT2 MAT3 通道的中断配置*/
  45. //匹配寄存器值匹配后的工作模式,计数到以后: 继续计数不产生中断
  46. T16Nx_MAT2ITConfig(T16N0, T16Nx_Go_No);
  47. //匹配寄存器值匹配后的工作模式,清零并重新计数,产生中断
  48. T16Nx_MAT3ITConfig(T16N0, T16Nx_Clr_Int);
  49. /*MAT2 MAT3 匹配后的输出电平高低*/
  50. T16Nx_MAT2Out1Config(T16N0,
  51. T16Nx_Out_Low); //匹配后输出端口的模式,输出高还是低
  52. T16Nx_MAT3Out1Config(T16N0,
  53. T16Nx_Out_High); //匹配后输出端口的模式,输出高还是低
  54. //以上是设置模式,输出高低电平
  55. T16Nx_SetCNT1(T16N0, 0); //设定计数器的初始值
  56. T16Nx_SetMAT2(T16N0, Mat2); //设置匹配寄存器的数值
  57. T16Nx_SetMAT3(T16N0, Mat3); //设置匹配寄存器的数值
  58. //设置计数器峰值//根据这个得到定时的时钟48M/48000=1khZ(在独立模式下PWM的周期由TOP1决定为TOP+1,周期算出来是1ms)
  59. T16Nx_SetTOP1(T16N0, top);
  60. //以上是设置占空比
  61. /* 配置输出管脚 */
  62. GPIO_InitSettingType initset;
  63. initset.Signal = GPIO_Pin_Signal_Digital; //数字
  64. initset.Dir = GPIO_Direction_Output; //输出模式
  65. initset.Func = GPIO_Reuse_Func2; //复用到T16N0_1功能
  66. initset.ODE = GPIO_ODE_Output_Disable; //开漏使能
  67. initset.DS = GPIO_DS_Output_Normal; //普通电流模式
  68. initset.PUE = GPIO_PUE_Input_Enable; //弱上拉使能
  69. initset.PDE = GPIO_PDE_Input_Disable; //弱下拉禁止
  70. /* 配置PA4为T16N0输出通道1 */
  71. GPIO_Init(GPIO_Pin_A4, &initset);
  72. T16Nx_Enable(T16N0);
  73. return;
  74. }
  75. //######################################################
  76. /**
  77. * @brief pwm的周期占空比
  78. *
  79. * @param frequency
  80. * @param duty
  81. */
  82. /**
  83. *
  84. * :
  85. *
  86. *
  87. *
  88. * :
  89. * 20->0
  90. * 0->20
  91. * 0->10
  92. *
  93. * ,
  94. *
  95. *
  96. *
  97. *
  98. *
  99. *
  100. * 1. ,
  101. * 2.
  102. *
  103. */
  104. uint32_t s_target_frequencyhz;
  105. uint32_t s_now_frequencyhz;
  106. uint32_t s_target_duty;
  107. uint32_t s_now_duty;
  108. void set_pwm_modbul_freq_duty(uint32_t frequencyhz, uint32_t duty) {
  109. // s_target_frequencyhz = frequencyhz;
  110. // if (s_target_frequencyhz <= MIN_PWM_FREQ) {
  111. // s_target_frequencyhz = MIN_PWM_FREQ;
  112. // }
  113. // s_target_duty = duty;
  114. set_pwm_t16_pa4(frequencyhz, duty);
  115. }
  116. // uint32_t get_next_value_on_step(uint32_t now, uint32_t target, uint32_t step) {
  117. // }
  118. // void pwm_module_stop() { set_pwm_modbul_freq_duty(MIN_PWM_FREQ, 0); }
  119. // void pwm_schdule() {
  120. // /**
  121. // * @brief 当前频率的占空比的百分之一变化
  122. // */
  123. // //计算当前频率下的目标占空比
  124. // uint32_t targetduty = 0;
  125. // uint32_t targetduty1 = s_target_duty;
  126. // uint32_t targetduty2 = (1.0 / s_target_frequencyhz) * (s_target_duty / 100.0) / (1.0 / s_now_frequencyhz) * 100;
  127. // targetduty = targetduty1 < targetduty2 ? targetduty1 : targetduty2;
  128. // if (targetduty != s_now_duty) {
  129. // set_pwm_t16_pa4(s_now_frequencyhz, targetduty);
  130. // return;
  131. // } else {
  132. // //计算下一次设置的频率
  133. // set_pwm_t16_pa4(s_now_frequencyhz, targetduty);
  134. // }
  135. // if (s_target_frequencyhz > s_now_frequencyhz) {
  136. // s_now_frequencyhz += 1000;
  137. // }
  138. // }
  139. //######################################################
  140. //# c语言的理解
  141. //# static , 字节长度 ,堆,栈,变量分布,
  142. //# 操作系统
  143. //# 线程,进程,信号量
  144. //#
  145. //#