main.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /**
  2. ******************************************************************************
  3. * @file Project/main.c
  4. * @author "Volodymyr M. Shylov" <shilow@ukr.net>
  5. * @version v.0.1
  6. * @date 24-11-2023
  7. * @brief Main program body. VA-meter from DC/DC XL4005
  8. ******************************************************************************
  9. * @attention
  10. *
  11. * "THE BEER-WARE LICENSE" (Revision 42):
  12. * <shilow@ukr.net> wrote this file. As long as you retain this notice you
  13. * can do whatever you want with this stuff. If we meet some day, and you think
  14. * this stuff is worth it, you can buy me a beer in return. Shilov V.N.
  15. *
  16. ******************************************************************************
  17. */
  18. /* Includes ------------------------------------------------------------------*/
  19. #include "stm8s.h"
  20. #include "board.h"
  21. #include "led.h"
  22. #include "delay.h"
  23. /* Private define ------------------------------------------------------------*/
  24. #define TIM4_PERIOD (uint8_t)124
  25. /* Private typedef -----------------------------------------------------------*/
  26. /* Private constants ---------------------------------------------------------*/
  27. /* Private variables ---------------------------------------------------------*/
  28. __IO uint16_t ConversionBuffer[ADC_SMPLS];
  29. __IO uint8_t BufferIndex = 0;
  30. static uint16_t Voltage;
  31. /* Private function prototypes -----------------------------------------------*/
  32. static void boardInit(void);
  33. static void showV(void);
  34. static void showC(void);
  35. void main(void)
  36. {
  37. /* Board Configuration */
  38. boardInit();
  39. Delay(100);
  40. /* Infinite loop */
  41. while (1) {
  42. Delay(100);
  43. if (BufferIndex >= ADC_SMPLS) {
  44. BufferIndex = 0;
  45. int8_t i;
  46. uint32_t vbuf = 0;
  47. for (i=0; i<64; i++) {
  48. vbuf += ConversionBuffer[i];
  49. }
  50. vbuf *= ADC_VREF;
  51. vbuf += 512;
  52. vbuf /= 1023;
  53. vbuf *= 45727;
  54. vbuf += 5000;
  55. vbuf /= 10000;
  56. Voltage = vbuf;
  57. }
  58. showV();
  59. showC();
  60. }
  61. }
  62. /* Private functions ---------------------------------------------------------*/
  63. static void showV(void) {
  64. uint16_t a = (Voltage + 5) / 10;
  65. LedDigits[0] = a / 1000;
  66. uint16_t b = a % 1000;
  67. LedDigits[1] = b / 100;
  68. uint8_t c = b % 100;
  69. LedDigits[2] = c / 10;
  70. LedDigits[3] = c % 10;
  71. }
  72. static void showC(void) {
  73. LedDigits[4] = 5;
  74. LedDigits[5] = 6;
  75. LedDigits[6] = 7;
  76. LedDigits[7] = 8;
  77. }
  78. static void boardInit(void) {
  79. /* Насколько я понял, для частот выше 16МГц нужно вводить задержку при работе с FLASH:
  80. - Before using the HSE clock make sure that the "Flash_Wait_States" is set to 1.
  81. - To do so :
  82. - with STVD (menu: Debug Instrument -> MCU configuration -> Options)
  83. - with EWSTM8 (menu: ST-LINK -> Option bytes ->Flash_Wait_States: 1)
  84. */
  85. /* Initialization of the clock to 16MHz */
  86. CLK->CKDIVR = 0x00;
  87. /* Disable clock of unused peripherial */
  88. CLK->PCKENR1 = (uint8_t)(~(CLK_PCKENR1_UART1 | CLK_PCKENR1_SPI));
  89. CLK->PCKENR2 = (uint8_t)(~(CLK_PCKENR2_CAN | CLK_PCKENR2_AWU)); // | CLK_PCKENR2_ADC
  90. /* SWIM clock */
  91. CLK->SWIMCCR = 0x00;
  92. /* SWITCH pin to Push-Pull, High, Fast */
  93. SWITCH_PORT->ODR |= (uint8_t)(SWITCH_PIN);
  94. SWITCH_PORT->DDR |= (uint8_t)(SWITCH_PIN);
  95. SWITCH_PORT->CR1 |= (uint8_t)(SWITCH_PIN);
  96. SWITCH_PORT->CR2 |= (uint8_t)(SWITCH_PIN);
  97. SWITCH_OFF;
  98. /* Configure GPIO used for LED to Push-Pull, High, Fast*/
  99. GPIOA->ODR |= (uint8_t)LED_SEG_CG;
  100. GPIOA->DDR |= (uint8_t)LED_SEG_CG;
  101. GPIOA->CR1 |= (uint8_t)LED_SEG_CG;
  102. GPIOA->CR2 |= (uint8_t)LED_SEG_CG;
  103. GPIOB->ODR |= (uint8_t)LED_SEG_DH;
  104. GPIOB->DDR |= (uint8_t)LED_SEG_DH;
  105. GPIOB->CR1 |= (uint8_t)LED_SEG_DH;
  106. GPIOB->CR2 |= (uint8_t)LED_SEG_DH;
  107. GPIOC->ODR |= (uint8_t)LED_SEG_BE;
  108. GPIOC->DDR |= (uint8_t)LED_SEG_BE;
  109. GPIOC->CR1 |= (uint8_t)LED_SEG_BE;
  110. GPIOC->CR2 |= (uint8_t)LED_SEG_BE;
  111. GPIOD->ODR |= (uint8_t)LED_SEG_AF;
  112. GPIOD->DDR |= (uint8_t)LED_SEG_AF;
  113. GPIOD->CR1 |= (uint8_t)LED_SEG_AF;
  114. GPIOD->CR2 |= (uint8_t)LED_SEG_AF;
  115. /* `SPI` pins to Push-Pull, High, Fast */
  116. SPI_PORT->ODR = (uint8_t)(SPI_SCK|SPI_DATA);
  117. SPI_PORT->DDR |= (uint8_t)(SPI_SCK|SPI_DATA);
  118. SPI_PORT->CR1 |= (uint8_t)(SPI_SCK|SPI_DATA);
  119. SPI_PORT->CR2 |= (uint8_t)(SPI_SCK|SPI_DATA);
  120. /* I2C GPIO SDA SCL - HiZ, Open drain, Fast */
  121. //GPIOB->DDR |= (GPIO_PIN_4 | GPIO_PIN_5);
  122. //GPIOB->ODR |= (GPIO_PIN_4 | GPIO_PIN_5);
  123. //GPIOB->CR2 |= (GPIO_PIN_4 | GPIO_PIN_5);
  124. /** Configure ADC1 peripheral */
  125. /* Configure the data alignment */
  126. ADC1->CR2 |= (uint8_t)(ADC1_CR2_ALIGN);
  127. /* Set the single conversion mode */
  128. ADC1->CR1 &= (uint8_t)(~ADC1_CR1_CONT);
  129. /* Clear the ADC1 channels */
  130. ADC1->CSR &= (uint8_t)(~ADC1_CSR_CH);
  131. /* Select the ADC1 channel */
  132. ADC1->CSR |= (uint8_t)(ADC_CHNLU);
  133. /* Clear the SPSEL bits */
  134. ADC1->CR1 &= (uint8_t)(~ADC1_CR1_SPSEL);
  135. /* Select the prescaler division factor according to ADC1_PrescalerSelection values */
  136. ADC1->CR1 |= (uint8_t)(ADC1_PRESSEL_FCPU_D8);
  137. /* Enable the external Trigger */
  138. ADC1->CR2 |= (uint8_t)(ADC1_CR2_EXTTRIG);
  139. /* Set the 'Internal TIM1 TRGO event' as external trigger */
  140. ADC1->CR2 &= (uint8_t)~(ADC1_CR2_EXTSEL);
  141. /* disables the ADC1 Schmitt Trigger on a selected channel(s) */
  142. ADC1->TDRL |= (uint8_t)((uint8_t)0x01 << (uint8_t)ADC_SCHTU);
  143. /* Enable the ADC1 peripheral */
  144. ADC1->CR1 |= ADC1_CR1_ADON;
  145. /* Enable the ADC1 EOC interrupt */
  146. ADC1->CSR |= (uint8_t)ADC1_IT_EOCIE;
  147. /*Start Conversion */
  148. ADC1->CR1 |= ADC1_CR1_ADON;
  149. /** Configure TIM1 peripheral. Time Base configuration:
  150. Timer period - 3,125 ms
  151. TIM1_Period = 50
  152. TIM1_Prescaler = 1000
  153. TIM1_CounterMode = TIM1_COUNTERMODE_UP
  154. TIM1_RepetitionCounter = 0
  155. */
  156. /* Set the Autoreload value */
  157. TIM1->ARRH = (uint8_t)(49 >> 8);
  158. TIM1->ARRL = (uint8_t)(49);
  159. /* Set the Prescaler value */
  160. TIM1->PSCRH = (uint8_t)(999 >> 8);
  161. TIM1->PSCRL = (uint8_t)(999);
  162. /* Select the Counter Mode */
  163. TIM1->CR1 = 0x0;
  164. /* Set the Repetition Counter value */
  165. TIM1->RCR = 0;
  166. /* Trigrer configuration */
  167. TIM1->CR2 = (uint8_t)TIM1_TRGOSOURCE_UPDATE;
  168. /* Update Interrupt Enable */
  169. //TIM1->IER |= (uint8_t)TIM1_IT_UPDATE;
  170. /* Enable TIM1 */
  171. TIM1->CR1 |= TIM1_CR1_CEN;
  172. /**
  173. TIM4 configuration:
  174. - TIM4CLK is set to 16 MHz, the TIM4 Prescaler is equal to 128 so the TIM1 counter
  175. clock used is 16 MHz / 128 = 125 000 Hz
  176. - With 125 000 Hz we can generate time base:
  177. max time base is 2.048 ms if TIM4_PERIOD = 255 --> (255 + 1) / 125000 = 2.048 ms
  178. min time base is 0.016 ms if TIM4_PERIOD = 1 --> ( 1 + 1) / 125000 = 0.016 ms
  179. - In this example we need to generate a time base equal to 1 ms
  180. so TIM4_PERIOD = (0.001 * 125000 - 1) = 124
  181. */
  182. /* Time base configuration. Set the Prescaler value */
  183. TIM4->PSCR = (uint8_t)(TIM4_PRESCALER_128);
  184. /* Set the Autoreload value */
  185. TIM4->ARR = (uint8_t)(TIM4_PERIOD);
  186. /* Clear TIM4 update flag */
  187. TIM4->SR1 = (uint8_t)(~TIM4_FLAG_UPDATE);
  188. /* Enable update interrupt */
  189. TIM4->IER |= (uint8_t)TIM4_IT_UPDATE;
  190. /* enable interrupts */
  191. enableInterrupts();
  192. /* Enable TIM4 */
  193. TIM4->CR1 |= (uint8_t)TIM4_CR1_CEN;
  194. }
  195. /************************ (C) COPYRIGHT Shilov V.N. *****END OF FILE****/