A bundled STM32F10x Std Periph and CMSIS library
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.
 
 
 
 
 

144 lines
4.0 KiB

  1. /*
  2. PWM.h - PWM API header file.
  3. includes:
  4. PWM_Output_Init, PWM_Enable, PWM_Disable, PWM_SetFreq, PWM_SetDC, PWM_SetDT
  5. Copyright (C) 2011 Houtouridis Christos (http://houtouridis.blogspot.com/)
  6. This program is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. Author: Houtouridis Christos <houtouridis.ch@gmail.com>
  17. Date: 11/2011
  18. Version: 0.1
  19. */
  20. #ifndef __PWM_h__
  21. #define __PWM_h__
  22. #include <stm32f10x.h>
  23. #include <stm32f10x_systick.h>
  24. #include <stm32f10x_clock.h>
  25. #include <math.h>
  26. /* ================ User Defines ======================*/
  27. #define PWM_DATA_SIZE (4)
  28. #define PWM_MINIMUM_STEPS (100)
  29. #define PWM_STD_DC (40)
  30. #define PWM_STD_FREQ (10000)
  31. #define PWM_DEBUG_MODE (1)
  32. /* ========= General Defines ========*/
  33. #define PWM_MAXIMUM_STEPS (0xFFFF)
  34. #define PWM_DT_A_MAX (0x7F)
  35. #define PWM_DT_B_MAX (0xFE)
  36. #define PWM_DT_C_MAX (0x1F8)
  37. #define PWM_DT_D_MAX (0x3F0)
  38. #define PWM_DTG_A_MASK (0)
  39. #define PWM_DTG_B_MASK (0x80)
  40. #define PWM_DTG_C_MASK (0xC0)
  41. #define PWM_DTG_D_MASK (0xE0)
  42. #define PWM_TIM_CCMR1_1_MODE1 ((uint16_t)0x0060)
  43. #define PWM_TIM_CCMR1_1_MODE2 ((uint16_t)0x0070)
  44. #define PWM_TIM_CCMR1_2_MODE1 ((uint16_t)0x6000)
  45. #define PWM_TIM_CCMR1_2_MODE2 ((uint16_t)0x7000)
  46. #define PWM_TIM_CCMR2_3_MODE1 ((uint16_t)0x0060)
  47. #define PWM_TIM_CCMR2_3_MODE2 ((uint16_t)0x0060)
  48. #define PWM_TIM_CCMR2_4_MODE1 ((uint16_t)0x6000)
  49. #define PWM_TIM_CCMR2_4_MODE2 ((uint16_t)0x7000)
  50. /*================ General Data Types ======================*/
  51. typedef enum
  52. {
  53. TIM_CH1, TIM_CH1N, TIM_CH1CMP,
  54. TIM_CH2, TIM_CH2N, TIM_CH2CMP,
  55. TIM_CH3, TIM_CH3N, TIM_CH3CMP,
  56. TIM_CH4
  57. }PWM_Channel_t;
  58. /*
  59. See User manual page 257 and 227
  60. */
  61. typedef enum
  62. {
  63. PWM_MODE_1_UP, PWM_MODE_1_DOWN,
  64. PWM_MODE_2_UP, PWM_MODE_2_DOWN
  65. }PWM_Mode_t;
  66. typedef uint16_t PWM_PSC_t;
  67. // Timer typedefs
  68. typedef TIM_TypeDef PWM_Timer_t;
  69. typedef uint32_t PWM_Timer_Freq_t;
  70. // PWM typedefs
  71. typedef float PWM_Freq_t;
  72. typedef float PWM_DC_t;
  73. typedef float PWM_DT_t;
  74. typedef uint8_t PWM_DTG_t;
  75. typedef volatile struct
  76. {
  77. PWM_Timer_t *tim;
  78. PWM_Timer_Freq_t tim_freq;
  79. PWM_Channel_t ch;
  80. PWM_Mode_t mode;
  81. PWM_Freq_t freq;
  82. PWM_DC_t dc;
  83. PWM_DT_t dt;
  84. }PWM_t;
  85. typedef enum
  86. {
  87. PWM_EXIT_OK, PWM_EXIT_ERROR
  88. }PWM_EXIT_t;
  89. /*================ Static Functions ======================*/
  90. #define PWM_DTG_A(__dt, __tfreq) ( (PWM_DTG_t)(__dt * __tfreq) )
  91. #define PWM_DTG_B(__dt, __tfreq) ( (PWM_DTG_t)(((__dt * __tfreq) / 2 ) - 64 ) )
  92. #define PWM_DTG_C(__dt, __tfreq) ( (PWM_DTG_t)(((__dt * __tfreq) / 8 ) - 32 ) )
  93. #define PWM_DTG_D(__dt, __tfreq) ( (PWM_DTG_t)(((__dt * __tfreq) / 16) - 32 ) )
  94. /*================ Exported Functions ======================*/
  95. PWM_EXIT_t PWM_Output_Init (PWM_t *pwm);
  96. PWM_EXIT_t PWM_Enable (PWM_t *pwm);
  97. PWM_EXIT_t PWM_Disable (PWM_t *pwm);
  98. PWM_EXIT_t PWM_SetFreq (PWM_t *pwm, PWM_Freq_t f);
  99. PWM_EXIT_t PWM_SetDC (PWM_t *pwm, PWM_DC_t dc);
  100. PWM_EXIT_t PWM_SetDT (PWM_t *pwm, PWM_DT_t dt);
  101. #endif //#ifndef __PWM_h__