|
@@ -1,4 +1,4 @@
|
|
|
-/**
|
|
|
+/**current_mode
|
|
|
******************************************************************************
|
|
|
* @file Project/main.c
|
|
|
* @author "Volodymyr M. Shylov" <shilow@ukr.net>
|
|
@@ -27,11 +27,16 @@
|
|
|
#define TIM4_PERIOD (uint8_t)124
|
|
|
|
|
|
/* Private typedef -----------------------------------------------------------*/
|
|
|
+typedef enum {
|
|
|
+ voltage_mode,
|
|
|
+ current_mode
|
|
|
+} measure_mode_t;
|
|
|
/* Private constants ---------------------------------------------------------*/
|
|
|
/* Private variables ---------------------------------------------------------*/
|
|
|
__IO uint16_t ConversionBuffer[ADC_SMPLS];
|
|
|
__IO uint8_t BufferIndex = 0;
|
|
|
-static uint16_t Voltage;
|
|
|
+static uint16_t Voltage, Current;
|
|
|
+static measure_mode_t MeasureMode = voltage_mode;
|
|
|
|
|
|
/* Private function prototypes -----------------------------------------------*/
|
|
|
static void boardInit(void);
|
|
@@ -46,21 +51,39 @@ void main(void)
|
|
|
|
|
|
/* Infinite loop */
|
|
|
while (1) {
|
|
|
- Delay(100);
|
|
|
+ wfi();
|
|
|
if (BufferIndex >= ADC_SMPLS) {
|
|
|
BufferIndex = 0;
|
|
|
int8_t i;
|
|
|
- uint32_t vbuf = 0;
|
|
|
+ uint32_t tbuf = 0;
|
|
|
for (i=0; i<ADC_SMPLS; i++) {
|
|
|
- vbuf += ConversionBuffer[i];
|
|
|
+ tbuf += ConversionBuffer[i];
|
|
|
}
|
|
|
- vbuf /= ADC_SMPLS;
|
|
|
- vbuf *= VOLTAGE_MUL;
|
|
|
- vbuf /= VOLT_MUL_MUL;
|
|
|
- Voltage = vbuf;
|
|
|
+
|
|
|
+ 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();
|
|
|
}
|
|
|
- showV();
|
|
|
- showC();
|
|
|
}
|
|
|
|
|
|
}
|
|
@@ -77,10 +100,13 @@ static void showV(void) {
|
|
|
}
|
|
|
|
|
|
static void showC(void) {
|
|
|
- LedDigits[4] = (uint8_t)(VOLTAGE_MUL>>8 & 0xf );
|
|
|
- LedDigits[5] = (uint8_t)(VOLTAGE_MUL>>4 & 0xf);
|
|
|
- LedDigits[6] = (uint8_t)(VOLTAGE_MUL & 0xf);
|
|
|
- LedDigits[7] = (uint8_t)(VOLTAGE_MUL>>12);
|
|
|
+ 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) {
|
|
@@ -158,12 +184,10 @@ static void boardInit(void) {
|
|
|
/* 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)((uint8_t)0x01 << (uint8_t)ADC_SCHTU);
|
|
|
- /* Enable the ADC1 peripheral */
|
|
|
- ADC1->CR1 |= ADC1_CR1_ADON;
|
|
|
+ ADC1->TDRL |= (uint8_t)((1 << ADC_SCHTU) | (1 << ADC_SCHTI));
|
|
|
/* Enable the ADC1 EOC interrupt */
|
|
|
ADC1->CSR |= (uint8_t)ADC1_IT_EOCIE;
|
|
|
- /*Start Conversion */
|
|
|
+ /* Enable the ADC1 peripheral */
|
|
|
ADC1->CR1 |= ADC1_CR1_ADON;
|
|
|
|
|
|
/** Configure TIM1 peripheral. Time Base configuration:
|
|
@@ -177,8 +201,8 @@ static void boardInit(void) {
|
|
|
TIM1->ARRH = (uint8_t)(49 >> 8);
|
|
|
TIM1->ARRL = (uint8_t)(49);
|
|
|
/* Set the Prescaler value */
|
|
|
- TIM1->PSCRH = (uint8_t)(999 >> 8);
|
|
|
- TIM1->PSCRL = (uint8_t)(999);
|
|
|
+ TIM1->PSCRH = (uint8_t)(499 >> 8);
|
|
|
+ TIM1->PSCRL = (uint8_t)(499);
|
|
|
/* Select the Counter Mode */
|
|
|
TIM1->CR1 = 0x0;
|
|
|
/* Set the Repetition Counter value */
|