|
@@ -21,15 +21,30 @@
|
|
|
|
|
|
/* Includes ------------------------------------------------------------------*/
|
|
|
#include "stm8s.h"
|
|
|
-#include "rtos.h"
|
|
|
+//#include "rtos.h"
|
|
|
|
|
|
/* Private defines -----------------------------------------------------------*/
|
|
|
+// for 1 msek ticks
|
|
|
+#define TIM4_PERIOD (uint8_t)124
|
|
|
+
|
|
|
#define ADC_PORT GPIOD
|
|
|
#define ADC_PINS GPIO_PIN_3
|
|
|
#define ADC_CHNL ADC1_CHANNEL_4
|
|
|
#define ADC_SCHT ADC1_SCHMITTTRIG_CHANNEL4
|
|
|
#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_PIN GPIO_PIN_3
|
|
|
#define LED_ANOD2_PORT GPIOD
|
|
@@ -37,19 +52,26 @@
|
|
|
#define LED_ANOD3_PORT GPIOD
|
|
|
#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_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_1 GPIOB->ODR &= 0xEF; GPIOC->ODR &= 0xE9;
|
|
|
#define LED_OUT_2 GPIOB->ODR &= 0xCF; GPIOC->ODR &= 0x31;
|
|
@@ -67,11 +89,16 @@
|
|
|
#define LED_ANODE_ON3 LED_ANOD3_PORT->ODR |= LED_ANOD3_PIN;
|
|
|
|
|
|
#define SUB_SECOND_CNT 5
|
|
|
+#define LED_ONE_PERIOD 5
|
|
|
+
|
|
|
+#define ADC_ZERO_EEADDR FLASH_DATA_START_PHYSICAL_ADDRESS
|
|
|
|
|
|
/* Private function prototypes -----------------------------------------------*/
|
|
|
+static void ADC_CorrectZero(void);
|
|
|
static void ADC_Config(void);
|
|
|
static void GPIO_Config(void);
|
|
|
static void TIM1_Config(void);
|
|
|
+static void TIM4_Config(void);
|
|
|
static void OutputToLed(void);
|
|
|
static void PrepareForLed(void);
|
|
|
//static void CountCapacity(void);
|
|
@@ -81,12 +108,14 @@ static void PrepareForLed(void);
|
|
|
/* Private variables ---------------------------------------------------------*/
|
|
|
__IO uint16_t ConversionBuffer[ADC_SMPLS];
|
|
|
__IO uint8_t BufferIndex = 0;
|
|
|
+__IO uint8_t LedCnt = 0;
|
|
|
static uint16_t Current = 0;
|
|
|
static uint32_t Capacity = 0;
|
|
|
static uint8_t LedDigits[3] = {0, 0, 0};
|
|
|
static uint8_t LedPoint = 3;
|
|
|
static uint16_t SubSecondBfr = 0;
|
|
|
static uint8_t SubSecondCnt = SUB_SECOND_CNT;
|
|
|
+static uint16_t ADC_ZeroFix = 0;
|
|
|
|
|
|
void main(void)
|
|
|
{
|
|
@@ -112,19 +141,39 @@ void main(void)
|
|
|
TIM1_Config();
|
|
|
|
|
|
/* 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();
|
|
|
+
|
|
|
+ /** If btn pin == 0 -- auto fix adc zero level
|
|
|
+ else -- read zero value from eeprom
|
|
|
+ Value stored in MSB:LSB
|
|
|
+ */
|
|
|
+ if((BTN_PORT->IDR & BTN_PINS) == 0){
|
|
|
+ ADC_CorrectZero();
|
|
|
+ } else {
|
|
|
+ ADC_ZeroFix = (uint16_t)(FLASH_ReadByte(ADC_ZERO_EEADDR) << 8);
|
|
|
+ ADC_ZeroFix |= FLASH_ReadByte((ADC_ZERO_EEADDR+1));
|
|
|
+ }
|
|
|
+
|
|
|
/* Infinite loop */
|
|
|
while (1)
|
|
|
{
|
|
|
+ if (LedCnt >= LED_ONE_PERIOD) {
|
|
|
+ LedCnt = 0;
|
|
|
+ OutputToLed();
|
|
|
+ }
|
|
|
+
|
|
|
if (BufferIndex >= ADC_SMPLS) {
|
|
|
BufferIndex = 0;
|
|
|
|
|
|
- /* èëè îáðàáàòûâàåì ñðàçó, èëè çàäà÷ó ñ 0-îé çàäåðæêîé? */
|
|
|
-
|
|
|
/* Oversampling */
|
|
|
uint32_t tbuf = 0;
|
|
|
uint8_t i = 0;
|
|
@@ -141,32 +190,76 @@ void main(void)
|
|
|
ïîäåëèëè íà ìíîæèòåëü ÎÓ -- 250 ìÂ
|
|
|
è ïîäåëèëè íà ñîïðîòèâëåíèå øóíòà -- 5000 ìÀ
|
|
|
*/
|
|
|
- tbuf >>= 3; // pre div
|
|
|
- tbuf *= 3300; // Vref = Vcc
|
|
|
- tbuf = (tbuf + 4096) / 8191; // get ADC input voltage in mV
|
|
|
- tbuf *= 1000; // get voltage in uV
|
|
|
- tbuf = (tbuf + 14) / 28; // get voltage from shunt
|
|
|
- /* â Current òîê â ìèëèàìïåðàõ */
|
|
|
- Current = (tbuf + 3) / 6; // shunt resistance in mOhms
|
|
|
-
|
|
|
- SubSecondBfr += Current;
|
|
|
+ if(tbuf > ADC_ZeroFix){
|
|
|
+ tbuf -= ADC_ZeroFix;
|
|
|
+
|
|
|
+ tbuf >>= 3; // pre div
|
|
|
+ tbuf *= 3335; // Vref = Vcc
|
|
|
+ tbuf = (tbuf + 4096) / 8191; // get ADC input voltage in mV
|
|
|
+ tbuf *= 1000; // -- for OU divider //get voltage in uV
|
|
|
+ tbuf = (tbuf + 3147) / 6294; // get voltage from shunt
|
|
|
+ /* â Current òîê â ìèëèàìïåðàõ */
|
|
|
+ tbuf *= 1000; // get voltage in uV
|
|
|
+ Current = (tbuf + 25) / 50; // shunt resistance in mOhms
|
|
|
+
|
|
|
+ SubSecondBfr += Current;
|
|
|
+
|
|
|
+ } else {
|
|
|
+ tbuf = 0;
|
|
|
+ Current = 0;
|
|
|
+ }
|
|
|
+
|
|
|
SubSecondCnt --;
|
|
|
|
|
|
if(SubSecondCnt == 0){
|
|
|
- Capacity += SubSecondBfr;
|
|
|
+ Capacity += ( (SubSecondBfr + (SUB_SECOND_CNT/2)) / SUB_SECOND_CNT );
|
|
|
SubSecondBfr = 0;
|
|
|
SubSecondCnt = SUB_SECOND_CNT;
|
|
|
}
|
|
|
|
|
|
+ PrepareForLed();
|
|
|
+
|
|
|
} // End of if (BufferIndex >= ADC_SMPLS)
|
|
|
|
|
|
- RTOS_DispatchTask();
|
|
|
+ //RTOS_DispatchTask();
|
|
|
wfi();
|
|
|
} // End of infinity
|
|
|
|
|
|
} // End of main()
|
|
|
|
|
|
|
|
|
+/** Êîððåêòèðóåì íàïðÿæåíèå ñìåùåíèÿ íóëÿ ÎÓ íà âõîäå ÀÖÏ */
|
|
|
+static void ADC_CorrectZero(void) {
|
|
|
+ /* æä¸ì îêîí÷àíèÿ öèêëà èçìåðåíèé */
|
|
|
+ do {
|
|
|
+ } while (BufferIndex < ADC_SMPLS);
|
|
|
+
|
|
|
+ BufferIndex = 0;
|
|
|
+
|
|
|
+ /* Ïîëó÷àåì 16-òè áèòíîå çíà÷åíèå îò ÀÖÏ */
|
|
|
+ uint8_t i = 0;
|
|
|
+ for(i=0; i<ADC_SMPLS; i++){
|
|
|
+ ADC_ZeroFix += ConversionBuffer[i];
|
|
|
+ }
|
|
|
+
|
|
|
+ /* Ñîõðàíÿåì ïîëó÷åííîå çíà÷åíèå â EEPROM */
|
|
|
+
|
|
|
+ /*Define FLASH programming time*/
|
|
|
+ FLASH_SetProgrammingTime(FLASH_PROGRAMTIME_STANDARD);
|
|
|
+
|
|
|
+ /* Unlock Data memory */
|
|
|
+ FLASH_Unlock(FLASH_MEMTYPE_DATA);
|
|
|
+
|
|
|
+ /* MSB */
|
|
|
+ FLASH_ProgramByte(ADC_ZERO_EEADDR, (uint8_t)(ADC_ZeroFix>>8));
|
|
|
+ /* LSB */
|
|
|
+ FLASH_ProgramByte((ADC_ZERO_EEADDR+1), (uint8_t)(ADC_ZeroFix));
|
|
|
+
|
|
|
+ /* Lock Data memory */
|
|
|
+ FLASH_Lock(FLASH_MEMTYPE_DATA);
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
/** Prepare current value for output ot led */
|
|
|
static void PrepareForLed(void) {
|
|
|
uint16_t value;
|
|
@@ -178,19 +271,22 @@ static void PrepareForLed(void) {
|
|
|
}
|
|
|
|
|
|
if(value > 9999){
|
|
|
+ /* XX.X Amper */
|
|
|
LedPoint = 1;
|
|
|
LedDigits[0] = value / 10000;
|
|
|
value = value % 10000;
|
|
|
LedDigits[1] = value / 1000;
|
|
|
LedDigits[2] = (value % 1000) / 100;
|
|
|
} else if(value > 999){
|
|
|
+ /* X.XX Amper */
|
|
|
LedPoint = 0;
|
|
|
LedDigits[0] = value / 1000;
|
|
|
value = value % 1000;
|
|
|
LedDigits[1] = value / 100;
|
|
|
LedDigits[2] = (value % 100) / 10;
|
|
|
} else {
|
|
|
- LedPoint = 2;
|
|
|
+ /* XXX mili Amper */
|
|
|
+ LedPoint = 3;
|
|
|
LedDigits[0] = value / 100;
|
|
|
value = value % 100;
|
|
|
LedDigits[1] = value / 10;
|
|
@@ -348,6 +444,36 @@ static void TIM1_Config(void)
|
|
|
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
|
|
|
|