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.

87 lines
3.7 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
  1. #include "pwm.h"
  2. #define CLCK 48
  3. static double calculate_top(double target_frequency_hz) {
  4. int clck = 0;
  5. int top = 0;
  6. clck = CLCK * 1000 * 1000;
  7. top = clck / target_frequency_hz;
  8. return top;
  9. }
  10. void set_pwm_t16_pa4(int freqhz, float duty) {
  11. double top_double = calculate_top(freqhz); //根据需要的频率计算出TOP(自动重装载值)
  12. uint16_t top = (uint16_t)top_double;
  13. uint16_t Mat2 = (uint16_t)top_double * (duty / 100.0);
  14. uint16_t Mat3 = top;
  15. if (Mat2 >= top) Mat2 = top - 1;
  16. printf("Mat2:%d\r\n", Mat2);
  17. printf("Mat3:%d\r\n", Mat3);
  18. printf("top:%d\r\n", top);
  19. /////////////////////////////////////////////////
  20. T16Nx_Disable(T16N0);
  21. // PA4 T16N0_1
  22. T16Nx_BaseInitStruType x;
  23. T16Nx_PWMInitStruType y;
  24. /* 初始化T16Nx定时器*/
  25. x.T16Nx_ClkS = T16Nx_ClkS_PCLK; //时钟源48M
  26. x.T16Nx_SYNC = Disable; //不同步
  27. x.T16Nx_EDGE = T16Nx_EDGE_Rise; //上升沿触发
  28. x.T16Nx_Mode = T16Nx_Mode_PWM; // 选用PWM模式
  29. x.T16Nx_PREMAT = 0x01; /* 预分频比1:1 */
  30. T16Nx_BaseInit(T16N0, &x);
  31. /* 配置T16N0通道1输出 */
  32. y.T16Nx_MOE0 = Disable;
  33. y.T16Nx_MOE1 = Enable;
  34. y.T16Nx_POL0 = POSITIVE; //在串口发送的时候,正极性代表发送的数据与接受的数据相同,负极性代表与发送的数据相反,在这么不知道有没有作用
  35. y.T16Nx_POL1 = POSITIVE;
  36. y.T16Nx_PWMMODE = T16Nx_PWMMode_INDEP; //选择独立模式
  37. y.PWMDZE = Disable; // PWM互补模式死区使能
  38. y.REGBUFEN = Enable; //缓冲寄存器使能 (REGBUFEN目前不知道干什么用的)
  39. T16Nx_PMWOutInit(T16N0, &y);
  40. /* 配置T16N0 通道1输出 */
  41. /*MAT2 MAT3 通道的中断配置*/
  42. //匹配寄存器值匹配后的工作模式,计数到以后: 继续计数不产生中断
  43. T16Nx_MAT2ITConfig(T16N0, T16Nx_Go_No);
  44. //匹配寄存器值匹配后的工作模式,清零并重新计数,产生中断
  45. T16Nx_MAT3ITConfig(T16N0, T16Nx_Clr_Int);
  46. /*MAT2 MAT3 匹配后的输出电平高低*/
  47. T16Nx_MAT2Out1Config(T16N0,
  48. T16Nx_Out_Low); //匹配后输出端口的模式,输出高还是低
  49. T16Nx_MAT3Out1Config(T16N0,
  50. T16Nx_Out_High); //匹配后输出端口的模式,输出高还是低
  51. //以上是设置模式,输出高低电平
  52. T16Nx_SetCNT1(T16N0, 0); //设定计数器的初始值
  53. T16Nx_SetMAT2(T16N0, Mat2); //设置匹配寄存器的数值
  54. T16Nx_SetMAT3(T16N0, Mat3); //设置匹配寄存器的数值
  55. //设置计数器峰值//根据这个得到定时的时钟48M/48000=1khZ(在独立模式下PWM的周期由TOP1决定为TOP+1,周期算出来是1ms)
  56. T16Nx_SetTOP1(T16N0, top);
  57. //以上是设置占空比
  58. /* 配置输出管脚 */
  59. GPIO_InitSettingType initset;
  60. initset.Signal = GPIO_Pin_Signal_Digital; //数字
  61. initset.Dir = GPIO_Direction_Output; //输出模式
  62. initset.Func = GPIO_Reuse_Func2; //复用到T16N0_1功能
  63. initset.ODE = GPIO_ODE_Output_Disable; //开漏使能
  64. initset.DS = GPIO_DS_Output_Normal; //普通电流模式
  65. initset.PUE = GPIO_PUE_Input_Enable; //弱上拉使能
  66. initset.PDE = GPIO_PDE_Input_Disable; //弱下拉禁止
  67. /* 配置PA4为T16N0输出通道1 */
  68. GPIO_Init(GPIO_Pin_A4, &initset);
  69. T16Nx_Enable(T16N0);
  70. return;
  71. }
  72. //######################################################
  73. /**
  74. * @brief pwm的周期占空比
  75. *
  76. * @param frequency
  77. * @param duty
  78. */
  79. void set_pwm_modbul_freq_duty(uint32_t frequencyhz, uint32_t duty) {
  80. printf("set_pwm_modbul_freq_duty freq:%d,duty%d\n", frequencyhz, duty);
  81. set_pwm_t16_pa4(frequencyhz, duty);
  82. }
  83. //######################################################