/* * This program sets up ADC0 Sample Sequener 0 to convert * channel 1 and writes the conversion result to the LEDs. * Analog channel 1 uses PORTE 2 pin and is connected to * a potentiometer on Wytec EduBase board. */ #include "TM4C123GH6PM.h" void delayMs(int n); void delayUs(int n); int main(void) { int sample; SYSCTL->RCGCGPIO |= 0x10; // enable clock to GPIOE SYSCTL->RCGCADC |= 1; // enable clock to ADC0 SYSCTL->RCGCGPIO |= 0x02; // enable clock to GPIOB // initialize port pin PE2 for ADC0 input GPIOE->AFSEL |= 0x04; // enable alternate function GPIOE->DEN &= ~0x04; // disable digital function GPIOE->AMSEL |= 0x04; // 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 |= 0x00000001; // set channel 1 for first and only sample ADC0->SSCTL0 |= 0x00000006; // finish sequence at 1st 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 while(1) { ADC0->PSSI |= 1; // start a conversion at sequencer 0 while((ADC0->RIS & 1) == 0) ; // wait for conversion sequence complete sample = ADC0->SSFIFO0; // read conversion result from FIFO ADC0->ISC = 1; // clear completion flag GPIOB->DATA = sample >> 8; // write the most significant 4 bits of the sample to LEDs delayMs(100); } } // delay n milliseconds (16 MHz CPU clock) void delayMs(int n) { int i, j; for(i = 0 ; i < n; i++) for(j = 0; j < 3180; j++) {} // do nothing for 1 ms } // delay n microseconds (16 MHz CPU clock) void delayUs(int n) { int i, j; for(i = 0 ; i < n; i++) for(j = 0; j < 3; j++) {} // do nothing for 1 us } // This function is called by the startup assembly // code to perform system specific initialization tasks. void SystemInit(void) { // Grant coprocessor access // This is required since TM4C123G has // a floating point coprocessor SCB->CPACR |= 0x00f00000; }