Other Parts Discussed in Thread: TM4C123GH6PM
Hey there.
Firstly just some info on what I am using:
IDE: Keil uVision5
Microcontroller - TM4C123GH6PM
Now onto the problem:
I am trying to get the hang of sampling from 4 ADC channels in a sequence, each of which is connected to the output voltage pin of its own infrared sensor. In order to check that everything works correctly, I use an if statement to turn on the LED connected to port F on the TIVA board if the third sample exceeds a certain trigger voltage. However, the LED turns on whenever I put my hand in front of any of the sensors, not just the one that is supposed to be sampled first.
A note before I give my source code. I am struggling to understand the ADCSequenceDataGet function in the TIVAWARE API. Should the address I put into the third argument of this function be pointing to an array if I want to gather multiple samples in a sequence? And if so, does the ADCSequenceDataGet function just take care of the rest by filling in the elements with the samples? Just looking at it in the adc.c file didn't really help me answer this question as I just saw a lot of stuff I didn't fully understand.
uint32_t resultBuffer[4] = {0};
uint32_t *resultBufferAdd;
int main(void)
{
//assign pointer used by ADCSequenceDataGet function to resultBuffer array
resultBufferAdd = &resultBuffer[0];
//ENABLE GPIO AND ADC PERIPHERALS
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE);
SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC0);
//Configure port F as output for use of multicolour LED on Tiva Board
GPIOPinTypeGPIOOutput(0x40025000, GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3);
//Configure ADC0 Sampling Sequencer 1 for 4 samples with channel Ain3 being the final channel to sample in the sequence
ADCSequenceConfigure(0x40038000, 1, ADC_TRIGGER_PROCESSOR, 0);
ADCSequenceStepConfigure(0x40038000, 1, 0, ADC_CTL_CH0);
ADCSequenceStepConfigure(0x40038000, 1, 1, ADC_CTL_CH1);
ADCSequenceStepConfigure(0x40038000, 1, 2, ADC_CTL_CH2);
ADCSequenceStepConfigure(0x40038000, 1, 3, ADC_CTL_IE | ADC_CTL_END | ADC_CTL_CH3);
ADCSequenceEnable(0x40038000, 1);
while(1)
{
ADCProcessorTrigger(0x40038000, 1); //Begin sampling
while(!ADCIntStatus(0x40038000, 1, false)){} //wait for sample sequence to finish
ADCSequenceDataGet(0x40038000, 1, resultBufferAdd); //Get samples and place in resultBuffer array
if(resultBuffer[2]>1000){GPIO_PORTF_DATA_R=0x04;} //if the 3rd element is over 1000, turn LED on
else{GPIO_PORTF_DATA_R=0x00;}//if the third element is not over 1000, turn LED off
}
}
Thank you for taking the time to read.