From c1acd6924c71d901e412c0f3716b730ffe21df15 Mon Sep 17 00:00:00 2001 From: Philipp Klaus Date: Thu, 20 Jul 2017 12:26:29 +0200 Subject: [PATCH] PK: ADCuC now with float calibration values in EEPROM --- atmega32u4/adcuc/Makefile | 2 +- atmega32u4/adcuc/README.md | 35 +++++++++++++++++++++++++++++++++++ atmega32u4/adcuc/main.c | 24 +++++++++++++++++++++--- 3 files changed, 57 insertions(+), 4 deletions(-) create mode 100644 atmega32u4/adcuc/README.md diff --git a/atmega32u4/adcuc/Makefile b/atmega32u4/adcuc/Makefile index 3062ad7..690051f 100644 --- a/atmega32u4/adcuc/Makefile +++ b/atmega32u4/adcuc/Makefile @@ -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 index 0000000..8f09a35 --- /dev/null +++ b/atmega32u4/adcuc/README.md @@ -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. + diff --git a/atmega32u4/adcuc/main.c b/atmega32u4/adcuc/main.c index 039a67e..1b20ddd 100644 --- a/atmega32u4/adcuc/main.c +++ b/atmega32u4/adcuc/main.c @@ -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); -- 2.43.0