maincpp.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 freqout {
  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. };
  35. uint8_t chsum(uint8_t * data);
  36. uint8_t txdata(uint8_t * data);
  37. freqout port1freq;
  38. freqout port2freq;
  39. //#include "STM_ENC28_J60.h"
  40. volatile uint16_t pwm1freq = 0;
  41. volatile uint16_t pwm1freqnew = 100;
  42. volatile uint16_t pwm2freq = 0;
  43. volatile uint16_t pwm2freqnew = 10000;
  44. volatile uint16_t val;
  45. volatile uint32_t pwm1counter;
  46. volatile uint32_t pwm1period;
  47. void maincpp()
  48. {
  49. port1freq.Init(&htim1, TIM_CHANNEL_1);
  50. port2freq.Init(&htim3, TIM_CHANNEL_4);
  51. // HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1);
  52. // HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_4);
  53. port1freq.freq = (360000/20)-1;
  54. port1freq.duty = port1freq.freq/2;
  55. htim1.Instance->CCR1 = port1freq.duty;
  56. htim1.Instance->ARR = port1freq.freq;
  57. port2freq.freq = (360000/10)-1;
  58. port2freq.duty = port2freq.freq/2;
  59. htim3.Instance->CCR1 = port2freq.duty;
  60. htim3.Instance->ARR = port2freq.freq;
  61. // HAL_TIM_Base_Start_IT(&htim17);
  62. while(1)
  63. {
  64. txdata(txbuff);
  65. HAL_Delay(1000);
  66. // if(pwm1freq != pwm1freqnew)
  67. // {
  68. // pwm1freq = pwm1freqnew;
  69. //// val = (map(pwm1freq, 1, 10000, 10000, 1))-1;
  70. // htim1.Instance->CCR1 = (pwm1freq/2);
  71. // htim1.Instance->ARR = pwm1freq;
  72. //
  73. // }
  74. // if(pwm2freq != pwm2freqnew)
  75. // {
  76. // val = (360000/pwm2freqnew)-1;
  77. // pwm1freqnew = val;
  78. // pwm2freq = pwm2freqnew;
  79. //
  80. //
  81. // }
  82. }
  83. }
  84. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  85. {
  86. /* Prevent unused argument(s) compilation warning */
  87. if(htim->Instance == TIM17)
  88. {
  89. pwm1counter++;
  90. if(pwm1counter >= pwm1period)
  91. {
  92. // HAL_GPIO_TogglePin(LED1_GPIO_Port, LED1_Pin);
  93. pwm1counter = 0;
  94. }
  95. }
  96. /* NOTE : This function should not be modified, when the callback is needed,
  97. the HAL_TIM_PeriodElapsedCallback could be implemented in the user file
  98. */
  99. }
  100. void freqout::Init(TIM_HandleTypeDef *port, uint32_t tchan) {
  101. timport = *port;
  102. timchan = tchan;
  103. HAL_TIM_PWM_Start(&timport, timchan);
  104. }
  105. uint8_t txdata(uint8_t * data) {
  106. txbuff[DATASIZE] = STREAMLENTH;
  107. txbuff[PORT1STAT] = port1freq.portStat;
  108. txbuff[PORT1VAL_H] = port1freq.freq>>8;
  109. txbuff[PORT1VAL_L] = port1freq.freq & 0x0F;
  110. txbuff[PORT2STAT] = port2freq.portStat;
  111. txbuff[PORT2VAL_H] = port2freq.freq>>8;
  112. txbuff[PORT2VAL_L] = port2freq.freq & 0x0F;
  113. txbuff[CHECKSUM] = chsum(data);
  114. HAL_UART_Transmit(&huart1, data, STREAMLENTH, HAL_MAX_DELAY);
  115. return 1;
  116. }
  117. uint8_t chsum(uint8_t * data) {
  118. uint8_t result = 0;
  119. for(int i = 0; i < STREAMLENTH-1; i++)
  120. {
  121. result ^= data[i];
  122. }
  123. return result;
  124. }