]> jspc29.x-matter.uni-frankfurt.de Git - avr.git/commitdiff
PK: ADCuC now with float calibration values in EEPROM
authorPhilipp Klaus <klaus@physik.uni-frankfurt.de>
Thu, 20 Jul 2017 10:26:29 +0000 (12:26 +0200)
committerPhilipp Klaus <klaus@physik.uni-frankfurt.de>
Thu, 20 Jul 2017 10:26:29 +0000 (12:26 +0200)
atmega32u4/adcuc/Makefile
atmega32u4/adcuc/README.md [new file with mode: 0644]
atmega32u4/adcuc/main.c

index 3062ad7ea97eacb9dea7b4d164b3c51164b18a38..690051f49f9a620894d73ec27c3cddaef59f6a2c 100644 (file)
@@ -37,7 +37,7 @@ CINCS =
 
 CDEBUG = -g$(DEBUG)
 CWARN = -Wall -Wstrict-prototypes 
-CTUNING = -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums  -Wl,--relax -Wl,-u,vfprintf -lprintf_flt -lm
+CTUNING = -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums  -Wl,--relax -Wl,-u,vfscanf -lscanf_flt -Wl,-u,vfprintf -lprintf_flt -lm
 #CEXTRA = -Wa,-adhlns=$(<:.c=.lst)
 CFLAGS = $(CDEBUG) $(CDEFS) $(CINCS) -O$(OPT) $(CWARN) $(CSTANDARD) $(CEXTRA) $(CTUNING)
 
diff --git a/atmega32u4/adcuc/README.md b/atmega32u4/adcuc/README.md
new file mode 100644 (file)
index 0000000..8f09a35
--- /dev/null
@@ -0,0 +1,35 @@
+
+## ADCuC
+
+### Protocol
+
+    # sending
+    > V1?\n
+    # returns
+    < A1 0.624\n
+    # The 1 denotes the ADC channel (1 .. 8)
+    # After the channel in the answer follows the voltage (float value).
+
+To calibrate a channel, type:
+
+    C 1 1.05
+    # to scale the channel #1 up by 5%
+
+On a brand new board, the channels need to be be initialized with
+
+    C 1 1.0
+    C 2 1.0
+    ...
+
+### Building and Flashing
+
+    make
+    # enter RESET mode of the chip (short RESET with GND)
+    sudo make program_bootloader
+
+### AVR Source Code Conventions
+
+* Indentation:
+    * No TABs, 2 spaces instead.
+    * Closing curly brackets should be indented too.
+
index 039a67e7a3b69353374da605e8db15d0e03a4228..1b20dddfc7c91f7fd11281e58c0505709405d701 100644 (file)
@@ -66,6 +66,8 @@ uint8_t rxcnt = 0;
 uint8_t rxbuf[7];
 uint8_t txbuf[32];
 
+float calibration[8] = {1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0};
+
 uint8_t conversion_active = 0;
 uint8_t channel = 0;
 
@@ -100,12 +102,25 @@ void getdata(uint8_t buf) {
     rxbuf[rxcnt++] = buf;
     }
   if (buf == '\n' || buf == '\r') {
-    if(rxcnt == 4) { //3 letter commands
-      if (rxbuf[0] == 'V' && rxbuf[2] == '?') {
-        channel = rxbuf[1]-49;
+    if(rxcnt == 4 && rxbuf[0] == 'V' && rxbuf[2] == '?') {
+      uint8_t chan = rxbuf[1]-48;
+      if (chan >= 1 && chan <= 8) {
+        channel = chan-1;
         conversion_active = 1;
         }
       }
+    if (rxbuf[0] == 'C') {
+      float calib_value;
+      uint8_t chan;
+      sscanf(rxbuf, "C %d %f\n", &chan, &calib_value);
+      if (chan >= 1 && chan <= 8) {
+        calibration[chan-1] = calib_value;
+        eeprom_update_float((float*)(0x100+(chan-1)), calib_value);
+        int nbytes = sprintf(txbuf, "OK %1d %.3f\n", chan, calib_value);
+        usb_serial_write(txbuf, nbytes);
+        usb_serial_flush_output();
+        }
+      }
     }
   if (rxcnt >= 7 || buf == '\n' || buf == '\r') { rxcnt = 0; }
   }
@@ -114,6 +129,7 @@ void original_value(uint8_t channel, uint8_t get_value_1, uint8_t get_value_2) {
   uint16_t val = (get_value_1 << 8) | get_value_2;
   float result = val;
   result = result / 8.0 / 1000.;
+  result *= calibration[channel];
   send_answer(channel+1, result);
   }
 
@@ -196,6 +212,8 @@ __attribute__((naked)) int main(void) {
   DDRE  = 0b00000000;
 
   //limit[3]  |= eeprom_read_byte((uint8_t*)0x26);
+  for (int i = 0; i < 8; i++)
+    calibration[i] = eeprom_read_float((float*)(0x100+i));
 
   //Timer0 at ~488 Hz overflow for ADC control and buttons
   TCCR0B = 4 << CS00; //(1 << CS01) | (1 << CS00);