// Affiche la temperature du capteur interne au MSP430. // le bouton P1.0 permet d'alterner l'affichage en Celsius ou Kelvin // les LEDS P2.0 à P2.3 s'allument en proportion avec l'augmentation de // temperature (1 LED chaque degré d'incrément) //****************************************************************************** #include "msp430xG46x.h" #include "intrinsics.h" #include "LCD.h" #include "stdio.h" #define ADC_DELTA 6 // ~ 1 Deg C delta for LED on #define CELSIUS 0 #define KELVIN 1 unsigned int ADC_start, mode=0; void main(void) { char str[9]; int i; float Temp, Volt; WDTCTL = WDTPW + WDTHOLD; // Stop watchdog ADC12CTL0 = SHT0_8 + REF2_5V + REFON + ADC12ON; // Config ADC12 ADC12CTL1 = SHS_1 + SHP + CONSEQ_2; // TA trig., // Sample/Hold Pulse Mode // Repetitive conversion of 1 channel ADC12MCTL0 = SREF_1 + INCH_10; // Channel A10, Vref+ ADC12IE |= 0x001; // Enable ADC12IFG.0 // fréquence du timer = 32*32768/CCR0 [Hz] TACCR0 = 13600; // Delay for reference start-up TACTL = TACLR + MC_1 + TASSEL_2; // up mode, SMCLK TACTL = TASSEL_2 + MC_2; // SMCLK, cont-mode TACCTL1 = OUTMOD_4; // Toggle on EQU1 (TAR = 0) ADC12CTL0 |= ENC; // Enable ADC12 while (!(ADC12IFG & 0x0001)); // Wait for first conversion? ADC_start = ADC12MEM0; // Read out 1st ADC value P2OUT = 0; // Clear P5 P2DIR |= 0x0F; // P2.x as output P1IE |= 0x01; // enable interrupt P1.0 P1IFG = 0; LCD_init(); __enable_interrupt(); while (1) { Volt = ADC12MEM0 * 2.5 / 4095; Temp = (Volt - 0.986) / 0.00355; if (mode == KELVIN) { Temp = Temp + 273.2; sprintf(str,"T=%5.1fK",Temp); } else sprintf(str,"T=%5.1fC",Temp); LCD_print (str); for (i=1;i<30000;i++); } } //routine d'interruption qui survient lorsque P1.0 a un flanc #pragma vector=PORT1_VECTOR __interrupt void Port1_ISR (void) // PORT1ISR (void) { if (P1IFG == 0x01) mode ^= 1; P1IFG = 0; } #pragma vector = ADC12_VECTOR __interrupt void ADC12_ISR(void) { unsigned int i, adc; adc = ADC12MEM0; i = (adc > ADC_start) * (adc - ADC_start) / ADC_DELTA; if (i < 1) P2OUT = 0x00; if (i >= 1) P2OUT = 0x01; if (i >= 2) P2OUT = 0x03; if (i >= 3) P2OUT = 0x07; if (i >= 4) P2OUT = 0x0F; }