maincpp.cpp 4.4 KB

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