V4 AVR Floating Point

Tips, Tricks and methods for programming, learn ways of making your programming life easier, and share your knowledge with others.

Moderators: Benj, Mods

Post Reply

Please rate this article out of 5: (1 being the worst and 5 being the best)

1
0
No votes
2
0
No votes
3
0
No votes
4
0
No votes
5
1
100%
 
Total votes: 1

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

V4 AVR Floating Point

Post by Benj »

Flowcode V4 for AVR is now ready for release. In most respects it is identical to Flowcode V4 for PIC, offering similar functionality, but targeted at an alternative family of devices. The similarities allow many Flowcode programs to be imported from one version of Flowcode to another (including Flowcode for ARMs).

One of the main differences between the PIC and AVR versions of Flowcode is the way in which each version handles floating-point variables and calculations. Flowcode for AVR provides a comprehensive range of mathematical and trigonometric functions in addition to the standard basic operations. These allow complex calculations to be written directly into Flowcode programs.

The example programs attached to this article show how floating-point calculations can be used to directly convert readings from a negative temperature coefficient (ntc) thermistor into printable temperature values.

The calculation uses the Steinhart-Hart equation to convert the thermistor's resistance value to the corresponding temperature.

The calculation requires three coefficients supplied by the thermistor manufacturer. The temperature, T, is found from the thermistor resistance, R, using the following formula:

1/T = K0 + K1 * ln(R) + K2 * (ln(R))^3

The coefficients for the Matrix Multimedia HSTMP temperature probe used for the programs are:

K0 = 1.02119e-3
K1 = 2.22468e-4
K2 = 1.33342e-7

When the EB-003 Sensor Board is used to connect the temperature probe to the circuit, one terminal of the thermistor is connected to 0V, and forms a potential divider circuit with a fixed 15kΞ© resistor connected to the 5V supply. The voltage read from the connection point of the thermistor and resistor is used to calculate the thermistor's resistance, and hence the measured temperature.

The voltage is measured using Flowcode V4's new ADC ReadAsVoltage macro. This returns a floating-point value for the voltage being read by the selected ADC channel. The voltage is used to calculate the thermistor's resistance, based on the known value of the fixed resistor and the supply voltage of 5V.

The thermistor's resistance value is used in a single line Steinhart-Hart calculation to determine the temperature being measured.

Floating-point values can not be displayed directly using the Flowcode LCD component. Two versions of the program attached to this article handle the display of the calculated temperature value slightly differently:

ThermoCalcDisplay1 uses the Flowcode V4 string function FloatToString$() to convert the floating-point value into a fixed format printable string, which is displayed using the LCD PrintString macro.

ThermoCalcDisplay2 uses the C standard library function sprintf(), in a C code block, to convert the floating-point value into a defined format printable string. Support for this function requires the selection of an alternative compiler batch file which includes the necessary C support code during compilation.

avrafp.bat has been included in the Flowcode V4 AVR installation as an alternative to the default avra.bat file. These files can be selected in the Compiler: Location section of the Chip -> Compiler Options menu.

The advantages of the second approach are:
Β· There are a number of different formats and precision levels that can be selected using standard C format instructions.
Β· Very large and very small values can be displayed using scientific notation.

The disadvantages of the second approach are:
Β· The use of a C code block means that the resulting program will not simulate correctly.
Β· The inclusion of the code required to handle the sprintf() function adds nearly 6K bytes to the size of the program. This limits its use to devices containing 8K, or more, of program (Flash) memory.
Attachments
ThermoCalcDisplay2.fcf_avr
(7 KiB) Downloaded 870 times
ThermoCalcDisplay1.fcf_avr
(7.5 KiB) Downloaded 741 times

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Stainless Steel Temperature Probe HSTMP - Look Up Table Appr

Post by Benj »

Here is a version of the temperature sensor file for the HSTMP sensor using a look up table approach rather then a calculation on the fly as shown previously.

This allows the microcontroller to collect the temperature with significantly less processing time by hard coding the values for each possible ADC reading into an array and then simply using the ADC read from the sensor as the index for the array.

This example is designed for PIC but should work well if imported into any other version of Flowcode. The look up table data is available via the supplementary code window. View -> Project Options -> Supplementary code.
ThermoCalcDisplay1.fcf
(7.5 KiB) Downloaded 829 times
Because the look up table is using C code you will not be able to simulate the program and targets other then 8-bit PICs may need to change the C code for the array slightly to match the expected syntax for their compiler. The data in the array itself will remain the same.

The Excel file is also provided to show how the look up table data was generated and uses a 15K pull up resistor as used on the EB003 and HP488 E-blocks.
Temp20Kntc_5VPU.xls
(110.5 KiB) Downloaded 704 times

soccermad
Posts: 19
Joined: Wed Feb 09, 2011 12:56 am
Has thanked: 2 times
Contact:

Re: Stainless Steel Temperature Probe HSTMP - Look Up Table

Post by soccermad »

Benj wrote:Here is a version of the temperature sensor file for the HSTMP sensor using a look up table approach rather then a calculation on the fly as shown previously.

This allows the microcontroller to collect the temperature with significantly less processing time by hard coding the values for each possible ADC reading into an array and then simply using the ADC read from the sensor as the index for the array.

This example is designed for PIC but should work well if imported into any other version of Flowcode. The look up table data is available via the supplementary code window. View -> Project Options -> Supplementary code.
The attachment ThermoCalcDisplay1.fcf is no longer available
Because the look up table is using C code you will not be able to simulate the program and targets other then 8-bit PICs may need to change the C code for the array slightly to match the expected syntax for their compiler. The data in the array itself will remain the same.

The Excel file is also provided to show how the look up table data was generated and uses a 15K pull up resistor as used on the EB003 and HP488 E-blocks.
The attachment Temp20Kntc_5VPU.xls is no longer available
I looked at the attached file and tried to make it comparable with my temperature because i am using the thermometer instead of the ADC.
Please is it possible for you to have a look into my file and see if you can edit it so the temperature value would be shown onto the LCD has the converted value (degree). Because I shall be attaching a vernier temperature probe to the EB003 board and set to an analogue setting.

Please any help or feedback will be appricated.
My temperature file is attached.
Attachments
REMOTE_temp(2).fcf_avr
(4.5 KiB) Downloaded 518 times

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Stainless Steel Temperature Probe HSTMP - Look Up Table

Post by medelec35 »

soccermad wrote:
I looked at the attached file and tried to make it comparable with my temperature because i am using the thermometer instead of the ADC.
Please is it possible for you to have a look into my file and see if you can edit it so the temperature value would be shown onto the LCD has the converted value (degree). Because I shall be attaching a vernier temperature probe to the EB003 board and set to an analogue setting.

Please any help or feedback will be appricated.
My temperature file is attached.
Hiya soccermad,
I believe the old thermometer is the ADC but it's just sampling port as INT value (0 to 1023) and has been altered to look like a thermometer. It will not read a sensor that is connected to and directly convert to a temperature because there are so many different types of sensors on the market.

For you to have a readout directly in deg c, the only ways are to use look up table method.
Or
Only if sensor has linear characteristic's to use a formula method.

For the look-up table method you can use excel (or download open office) and create a LUT spread sheet, that has data that you can directly paste on to Flowchart.
There are a few samples of these on this forum including the excel sheet Ben posted just above!

If you feel that is too much then you have three alternatives I can think of.
1) Stick with just switching on a port when sensor is reading above a fixed temperature:
In your case you need to find out the voltage on the ADC when the probe is at 40 deg C. Then Convert this voltage to a Integer.
E.g if at 40 deg, voltage on the pin being sampled is 2.5V then INT (TEMP value) = (1023/5) * 2.5 = 204 *2.5 = 510
(Note in pic maths you must round down. So 204.6 is rounded down to 204)
So you change your flowchart to:

Code: Select all

 Setpoint = 510
Alternately If you can get a 40 deg source, then put probe in the 40 deg. The LCD will show the int reading at 40 deg, so you can change your setpoint to do that.

Or

2) The very simplest way to get a direct temperature reading is change your sensor to this one:
http://uk.farnell.com/microchip/mcp9701 ... tt=1439484
I can then create a flowchart for you to give a direct temperature read out.

3) Post data sheets of the sensor you are using,and details of series resistor (perhaps you can post a circuit diagram) and I can help you with creating or altering an existing Flowchart to incorporate Temperature read out.

Martin
Martin

soccermad
Posts: 19
Joined: Wed Feb 09, 2011 12:56 am
Has thanked: 2 times
Contact:

Re: V4 AVR Floating Point

Post by soccermad »

i can not find the data sheet for the "Wide range stainless steel temperature probe" which was optained from the matrix multimedia website they do ot provide a data sheet with that product. it will be connected to a sensor board (EB003) via analouge.

i did try to look through the table that ben uploaded but to be honest i do not understand anything on it at all

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: V4 AVR Floating Point

Post by Benj »

Hello,

All our sensors are manufactured by Vernier, were simply a re-seller for them.

http://www.vernier.com/probes/tmp-bta.html

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: V4 AVR Floating Point

Post by medelec35 »

Ben,

Do you have a spread sheet that is in a similar format to Temp20Kntc_5VPU.xls , but uses Beta instead of Steinhart & Hart coefficients
please?
I have a thermistor and resistor in series, but the thermistor is connected between +VDD and resistor.
It is the other end of resistor that's connected to GND.

I am trying to work it out myself but struggling a bit.
Using
1/((1/298)+LN(resistance/R@25C)/Beta)-273.15

For calculating temperature.
so I have to convert bytes from resistance which is determined from ratio of r1/r1+r2 etc.
If you already have such a spreadsheet, I would really appreciate it.

Thanks.

Martin
Martin

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: V4 AVR Floating Point

Post by medelec35 »

Ben,
I have adapted sheet so loadR and sensor have been swapped.
Its not quiet right yet since at 25C the tot resistance is as though there is not a load resistor connected.
Would you or some one more skilled than me at maths be able to let me know where I am going wrong please.
I know the problem is with Resistance (column E)
It's not a case of just inverting bytes.

Thanks

Martin

Edit a simple test shows it does work ok. If both load resistor and thermistor values are set to the same value, then at 25C the total series resistance should be half.
Therefore Voltage..hence bytes should also be half. It is so all is well :)

What I stated at the beginning of this post was as it should be.
Attachments
temp10K load R swaped.xls
(110 KiB) Downloaded 528 times
Martin

Post Reply