/* ADC - Potentiometer, Photo Sensor to LEDs * * This program sets up ADC0 Sample Sequener 0 to convert * channel 1, channel 2, and channel 8. * Analog channel 1 uses PORTE 2 pin and is connected to * a potentiometer on EduPad board. * Analog Channel 2 uses PORTE 1 pin and is connected to * a photo sensor. * Analog Channel 8 uses PORTE 5 pin and is connected to * a temperature sensor. * * On EduPad board, PB3-PB0 are connected to the four LEDs. * Pushbutton switches are connected to PD1, PD0. * SW5 - PD0 * SW4 - PD1 * * The combinations of the pushbutton switches selects the * conversion result for display on the LEDs. * SW4 SW5 Display * 0 0 potentiometer * 0 1 photo sensor * 1 0 temperature sensor * 1 1 no output * * Built and tested with Keil MDK-ARM v5.24a and TM4C_DFP v1.1.0 */ #include "TM4C123GH6PM.h" void delayMs(int n); void delayUs(int n); int main(void) { int potentiometer; int photosensor; int temperature; SYSCTL->RCGCGPIO |= 0x10; // enable clock to GPIOE SYSCTL->RCGCADC |= 1; // enable clock to ADC0 SYSCTL->RCGCGPIO |= 0x02; // enable clock to GPIOB SYSCTL->RCGCGPIO |= 0x08; // enable clock to GPIOD // initialize port pin PE5, 2, and 1 for ADC0 input GPIOE->AFSEL |= 0x26; // enable alternate function GPIOE->DEN &= ~0x26; // disable digital function GPIOE->AMSEL |= 0x26; // enable analog function // initialize ADC0 Sample Sequencer 0 ADC0->ACTSS &= ~1; // disable ADC0 sequencer 0 during configuration ADC0->EMUX &= ~0x000F; // software trigger for sequencer 0 ADC0->SSMUX0 &= ~0xFFFFFFFF; // clear channel selects first ADC0->SSMUX0 |= 0x00000821; // ch 1 for 1st sample, ch 2 for 2nd, and ch 8 for 3rd ADC0->SSCTL0 |= 0x00000600; // finish sequence at 3rd sample and post RIS bit ADC0->ACTSS |= 1; // enable ADC0 sequencer 0 // enable PORTB 3-0 as output for LEDs GPIOB->DIR |= 0x0F; // set PORTB 3-0 as output pins GPIOB->DEN |= 0x0F; // set PORTB 3-0 as digital pins // enable PORTD 3-0 as input GPIOD->DIR &= ~0x0F; // set pin 3-0 as input GPIOD->AMSEL &= ~0x0F; // disable analog functions GPIOD->DEN |= 0x0F; // set pin 3-0 as digital pins while(1) { ADC0->PSSI |= 1; // start a conversion at sequencer 0 while((ADC0->RIS & 1) == 0) ; // wait for conversion sequence complete potentiometer = ADC0->SSFIFO0; // read conversion result of ch 1 from FIFO photosensor = ADC0->SSFIFO0; // read conversion result of ch 2 from FIFO temperature = ADC0->SSFIFO0; // read conversion result of ch 8 from FIFO ADC0->ISC = 1; // clear completion flag switch(GPIOD->DATA & 3) { case 0: // write the most significant 4 bits of the potentiometer reading to LEDs GPIOB->DATA = potentiometer >> 8; break; case 1: // write the most significant 4 bits of the photosensor reading to LEDs GPIOB->DATA = photosensor >> 8; break; case 2: // write bits 5-2 of the temperature reading to LEDs GPIOB->DATA = temperature >> 2; break; default: GPIOB->DATA = 0; } delayMs(100); } } /* delay n milliseconds (50 MHz CPU clock) */ void delayMs(int n) { int i, j; for(i = 0 ; i< n; i++) for(j = 0; j < 6265; j++) {} /* do nothing for 1 ms */ }