# Reflow Toaster Oven Vid 5: ADC10 ISR

This (ADC10 interrupt service routine) is one of the most confusing sections. Here, I sample and do most of the math and averaging calculations regarding room temperature and thermal couple temperature.

Code section:

```/* *******************************
*	All samples are 2mV/sample
*	Samples 12 channels
*/
{

if(TC1_cnt < 128){
TC1_sum += adc >> 2;			// sum up TC degrees C
TC1_half_sum += adc >> 1;	// sum up TC 1/2 degrees C
room_temp_sum += (adc);		// sum up room temp from MCP9700A
TC1_cnt++;
}else{
/* room temp is 10mV/degree C so 5samples/degree C */
room_temp_avg = ((room_temp_sum >> 7) - 250); // subtract out 500mV offset and divide by 128
room_temp_avg += room_temp_avg; // multiply room temp by 2..this isn't necessary but it changes the division

/* simple divide by 128 resulting in the average temps */
avg_temp = (TC1_sum >> 7);		// average thermalcouple temps
avg_half_temp = TC1_sum >> 7;

/* split room temp in half- only sent to GUI, GUI divides
* these two values by 10 after recombining them into a
* single word.
*/
room_temp1 = ((room_temp_avg >> 8) & 0xff00); // msb
room_temp2 = (room_temp_avg & 0x00ff);

// reset counters and flags
TC1_cnt = 0;
TC1_sum = 0;
room_temp_sum = 0;
offset_temp_flag = 1; // Start for loop that divides roomtemp_avg by 10
}

ADC10SA = (short)&adc;	// reset DTC address to start over at beginning of array (type cast so the compiler knows you want to store the pointer as an integer)
}

```

In the if statement I sum up 128 values of room temp samples and the thermal couple samples. I also sum up the thermal couple temp in 1/2 degrees C. The right shifts are done because I need to divide the thermal couple sampled value by 4. This is because each sample is 2mV/sample and the output of the thermal couple is 8mV/degrees C. To divide by 2 or powers of 2 in binary you just have to left shift. This is the reason I designed the analog side to output a value of 8mV/C for the thermal couple. This way I get an average in degrees C and 1/2 degrees C rather than samples. This makes the rest of the code much simpler.

After I get 128 samples I execute the else section. The thermal couple average temp is produce by dividing by 128 to give us an average temp over 128 samples. The same is true for the 1/2 degrees C value. The room temp value is a little more difficult though.

For room temp, I divide by 128 like I did with the other two, but this value is still in samples of 5 samples/degrees C. This is because, per the Microchip datasheet, the MCP9700A outputs 1 degree C /10mV. The datasheet also states that at 0 degrees C the MCP9700A puts out 500mV. Since we want to reference everything (temperature wise) to 0 degrees C (effectively normalize to 0 C) I subtract off the 500mV. *Remember that this is 500mV because each sample is 2mV so 2mV/sample * 250 samples = 500 mV.

There is still a problem though. The room temp is in samples (5 samples/C) which is not divisible by right shifting. I solved this two ways as I’ll explain now and later. The first way was to divide by 5. I had already started working on dividing by 10 so i just added room temp to itself (multiplying by two) to get it in 10 samples/degrees C. I then ran it through a routine up top that I will go over later to do my embedded division. The second way was to get the room temp in 10 samples/C and send this value to my python GUI and let the GUI divide by 10. The roomtemp1 and roomtemp2 are sent over to the GUI, but the embedded system runs off of the value produced by the embedded division routine. I mainly did this to show two good ways of solving a common problem like this.

Once these calculations are done, I reset my counters and flags. After that I reset our starting addres for the DTC controller (built in controller that moves our ADC samples into memory for us so the CPU doesn’t have to), clear the flag and start another sequence of samples.

Data Package:
Note-I haven’t been able to get the python app packed up as an exe yet because of some technical problems, but when I get this figured out I’ll update the package. The source code (python GUI and embedded C) is packed up as the aptana project and CCS studion project respectively. It should be simple to import these and check out the code. If importing doesn’t work just check out the source files or create your own project and add the source files only to it.