SWM341_pwm.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #ifndef __SWM341_PWM_H__
  2. #define __SWM341_PWM_H__
  3. typedef struct {
  4. uint8_t Mode; //PWM_EDGE_ALIGNED、PWM_CENTER_ALIGNED、PWM_ASYM_CENTER_ALIGNED
  5. uint16_t Clkdiv; //1--1024
  6. uint16_t Period; //周期
  7. uint16_t HdutyA; //占空比,高电平时长;PWM_ASYM_CENTER_ALIGNED 模式下设定前半周期的高电平时长
  8. uint16_t HdutyA2; //只在 PWM_ASYM_CENTER_ALIGNED 模式下使用,用于设定后半周期的高电平时长
  9. uint16_t DeadzoneA; //死区时长,取值0--1023
  10. uint8_t IdleLevelA; //停止状态下PWMxA输出电平
  11. uint8_t IdleLevelAN; //停止状态下PWMxAN输出电平
  12. uint8_t OutputInvA; //PWMxA输出取反
  13. uint8_t OutputInvAN; //PWMxAN输出取反
  14. uint16_t HdutyB;
  15. uint16_t HdutyB2;
  16. uint16_t DeadzoneB;
  17. uint8_t IdleLevelB;
  18. uint8_t IdleLevelBN;
  19. uint8_t OutputInvB;
  20. uint8_t OutputInvBN;
  21. uint8_t UpOvfIE; //上升沿计数器溢出中断使能,PWM_EDGE_ALIGNED 模式下向上计数,因此只有上升沿
  22. uint8_t DownOvfIE; //下降沿计数器溢出中断使能,另外两种模式下先向上计数、后向下计数,上升沿是前半周期、下降沿是后半周期
  23. uint8_t UpCmpAIE; //上升沿比较器A匹配中断使能
  24. uint8_t DownCmpAIE; //下降沿比较器A匹配中断使能
  25. uint8_t UpCmpBIE; //上升沿比较器B匹配中断使能
  26. uint8_t DownCmpBIE; //下降沿比较器B匹配中断使能
  27. } PWM_InitStructure;
  28. #define PWM_EDGE_ALIGNED 0
  29. #define PWM_CENTER_ALIGNED 1
  30. #define PWM_ASYM_CENTER_ALIGNED 2 // 非对称中心对齐模式,上升沿和下降沿具有不同的翻转点比较值
  31. #define PWM_CH_A 0
  32. #define PWM_CH_B 1
  33. #define PWM0_MSK (1 << 0)
  34. #define PWM1_MSK (1 << 1)
  35. #define PWM2_MSK (1 << 2)
  36. #define PWM3_MSK (1 << 3)
  37. #define PWM4_MSK (1 << 4)
  38. #define PWM_BRK0 (1 << 0)
  39. #define PWM_BRK1 (1 << 1)
  40. #define PWM_BRK2 (1 << 2)
  41. #define PWM_DIR_UP 0
  42. #define PWM_DIR_DOWN 1
  43. #define PWM_TRG_0 0 //计数器溢出或与指定值相等时 PWM 输出触发信号
  44. #define PWM_TRG_1 1
  45. #define PWM_TRG_2 2
  46. #define PWM_TRG_3 3
  47. #define PWM_TRG_4 4
  48. #define PWM_TRG_5 5
  49. #define PWM_TRG_6 6
  50. #define PWM_TRG_7 7
  51. #define PWM_EVT_DIS 0 //外部事件信号为高时 PWM 启动计数、停止计数、暂停计数、屏蔽输出
  52. #define PWM_EVT_0 1
  53. #define PWM_EVT_1 2
  54. #define PWM_EVT_2 3
  55. #define PWM_EVT_3 4
  56. #define PWM_EVT_4 5
  57. #define PWM_EVT_TIMR0 6
  58. #define PWM_EVT_TIMR1 7
  59. /* Interrupt Type */
  60. #define PWM_IT_OVF_UP (1 << 0) //向上计数时计数器溢出,PWM_EDGE_ALIGNED 模式下向上计数,因此只有上升沿
  61. #define PWM_IT_OVF_DOWN (1 << 1) //向下计数时计数器溢出,另外两种模式下先向上计数、后向下计数,上升沿是前半周期、下降沿是后半周期
  62. #define PWM_IT_CMPA_UP (1 << 2) //向上计数时计数器值与CMPA相等
  63. #define PWM_IT_CMPB_UP (1 << 3) //向上计数时计数器值与CMPB相等
  64. #define PWM_IT_CMPA_DOWN (1 << 4) //向下计数时计数器值与CMPA相等
  65. #define PWM_IT_CMPB_DOWN (1 << 5) //向下计数时计数器值与CMPB相等
  66. #define PWM_BRKIT_BRK0 (1 << 0) //PWM_BRK0引脚上出现刹车信号
  67. #define PWM_BRKIT_BRK1 (1 << 1)
  68. #define PWM_BRKIT_BRK2 (1 << 2)
  69. void PWM_Init(PWM_TypeDef * PWMx, PWM_InitStructure * initStruct); //PWM初始化
  70. void PWM_Start(uint32_t pwm); //启动PWM,开始PWM输出
  71. void PWM_Stop(uint32_t pwm); //关闭PWM,停止PWM输出
  72. void PWM_Restart(uint32_t pwm);
  73. void PWM_ReloadEn(uint32_t pwm);
  74. void PWM_ReloadDis(uint32_t pwm);
  75. void PWM_BrkInPolarity(uint32_t brk, uint32_t level);
  76. void PWM_BrkConfig(PWM_TypeDef * PWMx, uint32_t chn, uint32_t brk, uint32_t out, uint32_t outN, uint32_t outHold, uint32_t stpCount);
  77. void PWM_OvfTrigger(PWM_TypeDef * PWMx, uint32_t en_up, uint32_t en_down, uint32_t trig_chn);
  78. void PWM_CmpTrigger(PWM_TypeDef * PWMx, uint16_t match, uint32_t dir, uint32_t width, uint32_t trig_chn, uint32_t adc_trig_pos);
  79. void PWM_OutMask(PWM_TypeDef * PWMx, uint32_t chn, uint32_t evt, uint32_t out, uint32_t evt_n, uint32_t out_n);
  80. void PWM_SetPeriod(PWM_TypeDef * PWMx, uint16_t period); //设置周期
  81. uint16_t PWM_GetPeriod(PWM_TypeDef * PWMx); //获取周期
  82. void PWM_SetHDuty(PWM_TypeDef * PWMx, uint32_t chn, uint16_t hduty); //设置高电平时长
  83. uint16_t PWM_GetHDuty(PWM_TypeDef * PWMx, uint32_t chn); //获取高电平时长
  84. void PWM_SetHDuty2(PWM_TypeDef * PWMx, uint32_t chn, uint16_t hduty, uint16_t hduty2); //设置高电平时长,用于非对称中心对齐模式
  85. void PWM_GetHDuty2(PWM_TypeDef * PWMx, uint32_t chn, uint16_t *hduty, uint16_t *hduty2); //获取高电平时长,用于非对称中心对齐模式
  86. void PWM_SetDeadzone(PWM_TypeDef * PWMx, uint32_t chn, uint16_t deadzone); //设置死区时长
  87. uint16_t PWM_GetDeadzone(PWM_TypeDef * PWMx, uint32_t chn); //获取死区时长
  88. void PWM_IntEn(PWM_TypeDef * PWMx, uint32_t it); //中断使能
  89. void PWM_IntDis(PWM_TypeDef * PWMx, uint32_t it); //中断禁能
  90. void PWM_IntClr(PWM_TypeDef * PWMx, uint32_t it); //中断标志清除
  91. uint32_t PWM_IntStat(PWM_TypeDef * PWMx, uint32_t it); //中断标志查询
  92. void PWM_BrkIntEn(uint32_t brkit); //刹车中断使能
  93. void PWM_BrkIntDis(uint32_t brkit); //刹车中断禁能
  94. void PWM_BrkIntClr(uint32_t brkit); //刹车中断标志清除
  95. uint32_t PWM_BrkIntStat(uint32_t brkit); //刹车中断标志查询
  96. #endif //__SWM341_PWM_H__