maincpp.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. port1freq.Init(&htim1, TIM_CHANNEL_1);
  62. port2freq.Init(&htim3, TIM_CHANNEL_4);
  63. // HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1);
  64. // HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_4);
  65. port1freq.setupoutput(10);
  66. port2freq.setupoutput(10);
  67. // port1freq.freq = (360000/10)-1;
  68. // port1freq.duty = port1freq.freq/2;
  69. // htim1.Instance->CCR1 = port1freq.duty;
  70. // htim1.Instance->ARR = port1freq.freq;
  71. // port2freq.freq = (360000/2000)-1;
  72. // port2freq.duty = port2freq.freq/2;
  73. // // htim3.Instance->CCR1
  74. // htim3.Instance->CCR4 = port2freq.duty;
  75. // htim3.Instance->ARR = port2freq.freq;
  76. // HAL_TIM_Base_Start_IT(&htim17);
  77. while(1)
  78. {
  79. txdata(txbuff);
  80. HAL_Delay(1000);
  81. // if(pwm1freq != pwm1freqnew)
  82. // {
  83. // pwm1freq = pwm1freqnew;
  84. //// val = (map(pwm1freq, 1, 10000, 10000, 1))-1;
  85. // htim1.Instance->CCR1 = (pwm1freq/2);
  86. // htim1.Instance->ARR = pwm1freq;
  87. //
  88. // }
  89. // if(pwm2freq != pwm2freqnew)
  90. // {
  91. // val = (360000/pwm2freqnew)-1;
  92. // pwm1freqnew = val;
  93. // pwm2freq = pwm2freqnew;
  94. //
  95. //
  96. // }
  97. }
  98. }
  99. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  100. {
  101. /* Prevent unused argument(s) compilation warning */
  102. if(htim->Instance == TIM17)
  103. {
  104. pwm1counter++;
  105. if(pwm1counter >= pwm1period)
  106. {
  107. // HAL_GPIO_TogglePin(LED1_GPIO_Port, LED1_Pin);
  108. pwm1counter = 0;
  109. }
  110. }
  111. /* NOTE : This function should not be modified, when the callback is needed,
  112. the HAL_TIM_PeriodElapsedCallback could be implemented in the user file
  113. */
  114. }
  115. void PWMOUT::Init(TIM_HandleTypeDef *port, uint32_t tchan) {
  116. timport = *port;
  117. timchan = tchan;
  118. HAL_TIM_PWM_Start(&timport, timchan);
  119. }
  120. uint32_t PWMOUT::setupoutput(uint32_t pwmfreq) {
  121. uint32_t scale = 360000;
  122. uint32_t autoreloadr = scale/pwmfreq;
  123. switch (timchan)
  124. {
  125. case TIM_CHANNEL_1:
  126. timport.Instance->CCR1 = autoreloadr/2;
  127. break;
  128. case TIM_CHANNEL_2:
  129. timport.Instance->CCR2 = autoreloadr/2;
  130. break;
  131. case TIM_CHANNEL_3:
  132. timport.Instance->CCR3 = autoreloadr/2;
  133. break;
  134. case TIM_CHANNEL_4:
  135. timport.Instance->CCR4 = autoreloadr/2;
  136. break;
  137. }
  138. timport.Instance->ARR = autoreloadr;
  139. freq = pwmfreq;
  140. return freq;
  141. }
  142. uint8_t txdata(uint8_t * data) {
  143. txbuff[DATASIZE] = STREAMLENTH;
  144. txbuff[PORT1STAT] = port1freq.portStat;
  145. txbuff[PORT1VAL_H] = port1freq.freq>>8;
  146. txbuff[PORT1VAL_L] = port1freq.freq & 0x0F;
  147. txbuff[PORT2STAT] = port2freq.portStat;
  148. txbuff[PORT2VAL_H] = port2freq.freq>>8;
  149. txbuff[PORT2VAL_L] = port2freq.freq & 0x0F;
  150. txbuff[CHECKSUM] = chsum(data);
  151. HAL_UART_Transmit(&huart1, data, STREAMLENTH, HAL_MAX_DELAY);
  152. return 1;
  153. }
  154. uint8_t chsum(uint8_t * data) {
  155. uint8_t result = 0;
  156. for(int i = 0; i < STREAMLENTH-1; i++)
  157. {
  158. result ^= data[i];
  159. }
  160. return result;
  161. }