Hi everyone!
I want to make a simple program to understand the basic use of the ADC module of my Tiva TM4C123G launchpad. The idea is to configure a Timer to interrupt in a certain period of time, and once in the interrupt tell the processor to trigger a concversion. I already made the code, but there must be something wrong in the ADC config. that don´t allow me to make more than one conversion.... Here is the code:
#include <stdint.h>
#include <stdbool.h>
#include "inc/tm4c123gh6pm.h"
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "driverlib/sysctl.h"
#include "driverlib/interrupt.h"
#include "driverlib/gpio.h"
#include "driverlib/timer.h"
#include "driverlib/adc.h"
uint32_t muestra;
uint32_t i;
uint32_t datos[10];
void main(void) {
int32_t ui32Period;
i = 0;
// Clock: 40MHz
SysCtlClockSet(SYSCTL_SYSDIV_5|SYSCTL_USE_PLL|SYSCTL_XTAL_16MHZ|SYSCTL_OSC_MAIN);
//control LEDs:
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3);
// ********** Timer Config ********** //
SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0);
TimerConfigure(TIMER0_BASE, TIMER_CFG_PERIODIC);
ui32Period = SysCtlClockGet()/2; // Interrupts evrey half second
TimerLoadSet(TIMER0_BASE, TIMER_A, ui32Period -1);
// ********** ADC Config ********** //
SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC0);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE);
GPIOPinTypeADC(GPIO_PORTE_BASE, GPIO_PIN_3);
// Select sequencer 3 (FIFO depth 1)
ADCSequenceDisable(ADC0_BASE, 3);
ADCSequenceConfigure(ADC0_BASE, 3, ADC_TRIGGER_PROCESSOR, 0);
//Only one step config:
ADCSequenceStepConfigure(ADC0_BASE, 3, 0, ADC_CTL_CH0 | ADC_CTL_IE | ADC_CTL_END);
ADCSequenceEnable(ADC0_BASE, 3);
//ADCIntClear(ADC0_BASE, 3);
//Interrupts enable
IntEnable(INT_TIMER0A);
TimerIntEnable(TIMER0_BASE, TIMER_TIMA_TIMEOUT);
IntMasterEnable();
ADCIntEnable(ADC0_BASE, 3);
IntEnable(INT_ADC0SS3); //habilitamos la interrupcion del secuenciador 3
TimerEnable(TIMER0_BASE, TIMER_A);
while(1){}
}
//Timer IRS:
void InterrupcionTimer0(void){
// Clear Flags
TimerIntClear(TIMER0_BASE, TIMER_TIMA_TIMEOUT);
ADCIntClear(ADC0_BASE, 3);
//Trigger the conversion (by the processor):
ADCProcessorTrigger(ADC0_BASE, 3);
//Wait for the conversion
while(!ADCIntStatus(ADC0_BASE, 3, false)){}
ADCSequenceDataGet(ADC0_BASE, 3, &muestra);
if (i<10)
datos[i] = muestra;
i = i + 1;
if(GPIOPinRead(GPIO_PORTF_BASE, GPIO_PIN_2)){
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3, 0);
}
else
{
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, 4);
}
SysCtlDelay(SysCtlClockGet() / 12);
}