# TM4C123 Temperature sensor

## Temperature sensor

There is an internal temperature sensor embedded into the TI ARM Tiva chip. This gives us the temperature inside the ARM chip itself. The temperature sensor does not have any separate enable control, because it also contains the band-gap reference and must always be enabled. The reference is also supplied to other analog modules, not just the ADC. In addition, the temperature sensor has a second power-down input in the 3.3V domain which provides control by the Hibernation module.

The internal temperature sensor converts a temperature measurement into a voltage. This voltage value, VTSENS, is given by the following equation (where TEMP is the temperature in °C):

```VTSENS = 2.7 - ((TEMP + 55)/75)
```

The temperature sensor reading can be sampled in a sample sequencer by setting the TSn bit in the ADCSSCTLn register. The temperature reading from the temperature sensor can also be given as a function of the ADC value. The following formula calculates temperature (TEMP in °C) based on the ADC reading that is an ADC_output given as an unsigned decimal number from 0 to 4095 and the maximum ADC voltage range (Vref(+) Vref(–)):

```Temp = 147.5 – ((75 × Vref(+) – Vref(–)) × ADC_output)) / 4096
```

Since the Vref = 3.3 V in TI Tiva Launchpad, we have:

```Temp = 147.5 – ((75 × 3.3V) × ADC_output) / 4096
Temp = 147.5 – (247.5 × ADC_output) / 4096
```

Example: Find the temperature inside the ARM chip for the TI Tiva Launchpad if the reading of ADC output is = 0x7D0.
Solution: The 0x7D0 is 2000 in decimal. Now, we have

```Temp = 147.5 – (247.5x2000) / 4096 = 147.5 – 120.85 = 26.6 Celsius
```

### Source Code

1. `/* Convert on-chip temperature`
2. ` *`
3. ` * Runs on TM4C123 LaunchPad`
4. ` *`
5. ` * This program converts the on-chip temperature sensor output using`
6. ` * sample sequencer 3 and timer trigger at 1 Hz.`
7. ` *`
8. `*/`
9. ` `
10. `#include <stdint.h>`
11. `#include "inc/tm4c123gh6pm.h"`
12. ` `
13. `int main(void)`
14. `{`
15. `    volatile int temperature;`
16. ` `
17. `    /* enable clocks */`
18. `    SYSCTL_RCGCADC_R |= 1;       /* enable clock to ADC0 */`
19. `    SYSCTL_RCGCWTIMER_R |= 1;    /* enable clock to WTimer Block 0 */`
20. ` `
21. `    /* initialize ADC0 */`
22. `    ADC0_ACTSS_R &= ~8;          /* disable SS3 during configuration */`
23. `    ADC0_EMUX_R &= ~0xF000;`
24. `    ADC0_EMUX_R |= 0x5000;       /* timer trigger conversion seq 0 */`
25. `    ADC0_SSMUX3_R = 0;           /* get input from channel 0 */`
26. `    ADC0_SSCTL3_R |= 0x0E;       /* take chip temperature, set flag at 1st sample */`
27. `    ADC0_ACTSS_R |= 8;           /* enable ADC0 sequencer 3 */`
28. ` `
29. `    /* initialize wtimer 0 to trigger ADC at 1 sample/sec */`
30. `    WTIMER0_CTL_R = 0;           /* disable WTimer before initialization */`
31. `    WTIMER0_CFG_R = 0x04;        /* 32-bit option */`
32. `    WTIMER0_TAMR_R = 0x02;       /* periodic mode and down-counter */`
33. `    WTIMER0_TAILR_R = 16000000;  /* WTimer A interval load value reg (1 s) */`
34. `    WTIMER0_CTL_R |= 0x20;       /* timer triggers ADC */`
35. `    WTIMER0_CTL_R |= 0x01;       /* enable WTimer A after initialization */`
36. ` `
37. `    while(1) {`
38. `        while((ADC0_RIS_R & 8) == 0)`
39. `            ;                    /* wait for conversion complete */`
40. `        temperature = 147 - (247 * ADC0_SSFIFO3_R) / 4096;`
41. `        ADC0_ISC_R = 8;          /* clear completion flag */`
42. `    }`
43. `}`