|
|
View previous topic :: View next topic |
Author |
Message |
hansknec
Joined: 30 Sep 2004 Posts: 25 Location: Virginia, USA
|
printf Format |
Posted: Thu Jul 28, 2005 1:06 pm |
|
|
I have designed a device, essentially an autoranging ammeter, where the PIC reads an ADC and reports the output. The autoranging function is implemented as follows:
10 bit ADC (1024 bits). Bits of interest for my application are 0 to 999. If the reading is below 75 counts, I increase the gain of an input op-amp by 10 and thus the counts are now 750. Conversely if the counts increase to 950 I decrease the gain of the op-amp by 10 and the counts are then 95. This scheme gives me sufficient hysteresis between scales.
My application crosses 5 decades, and I report the output to LCD and a serial port in a formatted scientific notation of Amperes.
Example: counts are 550 from the ADC and the gain of the op-amp is such that this would equal 5.50 nanoamps measured. I want to report the value to both the LCD and serial as "5.50 e-09"
If counts are 216 and the gain is set for uA, then the reported output would be "2.16 e-06"
I originally wrote my code using Pic Basic Pro (didn't own CCS then ) and now I want to redo it in C, but I'm not quite sure about the formatting of the printf.
In Pic Basic, I would use the following command:
Code: | serout2 portb.1,16468,[#valu dig 2,".",#valu dig 1,#valu dig 0,"e-0",#rscale,13] |
This would take the counts of "valu", which would be 0 to 999, and report digit 2, followed by a "." decimal point, then digit 1 and digit 0, and then print an "e -0", and then finally follow up with the number of the scale I'm on which was 5 through 9.
Obviously, PBP doesn't support floating point math and this was my own fix for the problem.
I'm suspecting that the answer in "C" will be something simple like just taking my counts and multplying it by a float corresponding to my scale and displaying it as a float using the %e format, but right now I don't have access to my programmer/ debugger and I'm writing the code blind.
(did I just answer my own question?)
On a related question, Anybody know of a web site or utility that lets me test code blind and see the result for a given printf?
Any help would be greatly appreciated.
-John |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Jul 28, 2005 1:32 pm |
|
|
Quote: |
Anybody know of a web site or utility that lets me test code blind and
see the result for a given printf ? |
You can use the simulator in MPLAB 7.20 with "UART1". The printf
output will be displayed in the Output Window, under the UART1 tab.
This lets you quickly check your printf formatting without having
to download to a prototype board each time. You don't need the
prototype board at all. This thread has instructions on how to do it:
http://www.ccsinfo.com/forum/viewtopic.php?t=23408&highlight=uart1 |
|
|
hansknec
Joined: 30 Sep 2004 Posts: 25 Location: Virginia, USA
|
|
Posted: Tue Aug 02, 2005 12:35 pm |
|
|
It was probably obvious to you veterans, but in case a newbe has my same question I will now provide the answer:
The %E format translates it perfectly. If my ADC counts are 965 and I multiply it by a float of 1e-8 then the printed result will be 9.65e-6. No need to split it apart like PBP required. It will print out to many decimal places unless bounds are inserted as follows:
printf(lcd_putc,"\fValue = %1.2E", adc_count);
The 1.2 designates 1 digit before the decimal point followed by 2 digits after the decimal point.
John |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|