8-bit AVR® Microcontrollers ADC Setup for Internal Temperature Sensor
8-bit AVR® Analog to Digital Converter (ADC) Internal Temp Sensor Example
Objective
This hands-on project helps you through a simple example of reading the on-chip temperature sensor. Reading the temperature sensor can be a rewarding project in itself. It confirms that you have completed the software build and the ADC hardware setup, and were able to program the microcontroller successfully. Finally, the debugger is used to view the temperature using the MPLAB® X Integrated Development Environment (IDE) output window.
The on-chip temperature sensor is coupled to a single-ended ADC8 channel. Selecting the ADC8 channel by writing ADMUX.MUX[3:0] to '1000' enables the temperature sensor. The internal 1.1 V voltage reference must also be selected for the ADC voltage reference source in the temperature sensor measurement. When the temperature sensor is enabled, the ADC converter can be used in a single conversion mode to measure the voltage over the temperature sensor.
The voltage sensitivity is approximately 1 mV/°C and the accuracy of the temperature measurement is ±10°C.
Resources
ATmega328PB Xplained Mini
MPLAB® X IDE
MPLAB® X XC8
Procedure
1. Project Creation
- Open MPLAB X IDE.
- Select File > New > Project.
- Select Microchip Embedded > Standalone Project.
- Select the device Family > 8-bit AVR MCUs (XMega/Mega/Tiny).
- Select Device > ATmega328PB.
- Select Tool>ATmega328PB Xplained Mini-Sn: ATMLxxxxxxxx...
- Select Next.
- Select XC8 compiler.
- Select Next.
- Enter Project Name. We will use Temp_Sensor_Example as the file name.
- Choose a location to save the project on your computer.
- Select Finish.
2. Create a main.c File
This project reads the internal temperature sensor, converts the result to degrees centigrade, then stores the result in ADC Temperature Result.
) A main.c file is need so we will add one to our project.
Right click on Source Files in the Project Explorer window and navigate to New > avr-main.c.
- Rename the File Name to main.
- Select Finish.
Modify main.c by entering copy/pasting the lines in the gray code block below just after the #include <avr/io.h> line.
unsigned int Ftemp;
int main(void)
{
/* Setup ADC to use int 1.1V reference
and select temp sensor channel */
ADMUX = (1<<REFS1) | (1<<REFS0) | (0<<ADLAR) | (1<<MUX3) | (0<<MUX2) | (0<<MUX1) | (0<<MUX0);
/* Set conversion time to
112usec = [(1/(8Mhz / 64)) * (14 ADC clocks per conversion)]
and enable the ADC*/
ADCSRA = (1<<ADPS2) | (1<<ADPS1) | (1<<ADEN);
/* Perform Dummy Conversion to complete ADC init */
ADCSRA |= (1<<ADSC);
/* wait for conversion to complete */
while ((ADCSRA & (1<<ADSC)) != 0);
/* Scan for changes on A/D input pin in an infinite loop */
while(1)
{
/* start a new conversion on channel 8 */
ADCSRA |= (1<<ADSC);
/* wait for conversion to complete */
while ((ADCSRA & (1<<ADSC)) != 0)
;
/* Calculate the temperature in C */
Ctemp = (ADC - 273);
Ftemp = (Ctemp * 1.8) + 32;
}
return 0;
}
3. Build Project and Program the Device
Select the Make and Program Device Main Project button at the top of the MPLAB X IDE GUI.
If the project builds correctly with no errors, we will enter debugging mode to get our temperature.
4. Debugging
Debugging a device is essential for determining how a program may be running.
Select the line number next to the Ftemp = (Ctemp * 1.8) + 32; line to create a breakpoint.
- Select the Debug Project button at the top of the MPLAB X GUI.
- The project will build and the program will be loaded into the Xplained board
- The program will halt when it hits the breakpoint and the breakpoint will turn green and in the Variables window at the bottom of the screen the first variable, Ctemp will have a value. Notice that Ftemp does not yet have a value because the program has not reached the point where the conversion has been made.
If the program is continued by selecting the PLAY button at the top of the MPLAB X GUI, it will continue and the value for Ftemp is now calculated. Notice that the value is in RED. This is because it changed since the last time the program was halted.
5. Disable debugWIRE and Close
It is a good practice to disable the debugWIRE after running in Debug mode. This will allow MPLAB X to program the Xplained board in the future.
Select Debug > Disconnect from Debug Tool from the top menu.
Analysis
Using the ADC is quite easy and the debug feature makes it easy to monitor the results.
Conclusions
This project can become the basis for future ADC-related projects.