|
@@ -21,15 +21,30 @@
|
|
|
|
|
|
/* Includes ------------------------------------------------------------------*/
|
|
/* Includes ------------------------------------------------------------------*/
|
|
#include "stm8s.h"
|
|
#include "stm8s.h"
|
|
-#include "rtos.h"
|
|
|
|
|
|
+//#include "rtos.h"
|
|
|
|
|
|
/* Private defines -----------------------------------------------------------*/
|
|
/* Private defines -----------------------------------------------------------*/
|
|
|
|
+// for 1 msek ticks
|
|
|
|
+#define TIM4_PERIOD (uint8_t)124
|
|
|
|
+
|
|
#define ADC_PORT GPIOD
|
|
#define ADC_PORT GPIOD
|
|
#define ADC_PINS GPIO_PIN_3
|
|
#define ADC_PINS GPIO_PIN_3
|
|
#define ADC_CHNL ADC1_CHANNEL_4
|
|
#define ADC_CHNL ADC1_CHANNEL_4
|
|
#define ADC_SCHT ADC1_SCHMITTTRIG_CHANNEL4
|
|
#define ADC_SCHT ADC1_SCHMITTTRIG_CHANNEL4
|
|
#define ADC_SMPLS 64
|
|
#define ADC_SMPLS 64
|
|
|
|
|
|
|
|
+#define BTN_PORT (GPIOA)
|
|
|
|
+#define BTN_PINS (GPIO_PIN_2)
|
|
|
|
+
|
|
|
|
+/*
|
|
|
|
+ AAA
|
|
|
|
+F B
|
|
|
|
+F B
|
|
|
|
+ GGG
|
|
|
|
+E C
|
|
|
|
+E C
|
|
|
|
+ DDD P
|
|
|
|
+*/
|
|
#define LED_ANOD1_PORT GPIOA
|
|
#define LED_ANOD1_PORT GPIOA
|
|
#define LED_ANOD1_PIN GPIO_PIN_3
|
|
#define LED_ANOD1_PIN GPIO_PIN_3
|
|
#define LED_ANOD2_PORT GPIOD
|
|
#define LED_ANOD2_PORT GPIOD
|
|
@@ -37,19 +52,26 @@
|
|
#define LED_ANOD3_PORT GPIOD
|
|
#define LED_ANOD3_PORT GPIOD
|
|
#define LED_ANOD3_PIN GPIO_PIN_4
|
|
#define LED_ANOD3_PIN GPIO_PIN_4
|
|
|
|
|
|
-#define LED_SEG1_PORT GPIOA
|
|
|
|
-#define LED_SEG1_PINS GPIO_PIN_1
|
|
|
|
-#define LED_SEG2_PORT GPIOB
|
|
|
|
-#define LED_SEG2_PINS (GPIO_PIN_5 | GPIO_PIN_4)
|
|
|
|
-#define LED_SEG3_PORT GPIOC
|
|
|
|
-#define LED_SEG3_PINS (GPIO_PIN_4 | GPIO_PIN_6 | GPIO_PIN_7 | GPIO_PIN_3 | GPIO_PIN_5)
|
|
|
|
|
|
+#define LED_SEG1_PORT GPIOA
|
|
|
|
+#define LED_SEG1_PINS GPIO_PIN_1
|
|
|
|
+#define LED_SEG_F GPIO_PIN_1
|
|
|
|
|
|
-#define BTN_PORT (GPIOA)
|
|
|
|
-#define BTN_PINS (GPIO_PIN_2)
|
|
|
|
|
|
+#define LED_SEG2_PORT GPIOB
|
|
|
|
+#define LED_SEG2_PINS (GPIO_PIN_5 | GPIO_PIN_4)
|
|
|
|
+#define LED_SEG_A GPIO_PIN_5
|
|
|
|
+#define LED_SEG_B GPIO_PIN_4
|
|
|
|
+
|
|
|
|
+#define LED_SEG3_PORT GPIOC
|
|
|
|
+#define LED_SEG3_PINS (GPIO_PIN_4 | GPIO_PIN_6 | GPIO_PIN_7 | GPIO_PIN_3 | GPIO_PIN_5)
|
|
|
|
+#define LED_SEG_G GPIO_PIN_3
|
|
|
|
+#define LED_SEG_C GPIO_PIN_4
|
|
|
|
+#define LED_SEG_P GPIO_PIN_5
|
|
|
|
+#define LED_SEG_D GPIO_PIN_6
|
|
|
|
+#define LED_SEG_E GPIO_PIN_7
|
|
|
|
|
|
#define LED_OUT_OFF {LED_SEG1_PORT->ODR |= LED_SEG1_PINS; LED_SEG2_PORT->ODR |= LED_SEG2_PINS; LED_SEG3_PORT->ODR |= LED_SEG3_PINS;}
|
|
#define LED_OUT_OFF {LED_SEG1_PORT->ODR |= LED_SEG1_PINS; LED_SEG2_PORT->ODR |= LED_SEG2_PINS; LED_SEG3_PORT->ODR |= LED_SEG3_PINS;}
|
|
-#define LED_OUT_DP GPIOC->ODR &= (~GPIO_PIN_5);
|
|
|
|
-#define LED_OUT_MM GPIOC->ODR &= 0xF7;
|
|
|
|
|
|
+#define LED_OUT_DP GPIOC->ODR &= (~LED_SEG_P);
|
|
|
|
+#define LED_OUT_MM GPIOC->ODR &= LED_SEG_G;
|
|
#define LED_OUT_0 GPIOA->ODR &= 0xFD; GPIOB->ODR &= 0xCF; GPIOC->ODR &= 0x29;
|
|
#define LED_OUT_0 GPIOA->ODR &= 0xFD; GPIOB->ODR &= 0xCF; GPIOC->ODR &= 0x29;
|
|
#define LED_OUT_1 GPIOB->ODR &= 0xEF; GPIOC->ODR &= 0xE9;
|
|
#define LED_OUT_1 GPIOB->ODR &= 0xEF; GPIOC->ODR &= 0xE9;
|
|
#define LED_OUT_2 GPIOB->ODR &= 0xCF; GPIOC->ODR &= 0x31;
|
|
#define LED_OUT_2 GPIOB->ODR &= 0xCF; GPIOC->ODR &= 0x31;
|
|
@@ -66,22 +88,30 @@
|
|
#define LED_ANODE_ON2 LED_ANOD2_PORT->ODR |= LED_ANOD2_PIN;
|
|
#define LED_ANODE_ON2 LED_ANOD2_PORT->ODR |= LED_ANOD2_PIN;
|
|
#define LED_ANODE_ON3 LED_ANOD3_PORT->ODR |= LED_ANOD3_PIN;
|
|
#define LED_ANODE_ON3 LED_ANOD3_PORT->ODR |= LED_ANOD3_PIN;
|
|
|
|
|
|
|
|
+#define SUB_SECOND_CNT 5
|
|
|
|
+#define LED_ONE_PERIOD 5
|
|
|
|
|
|
/* Private function prototypes -----------------------------------------------*/
|
|
/* Private function prototypes -----------------------------------------------*/
|
|
static void ADC_Config(void);
|
|
static void ADC_Config(void);
|
|
static void GPIO_Config(void);
|
|
static void GPIO_Config(void);
|
|
static void TIM1_Config(void);
|
|
static void TIM1_Config(void);
|
|
|
|
+static void TIM4_Config(void);
|
|
static void OutputToLed(void);
|
|
static void OutputToLed(void);
|
|
static void PrepareForLed(void);
|
|
static void PrepareForLed(void);
|
|
|
|
+//static void CountCapacity(void);
|
|
|
|
|
|
/* Private functions ---------------------------------------------------------*/
|
|
/* Private functions ---------------------------------------------------------*/
|
|
|
|
|
|
/* Private variables ---------------------------------------------------------*/
|
|
/* Private variables ---------------------------------------------------------*/
|
|
__IO uint16_t ConversionBuffer[ADC_SMPLS];
|
|
__IO uint16_t ConversionBuffer[ADC_SMPLS];
|
|
__IO uint8_t BufferIndex = 0;
|
|
__IO uint8_t BufferIndex = 0;
|
|
|
|
+__IO uint8_t LedCnt = 0;
|
|
static uint16_t Current = 0;
|
|
static uint16_t Current = 0;
|
|
|
|
+static uint32_t Capacity = 0;
|
|
static uint8_t LedDigits[3] = {0, 0, 0};
|
|
static uint8_t LedDigits[3] = {0, 0, 0};
|
|
static uint8_t LedPoint = 3;
|
|
static uint8_t LedPoint = 3;
|
|
|
|
+static uint16_t SubSecondBfr = 0;
|
|
|
|
+static uint8_t SubSecondCnt = SUB_SECOND_CNT;
|
|
|
|
|
|
void main(void)
|
|
void main(void)
|
|
{
|
|
{
|
|
@@ -107,13 +137,25 @@ void main(void)
|
|
TIM1_Config();
|
|
TIM1_Config();
|
|
|
|
|
|
/* Initialize dispatcher */
|
|
/* Initialize dispatcher */
|
|
- RTOS_Init();
|
|
|
|
- RTOS_SetTask(OutputToLed, 0, 5);
|
|
|
|
- RTOS_SetTask(PrepareForLed, 210, 200);
|
|
|
|
|
|
+ //RTOS_Init();
|
|
|
|
+ //RTOS_SetTask(OutputToLed, 0, 5);
|
|
|
|
+ //RTOS_SetTask(PrepareForLed, 210, 200);
|
|
|
|
+ //RTOS_SetTask(CountCapacity, 1005, 1000);
|
|
|
|
+
|
|
|
|
+ /* TIM4 configuration -----------------------------------------*/
|
|
|
|
+ TIM4_Config();
|
|
|
|
+
|
|
|
|
+ /* enable interrupts */
|
|
|
|
+ enableInterrupts();
|
|
|
|
|
|
/* Infinite loop */
|
|
/* Infinite loop */
|
|
while (1)
|
|
while (1)
|
|
{
|
|
{
|
|
|
|
+ if (LedCnt >= LED_ONE_PERIOD) {
|
|
|
|
+ LedCnt = 0;
|
|
|
|
+ OutputToLed();
|
|
|
|
+ }
|
|
|
|
+
|
|
if (BufferIndex >= ADC_SMPLS) {
|
|
if (BufferIndex >= ADC_SMPLS) {
|
|
BufferIndex = 0;
|
|
BufferIndex = 0;
|
|
|
|
|
|
@@ -142,9 +184,21 @@ void main(void)
|
|
tbuf = (tbuf + 14) / 28; // get voltage from shunt
|
|
tbuf = (tbuf + 14) / 28; // get voltage from shunt
|
|
/* â Current òîê â ìèëèàìïåðàõ */
|
|
/* â Current òîê â ìèëèàìïåðàõ */
|
|
Current = (tbuf + 3) / 6; // shunt resistance in mOhms
|
|
Current = (tbuf + 3) / 6; // shunt resistance in mOhms
|
|
|
|
+
|
|
|
|
+ SubSecondBfr += Current;
|
|
|
|
+ SubSecondCnt --;
|
|
|
|
+
|
|
|
|
+ if(SubSecondCnt == 0){
|
|
|
|
+ Capacity += ( (SubSecondBfr + (SUB_SECOND_CNT/2)) / SUB_SECOND_CNT );
|
|
|
|
+ SubSecondBfr = 0;
|
|
|
|
+ SubSecondCnt = SUB_SECOND_CNT;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ PrepareForLed();
|
|
|
|
+
|
|
} // End of if (BufferIndex >= ADC_SMPLS)
|
|
} // End of if (BufferIndex >= ADC_SMPLS)
|
|
|
|
|
|
- RTOS_DispatchTask();
|
|
|
|
|
|
+ //RTOS_DispatchTask();
|
|
wfi();
|
|
wfi();
|
|
} // End of infinity
|
|
} // End of infinity
|
|
|
|
|
|
@@ -155,7 +209,11 @@ void main(void)
|
|
static void PrepareForLed(void) {
|
|
static void PrepareForLed(void) {
|
|
uint16_t value;
|
|
uint16_t value;
|
|
|
|
|
|
- value = Current;
|
|
|
|
|
|
+ if((BTN_PORT->IDR & BTN_PINS) == 0){
|
|
|
|
+ value = Capacity / 3600;
|
|
|
|
+ } else {
|
|
|
|
+ value = Current;
|
|
|
|
+ }
|
|
|
|
|
|
if(value > 9999){
|
|
if(value > 9999){
|
|
LedPoint = 1;
|
|
LedPoint = 1;
|
|
@@ -328,6 +386,36 @@ static void TIM1_Config(void)
|
|
TIM1_Cmd(ENABLE);
|
|
TIM1_Cmd(ENABLE);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+/**
|
|
|
|
+ * @brief Configure TIM4.
|
|
|
|
+ * @param None
|
|
|
|
+ * @retval None
|
|
|
|
+ */
|
|
|
|
+static void TIM4_Config(void)
|
|
|
|
+{
|
|
|
|
+ TIM4_DeInit();
|
|
|
|
+
|
|
|
|
+ /*
|
|
|
|
+ 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 */
|
|
|
|
+ TIM4_TimeBaseInit(TIM4_PRESCALER_128, TIM4_PERIOD);
|
|
|
|
+ /* Clear TIM4 update flag */
|
|
|
|
+ TIM4_ClearFlag(TIM4_FLAG_UPDATE);
|
|
|
|
+ /* Enable update interrupt */
|
|
|
|
+ TIM4_ITConfig(TIM4_IT_UPDATE, ENABLE);
|
|
|
|
+
|
|
|
|
+ /* Enable TIM4 */
|
|
|
|
+ TIM4_Cmd(ENABLE);
|
|
|
|
+}
|
|
|
|
|
|
#ifdef USE_FULL_ASSERT
|
|
#ifdef USE_FULL_ASSERT
|
|
|
|
|