main.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 "i2c.h"
  22. #include "led.h"
  23. #include "delay.h"
  24. #include "sensor.h"
  25. /* Private define ------------------------------------------------------------*/
  26. #define TIM4_PERIOD (uint8_t)124
  27. /* Private typedef -----------------------------------------------------------*/
  28. /* Private constants ---------------------------------------------------------*/
  29. /* Private variables ---------------------------------------------------------*/
  30. __IO uint16_t ConversionBuffer[ADC_SMPLS];
  31. __IO uint8_t BufferIndex = 0;
  32. static aht20_t Sensor;
  33. /* Private function prototypes -----------------------------------------------*/
  34. static void boardInit(void);
  35. static void showT(void);
  36. static void showH(void);
  37. void main(void)
  38. {
  39. /* Board Configuration */
  40. boardInit();
  41. Delay(1000);
  42. /* I2C Configuration */
  43. i2c_init();
  44. AHT20_Init();
  45. LedDigits[3] = led_O;
  46. LedDigits[7] = led_H;
  47. /* Infinite loop */
  48. while (1) {
  49. AHT20_StartMeasure();
  50. LedPoint[1] = 0;
  51. LedPoint[5] = 0;
  52. Delay(450);
  53. if (AHT20_GetData(&Sensor)) {
  54. LedDigits[0] = led_Minus;
  55. LedDigits[1] = led_Minus;
  56. LedDigits[2] = led_Minus;
  57. LedDigits[4] = led_Minus;
  58. LedDigits[5] = led_Minus;
  59. LedDigits[6] = led_Minus;
  60. } else {
  61. showT();
  62. showH();
  63. }
  64. LedPoint[1] = 1;
  65. LedPoint[5] = 1;
  66. Delay(450);
  67. }
  68. }
  69. /* Private functions ---------------------------------------------------------*/
  70. static void showT(void) {
  71. int16_t t = Sensor.Temperature;
  72. uint8_t a;
  73. if (t < 0) {
  74. t *= -1;
  75. }
  76. LedDigits[0] = t / 100;
  77. a = t % 100;
  78. LedDigits[1] = a / 10;
  79. LedDigits[2] = a % 10;
  80. }
  81. static void showH(void) {
  82. uint16_t h = Sensor.Humidity;
  83. uint8_t a;
  84. LedDigits[4] = h / 100;
  85. a = h % 100;
  86. LedDigits[5] = a / 10;
  87. LedDigits[6] = a % 10;
  88. }
  89. static void boardInit(void) {
  90. /* Насколько я понял, для частот выше 16МГц нужно вводить задержку при работе с FLASH:
  91. - Before using the HSE clock make sure that the "Flash_Wait_States" is set to 1.
  92. - To do so :
  93. - with STVD (menu: Debug Instrument -> MCU configuration -> Options)
  94. - with EWSTM8 (menu: ST-LINK -> Option bytes ->Flash_Wait_States: 1)
  95. */
  96. /* Initialization of the clock to 16MHz */
  97. CLK->CKDIVR = 0x00;
  98. /* Disable clock of unused peripherial */
  99. CLK->PCKENR1 = (uint8_t)(~(CLK_PCKENR1_UART1 | CLK_PCKENR1_SPI));
  100. CLK->PCKENR2 = (uint8_t)(~(CLK_PCKENR2_CAN | CLK_PCKENR2_AWU)); // | CLK_PCKENR2_ADC
  101. /* SWIM clock */
  102. CLK->SWIMCCR = 0x00;
  103. /* Configure GPIO used for LED to Push-Pull, High, Fast*/
  104. LED_SEG1_PORT->ODR |= (uint8_t)LED_SEG1_PINS;
  105. LED_SEG1_PORT->DDR |= (uint8_t)LED_SEG1_PINS;
  106. LED_SEG1_PORT->CR1 |= (uint8_t)LED_SEG1_PINS;
  107. LED_SEG1_PORT->CR2 |= (uint8_t)LED_SEG1_PINS;
  108. LED_SEG2_PORT->ODR |= (uint8_t)LED_SEG2_PINS;
  109. LED_SEG2_PORT->DDR |= (uint8_t)LED_SEG2_PINS;
  110. LED_SEG2_PORT->CR1 |= (uint8_t)LED_SEG2_PINS;
  111. LED_SEG2_PORT->CR2 |= (uint8_t)LED_SEG2_PINS;
  112. LED_SEG3_PORT->ODR |= (uint8_t)LED_SEG3_PINS;
  113. LED_SEG3_PORT->DDR |= (uint8_t)LED_SEG3_PINS;
  114. LED_SEG3_PORT->CR1 |= (uint8_t)LED_SEG3_PINS;
  115. LED_SEG3_PORT->CR2 |= (uint8_t)LED_SEG3_PINS;
  116. /* `SPI` pins to Push-Pull, High, Fast */
  117. SPI_PORT->ODR |= (uint8_t)(SPI_SCK|SPI_DATA);
  118. SPI_PORT->DDR |= (uint8_t)(SPI_SCK|SPI_DATA);
  119. SPI_PORT->CR1 |= (uint8_t)(SPI_SCK|SPI_DATA);
  120. SPI_PORT->CR2 |= (uint8_t)(SPI_SCK|SPI_DATA);
  121. /* I2C GPIO SDA SCL - HiZ, Open drain, Fast */
  122. GPIOB->DDR |= (GPIO_PIN_4 | GPIO_PIN_5);
  123. GPIOB->ODR |= (GPIO_PIN_4 | GPIO_PIN_5);
  124. GPIOB->CR2 |= (GPIO_PIN_4 | GPIO_PIN_5);
  125. /** Configure ADC */
  126. /* De-Init ADC peripheral*/
  127. //ADC1_DeInit();
  128. /* Init ADC1 peripheral */
  129. //ADC1_Init(ADC1_CONVERSIONMODE_SINGLE, ADC_CHNLU, ADC1_PRESSEL_FCPU_D12, \
  130. ADC1_EXTTRIG_TIM, ENABLE, ADC1_ALIGN_RIGHT, ADC_SCHTU, DISABLE);
  131. /* Enable EOC interrupt */
  132. //ADC1_ITConfig(ADC1_IT_EOCIE, ENABLE);
  133. /*Start Conversion */
  134. ////ADC1_StartConversion();
  135. /** Configure TIM1 */
  136. //TIM1_DeInit();
  137. /* Time Base configuration */
  138. /*
  139. Timer period - 3,125 ms
  140. TIM1_Period = 50
  141. TIM1_Prescaler = 1000
  142. TIM1_CounterMode = TIM1_COUNTERMODE_UP
  143. TIM1_RepetitionCounter = 0
  144. */
  145. //TIM1_TimeBaseInit(999, TIM1_COUNTERMODE_UP, 49, 0);
  146. /* Trigrer configuration */
  147. //TIM1_SelectOutputTrigger(TIM1_TRGOSOURCE_UPDATE);
  148. /* Update Interrupt Enable */
  149. ////TIM1_ITConfig(TIM1_IT_UPDATE, ENABLE);
  150. /* Enable TIM1 */
  151. //TIM1_Cmd(ENABLE);
  152. /**
  153. TIM4 configuration:
  154. - TIM4CLK is set to 16 MHz, the TIM4 Prescaler is equal to 128 so the TIM1 counter
  155. clock used is 16 MHz / 128 = 125 000 Hz
  156. - With 125 000 Hz we can generate time base:
  157. max time base is 2.048 ms if TIM4_PERIOD = 255 --> (255 + 1) / 125000 = 2.048 ms
  158. min time base is 0.016 ms if TIM4_PERIOD = 1 --> ( 1 + 1) / 125000 = 0.016 ms
  159. - In this example we need to generate a time base equal to 1 ms
  160. so TIM4_PERIOD = (0.001 * 125000 - 1) = 124
  161. */
  162. /* Time base configuration. Set the Prescaler value */
  163. TIM4->PSCR = (uint8_t)(TIM4_PRESCALER_128);
  164. /* Set the Autoreload value */
  165. TIM4->ARR = (uint8_t)(TIM4_PERIOD);
  166. /* Clear TIM4 update flag */
  167. TIM4->SR1 = (uint8_t)(~TIM4_FLAG_UPDATE);
  168. /* Enable update interrupt */
  169. TIM4->IER |= (uint8_t)TIM4_IT_UPDATE;
  170. /* enable interrupts */
  171. enableInterrupts();
  172. /* Enable TIM4 */
  173. TIM4->CR1 |= (uint8_t)TIM4_CR1_CEN;
  174. }
  175. /************************ (C) COPYRIGHT Shilov V.N. *****END OF FILE****/