This thread has been locked.
If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.
Tool/software: Code Composer Studio
int main(void) { //Set clock to 16 MHz SysCtlClockSet(SYSCTL_SYSDIV_10 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ); // Enable the GPIOE,D and B peripheral SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE); SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD); SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB); // Enable the ADC0 module. // SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC0); // Wait for the GPIOE, B and D module to be ready. while(!SysCtlPeripheralReady(SYSCTL_PERIPH_GPIOE)) { } while(!SysCtlPeripheralReady(SYSCTL_PERIPH_GPIOD)) { } while(!SysCtlPeripheralReady(SYSCTL_PERIPH_GPIOB)) { } // Wait for the ADC0 module to be ready. // while(!SysCtlPeripheralReady(SYSCTL_PERIPH_ADC0)) { } //Configure ADC functions ADCSequenceConfigure(ADC0_BASE,3,ADC_TRIGGER_PROCESSOR,0); ADCSequenceStepConfigure(ADC0_BASE, 3, 0, ADC_CTL_CH0 | ADC_CTL_IE |ADC_CTL_END); ADCSequenceEnable(ADC0_BASE,3); ADCIntClear(ADC0_BASE, 3); ADCClockConfigSet(ADC0_BASE, ADC_CLOCK_SRC_PLL| ADC_CLOCK_RATE_FULL,24); /*void configADC(){ //configure PE# as an analog input SYSCTL_RCGCADC_R |= 1; //TURN ON ADC0 CLOCKING GPIO_PORTE_AFSEL_R |= 0X01; //SETTING ADC INPUT PINS GPIO_PORTE_DEN_R &= ~0X01; GPIO_PORTE_AMSEL_R |= 0X01; //TURN ON ANALOG OPERATION ADC0_CC_R = ADC_CC_CS_SYSPLL; ADC0_ACTSS_R &= ~ADC_ACTSS_ASEN3; ADC0_EMUX_R = ADC_EMUX_EM3_PROCESSOR; ADC0_SSMUX3_R = 3; ADC0_SSCTL3_R = ADC_SSCTL3_END0; ADC0_ACTSS_R |= ADC_ACTSS_ASEN3; }*/ //Configures ADC pins PE1 and PE2 for proper operation PG 267 //PE1 is Speed and PE2 is the Joystick GPIOPinTypeADC(GPIO_PORTE_BASE, GPIO_PIN_3); //Set output pins //GPIOPinTypeGPIOOutput(GPIO_PORTD_BASE, GPIO_PIN_0| GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3); GPIOPinTypeGPIOOutput(GPIO_PORTB_BASE, GPIO_PIN_4|GPIO_PIN_5); GPIOPinTypeGPIOOutput(GPIO_PORTE_BASE, GPIO_PIN_5); //Set input pins GPIOPinTypeGPIOInput(GPIO_PORTE_BASE, GPIO_PIN_1 | GPIO_PIN_2 ); //Write specific values to pins GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_5,GPIO_PIN_5 ); //Enable white/green A GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_4,0); //Reset blue/yellow C GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_2,0); //CCW(1 towards the rail,up)/CW(0 - towards the motor,down) orange/white D //clock signal /* delay = 1000000; while(1){ GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_0, GPIO_PIN_0); //B SysCtlDelay(delay); GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_0, 0); SysCtlDelay(delay); GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_0, GPIO_PIN_0); counter = counter + 1; }*/ //Reading the Speed Potentiometer input while(1) { ADCProcessorTrigger(ADC0_BASE,3); //wait for the conversion to be completed while(!ADCIntStatus(ADC0_BASE,3,false)) { } //clear Interrupt flag ADCIntClear(ADC0_BASE,3); //Read ADC input ADCSequenceDataGet(ADC0_BASE,3,array); //read the analog value for the x direction(array is the data) //ADCSequenceDataGet(ADC0_BASE,2,&array1); //read the analog value for speed speed_volt = ((array[0] * 3.3)/4095); //converting digital value to voltage //x_volt = ((array1 * 3.3)/4095); //converting digital value to voltage if (0.00<speed_volt<1.00) { //if analog input gotten from the potentiometer delay = 2000000; while(1) { GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_0, 0); SysCtlDelay(delay); GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_0, 0); SysCtlDelay(delay); GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_0, GPIO_PIN_0); counter = counter + 1; } }else if(2.00<speed_volt<3.00) { delay = 1000000; // while(counter<25){ //GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_0, GPIO_PIN_0); //SysCtlDelay(delay); GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_0, 0); SysCtlDelay(delay); GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_0, GPIO_PIN_0); SysCtlDelay(delay); GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_0, 0); //counter = counter + 1; //} } } }
Above is my code that is suppose to read analog value from the pin PE3 on the EK-TM4C123GXL launchpad. My biggest question is how does the program when which pin to check for an input from? Additionally the program goes to the else statement instead of the if condition when I set the voltage between 0 to 1V. I would really appreciate some help!
First you will notice that I edited your post. I used the "Insert Code, Attach Files and more..." function to paste your code using the <> option. That makes your code much more readable. I also did some formatting to make the level of parenthesis more clear.
Sadia Ahmed90 said:how does the program when which pin to check for an input from?
Not sure I understand your question. You configure ADC0 sequence 3 to sample ADC channel 0 (PE3) in line 35 of your code. In line 88 you tell the ADC converter to convert the channels configured for sequence 3.
Sadia Ahmed90 said:the program goes to the else statement instead of the if condition when I set the voltage between 0 to 1V
Your "if" statement on line 103 (and the else on line 115) are not doing what you think. You have two comparisons, "<", in that statement. They are processed in order left to right since they are the same operator. The result of a < comparison is a Boolean. It returns true or false, but really it returns a 1 for true and a 0 for false. What your if statement does is compares if zero is less than speed_volt. If true, the answer is interpreted as 1. Next it compares the answer, 1 to 1.00. is 1 less than 1.00. Since it is not, the code jumps down to the "else if" statement. What I think you really want is:
if ((0.00 < speed_volt) && (speed_volt < 1.00))
Thank you so much for your insight! I fixed that issue however, I think the program isn't reading the analog readings from the potentiometer correctly. Is there a driver lib function that I'm missing? By looking the datasheet and example code I have configure everything correctly but I know for sure it's not sampling the input pin. When I tested my program with the hardware (which only consists of the potentiometer) I consistently got 0.29 when I disconnected the potentiometer from the MCU pin my program still displayed a value of 0.29 for speed_volt. This means that the potentiometer changes had no effect on the value of speed_volt. I have no idea why this is the case and any advice would be helpful! Thanks in advance.