V4 AVR Floating Point
Posted: Tue Jan 26, 2010 11:21 am
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.
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.