Lisa Simone, "A Feynman Approach to Debugging," Embedded Systems Programming, December 2004 Appendix D Partial Software Listing unsigned char oven_pulse_width_counter = 100 - 1; unsigned char actual_temp_A2D_counts; unsigned char reference_temp_A2D_counts; unsigned char ovenPW = 0; #define Heater_output_pin portb.2 #define NOMINAL_TEMPERATURE_PW 17 #define GAIN 2.7 #define ON 1 #define OFF 0 /* -------------------------------------------------------------- Name: periodic_timer() Function: This is part of an interrupt service routine. The pwm_routine() is run every 10 msec and the new ovenPW is calculated every 1000 msec. ---------------------------------------------------------- */ void periodic_timer() { /* ------- 10 msec ----------- */ oven_ON_time_control_routine(); ... (removed code) ... /* ------- 1000 msec ----------- */ actual_temp_A2D_counts = read_actual_temperature_A2D(); reference_temp_A2D_counts = read_reference_temperature_A2D(); calculate_new_oven_ON_time(); } /* -------------------------------------------------------------- Name: oven_pulse_width_control_routine() Function: Control the ON duty cycle of the heater. This function is called every 10 msec and the counter is incremented. When the counter is less than the ovenPW value, the oven stays ON. When ovenPW is reached, the oven is turned OFF. The counter rolls over at 100 and starts the duty cycle all over again. ---------------------------------------------------------- */ void oven_ON_time_control_routine(void) { oven_pulse_width_counter++; if (oven_pulse_width_counter == PWM_100_PERCENT) { oven_pulse_width_counter = 0; if (ovenPW != 0) Heater_output_pin = ON; } if (oven_pulse_width_counter >= ovenPW) Heater_output_pin = OFF; } /* -------------------------------------------------------------- Name: calculate_new_oven_ON_time() Function: Calculate a new ovenPW based on the current measured temperatures. This function is called every 1 second. ----------------------------------------------------------- */ void calculate_new_oven_ON_time(void) { signed char delta_temperature; /* -------------------------------------------------------------- Calculate new pulse width based on current temperature. ---------------------------------------------------------- */ delta_temperature = actual_temp_A2D_counts - reference_temp_A2D_counts; ovenPW = NOMINAL_TEMPERATURE_PW + GAIN*delta_temperature; /* -------------------------------------------------------------- Make sure duty cycle math has not exceeded 100 percent. -------------------------------------------------------- */ if (ovenPW > 100) ovenPW = 100; if (ovenPW < 0) ovenPW = 0; }