|
@@ -1,9 +1,9 @@
|
|
/**
|
|
/**
|
|
******************************************************************************
|
|
******************************************************************************
|
|
- * @file Project/main.c
|
|
|
|
|
|
+ * @file Project/main.c
|
|
* @author "Vladimir N. Shilov" <shilow@ukr.net>
|
|
* @author "Vladimir N. Shilov" <shilow@ukr.net>
|
|
- * @version V0.0.1
|
|
|
|
- * @date 18-December-2018
|
|
|
|
|
|
+ * @version v.0.4
|
|
|
|
+ * @date 08-July-2019
|
|
* @brief Main program body
|
|
* @brief Main program body
|
|
******************************************************************************
|
|
******************************************************************************
|
|
* @attention
|
|
* @attention
|
|
@@ -14,27 +14,777 @@
|
|
* this stuff is worth it, you can buy me a beer in return. Shilov V.N.
|
|
* this stuff is worth it, you can buy me a beer in return. Shilov V.N.
|
|
*
|
|
*
|
|
******************************************************************************
|
|
******************************************************************************
|
|
- */
|
|
|
|
|
|
+ */
|
|
|
|
|
|
|
|
|
|
/* Includes ------------------------------------------------------------------*/
|
|
/* Includes ------------------------------------------------------------------*/
|
|
#include "stm8s.h"
|
|
#include "stm8s.h"
|
|
|
|
+#include "board.h"
|
|
|
|
+#include "rtos.h"
|
|
|
|
+#include "max7219.h"
|
|
|
|
+#include "i2c.h"
|
|
|
|
+#include "ina219.h"
|
|
|
|
+
|
|
|
|
+/* Private define ------------------------------------------------------------*/
|
|
|
|
+// кнопка считается нажатой через BTN_FACTOR * BTN_SCAN_PERIOD ms
|
|
|
|
+#define BTN_SCAN_PERIOD 10
|
|
|
|
+#define BTN_FACTOR 5
|
|
|
|
+
|
|
|
|
+/* Private typedef -----------------------------------------------------------*/
|
|
|
|
+typedef enum _mode_led {
|
|
|
|
+ dispNoState = 0x00,
|
|
|
|
+ displayV,
|
|
|
|
+ displayI,
|
|
|
|
+ displayP,
|
|
|
|
+ displayCI,
|
|
|
|
+ displayCP,
|
|
|
|
+ displaySh,
|
|
|
|
+ displayT
|
|
|
|
+} mode_led_t;
|
|
|
|
+
|
|
|
|
+typedef enum _events {
|
|
|
|
+ eventNoEvent = 0x00,
|
|
|
|
+ eventShortPress,
|
|
|
|
+ eventLongPress
|
|
|
|
+} event_t;
|
|
|
|
+
|
|
|
|
+typedef enum _button_name {
|
|
|
|
+ S1 = 0x00,
|
|
|
|
+ S2 = 0x01
|
|
|
|
+} button_name;
|
|
|
|
+
|
|
|
|
+typedef struct _button_pin {
|
|
|
|
+ GPIO_TypeDef * port;
|
|
|
|
+ GPIO_Pin_TypeDef pin;
|
|
|
|
+} button_t;
|
|
|
|
+
|
|
|
|
+/* Private constants ---------------------------------------------------------*/
|
|
|
|
+// перевод числа 0-7 в номер индикатора
|
|
|
|
+const static max7219_reg_t digitPosition[8] = {
|
|
|
|
+ RegDigit0, RegDigit1, RegDigit2, RegDigit3,
|
|
|
|
+ RegDigit4, RegDigit5, RegDigit6, RegDigit7
|
|
|
|
+};
|
|
|
|
+// перевод значения 0x00 - 0x0F в код индикатора
|
|
|
|
+const static max7219_sym_t digitValue[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
|
|
|
|
+};
|
|
|
|
+// определения пинов кнопок
|
|
|
|
+const static button_t Button[BUTTON_NUM] = {
|
|
|
|
+ {BUTTON1_PORT, BUTTON1_PIN},
|
|
|
|
+ {BUTTON2_PORT, BUTTON2_PIN},
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+/* Private variables ---------------------------------------------------------*/
|
|
|
|
+static uint16_t Voltage;
|
|
|
|
+static int16_t ShuntV;
|
|
|
|
+static uint16_t Current;
|
|
|
|
+static uint32_t Power;
|
|
|
|
+static uint32_t CapacityAH = 0;
|
|
|
|
+static uint32_t CapacityWH = 0;
|
|
|
|
+static struct _timer {
|
|
|
|
+ uint8_t hh;
|
|
|
|
+ uint8_t mm;
|
|
|
|
+ uint8_t ss;
|
|
|
|
+} Timer;
|
|
|
|
+
|
|
|
|
+static uint8_t btn1Cnt;
|
|
|
|
+static uint8_t btn2Cnt;
|
|
|
|
+
|
|
|
|
+static mode_led_t modeTopLine, modeBotLine;
|
|
|
|
|
|
-/* Private defines -----------------------------------------------------------*/
|
|
|
|
/* Private function prototypes -----------------------------------------------*/
|
|
/* Private function prototypes -----------------------------------------------*/
|
|
-/* Private functions ---------------------------------------------------------*/
|
|
|
|
|
|
+static void boardInit(void);
|
|
|
|
+
|
|
|
|
+static void showValue(uint32_t val, uint8_t pos);
|
|
|
|
+static void showU(uint8_t pos);
|
|
|
|
+static void showI(uint8_t pos);
|
|
|
|
+static void showP(uint8_t pos);
|
|
|
|
+static void showCI(uint8_t pos);
|
|
|
|
+static void showCP(uint8_t pos);
|
|
|
|
+static void showSH(uint8_t pos);
|
|
|
|
+static void showT(uint8_t pos);
|
|
|
|
+
|
|
|
|
+static void showLabelU(uint8_t pos);
|
|
|
|
+static void showLabelI(uint8_t pos);
|
|
|
|
+static void showLabelP(uint8_t pos);
|
|
|
|
+static void showLabelAH(uint8_t pos);
|
|
|
|
+static void showLabelWH(uint8_t pos);
|
|
|
|
+static void showLabelSH(uint8_t pos);
|
|
|
|
+static void showLabelT(uint8_t pos);
|
|
|
|
+
|
|
|
|
+static void displayMode(mode_led_t * disp, event_t event, uint8_t pos);
|
|
|
|
+
|
|
|
|
+static void blankLine(uint8_t pos);
|
|
|
|
+
|
|
|
|
+/* RTOS function prototypes -----------------------------------------------*/
|
|
|
|
+static void readINAValues(void);
|
|
|
|
+static void calculateValues(void);
|
|
|
|
+static void showTopLineU(void);
|
|
|
|
+static void showTopLineI(void);
|
|
|
|
+static void showTopLineP(void);
|
|
|
|
+static void showTopLineAH(void);
|
|
|
|
+static void showTopLineWH(void);
|
|
|
|
+static void showTopLineSH(void);
|
|
|
|
+static void showTopLineT(void);
|
|
|
|
+static void showBotLineU(void);
|
|
|
|
+static void showBotLineI(void);
|
|
|
|
+static void showBotLineP(void);
|
|
|
|
+static void showBotLineAH(void);
|
|
|
|
+static void showBotLineWH(void);
|
|
|
|
+static void showBotLineSH(void);
|
|
|
|
+static void showBotLineT(void);
|
|
|
|
+
|
|
|
|
+static void blankTopLine(void);
|
|
|
|
+static void blankBotLine(void);
|
|
|
|
+
|
|
|
|
+static void btnScan(void);
|
|
|
|
+static void btnTest(button_t const *btn, uint8_t *pressCount, void (*taskShortPress)(void), void (*taskLongPress)(void));
|
|
|
|
+static void btn1Short(void);
|
|
|
|
+static void btn1Long(void);
|
|
|
|
+static void btn2Short(void);
|
|
|
|
+static void btn2Long(void);
|
|
|
|
|
|
void main(void)
|
|
void main(void)
|
|
{
|
|
{
|
|
|
|
+ /* Board Configuration */
|
|
|
|
+ boardInit();
|
|
|
|
+
|
|
|
|
+ /* RTOS Configuration */
|
|
|
|
+ RTOS_Init();
|
|
|
|
+ enableInterrupts();
|
|
|
|
+
|
|
|
|
+ /* MAX7219 Configuration */
|
|
|
|
+ MAX7219_Config();
|
|
|
|
+ blankTopLine();
|
|
|
|
+ blankBotLine();
|
|
|
|
+
|
|
|
|
+ /* I2C Configuration */
|
|
|
|
+ i2c_master_init();
|
|
|
|
+
|
|
|
|
+ /* INA219 Configuration */
|
|
|
|
+ INA219_Config();
|
|
|
|
+
|
|
|
|
+ /* ROTS tasks */
|
|
|
|
+ RTOS_SetTask(btnScan, 0, BTN_SCAN_PERIOD);
|
|
|
|
+ RTOS_SetTask(readINAValues, 69, 250);
|
|
|
|
+ RTOS_SetTask(calculateValues, 70, 1000);
|
|
|
|
+ RTOS_SetTask(showTopLineU, 71, 250);
|
|
|
|
+ RTOS_SetTask(showBotLineI, 72, 250);
|
|
|
|
+
|
|
|
|
+ modeTopLine = displayV;
|
|
|
|
+ modeBotLine = displayI;
|
|
|
|
+
|
|
/* Infinite loop */
|
|
/* Infinite loop */
|
|
- while (1)
|
|
|
|
- {
|
|
|
|
|
|
+ while (1) {
|
|
|
|
+ RTOS_DispatchTask();
|
|
|
|
+ wfi();
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
}
|
|
}
|
|
|
|
|
|
-#ifdef USE_FULL_ASSERT
|
|
|
|
|
|
+/* Private functions ---------------------------------------------------------*/
|
|
|
|
+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)
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+ ErrorStatus status = ERROR;
|
|
|
|
+
|
|
|
|
+ /* Initialization of the clock to 16MHz */
|
|
|
|
+ CLK->CKDIVR = 0x00;
|
|
|
|
+
|
|
|
|
+ /* Disable clock of unused peripherial */
|
|
|
|
+ CLK->PCKENR1 = (uint8_t)(~CLK_PCKENR1_UART1);
|
|
|
|
+ CLK->PCKENR2 = (uint8_t)(~(CLK_PCKENR2_CAN | CLK_PCKENR2_ADC | CLK_PCKENR2_AWU));
|
|
|
|
+
|
|
|
|
+ /* Configure the system clock to use HSE clock source and to run at Crystal Mhz */
|
|
|
|
+ status = CLK_ClockSwitchConfig(CLK_SWITCHMODE_AUTO, CLK_SOURCE_HSE, DISABLE, CLK_CURRENTCLOCKSTATE_DISABLE);
|
|
|
|
+ if (status == ERROR) {
|
|
|
|
+ /* Configure the system clock to use HSI clock source and to run at 16Mhz */
|
|
|
|
+ // set FLAG for
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /* Configure GPIO used for buttons */
|
|
|
|
+ BUTTON1_PORT->DDR &= (uint8_t)(~(BUTTON1_PIN)); // Input Mode
|
|
|
|
+ BUTTON1_PORT->CR1 |= (uint8_t)BUTTON1_PIN; // Pull-Up
|
|
|
|
+ BUTTON1_PORT->CR2 &= (uint8_t)(~(BUTTON1_PIN)); // No Interrupt
|
|
|
|
+
|
|
|
|
+ BUTTON2_PORT->DDR &= (uint8_t)(~(BUTTON2_PIN)); // Input Mode
|
|
|
|
+ BUTTON2_PORT->CR1 |= (uint8_t)BUTTON2_PIN; // Pull-Up
|
|
|
|
+ BUTTON2_PORT->CR2 &= (uint8_t)(~(BUTTON2_PIN)); // No Interrupt
|
|
|
|
+
|
|
|
|
+ /* Init GPIO */
|
|
|
|
+ /* Set the MOSI,MISO and SCK at high level */
|
|
|
|
+ SPI_PORT->ODR |= SPI_MOSI;
|
|
|
|
+ SPI_PORT->DDR |= SPI_PINS;
|
|
|
|
+ SPI_PORT->CR1 |= SPI_PINS;
|
|
|
|
+ SPI_PORT->CR2 |= SPI_PINS;
|
|
|
|
+
|
|
|
|
+ /* Configure LOAD pin to Push-Pull, High, Fast*/
|
|
|
|
+ SPI_LOAD_PORT->ODR |= SPI_LOAD;
|
|
|
|
+ SPI_LOAD_PORT->DDR |= SPI_LOAD;
|
|
|
|
+ SPI_LOAD_PORT->CR1 |= SPI_LOAD;
|
|
|
|
+ SPI_LOAD_PORT->CR2 |= SPI_LOAD;
|
|
|
|
+
|
|
|
|
+ /* Init SPI */
|
|
|
|
+ /* SPI_MODE_MASTER, SPI_FIRSTBIT_MSB, SPI_BAUDRATEPRESCALER_2, SPI_CLOCKPOLARITY_LOW, SPI_CLOCKPHASE_1EDGE */
|
|
|
|
+ SPI->CR1 = 0x04;
|
|
|
|
+ /* SPI_DATADIRECTION_1LINE_TX, SPI_NSS_SOFT */
|
|
|
|
+ SPI->CR2 = 0xC0 | 0x02 | 0x01;
|
|
|
|
+ /* SPI Enable */
|
|
|
|
+ SPI->CR1 |= SPI_CR1_SPE;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static void btnScan(void) {
|
|
|
|
+ btnTest(&Button[S1], &btn1Cnt, btn1Short, btn1Long);
|
|
|
|
+ btnTest(&Button[S2], &btn2Cnt, btn2Short, btn2Long);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+/** проверка конкретной кнопки */
|
|
|
|
+static void btnTest(button_t const *btn, uint8_t *pressCount, void (*taskShortPress)(void), void (*taskLongPress)(void)) {
|
|
|
|
+ if ((btn->port->IDR & btn->pin) == 0) {
|
|
|
|
+ if (*pressCount < 255) {
|
|
|
|
+ *pressCount = *pressCount + 1;
|
|
|
|
+ }
|
|
|
|
+/*
|
|
|
|
+ if (*pressCount == BTN_FACTOR) {
|
|
|
|
+ // short
|
|
|
|
+ } else if(*pressCount == (BTN_FACTOR * 10)) {
|
|
|
|
+ // long
|
|
|
|
+ }
|
|
|
|
+*/
|
|
|
|
+ } else {
|
|
|
|
+ if (*pressCount >= (BTN_FACTOR * 10)) {
|
|
|
|
+ RTOS_SetTask(taskLongPress, 1, 0); // вызов функции при длительном нажатии
|
|
|
|
+ } else if (*pressCount >= BTN_FACTOR) {
|
|
|
|
+ RTOS_SetTask(taskShortPress, 1, 0); // вызов функции при коротком нажатии
|
|
|
|
+ }
|
|
|
|
+ *pressCount = 0;
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+/** переключение режимов отображения */
|
|
|
|
+static void displayMode(mode_led_t * disp, event_t event, uint8_t pos) {
|
|
|
|
+ if (pos != 0) {
|
|
|
|
+ pos = 4;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (event == eventShortPress) {
|
|
|
|
+ switch (*disp) {
|
|
|
|
+ case dispNoState:
|
|
|
|
+ showLabelU(pos);
|
|
|
|
+ if (pos == 0) {
|
|
|
|
+ RTOS_SetTask(showTopLineU, 1000, 250);
|
|
|
|
+ } else {
|
|
|
|
+ RTOS_SetTask(showBotLineU, 1000, 250);
|
|
|
|
+ }
|
|
|
|
+ *disp = displayV;
|
|
|
|
+ break;
|
|
|
|
+ case displayV:
|
|
|
|
+ if (pos == 0) {
|
|
|
|
+ RTOS_DeleteTask(showTopLineU);
|
|
|
|
+ RTOS_SetTask(showTopLineI, 1000, 250);
|
|
|
|
+ } else {
|
|
|
|
+ RTOS_DeleteTask(showBotLineU);
|
|
|
|
+ RTOS_SetTask(showBotLineI, 1000, 250);
|
|
|
|
+ }
|
|
|
|
+ showLabelI(pos);
|
|
|
|
+ *disp = displayI;
|
|
|
|
+ break;
|
|
|
|
+ case displayI:
|
|
|
|
+ if (pos == 0) {
|
|
|
|
+ RTOS_DeleteTask(showTopLineI);
|
|
|
|
+ RTOS_SetTask(showTopLineP, 1000, 250);
|
|
|
|
+ } else {
|
|
|
|
+ RTOS_DeleteTask(showBotLineI);
|
|
|
|
+ RTOS_SetTask(showBotLineP, 1000, 250);
|
|
|
|
+ }
|
|
|
|
+ showLabelP(pos);
|
|
|
|
+ *disp = displayP;
|
|
|
|
+ break;
|
|
|
|
+ case displayP:
|
|
|
|
+ if (pos == 0) {
|
|
|
|
+ RTOS_DeleteTask(showTopLineP);
|
|
|
|
+ RTOS_SetTask(showTopLineAH, 1000, 250);
|
|
|
|
+ } else {
|
|
|
|
+ RTOS_DeleteTask(showBotLineP);
|
|
|
|
+ RTOS_SetTask(showBotLineAH, 1000, 250);
|
|
|
|
+ }
|
|
|
|
+ showLabelAH(pos);
|
|
|
|
+ *disp = displayCI;
|
|
|
|
+ break;
|
|
|
|
+ case displayCI:
|
|
|
|
+ if (pos == 0) {
|
|
|
|
+ RTOS_DeleteTask(showTopLineAH);
|
|
|
|
+ RTOS_SetTask(showTopLineWH, 1000, 250);
|
|
|
|
+ } else {
|
|
|
|
+ RTOS_DeleteTask(showBotLineAH);
|
|
|
|
+ RTOS_SetTask(showBotLineWH, 1000, 250);
|
|
|
|
+ }
|
|
|
|
+ showLabelWH(pos);
|
|
|
|
+ *disp = displayCP;
|
|
|
|
+ break;
|
|
|
|
+ case displayCP:
|
|
|
|
+ if (pos == 0) {
|
|
|
|
+ RTOS_DeleteTask(showTopLineWH);
|
|
|
|
+ RTOS_SetTask(showTopLineSH, 1000, 250);
|
|
|
|
+ } else {
|
|
|
|
+ RTOS_DeleteTask(showBotLineWH);
|
|
|
|
+ RTOS_SetTask(showBotLineSH, 1000, 250);
|
|
|
|
+ }
|
|
|
|
+ showLabelSH(pos);
|
|
|
|
+ *disp = displaySh;
|
|
|
|
+ break;
|
|
|
|
+ case displaySh:
|
|
|
|
+ if (pos == 0) {
|
|
|
|
+ RTOS_DeleteTask(showTopLineSH);
|
|
|
|
+ RTOS_SetTask(showTopLineT, 1000, 250);
|
|
|
|
+ } else {
|
|
|
|
+ RTOS_DeleteTask(showBotLineSH);
|
|
|
|
+ RTOS_SetTask(showBotLineT, 1000, 250);
|
|
|
|
+ }
|
|
|
|
+ showLabelT(pos);
|
|
|
|
+ *disp = displayT;
|
|
|
|
+ break;
|
|
|
|
+ case displayT:
|
|
|
|
+ if (pos == 0) {
|
|
|
|
+ RTOS_DeleteTask(showTopLineT);
|
|
|
|
+ RTOS_SetTask(showTopLineU, 1000, 250);
|
|
|
|
+ } else {
|
|
|
|
+ RTOS_DeleteTask(showBotLineT);
|
|
|
|
+ RTOS_SetTask(showBotLineU, 1000, 250);
|
|
|
|
+ }
|
|
|
|
+ showLabelU(pos);
|
|
|
|
+ *disp = displayV;
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ } else if (event == eventLongPress) {
|
|
|
|
+ /* КА долгих нажатий */
|
|
|
|
+ switch (*disp) {
|
|
|
|
+ case displayCI:
|
|
|
|
+ if (pos == 0) {
|
|
|
|
+ RTOS_SetTask(blankTopLine, 0, 500);
|
|
|
|
+ RTOS_SetTask(showTopLineAH, 250, 500);
|
|
|
|
+ } else {
|
|
|
|
+ RTOS_SetTask(blankBotLine, 0, 500);
|
|
|
|
+ RTOS_SetTask(showBotLineAH, 250, 500);
|
|
|
|
+ }
|
|
|
|
+ showLabelAH(pos);
|
|
|
|
+ break;
|
|
|
|
+ case displayCP:
|
|
|
|
+ if (pos == 0) {
|
|
|
|
+ RTOS_SetTask(blankTopLine, 0, 500);
|
|
|
|
+ RTOS_SetTask(showTopLineWH, 250, 500);
|
|
|
|
+ } else {
|
|
|
|
+ RTOS_SetTask(blankBotLine, 0, 500);
|
|
|
|
+ RTOS_SetTask(showBotLineWH, 250, 500);
|
|
|
|
+ }
|
|
|
|
+ showLabelWH(pos);
|
|
|
|
+ break;
|
|
|
|
+ case displayT:
|
|
|
|
+ if (pos == 0) {
|
|
|
|
+ RTOS_SetTask(blankTopLine, 0, 500);
|
|
|
|
+ RTOS_SetTask(showTopLineT, 250, 500);
|
|
|
|
+ } else {
|
|
|
|
+ RTOS_SetTask(blankBotLine, 0, 500);
|
|
|
|
+ RTOS_SetTask(showBotLineT, 250, 500);
|
|
|
|
+ }
|
|
|
|
+ showLabelT(pos);
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+/** обработчик короткого нажатия первой кнопки */
|
|
|
|
+static void btn1Short(void) {
|
|
|
|
+ displayMode(&modeTopLine, eventShortPress, 0);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+/** обработчик длинного нажатия первой кнопки */
|
|
|
|
+static void btn1Long(void) {
|
|
|
|
+ displayMode(&modeTopLine, eventLongPress, 0);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+/** обработчик короткого нажатия второй кнопки */
|
|
|
|
+static void btn2Short(void) {
|
|
|
|
+ displayMode(&modeBotLine, eventShortPress, 4);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+/** обработчик длинного нажатия второй кнопки */
|
|
|
|
+static void btn2Long(void) {
|
|
|
|
+ Timer.ss = 0;
|
|
|
|
+ Timer.mm = 0;
|
|
|
|
+ Timer.hh = 0;
|
|
|
|
+ CapacityAH = 0;
|
|
|
|
+ CapacityWH = 0;
|
|
|
|
+ displayMode(&modeBotLine, eventLongPress, 4);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+/** Get values from INA219 and store to local variables. */
|
|
|
|
+static void readINAValues(void) {
|
|
|
|
+ Voltage = readBusVoltage();
|
|
|
|
+ ShuntV = readShuntVoltage();
|
|
|
|
+ Current = readBusCurrent();
|
|
|
|
+ Power = readBusPower();
|
|
|
|
+}
|
|
|
|
|
|
|
|
+/**
|
|
|
|
+ * Calculate Time, Capacitance AH and WH.
|
|
|
|
+ */
|
|
|
|
+static void calculateValues(void) {
|
|
|
|
+ if (Current != 0) {
|
|
|
|
+
|
|
|
|
+ // millivolt * milliamper = microwatt, convert it to milliwatt
|
|
|
|
+ //Power = ((uint32_t)(v * c) + 500) / 1000;
|
|
|
|
+
|
|
|
|
+ CapacityAH += Current;
|
|
|
|
+ CapacityWH += Power;
|
|
|
|
+
|
|
|
|
+ Timer.ss ++;
|
|
|
|
+ if (Timer.ss > 59) {
|
|
|
|
+ Timer.ss = 0;
|
|
|
|
+ Timer.mm ++;
|
|
|
|
+ if (Timer.mm > 59) {
|
|
|
|
+ Timer.mm = 0;
|
|
|
|
+ Timer.hh ++;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * вывод инфы на верхнем индикаторе
|
|
|
|
+ */
|
|
|
|
+static void showTopLineU(void) {
|
|
|
|
+ showU(0);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static void showTopLineI(void) {
|
|
|
|
+ showI(0);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static void showTopLineP(void) {
|
|
|
|
+ showP(0);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static void showTopLineAH(void) {
|
|
|
|
+ showCI(0);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static void showTopLineWH(void) {
|
|
|
|
+ showCP(0);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static void showTopLineSH(void) {
|
|
|
|
+ showSH(0);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static void showTopLineT(void) {
|
|
|
|
+ showT(0);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static void blankTopLine(void) {
|
|
|
|
+ blankLine(0);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * вывод инфы на нижнем индикаторе
|
|
|
|
+ */
|
|
|
|
+static void showBotLineU(void) {
|
|
|
|
+ showU(4);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static void showBotLineI(void) {
|
|
|
|
+ showI(4);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static void showBotLineP(void) {
|
|
|
|
+ showP(4);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static void showBotLineAH(void) {
|
|
|
|
+ showCI(4);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static void showBotLineWH(void) {
|
|
|
|
+ showCP(4);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static void showBotLineSH(void) {
|
|
|
|
+ showSH(4);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static void showBotLineT(void) {
|
|
|
|
+ showT(4);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static void blankBotLine(void) {
|
|
|
|
+ blankLine(4);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Output given value to given indicator
|
|
|
|
+ * param1: value to show, from '0.000' to '999.9'
|
|
|
|
+ * param2: starting position -- 0 for top
|
|
|
|
+ * any other for bottom.
|
|
|
|
+ */
|
|
|
|
+static void showValue(uint32_t val, uint8_t pos) {
|
|
|
|
+ uint8_t tmp;
|
|
|
|
+
|
|
|
|
+ if (val >= 100000) { // 000.0
|
|
|
|
+ tmp = (uint8_t)(val / 100000);
|
|
|
|
+ val %= 100000;
|
|
|
|
+ MAX7219_WriteData(digitPosition[pos], digitValue[tmp]);
|
|
|
|
+
|
|
|
|
+ tmp = (uint8_t)(val / 10000);
|
|
|
|
+ val %= 10000;
|
|
|
|
+ pos ++;
|
|
|
|
+ MAX7219_WriteData(digitPosition[pos], digitValue[tmp]);
|
|
|
|
+
|
|
|
|
+ tmp = (uint8_t)(val / 1000);
|
|
|
|
+ val %= 1000;
|
|
|
|
+ pos ++;
|
|
|
|
+ MAX7219_WriteData(digitPosition[pos], (digitValue[tmp] | Sym_Dot));
|
|
|
|
+
|
|
|
|
+ tmp = (uint8_t)(val / 100);
|
|
|
|
+ pos ++;
|
|
|
|
+ MAX7219_WriteData(digitPosition[pos], digitValue[tmp]);
|
|
|
|
+
|
|
|
|
+ } else if (val >= 10000) { // 00.00
|
|
|
|
+ tmp = (uint8_t)(val / 10000);
|
|
|
|
+ val %= 10000;
|
|
|
|
+ MAX7219_WriteData(digitPosition[pos], digitValue[tmp]);
|
|
|
|
+
|
|
|
|
+ tmp = (uint8_t)(val / 1000);
|
|
|
|
+ val %= 1000;
|
|
|
|
+ pos ++;
|
|
|
|
+ MAX7219_WriteData(digitPosition[pos], (digitValue[tmp] | Sym_Dot));
|
|
|
|
+
|
|
|
|
+ tmp = (uint8_t)(val / 100);
|
|
|
|
+ val %= 100;
|
|
|
|
+ pos ++;
|
|
|
|
+ MAX7219_WriteData(digitPosition[pos], digitValue[tmp]);
|
|
|
|
+
|
|
|
|
+ tmp = (uint8_t)(val / 10);
|
|
|
|
+ pos ++;
|
|
|
|
+ MAX7219_WriteData(digitPosition[pos], digitValue[tmp]);
|
|
|
|
+
|
|
|
|
+ } else { // 0.000
|
|
|
|
+ tmp = (uint8_t)(val / 1000);
|
|
|
|
+ val %= 1000;
|
|
|
|
+ MAX7219_WriteData(digitPosition[pos], (digitValue[tmp] | Sym_Dot));
|
|
|
|
+
|
|
|
|
+ tmp = (uint8_t)(val / 100);
|
|
|
|
+ val %= 100;
|
|
|
|
+ pos ++;
|
|
|
|
+ MAX7219_WriteData(digitPosition[pos], digitValue[tmp]);
|
|
|
|
+
|
|
|
|
+ tmp = (uint8_t)(val / 10);
|
|
|
|
+ val %= 10;
|
|
|
|
+ pos ++;
|
|
|
|
+ MAX7219_WriteData(digitPosition[pos], digitValue[tmp]);
|
|
|
|
+
|
|
|
|
+ pos ++;
|
|
|
|
+ tmp = val;
|
|
|
|
+ MAX7219_WriteData(digitPosition[pos], digitValue[tmp]);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * fill line by blank symbol
|
|
|
|
+ */
|
|
|
|
+void blankLine(uint8_t pos) {
|
|
|
|
+ uint8_t i = pos;
|
|
|
|
+ for (i=0; i<pos+4; i++) {
|
|
|
|
+ MAX7219_WriteData(digitPosition[i], Sym_BLANK);
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Output voltage values to given indicator
|
|
|
|
+ * param: starting position -- 0 for top
|
|
|
|
+ * any other for bottom.
|
|
|
|
+ */
|
|
|
|
+static void showU(uint8_t pos) {
|
|
|
|
+ showValue(Voltage, pos);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Output current values to given indicator
|
|
|
|
+ * param: starting position -- 0 for top
|
|
|
|
+ * any other for bottom.
|
|
|
|
+ */
|
|
|
|
+static void showI(uint8_t pos) {
|
|
|
|
+ showValue(Current, pos);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Output power values to given indicator
|
|
|
|
+ * param: starting position -- 0 for top
|
|
|
|
+ * any other for bottom.
|
|
|
|
+ */
|
|
|
|
+static void showP(uint8_t pos) {
|
|
|
|
+ showValue(Power, pos);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Output current capacity values to given indicator
|
|
|
|
+ * param: starting position -- 0 for top
|
|
|
|
+ * any other for bottom.
|
|
|
|
+ */
|
|
|
|
+static void showCI(uint8_t pos) {
|
|
|
|
+ showValue(((CapacityAH + 1800) / 3600), pos);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Output power capacity values to given indicator
|
|
|
|
+ * param: starting position -- 0 for top
|
|
|
|
+ * any other for bottom.
|
|
|
|
+ */
|
|
|
|
+static void showCP(uint8_t pos) {
|
|
|
|
+ showValue(((CapacityWH + 1800) / 3600), pos);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Output shunt voltage values to given indicator
|
|
|
|
+ * param: starting position -- 0 for top
|
|
|
|
+ * any other for bottom.
|
|
|
|
+ * LSB = 10 uV, may be negative!!!
|
|
|
|
+ */
|
|
|
|
+static void showSH(uint8_t pos) {
|
|
|
|
+ uint32_t val;
|
|
|
|
+
|
|
|
|
+ if (ShuntV < 0) {
|
|
|
|
+ val = - ShuntV;
|
|
|
|
+ } else {
|
|
|
|
+ val = ShuntV;
|
|
|
|
+ }
|
|
|
|
+ val *= 10;
|
|
|
|
+ showValue(val, pos);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Output time values to given indicator
|
|
|
|
+ * param: starting position -- 0 for top
|
|
|
|
+ * any other for bottom.
|
|
|
|
+ */
|
|
|
|
+static void showT(uint8_t pos) {
|
|
|
|
+ static uint8_t old_sek = 0;
|
|
|
|
+ uint8_t tmp;
|
|
|
|
+
|
|
|
|
+ if (Timer.hh > 0) {
|
|
|
|
+ tmp = Timer.hh / 10;
|
|
|
|
+ MAX7219_WriteData(digitPosition[pos], digitValue[tmp]);
|
|
|
|
+ pos ++;
|
|
|
|
+ tmp = Timer.hh % 10;
|
|
|
|
+ if (old_sek == Timer.ss) {
|
|
|
|
+ MAX7219_WriteData(digitPosition[pos], digitValue[tmp]);
|
|
|
|
+ } else {
|
|
|
|
+ MAX7219_WriteData(digitPosition[pos], (digitValue[tmp] | Sym_Dot));
|
|
|
|
+ }
|
|
|
|
+ pos ++;
|
|
|
|
+ tmp = Timer.mm / 10;
|
|
|
|
+ MAX7219_WriteData(digitPosition[pos], digitValue[tmp]);
|
|
|
|
+ pos ++;
|
|
|
|
+ tmp = Timer.mm % 10;
|
|
|
|
+ MAX7219_WriteData(digitPosition[pos], digitValue[tmp]);
|
|
|
|
+ } else {
|
|
|
|
+ tmp = Timer.mm / 10;
|
|
|
|
+ MAX7219_WriteData(digitPosition[pos], digitValue[Timer.mm >> 4]);
|
|
|
|
+ pos ++;
|
|
|
|
+ tmp = Timer.mm % 10;
|
|
|
|
+ MAX7219_WriteData(digitPosition[pos], (digitValue[tmp] | Sym_Dot));
|
|
|
|
+ pos ++;
|
|
|
|
+ tmp = Timer.ss / 10;
|
|
|
|
+ MAX7219_WriteData(digitPosition[pos], digitValue[tmp]);
|
|
|
|
+ pos ++;
|
|
|
|
+ tmp = Timer.ss % 10;
|
|
|
|
+ MAX7219_WriteData(digitPosition[pos], digitValue[tmp]);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ old_sek = Timer.ss;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * show label for selected mode
|
|
|
|
+ */
|
|
|
|
+static void showLabelU(uint8_t pos) {
|
|
|
|
+ MAX7219_WriteData(digitPosition[pos], Sym_Minus);
|
|
|
|
+ pos ++;
|
|
|
|
+ MAX7219_WriteData(digitPosition[pos], Sym_U);
|
|
|
|
+ pos ++;
|
|
|
|
+ MAX7219_WriteData(digitPosition[pos], Sym_Minus);
|
|
|
|
+ pos ++;
|
|
|
|
+ MAX7219_WriteData(digitPosition[pos], Sym_BLANK);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static void showLabelI(uint8_t pos) {
|
|
|
|
+ MAX7219_WriteData(digitPosition[pos], Sym_Minus);
|
|
|
|
+ pos ++;
|
|
|
|
+ MAX7219_WriteData(digitPosition[pos], Sym_1);
|
|
|
|
+ pos ++;
|
|
|
|
+ MAX7219_WriteData(digitPosition[pos], Sym_Minus);
|
|
|
|
+ pos ++;
|
|
|
|
+ MAX7219_WriteData(digitPosition[pos], Sym_BLANK);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static void showLabelP(uint8_t pos) {
|
|
|
|
+ MAX7219_WriteData(digitPosition[pos], Sym_Minus);
|
|
|
|
+ pos ++;
|
|
|
|
+ MAX7219_WriteData(digitPosition[pos], Sym_P);
|
|
|
|
+ pos ++;
|
|
|
|
+ MAX7219_WriteData(digitPosition[pos], Sym_Minus);
|
|
|
|
+ pos ++;
|
|
|
|
+ MAX7219_WriteData(digitPosition[pos], Sym_BLANK);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static void showLabelAH(uint8_t pos) {
|
|
|
|
+ MAX7219_WriteData(digitPosition[pos], Sym_Minus);
|
|
|
|
+ pos ++;
|
|
|
|
+ MAX7219_WriteData(digitPosition[pos], Sym_A);
|
|
|
|
+ pos ++;
|
|
|
|
+ MAX7219_WriteData(digitPosition[pos], Sym_H);
|
|
|
|
+ pos ++;
|
|
|
|
+ MAX7219_WriteData(digitPosition[pos], Sym_Minus);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static void showLabelWH(uint8_t pos) {
|
|
|
|
+ MAX7219_WriteData(digitPosition[pos], Sym_Minus);
|
|
|
|
+ pos ++;
|
|
|
|
+ MAX7219_WriteData(digitPosition[pos], Sym_P);
|
|
|
|
+ pos ++;
|
|
|
|
+ MAX7219_WriteData(digitPosition[pos], Sym_H);
|
|
|
|
+ pos ++;
|
|
|
|
+ MAX7219_WriteData(digitPosition[pos], Sym_Minus);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static void showLabelSH(uint8_t pos) {
|
|
|
|
+ MAX7219_WriteData(digitPosition[pos], Sym_Minus);
|
|
|
|
+ pos ++;
|
|
|
|
+ MAX7219_WriteData(digitPosition[pos], Sym_5);
|
|
|
|
+ pos ++;
|
|
|
|
+ MAX7219_WriteData(digitPosition[pos], Sym_h);
|
|
|
|
+ pos ++;
|
|
|
|
+ MAX7219_WriteData(digitPosition[pos], Sym_Minus);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static void showLabelT(uint8_t pos) {
|
|
|
|
+ MAX7219_WriteData(digitPosition[pos], Sym_Minus);
|
|
|
|
+ pos ++;
|
|
|
|
+ MAX7219_WriteData(digitPosition[pos], Sym_t);
|
|
|
|
+ pos ++;
|
|
|
|
+ MAX7219_WriteData(digitPosition[pos], Sym_Minus);
|
|
|
|
+ pos ++;
|
|
|
|
+ MAX7219_WriteData(digitPosition[pos], Sym_BLANK);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+#ifdef USE_FULL_ASSERT
|
|
/**
|
|
/**
|
|
* @brief Reports the name of the source file and the source line number
|
|
* @brief Reports the name of the source file and the source line number
|
|
* where the assert_param error has occurred.
|
|
* where the assert_param error has occurred.
|
|
@@ -43,7 +793,7 @@ void main(void)
|
|
* @retval : None
|
|
* @retval : None
|
|
*/
|
|
*/
|
|
void assert_failed(u8* file, u32 line)
|
|
void assert_failed(u8* file, u32 line)
|
|
-{
|
|
|
|
|
|
+{
|
|
/* User can add his own implementation to report the file name and line number,
|
|
/* 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) */
|
|
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
|
|
|
|
|