/* ADC - Potentiometer, Photo Sensor, Temperature sensor to LEDs * * This program is an example of performing a sequence of * multiple channel conversion. The sequence starts at result * memory register 4 and ends at result memory register 6. * The assignments of input channels are as follows: * MEM4 - A8 - P4.5 - light sensor * MEM5 - A6 - P4.7 - potentiometer * MEM6 - A10 - P4.3 - temperature sensor * * Once the conversions are started by software, the program * polls for the completion flag of MEM6, the last channel. * When the last channel conversion is completed, the conversion * results are read and displayed on the LEDs. * The pushbutton switches are used to select the channel for display: * SW4 SW5 * 0 0 - light sensor * 1 0 - potentiometer * 0 1 - temperature sensor * 1 1 - no output * * Built and tested with MSP432P401R Rev. C, Keil MDK-ARM v5.24a and MSP432P4xx_DFP v3.1.0 */ #include "msp.h" void display(int data); int main(void) { int data[4] = {0}; int shifts[4] = {8, 8, 0, 0}; P2->DIR |= 0x30; /* P2.5, P2.4 set as output for LEDs */ P3->DIR |= 0x0C; /* P3.3, P3.2 set as output for LEDs */ P6->DIR &= ~2; /* P6.1 set as input */ P4->DIR &= ~0x15; /* P4.4, P4.2, P4.0 input */ ADC14->CTL0 = 0x00000010; /* power on and disabled during configuration */ ADC14->CTL0 |= 0x040A0380; /* S/H pulse mode, sysclk, 32 sample clocks, multiple channel conversion sequence, software trigger */ ADC14->CTL1 = 0x00040020; /* 12-bit resolution, starting from result memory 4 */ ADC14->MCTL[4] |= 8; /* select A8 input; Vref=AVCC */ P4->SEL1 |= 0x20; /* Configure P4.5 for A8 */ P4->SEL0 |= 0x20; ADC14->MCTL[5] |= 6; /* select A6 input; Vref=AVCC */ P4->SEL1 |= 0x80; /* Configure P4.7 for A6 */ P4->SEL0 |= 0x80; ADC14->MCTL[6] |= 10 | 0x80; /* select A10 input; Vref=AVCC; end of sequence */ P4->SEL1 |= 0x08; /* Configure P4.3 for A10 */ P4->SEL0 |= 0x08; ADC14->CTL0 |= 0x00000002; /* enable the converter */ while (1) { int channel = 0; ADC14->CTL0 |= 1; /* start a conversion sequence */ while (!(ADC14->IFGR0 & 0x00000040)); /* wait till last channel in the sequence complete */ data[0] = ADC14->MEM[4]; /* read the conversion results */ data[1] = ADC14->MEM[5]; data[2] = ADC14->MEM[6]; if (P4->IN & 1 << 0) channel |= 1; if (P6->IN & 1 << 1) channel |= 2; display(data[channel] >> shifts[channel]); } } void display(int data) { if (data & 8) /* bit 3 -> LED3 */ P2->OUT |= 0x10; else P2->OUT &= ~0x10; if (data & 4) /* bit 2 -> LED2 */ P2->OUT |= 0x20; else P2->OUT &= ~0x20; if (data & 2) /* bit 1 -> LED1 */ P3->OUT |= 8; else P3->OUT &= ~8; if (data & 1) /* bit 0 -> LED0 */ P3->OUT |= 4; else P3->OUT &= ~4; }