maincpp.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * maincpp.cpp
  3. *
  4. * Created on: 22 Jun 2022
  5. * Author: garth
  6. */
  7. #include "main.h"
  8. #include "gpio.h"
  9. #include "tim.h"
  10. #include "extra.h"
  11. #include "usart.h"
  12. #include "string.h"
  13. #define STREAMLENTH 8
  14. uint8_t rxbuff[64];
  15. uint8_t txbuff[STREAMLENTH];
  16. enum {
  17. DATASIZE,
  18. PORT1STAT,
  19. PORT1VAL_H,
  20. PORT1VAL_L,
  21. PORT2STAT,
  22. PORT2VAL_H,
  23. PORT2VAL_L,
  24. CHECKSUM
  25. };
  26. struct PWMOUT {
  27. uint8_t portStat;
  28. uint16_t freq;
  29. uint16_t duty;
  30. TIM_HandleTypeDef timport;
  31. uint32_t timchan;
  32. void Init(TIM_HandleTypeDef *port, uint32_t tchan);
  33. void output(uint16_t freq);
  34. uint32_t setupoutput(uint32_t freq);
  35. };
  36. uint8_t chsum(uint8_t * data);
  37. uint8_t txdata(uint8_t * data);
  38. PWMOUT port1freq;
  39. PWMOUT port2freq;
  40. uint16_t numberofdigits = 0;
  41. uint8_t count = 0;
  42. //#include "STM_ENC28_J60.h"
  43. volatile uint16_t pwm1freq = 0;
  44. volatile uint16_t pwm1freqnew = 100;
  45. volatile uint16_t pwm2freq = 0;
  46. volatile uint16_t pwm2freqnew = 9999;
  47. volatile uint16_t val;
  48. volatile uint32_t pwm1counter;
  49. volatile uint32_t pwm1period;
  50. void maincpp()
  51. {
  52. numberofdigits = pwm2freqnew;
  53. while(numberofdigits != 0) {
  54. numberofdigits=numberofdigits/10;
  55. count++;
  56. }
  57. port1freq.Init(&htim1, TIM_CHANNEL_1);
  58. port2freq.Init(&htim3, TIM_CHANNEL_4);
  59. // HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1);
  60. // HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_4);
  61. port1freq.setupoutput(10);
  62. port2freq.setupoutput(10);
  63. // port1freq.freq = (360000/10)-1;
  64. // port1freq.duty = port1freq.freq/2;
  65. // htim1.Instance->CCR1 = port1freq.duty;
  66. // htim1.Instance->ARR = port1freq.freq;
  67. // port2freq.freq = (360000/2000)-1;
  68. // port2freq.duty = port2freq.freq/2;
  69. // // htim3.Instance->CCR1
  70. // htim3.Instance->CCR4 = port2freq.duty;
  71. // htim3.Instance->ARR = port2freq.freq;
  72. // HAL_TIM_Base_Start_IT(&htim17);
  73. while(1)
  74. {
  75. txdata(txbuff);
  76. HAL_Delay(1000);
  77. // if(pwm1freq != pwm1freqnew)
  78. // {
  79. // pwm1freq = pwm1freqnew;
  80. //// val = (map(pwm1freq, 1, 10000, 10000, 1))-1;
  81. // htim1.Instance->CCR1 = (pwm1freq/2);
  82. // htim1.Instance->ARR = pwm1freq;
  83. //
  84. // }
  85. // if(pwm2freq != pwm2freqnew)
  86. // {
  87. // val = (360000/pwm2freqnew)-1;
  88. // pwm1freqnew = val;
  89. // pwm2freq = pwm2freqnew;
  90. //
  91. //
  92. // }
  93. }
  94. }
  95. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  96. {
  97. /* Prevent unused argument(s) compilation warning */
  98. if(htim->Instance == TIM17)
  99. {
  100. pwm1counter++;
  101. if(pwm1counter >= pwm1period)
  102. {
  103. // HAL_GPIO_TogglePin(LED1_GPIO_Port, LED1_Pin);
  104. pwm1counter = 0;
  105. }
  106. }
  107. /* NOTE : This function should not be modified, when the callback is needed,
  108. the HAL_TIM_PeriodElapsedCallback could be implemented in the user file
  109. */
  110. }
  111. void PWMOUT::Init(TIM_HandleTypeDef *port, uint32_t tchan) {
  112. timport = *port;
  113. timchan = tchan;
  114. HAL_TIM_PWM_Start(&timport, timchan);
  115. }
  116. uint32_t PWMOUT::setupoutput(uint32_t pwmfreq) {
  117. uint32_t scale = 360000;
  118. uint32_t autoreloadr = scale/pwmfreq;
  119. switch (timchan)
  120. {
  121. case TIM_CHANNEL_1:
  122. timport.Instance->CCR1 = autoreloadr/2;
  123. break;
  124. case TIM_CHANNEL_2:
  125. timport.Instance->CCR2 = autoreloadr/2;
  126. break;
  127. case TIM_CHANNEL_3:
  128. timport.Instance->CCR3 = autoreloadr/2;
  129. break;
  130. case TIM_CHANNEL_4:
  131. timport.Instance->CCR4 = autoreloadr/2;
  132. break;
  133. }
  134. timport.Instance->ARR = autoreloadr;
  135. freq = pwmfreq;
  136. return freq;
  137. }
  138. uint8_t txdata(uint8_t * data) {
  139. txbuff[DATASIZE] = STREAMLENTH;
  140. txbuff[PORT1STAT] = port1freq.portStat;
  141. txbuff[PORT1VAL_H] = port1freq.freq>>8;
  142. txbuff[PORT1VAL_L] = port1freq.freq & 0x0F;
  143. txbuff[PORT2STAT] = port2freq.portStat;
  144. txbuff[PORT2VAL_H] = port2freq.freq>>8;
  145. txbuff[PORT2VAL_L] = port2freq.freq & 0x0F;
  146. txbuff[CHECKSUM] = chsum(data);
  147. HAL_UART_Transmit(&huart1, data, STREAMLENTH, HAL_MAX_DELAY);
  148. return 1;
  149. }
  150. uint8_t chsum(uint8_t * data) {
  151. uint8_t result = 0;
  152. for(int i = 0; i < STREAMLENTH-1; i++)
  153. {
  154. result ^= data[i];
  155. }
  156. return result;
  157. }