Jump to content
43oh

Recommended Posts

Hi everyone!

 

im trying to create a guitar tuner with my tm4c1294 but im having some problems. I also would like to use Systick .

 

The problem is that it not detect well the frecuency, "tiempo" variable doesnt increment as fast as it should. It should detect frequencies between 80 - 330Hz but doesnt detect anything

 

Could any one help me?

 

Thanks

#include <stdint.h>
#include "driverlib\systick.h"
#include "driverlib\systick.c"
#include "driverlib/sysctl.h"
#include "driverlib/adc.h"


unsigned long start_times[10];
unsigned long stop_times[10];
int long Vin;
int datoA;
float frecuencia;
float pendiente;
float tiempo;
int contador;
                                   


#define TickerPeriod 16777215


#define ADCpin1 27


void setup()
{


  SysTickDisable();                      // Deshabilita SysTick durante la configuracion
  SysTickPeriodSet(TickerPeriod);             // Define el periodo del contador. Al llegar a cero genera la intrrupcion
  SysTickIntRegister(&Ticker);   // Se vincula a la ISR de la interrupción causada por el SysTick
  SysTickIntEnable();                    // Se habilita la interrupción del SysTick
  SysTickEnable();                       // Se habilita el SysTick, después de haber sido configurado
  IntMasterEnable();                     // Se habilitan todas las interrupciones


  Serial.begin(9600);


  //ADC configuration
  ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC0); //enables a peripheral                  
  ROM_ADCSequenceConfigure(ADC0_BASE, 3, ADC_TRIGGER_PROCESSOR, 0); // Configures the trigger source and priority of a sample sequence.
  ROM_ADCHardwareOversampleConfigure(ADC0_BASE,0); //Configures the hardware oversampling factor of the ADC.
  ROM_ADCSequenceEnable(ADC0_BASE, 3); //enables a sample sequence.         
  ROM_ADCIntClear(ADC0_BASE, 3); //Clears sample sequence interrupt source.




}


void loop()
{


  frecuencia = (stop_times[1] - start_times[1])*float(tiempo);
if (contador = 20)
{
  Serial.print("\nADC = ");   
  Serial.print(Vin);
  Serial.print("\tTiempo captura = ");   
  Serial.print(stop_times[1] - start_times[1]);
  Serial.print("\tPendiente = ");   
  Serial.print(pendiente);
  Serial.print("\tFrecuencia = ");   
  Serial.print(frecuencia);
  
  contador = 0;
}
}




void Ticker()                          
{
  start_times[1] = micros();


  Vin = analogRead1(ADCpin1);
  pendiente = Vin - datoA;
  
  datoA = Vin;




  if (pendiente > 1 && Vin > 2047)
  {
    tiempo++;
  }
  if (Vin < 2047)
  {
    tiempo = 0;
  }


  if(pendiente < 0 && Vin > 2047)
  {
    tiempo = 0;
  }
  contador++;
  stop_times[1] = micros();
}




uint16_t analogRead1(uint8_t pin) {


  uint16_t value[1];
  uint32_t channel = digitalPinToADCIn(pin);




  ROM_ADCSequenceStepConfigure(ADC0_BASE, 3, 0, channel | ADC_CTL_IE | ADC_CTL_END); // This function configures the ADC for one step of a sample sequence.
  if (channel == NOT_ON_ADC) 
  { //invalid ADC pin
    return 0;
  }
  ROM_ADCProcessorTrigger(ADC0_BASE, 3); // Causes a processor trigger for a sample sequence.
  while(!ROM_ADCIntStatus(ADC0_BASE, 3, false))
  { //Gets the current interrupt status.
  }
  ROM_ADCIntClear(ADC0_BASE, 3); //Clears sample sequence interrupt source.
  ROM_ADCSequenceDataGet(ADC0_BASE, 3, (unsigned long*) value); //Gets the captured data for a sample sequence.
  return value[0];
}
Link to post
Share on other sites

I have changed the TickerPeriod value for 1363 so it work around 88kHz and the way to calculate frequency so the actual loop function looks (i post the whole code):

// MISEA-UC3M 2014-15
// Programa base
// Proyectos Experimentales I
// Proyectos Experimentales II
// Copy Right Universidad Carlos III de Madrid

#include <stdint.h>              // libreria estandar de tipos enteros (utilizados en las librerias siguientes)
#include "driverlib\systick.h"   // libreria estandar para el manejo de SysTick (header)
#include "driverlib\systick.c"   // libreria estandar para el manejo de SysTick (functions)
#include "driverlib/sysctl.h"
#include "driverlib/adc.h"

unsigned long start_times[10];
unsigned long stop_times[10];
int long Vin;
int datoA;
float frecuencia;
float pendiente;
float tiempo;
int contador;
                                   

#define TickerPeriod 1363    
/* PERIODO DEL CONTADOR SYSTICK. LA FRECUENCIA DEL RELOJ ES 120 MHz, Y CUENTA TICKS DEL RELOJ.
Cuando llega a 0 salta la interrupción.
El máximo valor del contador es 2^24-1=16777215 (7.14 Hz)
Para que corra a 88kHz --> TickerPeriod = 1363
*/
#define ADCpin1 7 //nuestra funcion


void setup()
{

  SysTickDisable();                      // Deshabilita SysTick durante la configuracion
  SysTickPeriodSet(TickerPeriod);             // Define el periodo del contador. Al llegar a cero genera la intrrupcion
  SysTickIntRegister(&Ticker);   // Se vincula a la ISR de la interrupción causada por el SysTick
  SysTickIntEnable();                    // Se habilita la interrupción del SysTick
  SysTickEnable();                       // Se habilita el SysTick, después de haber sido configurado
  IntMasterEnable();                     // Se habilitan todas las interrupciones

  Serial.begin(9600);
  // put your setup code here, to run once:

  //ADC configuration
  ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC0); //enables a peripheral                  
  ROM_ADCSequenceConfigure(ADC0_BASE, 3, ADC_TRIGGER_PROCESSOR, 0); // Configures the trigger source and priority of a sample sequence.
  ROM_ADCHardwareOversampleConfigure(ADC0_BASE,0); //Configures the hardware oversampling factor of the ADC.
  ROM_ADCSequenceEnable(ADC0_BASE, 3); //enables a sample sequence.         
  ROM_ADCIntClear(ADC0_BASE, 3); //Clears sample sequence interrupt source.



}

void loop()
{
  // put your main code here, to run repeatedly:
  // print out the results
  
  frecuencia = (120000/TickerPeriod)*tiempo;
if (contador = 10)
{
  Serial.print("\nADC = ");   
  Serial.print(Vin);
  Serial.print("\tTiempo captura = ");   
  Serial.print(stop_times[1] - start_times[1]);
  Serial.print("\tPendiente = ");   
  Serial.print(pendiente);
  Serial.print("\tFrecuencia = ");   
  Serial.print(frecuencia);
  
  contador = 0;
}

}


void Ticker()                          
{
 // start_times[1] = micros();

  Vin = analogRead1(ADCpin1);
  pendiente = Vin - datoA;
  
  datoA = Vin;


  if (pendiente > 1 && Vin > 2000)
  {
    tiempo++;
  }
  if (Vin < 2047)
  {
    tiempo = 0;
  }

  if(pendiente < 0 && Vin > 2000)
  {
    tiempo = 0;
  }
  contador++;
 // stop_times[1] = micros();
}


uint16_t analogRead1(uint8_t pin) {

  uint16_t value[1];
  uint32_t channel = digitalPinToADCIn(pin);


  ROM_ADCSequenceStepConfigure(ADC0_BASE, 3, 0, channel | ADC_CTL_IE | ADC_CTL_END); // This function configures the ADC for one step of a sample sequence.
  if (channel == NOT_ON_ADC) 
  { //invalid ADC pin
    return 0;
  }
  ROM_ADCProcessorTrigger(ADC0_BASE, 3); // Causes a processor trigger for a sample sequence.
  while(!ROM_ADCIntStatus(ADC0_BASE, 3, false))
  { //Gets the current interrupt status.
  }
  ROM_ADCIntClear(ADC0_BASE, 3); //Clears sample sequence interrupt source.
  ROM_ADCSequenceDataGet(ADC0_BASE, 3, (unsigned long*) value); //Gets the captured data for a sample sequence.
  return value[0];
}







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...