123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306 |
- /**
- ******************************************************************************
- * @file VAPC-meter
- * @author Vladimir N. Shilov <shilow@ukr.net>
- * @version V0.0.1
- * @date 2015.05.15
- * @brief Main program body
- ******************************************************************************
- * @attention
- *
- * ----------------------------------------------------------------------------
- * "THE BEER-WARE LICENSE" (Revision 42):
- * <shilow@ukr.net> 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 "stm8l15x.h"
- #include "rtos.h"
- #include "max7219.h"
- #include "adc.h"
- /** @addtogroup STM8L15x_StdPeriph_Template
- * @{
- */
- /* Private typedef -----------------------------------------------------------*/
- /* Private define ------------------------------------------------------------*/
- #define LED_RED_PORT GPIOC
- #define LED_RED_PIN GPIO_Pin_4
- #define LED_GREEN_PORT GPIOB
- #define LED_GREEN_PIN GPIO_Pin_7
- #define VALUES_BUFFER_SIZE 10
- /* Private macro -------------------------------------------------------------*/
- #define LED_RED_ON LED_RED_PORT->ODR &= (uint8_t)(~LED_RED_PIN)
- #define LED_RED_OFF LED_RED_PORT->ODR |= LED_RED_PIN
- #define LED_GREEN_ON LED_GREEN_PORT->ODR &= (uint8_t)(~LED_GREEN_PIN)
- #define LED_GREEN_OFF LED_GREEN_PORT->ODR |= LED_GREEN_PIN
- /* Private constant ----------------------------------------------------------*/
- // перевод числа 0-7 в номер индикатора
- static const max7219_reg_t dig[8] = {
- RegDigit0, RegDigit1, RegDigit2, RegDigit3,
- RegDigit4, RegDigit5, RegDigit6, RegDigit7
- };
- // перевод значения 0x00 - 0x0F в код индикатора
- static const max7219_sym_t num[16] = {
- Sym_0, Sym_1, Sym_2, Sym_3, Sym_4, Sym_5, Sym_6, Sym_7,
- Sym_8, Sym_9, Sym_A, Sym_b, Sym_C, Sym_d, Sym_E, Sym_F
- };
- /* Private variables ---------------------------------------------------------*/
- static uint16_t bufVoltage[VALUES_BUFFER_SIZE];
- static uint16_t bufCurrent[VALUES_BUFFER_SIZE];
- /* averaged values for 1 sek */
- static uint16_t Voltage;
- static uint16_t Current;
- static uint32_t Power;
- static uint32_t CapacityAH = 0;
- static uint32_t CapacityWH = 0;
- /* Private function prototypes -----------------------------------------------*/
- static void GPIO_Config(void);
- static void CLK_Config(void);
- /* RTOS function prototypes -----------------------------------------------*/
- static void getADCValues(void);
- static void calculateValues(void);
- static void showTopLineU(void);
- static void showBotLineI(void);
- /* Private functions ---------------------------------------------------------*/
- /**
- * @brief Main program.
- * @param None
- * @retval None
- */
- void main(void)
- {
- /* Clock configuration -----------------------------------------*/
- CLK_Config();
- /* GPIO Configuration -----------------------------------------*/
- GPIO_Config();
- /* RTOS Configuration */
- RTOS_Config();
- /* ADC Configuration and start */
- ADC_Config();
- /* MAX7219 Configuration */
- MAX7219_Config();
- /* ROTS tasks */
- RTOS_SetTask(getADCValues,100,100);
- RTOS_SetTask(calculateValues,101,1000);
- RTOS_SetTask(showTopLineU,102,250);
- RTOS_SetTask(showBotLineI,103,250);
- /* Infinite loop */
- while (1) {
- RTOS_DispatchTask();
- wfi();
- }
- }
- /**
- * Забираем обработанные данные из быстрого буфера
- * и складываем в медленный.
- */
- static void getADCValues(void) {
- static uint8_t idxVal=0;
- uint16_t * avgVal;
- avgVal = ADC_GetValues();
- bufVoltage[idxVal] = *avgVal;
- bufCurrent[idxVal] = *(avgVal + 1);
- idxVal ++;
- if (idxVal == VALUES_BUFFER_SIZE) {
- idxVal = 0;
- }
- }
- /**
- * Average values from slow buffer.
- * Calculate Power, Capacitance IH and WH.
- */
- static void calculateValues(void) {
- uint8_t i;
- uint16_t c=0;
- uint32_t v=0;
- for (i=0; i<VALUES_BUFFER_SIZE; i++) {
- v += bufVoltage[i];
- c += bufCurrent[i];
- }
- Current = (c + (VALUES_BUFFER_SIZE/2)) / VALUES_BUFFER_SIZE;
- Voltage = (v + (VALUES_BUFFER_SIZE/2)) / VALUES_BUFFER_SIZE;
- Power = ((Voltage * Current) + 500) / 1000;
- CapacityAH += Current;
- CapacityWH += Power;
- }
- /**
- * Output voltage values to top indicator
- */
- static void showTopLineU(void) {
- uint32_t vlt = 0;
- uint8_t tmp;
- for (tmp=0; tmp<VALUES_BUFFER_SIZE; tmp++) {
- vlt += bufVoltage[tmp];
- }
- vlt /= VALUES_BUFFER_SIZE;
- // LED_RED_ON;
- if(vlt >= 10000){
- /* Tens Thousands voltage value*/
- tmp = (uint8_t)(vlt / 10000);
- vlt %= 10000;
- MAX7219_WriteData(dig[0], num[tmp]);
- /* Thousands voltage value*/
- tmp = (uint8_t)(vlt / 1000);
- vlt %= 1000;
- MAX7219_WriteData(dig[1], (num[tmp] | SYM_DOT));
- /* Hundreds voltage value */
- tmp = (uint8_t)(vlt / 100);
- vlt %= 100;
- MAX7219_WriteData(dig[2], num[tmp]);
- /* Tens voltage value */
- tmp = (uint8_t)(vlt / 10);
- MAX7219_WriteData(dig[3], num[tmp]);
- } else {
- /* Thousands voltage value*/
- tmp = (uint8_t)(vlt / 1000);
- vlt %= 1000;
- MAX7219_WriteData(dig[0], (num[tmp] | SYM_DOT));
- /* Hundreds voltage value */
- tmp = (uint8_t)(vlt / 100);
- vlt %= 100;
- MAX7219_WriteData(dig[1], num[tmp]);
- /* Tens voltage value */
- tmp = (uint8_t)(vlt / 10);
- vlt %= 10;
- MAX7219_WriteData(dig[2], num[tmp]);
- /* Ones voltage value */
- MAX7219_WriteData(dig[3], num[(uint8_t)vlt]);
- }
- }
- /**
- * Output current values to bottom indicator
- */
- static void showBotLineI(void){
- // LED_RED_OFF; // индикация режима
- uint16_t crnt = 0;
- uint8_t tmp;
- for (tmp=0; tmp<VALUES_BUFFER_SIZE; tmp++) {
- crnt += bufCurrent[tmp];
- }
- crnt /= VALUES_BUFFER_SIZE;
- if (crnt >= 1000) {
- tmp = crnt / 1000;
- crnt %= 1000;
- } else {
- tmp = 0;
- }
- MAX7219_WriteData(dig[4], (num[tmp] | SYM_DOT));
- if (crnt >= 100) {
- tmp = crnt / 100;
- crnt %= 100;
- } else {
- tmp = 0;
- }
- MAX7219_WriteData(dig[5], num[tmp]);
- if (crnt >= 10) {
- tmp = crnt / 10;
- crnt %= 10;
- } else {
- tmp = 0;
- }
- MAX7219_WriteData(dig[6], num[tmp]);
- MAX7219_WriteData(dig[7], num[crnt]);
- }
- /**
- * @brief Configure GPIO for button available on the VAPC board
- * @param None
- * @retval None
- */
- static void GPIO_Config(void)
- {
- /* Configure GPIO used to drive LEDs */
- GPIO_Init(LED_RED_PORT, LED_RED_PIN, GPIO_Mode_Out_PP_High_Fast);
- GPIO_Init(LED_GREEN_PORT, LED_GREEN_PIN, GPIO_Mode_Out_PP_High_Fast);
- /* Enable general interrupts */
- enableInterrupts();
- }
- /**
- * @brief Configure system clock to run at Maximum clock speed
- * @param None
- * @retval None
- */
- static void CLK_Config(void)
- {
- CLK_DeInit();
- /* Configure the clock source */
- CLK_SYSCLKSourceConfig(CLK_SYSCLKSource_HSI);
- /* Configure the System clock frequency to run at 16Mhz */
- CLK_SYSCLKDivConfig(CLK_SYSCLKDiv_1);
- CLK_HSICmd(ENABLE);
- }
- #ifdef USE_FULL_ASSERT
- /**
- * @brief Reports the name of the source file and the source line number
- * where the assert_param error has occurred.
- * @param file: pointer to the source file name
- * @param line: assert_param error line source number
- * @retval None
- */
- void assert_failed(uint8_t* file, uint32_t line)
- {
- /* User can add his own implementation to report the file name and line number,
- ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
- /* Infinite loop */
- while (1)
- {
- }
- }
- #endif
- /**
- * @}
- */
- /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
|