PWM.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * PWM.c
  3. *
  4. * Created on: 14 janv. 2019
  5. * Author: e_rcluze
  6. */
  7. #include "PWM.h"
  8. #include "tim.h"
  9. #include "gpio.h"
  10. void PWMSetDutyCycle(uint8_t sortie, uint8_t pourcentage) {
  11. TIM_OC_InitTypeDef sConfigOC;
  12. sConfigOC.Pulse = PourcentageToValue(pourcentage);
  13. switch (sortie) {
  14. case S0:
  15. HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_3);
  16. break;
  17. case S1:
  18. HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_4);
  19. break;
  20. case S2:
  21. HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_1);
  22. break;
  23. case S3:
  24. HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_2);
  25. break;
  26. case S4:
  27. HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_3);
  28. break;
  29. case S5:
  30. HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_4);
  31. break;
  32. case S6:
  33. HAL_TIM_PWM_ConfigChannel(&htim4, &sConfigOC, TIM_CHANNEL_1);
  34. break;
  35. case S7:
  36. HAL_TIM_PWM_ConfigChannel(&htim4, &sConfigOC, TIM_CHANNEL_2);
  37. break;
  38. case S8:
  39. HAL_TIM_PWM_ConfigChannel(&htim4, &sConfigOC, TIM_CHANNEL_3);
  40. break;
  41. case S9:
  42. HAL_TIM_PWM_ConfigChannel(&htim4, &sConfigOC, TIM_CHANNEL_4);
  43. break;
  44. }
  45. }
  46. void PWMStart(uint8_t sortie) {
  47. switch (sortie) {
  48. case S0:
  49. HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_3);
  50. break;
  51. case S1:
  52. HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_4);
  53. break;
  54. case S2:
  55. HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_1);
  56. break;
  57. case S3:
  58. HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_2);
  59. break;
  60. case S4:
  61. HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_3);
  62. break;
  63. case S5:
  64. HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_4);
  65. break;
  66. case S6:
  67. HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_1);
  68. break;
  69. case S7:
  70. HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_2);
  71. break;
  72. case S8:
  73. HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_3);
  74. break;
  75. case S9:
  76. HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_4);
  77. break;
  78. }
  79. }
  80. uint16_t PourcentageToValue(uint8_t val){
  81. return (uint16_t)val * PWM_MAX;
  82. }
  83. uint8_t PWMPourcentage(Heure *heure, Sortie *sortie){
  84. Heure hd,he;
  85. HeureAjout(hd,sortie->Demmarage,sortie->Pente);
  86. HeureSuppr(he,sortie->Extinxtion,sortie->Pente);
  87. if ((CompareHeure(*heure,sortie->Demmarage)>=0) & (CompareHeure(*heure,hd)<0)){
  88. return (DiffHeure(*heure,sortie->Demmarage)/sortie->Pente)*sortie->PuissanceMax;
  89. }
  90. else if ((CompareHeure(*heure,hd)>=0) &(CompareHeure(*heure,he)<=0)){
  91. return sortie->PuissanceMax;
  92. }
  93. else if ((CompareHeure(*heure,hd)>0) & (CompareHeure(*heure,sortie->Extinxtion)<=0)){
  94. return (DiffHeure(sortie->Extinxtion,*heure)/sortie->Pente)*sortie->PuissanceMax;
  95. }
  96. else{
  97. return 0;
  98. }
  99. }