/** ****************************************************************************** * @file Project/main.c * @author "Volodymyr M. Shylov" * @version v.0.1 * @date 24-11-2023 * @brief Main program body. VA-meter from DC/DC XL4005 ****************************************************************************** * @attention * * "THE BEER-WARE LICENSE" (Revision 42): * wrote this file. As long as you retain this notice you * can do whatever you want with this stuff. If we meet some day, and you think * this stuff is worth it, you can buy me a beer in return. Shilov V.N. * ****************************************************************************** */ /* Includes ------------------------------------------------------------------*/ #include "stm8s.h" #include "board.h" #include "i2c.h" #include "led.h" #include "delay.h" #include "sensor.h" /* Private define ------------------------------------------------------------*/ #define TIM4_PERIOD (uint8_t)124 /* Private typedef -----------------------------------------------------------*/ /* Private constants ---------------------------------------------------------*/ /* Private variables ---------------------------------------------------------*/ __IO uint16_t ConversionBuffer[ADC_SMPLS]; __IO uint8_t BufferIndex = 0; static aht20_t Sensor; /* Private function prototypes -----------------------------------------------*/ static void boardInit(void); static void showT(void); static void showH(void); void main(void) { /* Board Configuration */ boardInit(); Delay(1000); /* I2C Configuration */ i2c_init(); AHT20_Init(); LedDigits[3] = led_O; LedDigits[7] = led_H; /* Infinite loop */ while (1) { AHT20_StartMeasure(); LedPoint[1] = 0; LedPoint[5] = 0; Delay(450); if (AHT20_GetData(&Sensor)) { LedDigits[0] = led_Minus; LedDigits[1] = led_Minus; LedDigits[2] = led_Minus; LedDigits[4] = led_Minus; LedDigits[5] = led_Minus; LedDigits[6] = led_Minus; } else { showT(); showH(); } LedPoint[1] = 1; LedPoint[5] = 1; Delay(450); } } /* Private functions ---------------------------------------------------------*/ static void showT(void) { int16_t t = Sensor.Temperature; uint8_t a; if (t < 0) { t *= -1; } LedDigits[0] = t / 100; a = t % 100; LedDigits[1] = a / 10; LedDigits[2] = a % 10; } static void showH(void) { uint16_t h = Sensor.Humidity; uint8_t a; LedDigits[4] = h / 100; a = h % 100; LedDigits[5] = a / 10; LedDigits[6] = a % 10; } static void boardInit(void) { /* Насколько я понял, для частот выше 16МГц нужно вводить задержку при работе с FLASH: - Before using the HSE clock make sure that the "Flash_Wait_States" is set to 1. - To do so : - with STVD (menu: Debug Instrument -> MCU configuration -> Options) - with EWSTM8 (menu: ST-LINK -> Option bytes ->Flash_Wait_States: 1) */ /* Initialization of the clock to 16MHz */ CLK->CKDIVR = 0x00; /* Disable clock of unused peripherial */ CLK->PCKENR1 = (uint8_t)(~(CLK_PCKENR1_UART1 | CLK_PCKENR1_SPI)); CLK->PCKENR2 = (uint8_t)(~(CLK_PCKENR2_CAN | CLK_PCKENR2_AWU)); // | CLK_PCKENR2_ADC /* SWIM clock */ CLK->SWIMCCR = 0x00; /* Configure GPIO used for LED to Push-Pull, High, Fast*/ LED_SEG1_PORT->ODR |= (uint8_t)LED_SEG1_PINS; LED_SEG1_PORT->DDR |= (uint8_t)LED_SEG1_PINS; LED_SEG1_PORT->CR1 |= (uint8_t)LED_SEG1_PINS; LED_SEG1_PORT->CR2 |= (uint8_t)LED_SEG1_PINS; LED_SEG2_PORT->ODR |= (uint8_t)LED_SEG2_PINS; LED_SEG2_PORT->DDR |= (uint8_t)LED_SEG2_PINS; LED_SEG2_PORT->CR1 |= (uint8_t)LED_SEG2_PINS; LED_SEG2_PORT->CR2 |= (uint8_t)LED_SEG2_PINS; LED_SEG3_PORT->ODR |= (uint8_t)LED_SEG3_PINS; LED_SEG3_PORT->DDR |= (uint8_t)LED_SEG3_PINS; LED_SEG3_PORT->CR1 |= (uint8_t)LED_SEG3_PINS; LED_SEG3_PORT->CR2 |= (uint8_t)LED_SEG3_PINS; /* `SPI` pins to Push-Pull, High, Fast */ SPI_PORT->ODR |= (uint8_t)(SPI_SCK|SPI_DATA); SPI_PORT->DDR |= (uint8_t)(SPI_SCK|SPI_DATA); SPI_PORT->CR1 |= (uint8_t)(SPI_SCK|SPI_DATA); SPI_PORT->CR2 |= (uint8_t)(SPI_SCK|SPI_DATA); /* I2C GPIO SDA SCL - HiZ, Open drain, Fast */ GPIOB->DDR |= (GPIO_PIN_4 | GPIO_PIN_5); GPIOB->ODR |= (GPIO_PIN_4 | GPIO_PIN_5); GPIOB->CR2 |= (GPIO_PIN_4 | GPIO_PIN_5); /** Configure ADC */ /* De-Init ADC peripheral*/ //ADC1_DeInit(); /* Init ADC1 peripheral */ //ADC1_Init(ADC1_CONVERSIONMODE_SINGLE, ADC_CHNLU, ADC1_PRESSEL_FCPU_D12, \ ADC1_EXTTRIG_TIM, ENABLE, ADC1_ALIGN_RIGHT, ADC_SCHTU, DISABLE); /* Enable EOC interrupt */ //ADC1_ITConfig(ADC1_IT_EOCIE, ENABLE); /*Start Conversion */ ////ADC1_StartConversion(); /** Configure TIM1 */ //TIM1_DeInit(); /* Time Base configuration */ /* Timer period - 3,125 ms TIM1_Period = 50 TIM1_Prescaler = 1000 TIM1_CounterMode = TIM1_COUNTERMODE_UP TIM1_RepetitionCounter = 0 */ //TIM1_TimeBaseInit(999, TIM1_COUNTERMODE_UP, 49, 0); /* Trigrer configuration */ //TIM1_SelectOutputTrigger(TIM1_TRGOSOURCE_UPDATE); /* Update Interrupt Enable */ ////TIM1_ITConfig(TIM1_IT_UPDATE, ENABLE); /* Enable TIM1 */ //TIM1_Cmd(ENABLE); /** TIM4 configuration: - TIM4CLK is set to 16 MHz, the TIM4 Prescaler is equal to 128 so the TIM1 counter clock used is 16 MHz / 128 = 125 000 Hz - With 125 000 Hz we can generate time base: max time base is 2.048 ms if TIM4_PERIOD = 255 --> (255 + 1) / 125000 = 2.048 ms min time base is 0.016 ms if TIM4_PERIOD = 1 --> ( 1 + 1) / 125000 = 0.016 ms - In this example we need to generate a time base equal to 1 ms so TIM4_PERIOD = (0.001 * 125000 - 1) = 124 */ /* Time base configuration. Set the Prescaler value */ TIM4->PSCR = (uint8_t)(TIM4_PRESCALER_128); /* Set the Autoreload value */ TIM4->ARR = (uint8_t)(TIM4_PERIOD); /* Clear TIM4 update flag */ TIM4->SR1 = (uint8_t)(~TIM4_FLAG_UPDATE); /* Enable update interrupt */ TIM4->IER |= (uint8_t)TIM4_IT_UPDATE; /* enable interrupts */ enableInterrupts(); /* Enable TIM4 */ TIM4->CR1 |= (uint8_t)TIM4_CR1_CEN; } /************************ (C) COPYRIGHT Shilov V.N. *****END OF FILE****/