I’m learning MCU programming with Atmega32 & winavr. I need to convert ADC value to something else (say voltage=5*ADCvalue/1023) which needs to deal with floating point/double. Then to display on LCD I need to convert the calculated value to string/array (eg. Sprintf(….)). Both these (floating point number & print function) consumes large memory/code area of the microcontroller (even with the small versions made for MCU).
Can any one please help me with any alternative way. I goggled a lot but failed to figure out solution specific to my case (I’m not an expert in programming/MCU).
ISR(ADC_vect)
{
uint8_t thelow=ADCL;
uint16_t adc10bit=ADCH<<2 |thelow>>6;
float adcvolt=4.8*((adc10bit)/1023);
char adcvoltstr[10];
sprintf(adcvoltstr,“%.2f”,adcvolt); //sprintf() may not be used
there’s an ATmega32 specific forum on avrfreaks.net.
Lots of help there.
99% of the time you can do A/D sample data processing by using fixed point and avoid the overhead of floating point, to include the large size of the float support for printf()
If you don’t know about fixed point arithmetic on computers … you can read about it on the 'net or ask on the forum. It’s quite simple.
ALSO… your ISR needs a rewrite. You cannot call a C/C++ library from within an ISR. One should never do a delay() in an ISR. Concept of an ISR is to input the data, store it in a RAM buffer or list, tell the application (non-ISR) that there is more data available, exit. A few microseconds.
But more over…
As a beginner, you should not use an ISR at all; just use I/O instructions in the main() program and do a polling loop on A/D complete. This is a starter program.
You should never use floats unless there’s no way around it, or you aren’t worried about performance. There’s a lot of overhead, and since the MCU isn’t a floating point processor, it’s handling things internally as fixed point anyway.
ISRs should do as little work as possible. I won’t go as far as stevech and tell you not to use them at all, but I will agree that it’s easier to do things without them. (Avoiding ISRs makes everything ‘synchronous’, so your code is always where you think it is, whereas an interrupt could fire at any point within your code, and this could cause some strange race conditions. These are the same problems you see with multithreaded programming on desktop computers.)
Floats in small micros is a luxury few can afford. People coming from PC programming don’t realize that low cost micros don’t have floating point hardware and software floating point is (a) memory-intensive; (b) CPU intensive; (c) algorithmically not required most of the time! You can most often rearrange your thinking to use integer math, or use fixed point math.