Vladimir N. Shilov 11 hónapja
szülő
commit
d9e38b51f1
4 módosított fájl, 38 hozzáadás és 23 törlés
  1. 1 0
      hw/mcu.txt
  2. 8 10
      lib/led.c
  3. 2 2
      lib/led.h
  4. 27 11
      src/main.c

+ 1 - 0
hw/mcu.txt

@@ -45,6 +45,7 @@ Uвх будет от 14,19 до 14,85 вольт. маловато.
 
 при 36 кОм - 15,18-15,84 вольт;
 при 38,3 кОм - 15,939-16,599 вольт.
+Ку=4,93
 
 ---
 Усилитель напряжения токового шунта

+ 8 - 10
lib/led.c

@@ -9,12 +9,12 @@
 #define GPIO_LOW(a,b)     a->ODR &= ~b
 #define GPIO_TOGGLE(a,b)  a->ODR ^= b
 
-uint8_t LedDigits[LED_DIGITS_NUM] = {1, 2, 3, 4, 5, 6, 7, 8}; // digits to dsplay
-uint8_t LedPoint[LED_DIGITS_NUM] = {0, 1, 0, 0, 0, 1, 0, 0}; // dots for digits
+uint8_t LedDigits[LED_DIGITS_NUM] = {0xff}; // digits to dsplay
+uint8_t LedPoint[LED_DIGITS_NUM] = {0, 1, 0, 0, 0, 0, 0, 1}; // dots for digits
+/*                                                 1     2     3     4     5     6     7     8 */
+static const uint8_t led_num[LED_DIGITS_NUM] = {0x10, 0x08, 0x04, 0x02, 0x20, 0x40, 0x80, 0x01};
 
-static const uint16_t led_num[8] = {0x08, 0x04, 0x02, 0x01, 0x10, 0x20, 0x40, 0x80};
-
-static void led_SelectDigit (const uint8_t first);
+static void led_SelectDigit (const uint8_t pos);
 
 /*
  * Output current value to next led
@@ -96,14 +96,12 @@ void led_OutputValue(void) {
 
 /*
  * 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.
+ * t1, t2, t4, t3, b1, b2, b3, b4.
  */
-static void led_SelectDigit (const uint8_t first) {
+static void led_SelectDigit (const uint8_t pos) {
   uint8_t i;
-  uint8_t data = led_num[first];
+  uint8_t data = led_num[pos];
   for (i=0; i<8; i++) {
     GPIO_LOW(SPI_PORT, SPI_SCK); // prepare CLK
     if (data & 0x80) { // if msb == 1

+ 2 - 2
lib/led.h

@@ -36,7 +36,7 @@ E   C
 #define LED_SEG_AF_ODR    GPIOD->ODR
 #define LED_SEG_A         GPIO_PIN_1
 #define LED_SEG_F         GPIO_PIN_2
-#define LED_SEG_AF        (LED_SEG_A | LED_SEG_A)
+#define LED_SEG_AF        (LED_SEG_A | LED_SEG_F)
 #define LED_SEG_AF_ON(a)  LED_SEG_AF_ODR &= ~(a)
 
 /* LED symbols */
@@ -57,7 +57,7 @@ E   C
 #define LED_OUT_PL    LED_SEG_BE_ON(LED_SEG_B); LED_SEG_CG_ON(LED_SEG_CG)
 #define LED_OUT_O     LED_SEG_AF_ON(LED_SEG_AF); LED_SEG_BE_ON(LED_SEG_B); LED_SEG_CG_ON(LED_SEG_G);
 #define LED_OUT_U     LED_SEG_AF_ON(LED_SEG_F); LED_SEG_BE_ON(LED_SEG_BE); LED_SEG_CG_ON(LED_SEG_C); LED_SEG_DH_ON(LED_SEG_D)
-#define LED_OUT_A     LED_SEG_AF_ON(LED_SEG_AF); LED_SEG_BE_ON(LED_SEG_BE); LED_SEG_CG_ON(LED_SEG_C)
+#define LED_OUT_A     LED_SEG_AF_ON(LED_SEG_AF); LED_SEG_BE_ON(LED_SEG_BE); LED_SEG_CG_ON(LED_SEG_CG)
 
 /* shift register control pins */
 #define SPI_PORT      GPIOC

+ 27 - 11
src/main.c

@@ -31,6 +31,7 @@
 /* Private variables ---------------------------------------------------------*/
 __IO uint16_t ConversionBuffer[ADC_SMPLS];
 __IO uint8_t BufferIndex = 0;
+static uint16_t Voltage;
 
 /* Private function prototypes -----------------------------------------------*/
 static void boardInit(void);
@@ -48,6 +49,18 @@ void main(void)
     Delay(100);
     if (BufferIndex >= ADC_SMPLS) {
       BufferIndex = 0;
+      int8_t i;
+      uint32_t vbuf = 0;
+      for (i=0; i<64; i++) {
+        vbuf += ConversionBuffer[i];  
+      }
+      vbuf *= ADC_VREF;
+      vbuf += 512;
+      vbuf /= 1023;
+      vbuf *= 45727;
+      vbuf += 5000;
+      vbuf /= 10000;
+      Voltage = vbuf;
     }
     showV();
     showC();
@@ -57,17 +70,20 @@ void main(void)
 
 /* Private functions ---------------------------------------------------------*/
 static void showV(void) {
-  LedDigits[0] = 0;
-  LedDigits[1] = 1;
-  LedDigits[2] = 2;
-  LedDigits[3] = 3; //led_U;
+  uint16_t a = (Voltage + 5) / 10;
+  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) {
- LedDigits[4] = 4;
- LedDigits[5] = 5;
- LedDigits[6] = 6;
- LedDigits[7] = 7; //led_A;
+ LedDigits[4] = 5;
+ LedDigits[5] = 6;
+ LedDigits[6] = 7;
+ LedDigits[7] = 8;
 }
 
 static void boardInit(void) {
@@ -88,7 +104,7 @@ static void boardInit(void) {
   /* SWIM clock */
   CLK->SWIMCCR = 0x00;
 
-  /* ІЦШЕСР pin to Push-Pull, High, Fast */
+  /* SWITCH pin to Push-Pull, High, Fast */
   SWITCH_PORT->ODR |= (uint8_t)(SWITCH_PIN);
   SWITCH_PORT->DDR |= (uint8_t)(SWITCH_PIN);
   SWITCH_PORT->CR1 |= (uint8_t)(SWITCH_PIN);
@@ -117,7 +133,7 @@ static void boardInit(void) {
   GPIOD->CR2 |= (uint8_t)LED_SEG_AF;
 
   /* `SPI` pins to Push-Pull, High, Fast */
-  SPI_PORT->ODR |= (uint8_t)(SPI_SCK|SPI_DATA);
+  SPI_PORT->ODR = (uint8_t)(SPI_SCK|SPI_DATA);
   SPI_PORT->DDR |= (uint8_t)(SPI_SCK|SPI_DATA);
   SPI_PORT->CR1 |= (uint8_t)(SPI_SCK|SPI_DATA);
   SPI_PORT->CR2 |= (uint8_t)(SPI_SCK|SPI_DATA);
@@ -139,7 +155,7 @@ static void boardInit(void) {
   /* 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_D2);
+  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 */