Echelon Neuron C User Manual

Page 44

Advertising
background image

32

Focusing on a Single Device

// Uses a thermistor to measure temperature, and a
// quadrature encoder to enter setpoint. Activates either
// heating or cooling equipment via bit outputs.

///////////////// Include Files //////////////////
#include <stdlib.h>
//

for

muldiv()

////////////////////// Timers /////////////////
stimer repeating tmCheckHeatOrCool;
//

Automatically

repeating

timer


////////////////// Constants ///////////////////////
#define TEMP_DEG_F(t) (((long)t - 32L) * 50 / 9 + 2740)

// macro to convert degrees F to SNVT_temp

const SNVT_temp DESIRED_TEMP_MAX = TEMP_DEG_F(84);
const SNVT_temp DESIRED_TEMP_MIN = TEMP_DEG_F(56);
const SNVT_temp BAND_SIZE = 10;
// Guardband of +/- 1 deg C around desired temperature

//////////////// I/O Objects //////////////////////
IO_6 input ontime clock (1) invert ioTempRaw;
IO_4 input quadrature ioShaftIn;
IO_2 output bit ioHeatingOn = FALSE;
IO_3 output bit ioCoolingOn = FALSE;

//////////////// Global Variables ////////////////////
SNVT_temp newTemp = TEMP_DEG_F(70);// init to 70 deg F
SNVT_temp desiredTemp = TEMP_DEG_F(70);

enum {
OFF, HEATING, COOLING
} equip = OFF; // current state of HVAC equipment

/////////////////// Tasks //////////////////////
// I/O update task --
// read thermistor voltage-to-frequency converter
when (io_update_occurs(ioTempRaw)) {
// An update occurs periodically as the ontime is
// sampled. The new sample is placed in 'input_value.'
// Calculation is performed using 32-bit intermediate
// math, then the result stored as a SNVT_temp. The
// input is scaled based on the temperature coefficient
// of the thermistor.
newTemp = muldiv(input_value, 25000, 9216) + 2562;
}

/////////////////////////////////////////////////////
// I/O update task -- read quadrature encoder
// A quadrature input is used as a dial to select a new
// temperature setting.

when (io_update_occurs(ioShaftIn)) {
// An update occurs for a quadrature I/O object when the
// accumulated offset is nonzero. The value is placed in
// 'input_value' by the io_update_occurs event.
desiredTemp += input_value; // Assumes no overflow
desiredTemp = min(DESIRED_TEMP_MAX, desiredTemp);

Advertising