Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Trouble using double in Hi-Tech C with PIC16F628A

Status
Not open for further replies.

the_merovingian

Member level 1
Joined
Jul 25, 2009
Messages
32
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Location
UK
Activity points
1,558
Need some help using double data types in Hi-Tech C with a PIC 16F628A.

I have an LM335Z sensor connected to an ADC (0804) being driven by the PIC.

I know the values being read by the PIC from the ADC are correct (i.e. reading a value of 151, which roughly equals 23degC, as LM335Z does 10mV/degK, thus (151 * 1.96) - 273 = 22.96degC, the value of 1.96 comes from full scale of ADC, 5 volts, divided by the 8 bit scale, i.e. 5/255, multiplied by 100 to take into account the 10mV scale).

The output is pushed across an RS-232 link to the PC, so that's how I know the values are correct when I run - the issue is the sprintf / double isn't working as I expect.

The results I'm getting are:
Sample was: 151, temp calculated: -18512
Sample was: 150, temp calculated: -22528

The code is:

Code:
...
unsigned char sample_text[64];
double temp = (1.96 * sample) - 273;
sprintf(sample_text, "Sample was: %d, temp calculated: %d\n\0", sample, temp);
...

Any ideas?

Added after 15 minutes:

Looks like the issue was to do with the last %d in the sprintf - it should have been a %2.2f - but I don't have enough memory left on the device for the float library :(

Error [1360] C:\Program Files\HI-TECH Software\PICC\9.70\sources\float.c; 85. no space for auto/param

I converted the double to an signed char (leaving the %d) - the value comes out roughly right now - but the accuracy is terrible :(
 

You could try scaling up by 100.

Code:
unsigned char ad_result;
unsigned int temperature;

temperature = (unsigned int)(ad_result * 196);    /* 151 * 196 = 29596 */
temperature -= 27300;                             /* 29596 - 27300 = 2296 */
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top