Hi,
I would like to use my TM4C129 as a guitar tuner using it ADC port. I also trying to use Systick handler. I have tryed this code, but it doesnt work properly, it doesnt detect inputc frequency well.
Could anyone 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 1363
#define ADCpin1 7
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 = (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];
}