the_merovingian
Member level 1
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:
The code is:
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
I converted the double to an signed char (leaving the %d) - the value comes out roughly right now - but the accuracy is terrible
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