Jump to content
openanalog

Timer Interrupt ADC

Recommended Posts

Trying to use SysTick to set the sampling frequency of my ADC. Im a bit confused on the interaction of the two though. If my ADC is running at 125KSPS how do I need to set SysTick to 125KHz? 250Khz?? Or how does this work? my code: 

 

#include "Energia.h"
#include "inc/lm4f120h5qr.h"
#include <stdint.h>
#include <stdbool.h>
#include "driverlib/adc.h"
#include "inc/hw_types.h"
#include "driverlib/adc.h"
#include "driverlib/gpio.h"
#include "driverlib/sysctl.h"
#include "inc/hw_ints.h"
#include "driverlib/interrupt.h"
#include "driverlib/sysctl.h"
#include "driverlib/timer.h"
#include "driverlib/cpu.h"
 
 
int result;
int i = 0;
volatile unsigned long Counts=0;
void SysTick_Init(unsigned long period);
void SysTick_Handler(void);
void ADC0_InitSWTriggerSeq3_Ch9(void);
unsigned long ADC0_InSeq3(void);
byte audio_buff[511];
 
void setup(){ // running at 16 MHz
  SysCtlClockSet(SYSCTL_SYSDIV_2_5|SYSCTL_USE_PLL|SYSCTL_XTAL_16MHZ|SYSCTL_OSC_MAIN);
  ADC0_InitSWTriggerSeq3_Ch9();
 
  SysTick_Init(50);        // initialize SysTick timer, f = 1/(1/16000000/50)
  ROM_IntMasterEnable();      // enable after everything initialized
 
}
 
void loop(){
   while(1){                   // interrupts every 1ms, 500 Hz flash
   
  }
}
 
void SysTick_Init(unsigned long period){
  NVIC_ST_CTRL_R = 0;         // disable SysTick during setup
  NVIC_ST_RELOAD_R = period-1;// reload value
  NVIC_ST_CURRENT_R = 0;      // any write to current clears it
  NVIC_SYS_PRI3_R = (NVIC_SYS_PRI3_R&0x00FFFFFF)|0x40000000; // priority 2          
  NVIC_ST_CTRL_R = 0x07; // enable SysTick with core clock and interrupts
  // enable interrupts after all initialization is finished
}
void SysTick_Handler(void){
  if(i==511){ 
    i = 0;
  }
  
  ADC0_PSSI_R = 0x0008;            // 1) initiate SS3
  while((ADC0_RIS_R&0x08)==0){};   // 2) wait for conversion done
  result = ADC0_SSFIFO3_R&0xFFF;   // 3) read result
  audio_buff = result;
  ADC0_ISC_R = 0x0008;             // 4) acknowledge completion
  i++;
}
 
void ADC0_InitSWTriggerSeq3_Ch9(void){ volatile unsigned long delay;
  SYSCTL_RCGC2_R |= 0x00000010;   // 1) activate clock for Port E
  delay = SYSCTL_RCGC2_R;         //    allow time for clock to stabilize
  GPIO_PORTE_DIR_R &= ~0x04;      // 2) make PE4 input
  GPIO_PORTE_AFSEL_R |= 0x04;     // 3) enable alternate function on PE2
  GPIO_PORTE_DEN_R &= ~0x04;      // 4) disable digital I/O on PE2
  GPIO_PORTE_AMSEL_R |= 0x04;     // 5) enable analog function on PE2
  SYSCTL_RCGC0_R |= 0x00010000;   // 6) activate ADC0
  delay = SYSCTL_RCGC2_R;        
  SYSCTL_RCGC0_R &= ~0x00000300;  // 7) configure for 125K
  ADC0_SSPRI_R = 0x0123;          // 8) Sequencer 3 is highest priority
  ADC0_ACTSS_R &= ~0x0008;        // 9) disable sample sequencer 3
  ADC0_EMUX_R &= ~0xF000;         // 10) seq3 is software trigger
  ADC0_SSMUX3_R &= ~0x000F;       // 11) clear SS3 field
  ADC0_SSMUX3_R += 9;             //    set channel Ain9 (PE4)
  ADC0_SSCTL3_R = 0x0006;         // 12) no TS0 D0, yes IE0 END0
  ADC0_ACTSS_R |= 0x0008;         // 13) enable sample sequencer 3
}

Share this post


Link to post
Share on other sites

Please use the code attribute when posting code (the <> icon).

 

Should not do waiting in an interrupt service routine (SysTick_Handler).

Handler should just start the conversion, and have the ADC Interrupt handler deal with reading the result.

 

In direct answer to your question - set the systick interrupt to whatever frequency you want the ADC conversions at (e.g. 125 khz).

However, probably better solution would be to just trigger the ADC on a timer set at whatever frequency you want

(then you don't have to have the overhead of a handler triggering the ADC).

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...