All projects
02Live
Internal Temp → Color
Read the chip's own temperature, shift the LED color with heat.
ADC
01
What you learn
Converting an analog value to a number with the ADC and mapping raw data to a physical unit (Celsius). Warm the chip and watch the color shift.
02
Parts you need
03
Code
main.c
1#include <stdint.h>2#include "inc/hw_memmap.h"3#include "driverlib/sysctl.h"4#include "driverlib/gpio.h"5#include "driverlib/adc.h"6 7#define LED_ALL (GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3)8#define PWM_STEPS 2569 10static void showColor(uint8_t r, uint8_t g, uint8_t b, uint32_t periods) {11 volatile uint32_t p, i;12 for (p = 0; p < periods; p++)13 for (i = 0; i < PWM_STEPS; i++) {14 uint8_t out = 0;15 if (i < r) out |= GPIO_PIN_1;16 if (i < g) out |= GPIO_PIN_3;17 if (i < b) out |= GPIO_PIN_2;18 GPIOPinWrite(GPIO_PORTF_BASE, LED_ALL, out);19 SysCtlDelay(2);20 }21}22 23// Internal temperature sensor -> Celsius x10 (e.g. 253 = 25.3 C)24static int32_t readTempC_x10(void) {25 uint32_t buf[1];26 ADCProcessorTrigger(ADC0_BASE, 1);27 while (!ADCIntStatus(ADC0_BASE, 1, false)) {}28 ADCIntClear(ADC0_BASE, 1);29 ADCSequenceDataGet(ADC0_BASE, 1, buf);30 return 1475 - (2475 * (int32_t)buf[0]) / 4096; // datasheet formula31}32 33int main(void) {34 SysCtlClockSet(SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL |35 SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ);36 37 SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);38 while (!SysCtlPeripheralReady(SYSCTL_PERIPH_GPIOF)) {}39 GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, LED_ALL);40 41 SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC0);42 while (!SysCtlPeripheralReady(SYSCTL_PERIPH_ADC0)) {}43 ADCSequenceConfigure(ADC0_BASE, 1, ADC_TRIGGER_PROCESSOR, 0);44 ADCSequenceStepConfigure(ADC0_BASE, 1, 0,45 ADC_CTL_TS | ADC_CTL_IE | ADC_CTL_END); // TS = internal sensor46 ADCSequenceEnable(ADC0_BASE, 1);47 ADCIntClear(ADC0_BASE, 1);48 49 while (1) {50 int32_t t = readTempC_x10();51 if (t < 200) t = 200; // 20 C -> blue52 if (t > 400) t = 400; // 40 C -> red53 int32_t lv = ((t - 200) * 255) / 200; // 0..25554 if (lv < 128) showColor(0, lv * 2, 255 - lv * 2, 40);55 else showColor((lv - 128) * 2, 255 - (lv - 128) * 2, 0, 40);56 }57}04
Output
The simulation below runs in your browser — it mimics the real board.
- temp
- 25.0 C
- ADC
- 2027
- RGB
- 0,128,127