|
@@ -20,7 +20,8 @@
|
|
|
/* Includes ------------------------------------------------------------------*/
|
|
|
#include "stm8s.h"
|
|
|
#include "board.h"
|
|
|
-#include "i2c.h"
|
|
|
+#include "led.h"
|
|
|
+#include "delay.h"
|
|
|
|
|
|
/* Private define ------------------------------------------------------------*/
|
|
|
#define TIM4_PERIOD (uint8_t)124
|
|
@@ -28,98 +29,87 @@
|
|
|
#define LED_ONE_PERIOD 2
|
|
|
|
|
|
/* Private typedef -----------------------------------------------------------*/
|
|
|
-/* Private constants ---------------------------------------------------------*/
|
|
|
-static const uint16_t led_num[8] = {0x10, 0x20, 0x80, 0x40, 0x01, 0x02, 0x08, 0x04};
|
|
|
+typedef enum {
|
|
|
+ voltage_mode,
|
|
|
+ current_mode
|
|
|
+} measure_mode_t;
|
|
|
|
|
|
+/* Private constants ---------------------------------------------------------*/
|
|
|
/* Private variables ---------------------------------------------------------*/
|
|
|
__IO uint16_t ConversionBuffer[ADC_SMPLS];
|
|
|
__IO uint8_t BufferIndex = 0;
|
|
|
-__IO uint8_t LedCnt = 0;
|
|
|
-static uint8_t LedDigits[8] = {1, 2, 3, 4, 5, 6, 7, 8}; // digits to dsplay
|
|
|
-static uint8_t LedPoint[8] = {0, 0, 0, 1, 0, 0, 0, 1}; // dots for digits
|
|
|
+static uint16_t Voltage, Current;
|
|
|
+static measure_mode_t MeasureMode = voltage_mode;
|
|
|
|
|
|
/* Private function prototypes -----------------------------------------------*/
|
|
|
static void boardInit(void);
|
|
|
-static void led_SelectDigit (uint8_t first);
|
|
|
-static void led_OutputValue(void);
|
|
|
+static void showV(void);
|
|
|
+static void showC(void);
|
|
|
|
|
|
void main(void)
|
|
|
{
|
|
|
/* Board Configuration */
|
|
|
boardInit();
|
|
|
-
|
|
|
- /* I2C Configuration */
|
|
|
- //i2c_master_init();
|
|
|
+ Delay(1000);
|
|
|
|
|
|
/* Infinite loop */
|
|
|
while (1) {
|
|
|
- if (LedCnt >= LED_ONE_PERIOD) {
|
|
|
- LedCnt = 0;
|
|
|
- led_OutputValue();
|
|
|
- }
|
|
|
-
|
|
|
wfi();
|
|
|
+ if (BufferIndex >= ADC_SMPLS) {
|
|
|
+ BufferIndex = 0;
|
|
|
+ int8_t i;
|
|
|
+ uint32_t tbuf = 0;
|
|
|
+ for (i=0; i<ADC_SMPLS; i++) {
|
|
|
+ tbuf += ConversionBuffer[i];
|
|
|
+ }
|
|
|
+
|
|
|
+ if (MeasureMode == voltage_mode) {
|
|
|
+ MeasureMode = current_mode;
|
|
|
+ ADC1->CSR &= (uint8_t)(~ADC1_CSR_CH);
|
|
|
+ ADC1->CSR |= (uint8_t)(ADC_CHNLI);
|
|
|
+
|
|
|
+ tbuf /= ADC_SMPLS;
|
|
|
+ //tbuf *= VOLTAGE_MUL;
|
|
|
+ //tbuf /= VOLT_MUL_MUL;
|
|
|
+ Voltage = tbuf;
|
|
|
+ } else {
|
|
|
+ MeasureMode = voltage_mode;
|
|
|
+ ADC1->CSR &= (uint8_t)(~ADC1_CSR_CH);
|
|
|
+ ADC1->CSR |= (uint8_t)(ADC_CHNLU);
|
|
|
+
|
|
|
+ tbuf /= ADC_SMPLS;
|
|
|
+ //tbuf *= CURRENT_MUL;
|
|
|
+ //tbuf /= CURR_MUL_MUL;
|
|
|
+ Current = tbuf;
|
|
|
+ }
|
|
|
+
|
|
|
+ /* arrived new data, show it */
|
|
|
+ showV();
|
|
|
+ showC();
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
/* Private functions ---------------------------------------------------------*/
|
|
|
-/*
|
|
|
- * Output current value to next led
|
|
|
- */
|
|
|
-static void led_OutputValue(void) {
|
|
|
- static uint8_t ledn = 0;
|
|
|
-
|
|
|
- /* all off */
|
|
|
- LED_OUT_OFF;
|
|
|
-
|
|
|
- /* Fire on nex led */
|
|
|
- led_SelectDigit(ledn);
|
|
|
-
|
|
|
- /* out next value */
|
|
|
- switch (LedDigits[ledn]) {
|
|
|
- case 0:
|
|
|
- LED_OUT_0;
|
|
|
- break;
|
|
|
- case 1:
|
|
|
- LED_OUT_1;
|
|
|
- break;
|
|
|
- case 2:
|
|
|
- LED_OUT_2;
|
|
|
- break;
|
|
|
- case 3:
|
|
|
- LED_OUT_3;
|
|
|
- break;
|
|
|
- case 4:
|
|
|
- LED_OUT_4;
|
|
|
- break;
|
|
|
- case 5:
|
|
|
- LED_OUT_5;
|
|
|
- break;
|
|
|
- case 6:
|
|
|
- LED_OUT_6;
|
|
|
- break;
|
|
|
- case 7:
|
|
|
- LED_OUT_7;
|
|
|
- break;
|
|
|
- case 8:
|
|
|
- LED_OUT_8;
|
|
|
- break;
|
|
|
- case 9:
|
|
|
- LED_OUT_9;
|
|
|
- break;
|
|
|
- default:
|
|
|
- LED_OUT_MM;
|
|
|
- }
|
|
|
- /*
|
|
|
- if(LedPoint[ledn] != 0) {
|
|
|
- LED_OUT_DP;
|
|
|
- }
|
|
|
- */
|
|
|
- ledn ++;
|
|
|
- if (ledn > 7) {
|
|
|
- ledn = 0;
|
|
|
- }
|
|
|
+static void showV(void) {
|
|
|
+ uint16_t a = Voltage;
|
|
|
+ LedDigits[0] = a / 1000;
|
|
|
+ uint16_t b = a % 1000;
|
|
|
+ LedDigits[1] = b / 100;
|
|
|
+ uint8_t c = b % 100;
|
|
|
+ LedDigits[2] = c / 10;
|
|
|
+ LedDigits[3] = c % 10;
|
|
|
+}
|
|
|
+
|
|
|
+static void showC(void) {
|
|
|
+ uint16_t a = Current;
|
|
|
+ LedDigits[4] = a / 1000;
|
|
|
+ uint16_t b = a % 1000;
|
|
|
+ LedDigits[5] = b / 100;
|
|
|
+ uint8_t c = b % 100;
|
|
|
+ LedDigits[6] = c / 10;
|
|
|
+ LedDigits[7] = c % 10;
|
|
|
}
|
|
|
|
|
|
static void boardInit(void) {
|
|
@@ -162,41 +152,58 @@ static void boardInit(void) {
|
|
|
SPI_PORT->CR1 |= (uint8_t)(SPI_SCK|SPI_DATA);
|
|
|
SPI_PORT->CR2 |= (uint8_t)(SPI_SCK|SPI_DATA);
|
|
|
|
|
|
- /** 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 */
|
|
|
- /*
|
|
|
+ /* 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 ADC1 peripheral */
|
|
|
+ /* Configure the data alignment */
|
|
|
+ ADC1->CR2 |= (uint8_t)(ADC1_CR2_ALIGN);
|
|
|
+ /* Set the single conversion mode */
|
|
|
+ ADC1->CR1 &= (uint8_t)(~ADC1_CR1_CONT);
|
|
|
+ /* Clear the ADC1 channels */
|
|
|
+ ADC1->CSR &= (uint8_t)(~ADC1_CSR_CH);
|
|
|
+ /* Select the ADC1 channel */
|
|
|
+ ADC1->CSR |= (uint8_t)(ADC_CHNLU);
|
|
|
+ /* Clear the SPSEL bits */
|
|
|
+ ADC1->CR1 &= (uint8_t)(~ADC1_CR1_SPSEL);
|
|
|
+ /* Select the prescaler division factor according to ADC1_PrescalerSelection values */
|
|
|
+ ADC1->CR1 |= (uint8_t)(ADC1_PRESSEL_FCPU_D8);
|
|
|
+ /* Enable the external Trigger */
|
|
|
+ ADC1->CR2 |= (uint8_t)(ADC1_CR2_EXTTRIG);
|
|
|
+ /* Set the 'Internal TIM1 TRGO event' as external trigger */
|
|
|
+ ADC1->CR2 &= (uint8_t)~(ADC1_CR2_EXTSEL);
|
|
|
+ /* disables the ADC1 Schmitt Trigger on a selected channel(s) */
|
|
|
+ ADC1->TDRL |= (uint8_t)((1 << ADC_SCHTU) | (1 << ADC_SCHTI));
|
|
|
+ /* Enable the ADC1 EOC interrupt */
|
|
|
+ ADC1->CSR |= (uint8_t)ADC1_IT_EOCIE;
|
|
|
+ /* Enable the ADC1 peripheral */
|
|
|
+ ADC1->CR1 |= ADC1_CR1_ADON;
|
|
|
+
|
|
|
+ /** Configure TIM1 peripheral. 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);
|
|
|
-
|
|
|
+ /* Set the Autoreload value */
|
|
|
+ TIM1->ARRH = (uint8_t)(49 >> 8);
|
|
|
+ TIM1->ARRL = (uint8_t)(49);
|
|
|
+ /* Set the Prescaler value */
|
|
|
+ TIM1->PSCRH = (uint8_t)(499 >> 8);
|
|
|
+ TIM1->PSCRL = (uint8_t)(499);
|
|
|
+ /* Select the Counter Mode */
|
|
|
+ TIM1->CR1 = 0x0;
|
|
|
+ /* Set the Repetition Counter value */
|
|
|
+ TIM1->RCR = 0;
|
|
|
/* Trigrer configuration */
|
|
|
- //TIM1_SelectOutputTrigger(TIM1_TRGOSOURCE_UPDATE);
|
|
|
-
|
|
|
+ TIM1->CR2 = (uint8_t)TIM1_TRGOSOURCE_UPDATE;
|
|
|
/* Update Interrupt Enable */
|
|
|
- ////TIM1_ITConfig(TIM1_IT_UPDATE, ENABLE);
|
|
|
-
|
|
|
+ //TIM1->IER |= (uint8_t)TIM1_IT_UPDATE;
|
|
|
/* Enable TIM1 */
|
|
|
- //TIM1_Cmd(ENABLE);
|
|
|
+ TIM1->CR1 |= TIM1_CR1_CEN;
|
|
|
|
|
|
/**
|
|
|
TIM4 configuration:
|
|
@@ -225,28 +232,6 @@ static void boardInit(void) {
|
|
|
TIM4->CR1 |= (uint8_t)TIM4_CR1_CEN;
|
|
|
}
|
|
|
|
|
|
-/*
|
|
|
- * Select LED Digit.
|
|
|
- * If first != 0 - select next, by shift '1' on outputs,
|
|
|
- * else select first digit.
|
|
|
- * led digits sequence (b - bottom, t - top):
|
|
|
- * b1, b2, b4, b3, t1, t2, t4, t3.
|
|
|
- */
|
|
|
-static void led_SelectDigit (const uint8_t first) {
|
|
|
- uint8_t i;
|
|
|
- uint8_t data = led_num[first];
|
|
|
- for (i=0; i<8; i++) {
|
|
|
- GPIO_LOW(SPI_PORT, SPI_SCK); // prepare CLK
|
|
|
- if (data & 0x80) { // if msb == 1
|
|
|
- GPIO_HIGH(SPI_PORT, SPI_DATA); // DATA = 1
|
|
|
- } else {
|
|
|
- GPIO_LOW(SPI_PORT, SPI_DATA); // DATA = 0
|
|
|
- }
|
|
|
- GPIO_HIGH(SPI_PORT, SPI_SCK); // shift bit
|
|
|
- data <<= 1;
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
#ifdef USE_FULL_ASSERT
|
|
|
/**
|
|
|
* @brief Reports the name of the source file and the source line number
|