From d45aad4c74c73c327f0647e6c839d3c95ce63a02 Mon Sep 17 00:00:00 2001 From: Philipp Klaus Date: Thu, 13 Jul 2017 15:21:08 +0200 Subject: [PATCH] ADCuC: float output on USB w/ sprintf() --- atmega32u4/adcuc/Makefile | 2 +- atmega32u4/adcuc/main.c | 24 +++++++++--------------- 2 files changed, 10 insertions(+), 16 deletions(-) diff --git a/atmega32u4/adcuc/Makefile b/atmega32u4/adcuc/Makefile index df1b1fc..3062ad7 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 +CTUNING = -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -Wl,--relax -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/main.c b/atmega32u4/adcuc/main.c index 491804b..039a67e 100644 --- a/atmega32u4/adcuc/main.c +++ b/atmega32u4/adcuc/main.c @@ -5,6 +5,7 @@ #include #include #include +#include #include #define FIRMWARE_VERSION 0x011 @@ -63,7 +64,7 @@ volatile uint16_t time = 0; uint8_t rxcnt = 0; uint8_t rxbuf[7]; -uint8_t txbuf[16]; +uint8_t txbuf[32]; uint8_t conversion_active = 0; uint8_t channel = 0; @@ -81,22 +82,13 @@ uint8_t hex_to_int(uint8_t h) { //assumes valid number return h-0x61+10; } -void send_answer_hex(uint8_t chan, uint16_t v) { - txbuf[0]='A';//ADC - txbuf[1]=nib_to_hex(chan,0); - txbuf[2]='-';//temperature or modi - txbuf[3]=nib_to_hex(v,3); - txbuf[4]=nib_to_hex(v,2); - txbuf[5]=nib_to_hex(v,1); - txbuf[6]=nib_to_hex(v,0); - txbuf[7]='\n'; - txbuf[8] = 0; - usb_serial_write(txbuf,8); +void send_answer(uint8_t chan, float v) { + int nbytes = sprintf(txbuf, "A%1d %.3f\n", chan, v); + usb_serial_write(txbuf, nbytes); usb_serial_flush_output(); } - ISR(TIMER0_OVF_vect) { time++; asm volatile("wdr"); @@ -119,8 +111,10 @@ void getdata(uint8_t buf) { } void original_value(uint8_t channel, uint8_t get_value_1, uint8_t get_value_2) { - uint16_t result = (get_value_1 << 8) | get_value_2; - send_answer_hex(channel+1,result); + uint16_t val = (get_value_1 << 8) | get_value_2; + float result = val; + result = result / 8.0 / 1000.; + send_answer(channel+1, result); } void update_eeprom(void) { -- 2.43.0