Temperature Controlled Box – Source Code


#include <asf.h>
#include <stdio.h>

static volatile int16_t last_temperature;
#define INPUT_PIN              ADCCH_POS_PIN1
#define OUTPUT_STR_SIZE        40
struct pwm_config mypwm[4];
int32_t destemp;
#define MY_LED0     IOPORT_CREATE_PIN(PORTA, 16)
#define MY_fan                  IOPORT_CREATE_PIN(PORTA,19)
#define MY_LED1     IOPORT_CREATE_PIN(PORTA, 17)
static void adc_handler(ADC_t *adc, uint8_t ch_mask, adc_result_t result)
{
#ifdef CONF_BOARD_OLED_UG_2832HSWEG04
gfx_mono_draw_filled_rect(0,0,128,32,GFX_PIXEL_CLR);
#endif
int32_t temperature;
char out_str[OUTPUT_STR_SIZE];

/* Compute current temperature in Celsius, based on linearization
* of the temperature sensor adc data.
*/
ioport_init();

ioport_set_pin_dir(MY_LED0, IOPORT_DIR_OUTPUT);
ioport_set_pin_dir(MY_LED1, IOPORT_DIR_OUTPUT);
if (result > 697) {
temperature = (int8_t)((-0.0295 * result) + 40.5);
} if (result > 420) {
temperature = (int8_t)((-0.0474 * result) + 53.3);
} else {
temperature = (int8_t)((-0.0777 * result) + 65.1);
}

last_temperature = temperature;

// Write temperature to display
last_temperature = last_temperature * 1.8 + 32;

if (last_temperature-2>destemp)
{

snprintf(out_str, OUTPUT_STR_SIZE, "Temperature:%1d F AC  ", last_temperature);
gpio_set_pin_low(LED0);
gpio_set_pin_high(LED1);
ioport_set_pin_level(MY_LED0, 3.3);
ioport_set_pin_level(MY_fan, 5);
ioport_set_pin_level(MY_LED1, 0);

}
else if (last_temperature+2<destemp)
{

snprintf(out_str, OUTPUT_STR_SIZE, "Temperature:%1d F Heat", last_temperature);
gpio_set_pin_low(LED1);
gpio_set_pin_high(LED0);
ioport_set_pin_level(MY_LED0, 0);
ioport_set_pin_level(MY_fan, 0);
ioport_set_pin_level(MY_LED1, 3.3);
}
else// if(last_temperature==destemp || (last_temperature-2>=destemp && last_temperature+2<=destemp))
{

snprintf(out_str, OUTPUT_STR_SIZE, "Temperature:%1d F off ", last_temperature);
gpio_set_pin_high(LED0);
gpio_set_pin_high(LED1);
ioport_set_pin_level(MY_LED0, 0);
ioport_set_pin_level(MY_fan, 0);
ioport_set_pin_level(MY_LED1, 0);

}
//            snprintf(out_str, OUTPUT_STR_SIZE, "Temperature: %2d F", last_temperature);
gfx_mono_draw_string(out_str, 0, 0, &sysfont);

// Start next conversion.
adc_start_conversion(adc, ch_mask);
}

static void adc_handler2(ADC_t *adc, uint8_t ch_mask, adc_result_t result)
{
#ifdef CONF_BOARD_OLED_UG_2832HSWEG04
gfx_mono_draw_filled_rect(0,0,128,32,GFX_PIXEL_CLR);
#endif
char out_str[OUTPUT_STR_SIZE];
if(result>= 65000){
result = 64;
}
result = (int)(result/22);
if(result < 64){
result = 64;
}
destemp=result;
snprintf(out_str, OUTPUT_STR_SIZE, "Desired Temp: %1d F   ", result);
//result = (int)result/1.2;
//gfx_mono_draw_filled_rect(0,17,result,7,GFX_PIXEL_SET);
//gfx_mono_draw_filled_rect(result,17,128-result,7,GFX_PIXEL_CLR);

gfx_mono_draw_string(out_str, 0, 20, &sysfont);
adc_start_conversion(adc, ch_mask);
}

int main(void)
{
struct adc_config         adc_conf;
struct adc_config adc_conf2;
struct adc_channel_config adcch_conf;
struct adc_channel_config adcch_conf2;

board_init();
sysclk_init();
sleepmgr_init();
irq_initialize_vectors();
cpu_irq_enable();
gfx_mono_init();

// Enable backlight if display type is not OLED
#ifndef CONF_BOARD_OLED_UG_2832HSWEG04
ioport_set_pin_high(LCD_BACKLIGHT_ENABLE_PIN);
#endif

// Initialize configuration structures.
adc_read_configuration(&ADCA, &adc_conf);
adcch_read_configuration(&ADCA, ADC_CH0, &adcch_conf);

adc_read_configuration(&ADCB, &adc_conf2);
adcch_read_configuration(&ADCB, ADC_CH1, &adcch_conf2);

/* Configure the ADC module:
* - unsigned, 12-bit results
* - VCC voltage reference
* - 200 kHz maximum clock rate
* - manual conversion triggering
* - temperature sensor enabled
* - callback function
*/
adc_set_conversion_parameters(&adc_conf, ADC_SIGN_ON, ADC_RES_12,
ADC_REF_VCC);
adc_set_clock_rate(&adc_conf, 200000UL);
adc_set_conversion_trigger(&adc_conf, ADC_TRIG_MANUAL, 1, 0);
adc_enable_internal_input(&adc_conf, ADC_INT_TEMPSENSE);

adc_set_conversion_parameters(&adc_conf2, ADC_SIGN_ON, ADC_RES_12,
ADC_REF_VCC);
adc_set_clock_rate(&adc_conf2, 200000UL);
adc_set_conversion_trigger(&adc_conf2, ADC_TRIG_MANUAL, 1, 0);
adc_enable_internal_input(&adc_conf2, ADC_INT_TEMPSENSE);

adc_write_configuration(&ADCA, &adc_conf);
adc_set_callback(&ADCA, &adc_handler);

adc_write_configuration(&ADCB, &adc_conf2);
adc_set_callback(&ADCB, &adc_handler2);

/* Configure ADC channel 0:
* - single-ended measurement from temperature sensor
* - interrupt flag set on completed conversion
* - interrupts disabled
*/
adcch_set_input(&adcch_conf, ADCCH_POS_PIN1, ADCCH_NEG_NONE,
1);
adcch_set_interrupt_mode(&adcch_conf, ADCCH_MODE_COMPLETE);
adcch_enable_interrupt(&adcch_conf);

adcch_write_configuration(&ADCA, ADC_CH0, &adcch_conf);

// Enable the ADC and start the first conversion.
adc_enable(&ADCA);
adc_start_conversion(&ADCA, ADC_CH0);

adcch_set_input(&adcch_conf2, ADCCH_POS_PIN1, ADCCH_NEG_NONE,
1);
adcch_set_interrupt_mode(&adcch_conf2, ADCCH_MODE_COMPLETE);
adcch_enable_interrupt(&adcch_conf2);

adcch_write_configuration(&ADCB, ADC_CH1, &adcch_conf);

// Enable the ADC and start the first conversion.
adc_enable(&ADCB);
adc_start_conversion(&ADCB, ADC_CH1);

do {
// Sleep until ADC interrupt triggers.
sleepmgr_enter_sleep();
} while (1);
}