Getting Started

(lily) #1

Chapter 9 – Digital Meets Analog – ADC and DAC


itoa(fractpart, voltfractpart, 10);


// Send the voltage to the PC


logical part and still nothing worked. Finally, because there
s nothing logical to try, I commented out the itoa functions. And the Butterfly
Also, it started returning ‘!’ rather than the black club for
dn’t return the correct voltage, because I

e end of Chapter 6. Guess what? They

Later


{''0','\0'};

They work just fine in the original Butterfly
e hard way that if you assign memory to an
ray and then foolishly write beyond that memory, say to voltintpart[2], the third
ents you will in fact write to the byte
in ry that follows the array bytes which may not cause a problem if nothing
om the ASCII code for an


char voltintpart[]= {'0','0',0','\0'};

fractpart = ADCresult%50;


itoa(intpart, voltintpart, 10);


sendString("The reading is ");
sendChar(voltintpart [0]);
sendChar('.');
sendChar(voltfractpart [0]);
sendString(" volts.\r");
}


I c
a


ommented out each
w
no longer messed up.
each character I sent it. Of course, it di
wasn’t converting it to ASCII, but it was running fine otherwise and correctly
returned the light and temperature. The itoa function is in the standard library, so I
assumed that it must have a problem. I changed it to the itoa function (and the
other support functions) that we wrote at th
also fail! I went for a long walk.


, after more staring at the function I noticed:

char voltintpart[]=
char voltfractpart[]= {'0','\0'};


These can’t be the problem, can they?
code. But many years ago I learned th
ar
element of the array which only has two elem
memo
else is using that byte, or it might just change it fr
explanation point to the Microsoft extended ASCII code for a black club. So I
enlarged them to:


char voltfractpart[]= {''0',0','0','\0'};


And the code works just fine.

Free download pdf