From: Philipp Klaus Date: Thu, 20 Jul 2017 13:25:32 +0000 (+0200) Subject: ADCuC: Set and get calibration, slightly different commands X-Git-Url: https://jspc29.x-matter.uni-frankfurt.de/git/?a=commitdiff_plain;h=b1cb0edc279c65cab6a9b15cd259d21afd522335;p=avr.git ADCuC: Set and get calibration, slightly different commands --- diff --git a/atmega32u4/adcuc/README.md b/atmega32u4/adcuc/README.md index 8f09a35..4bb8f13 100644 --- a/atmega32u4/adcuc/README.md +++ b/atmega32u4/adcuc/README.md @@ -4,9 +4,9 @@ ### Protocol # sending - > V1?\n + > V 1?\n # returns - < A1 0.624\n + < V 1 0.624\n # The 1 denotes the ADC channel (1 .. 8) # After the channel in the answer follows the voltage (float value). diff --git a/atmega32u4/adcuc/main.c b/atmega32u4/adcuc/main.c index 1b20ddd..9eac02a 100644 --- a/atmega32u4/adcuc/main.c +++ b/atmega32u4/adcuc/main.c @@ -63,7 +63,7 @@ volatile uint16_t time = 0; uint8_t rxcnt = 0; -uint8_t rxbuf[7]; +uint8_t rxbuf[32]; uint8_t txbuf[32]; float calibration[8] = {1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0}; @@ -85,7 +85,7 @@ uint8_t hex_to_int(uint8_t h) { //assumes valid number } void send_answer(uint8_t chan, float v) { - int nbytes = sprintf(txbuf, "A%1d %.3f\n", chan, v); + int nbytes = sprintf(txbuf, "V %1d %.4f\n", chan, v); usb_serial_write(txbuf, nbytes); usb_serial_flush_output(); } @@ -102,27 +102,35 @@ void getdata(uint8_t buf) { rxbuf[rxcnt++] = buf; } if (buf == '\n' || buf == '\r') { - if(rxcnt == 4 && rxbuf[0] == 'V' && rxbuf[2] == '?') { - uint8_t chan = rxbuf[1]-48; + if(rxcnt == 5 && rxbuf[0] == 'V' && rxbuf[3] == '?') { + uint8_t chan = rxbuf[2]-48; if (chan >= 1 && chan <= 8) { channel = chan-1; conversion_active = 1; } } - if (rxbuf[0] == 'C') { + if(rxcnt == 5 && rxbuf[0] == 'C' && rxbuf[3] == '?') { + uint8_t chan = rxbuf[2]-48; + if (chan >= 1 && chan <= 8) { + int nbytes = sprintf(txbuf, "C %1d %.4f\n", chan, calibration[chan-1]); + usb_serial_write(txbuf, nbytes); + usb_serial_flush_output(); + } + } + if (rxbuf[0] == 'C' && rxbuf[3] != '?') { float calib_value; uint8_t chan; - sscanf(rxbuf, "C %d %f\n", &chan, &calib_value); - if (chan >= 1 && chan <= 8) { + uint8_t nfilled = sscanf(rxbuf, "C %d %f\n", &chan, &calib_value); + if (chan >= 1 && chan <= 8 && nfilled == 2) { 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); + eeprom_update_float((float*)(0x100+(chan-1)*4), calib_value); + int nbytes = sprintf(txbuf, "C %1d %.4f\n", chan, calib_value); usb_serial_write(txbuf, nbytes); usb_serial_flush_output(); } } } - if (rxcnt >= 7 || buf == '\n' || buf == '\r') { rxcnt = 0; } + if (rxcnt >= 31 || buf == '\n' || buf == '\r') { rxcnt = 0; } } void original_value(uint8_t channel, uint8_t get_value_1, uint8_t get_value_2) { @@ -213,7 +221,7 @@ __attribute__((naked)) int main(void) { //limit[3] |= eeprom_read_byte((uint8_t*)0x26); for (int i = 0; i < 8; i++) - calibration[i] = eeprom_read_float((float*)(0x100+i)); + calibration[i] = eeprom_read_float((float*)(0x100+i*4)); //Timer0 at ~488 Hz overflow for ADC control and buttons TCCR0B = 4 << CS00; //(1 << CS01) | (1 << CS00);