Christian Taedcke / Mbed 2 deprecated lpc1549_temperature

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 DigitalOut myled(LED1);
00004 Serial pc(USBTX, USBRX); // tx, rx
00005 AnalogIn ain(A0); //is ADC0_0: must be channel 0 of ADC0 or ADC1
00006 
00007 #define ADCn_INSEL_TEMPERATURE (0x3)
00008 #define SYSCON_PDRUNCFG_TS_PD (1 << 18)
00009 #define SYSCON_PDRUNCFG_IREF_PD (1 << 17)
00010 
00011 int main() {
00012     pc.printf("Starting program...\r\n");
00013     
00014     //Enable temperature sensor and internal voltage reference, see um chapter 31
00015     LPC_SYSCON->PDRUNCFG &= ~(SYSCON_PDRUNCFG_TS_PD | SYSCON_PDRUNCFG_IREF_PD);
00016     
00017     //Select source for channel 0: temperature sensor
00018     LPC_ADC0->INSEL = ADCn_INSEL_TEMPERATURE;
00019     
00020     while(1) {
00021         unsigned short raw = ain.read_u16();
00022         pc.printf("Raw u16 value %u\r\n", raw);
00023         pc.printf("Raw 12 Bit value %u\r\n", (raw >> 4));
00024         float value = ((float)raw) / (float) 0xffff;
00025         pc.printf("Float value %f\r\n", value);
00026         pc.printf("Read value %f mV\r\n", value * 3300.0f);
00027         float temperature = -(((value * 3300.0f) - 577.3f) / 2.29f); 
00028         pc.printf("Read temperature (about) %f deg C\r\n", temperature);
00029         myled = !myled;
00030         wait(1);
00031     }
00032 }