0% found this document useful (0 votes)
72 views5 pages

TP3 - FFT en C Con MXUXpresso

This document contains C code for initializing peripherals on a microcontroller including GPIO pins, UART, ADC, DAC, interrupts and timers. It defines constants, variables and data buffers for sampling analog signals at different sample rates, storing the samples in a buffer, and toggling LEDs to indicate the current sample rate. It also contains interrupt handler functions to handle events from the timer, buttons, and ADC conversions and change states accordingly.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views5 pages

TP3 - FFT en C Con MXUXpresso

This document contains C code for initializing peripherals on a microcontroller including GPIO pins, UART, ADC, DAC, interrupts and timers. It defines constants, variables and data buffers for sampling analog signals at different sample rates, storing the samples in a buffer, and toggling LEDs to indicate the current sample rate. It also contains interrupt handler functions to handle events from the timer, buttons, and ADC conversions and change states accordingly.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

#include <DSP/Include/arm_math.

h>
#include <stdio.h>
#include "board.h"
#include "peripherals.h"
#include "pin_mux.h"
#include "clock_config.h"
#include "MK64F12.h"
#include "fsl_debug_console.h"

/* TODO: insert other include files here. */


#include "fsl_gpio.h"
#include "fsl_common.h"
#include "stdbool.h"
#include "stdint.h"
#include <arm_math.h>
#include <arm_const_structs.h>

#include "fsl_device_registers.h"
#include "fsl_uart.h"
#include "fsl_adc16.h"
#include "arm_math.h"

/* TODO: insert other definitions and declarations here. */

//Muestreo de las se�ales ticks ADC


#define _8KHZ_SR 7500
#define _16KHZ_SR 3750
#define _22KHZ_SR 2727
#define _44KHZ_SR 1364
#define _48KHZ_SR 1250

int velMuestreo = 0; // 0=8K 1=16K 2=22K 3=44K 4=48K EQUIALENTE A "ESTADO" EN


JULI
bool transformada = false;

#define BUFFER_SIZE 2048

volatile uint32_t buffer_index = 0;

q15_t buffer[BUFFER_SIZE];
uint16_t DC_OFFSET = 2048;

volatile uint32_t valueAdc = 0;

q15_t input_buffer[BUFFER_SIZE];
q15_t output_buffer[BUFFER_SIZE];
float32_t fft_buffer[BUFFER_SIZE / 2];
uint32_t sample_rate = 8000;
uint16_t buffer_size = BUFFER_SIZE;
//bool fft_enabled = false;

/*
* @brief Application entry point.
*/
int main(void) {

/* Init board hardware. */

BOARD_InitBootPins();
BOARD_InitBootClocks();
BOARD_InitBootPeripherals();

BOARD_InitLEDsPins();
BOARD_InitButtonsPins();

BOARD_InitPeripherals();

#ifndef BOARD_INIT_DEBUG_CONSOLE_PERIPHERAL

BOARD_InitDebugConsole();

#endif

uart_config_t config;
UART_GetDefaultConfig(&config);
config.baudRate_Bps = 115200;
UART_Init(UART0, &config, CLOCK_GetFreq(kCLOCK_CoreSysClk));

while(1)
{
for (int i = 0; i < BUFFER_SIZE; i++)
{
// env�a "Resultado:" seguido del valor de la variable y un salto de
l�nea
char buffer_string[10];
sprintf(buffer_string, "%d\n", buffer[i]);
UART_WriteBlocking(UART0, (uint8_t *)buffer_string,
strlen(buffer_string));
SDK_DelayAtLeastUs(1000, SystemCoreClock);
}
}
return 0 ;
}

//---------------------------------------------------------------------------------
-----------------------------------------------------------

/* PORTA_IRQn interrupt handler */


void GPIOA_IRQHANDLER(void){
/* Get pin flags */
uint32_t pin_flags = GPIO_PortGetInterruptFlags(GPIOA);

/* Place your interrupt code here */

velMuestreo++;
if (velMuestreo == 5)
{
velMuestreo = 0;
}

switch(velMuestreo){
case 0:
GPIO_PortClear(BOARD_LED_RED_GPIO, 1u << BOARD_LED_RED_PIN);
GPIO_PortSet(BOARD_LED_GREEN_GPIO,1u << BOARD_LED_GREEN_PIN);
GPIO_PortSet(BOARD_LED_BLUE_GPIO, 1u << BOARD_LED_BLUE_PIN);
PIT_StopTimer(PIT_PERIPHERAL, PIT_CHANNEL_0);
PIT_SetTimerPeriod(PIT_PERIPHERAL, PIT_CHANNEL_0, _8KHZ_SR);
PIT_StartTimer(PIT_PERIPHERAL, PIT_CHANNEL_0);
break;

case 1:
GPIO_PortSet(BOARD_LED_RED_GPIO, 1u << BOARD_LED_RED_PIN);
GPIO_PortClear(BOARD_LED_GREEN_GPIO,1u << BOARD_LED_GREEN_PIN);
GPIO_PortSet(BOARD_LED_BLUE_GPIO, 1u << BOARD_LED_BLUE_PIN);
PIT_StopTimer(PIT_PERIPHERAL, PIT_CHANNEL_0);
PIT_SetTimerPeriod(PIT_PERIPHERAL, PIT_CHANNEL_0, _16KHZ_SR);
PIT_StartTimer(PIT_PERIPHERAL, PIT_CHANNEL_0);
break;

case 2:
GPIO_PortSet(BOARD_LED_RED_GPIO, 1u << BOARD_LED_RED_PIN);
GPIO_PortSet(BOARD_LED_GREEN_GPIO, 1u << BOARD_LED_GREEN_PIN);
GPIO_PortClear(BOARD_LED_BLUE_GPIO,1u << BOARD_LED_BLUE_PIN);
PIT_StopTimer(PIT_PERIPHERAL, PIT_CHANNEL_0);
PIT_SetTimerPeriod(PIT_PERIPHERAL, PIT_CHANNEL_0, _22KHZ_SR);
PIT_StartTimer(PIT_PERIPHERAL, PIT_CHANNEL_0);
break;

case 3:
GPIO_PortClear(BOARD_LED_RED_GPIO, 1u << BOARD_LED_RED_PIN);
GPIO_PortClear(BOARD_LED_GREEN_GPIO, 1u << BOARD_LED_GREEN_PIN);
GPIO_PortClear(BOARD_LED_BLUE_GPIO,1u << BOARD_LED_BLUE_PIN);
PIT_StopTimer(PIT_PERIPHERAL, PIT_CHANNEL_0);
PIT_SetTimerPeriod(PIT_PERIPHERAL, PIT_CHANNEL_0, _44KHZ_SR);
PIT_StartTimer(PIT_PERIPHERAL, PIT_CHANNEL_0);
break;

case 4:
GPIO_PortClear(BOARD_LED_RED_GPIO, 1u << BOARD_LED_RED_PIN);
GPIO_PortClear(BOARD_LED_GREEN_GPIO, 1u << BOARD_LED_GREEN_PIN);
GPIO_PortSet(BOARD_LED_BLUE_GPIO,1u << BOARD_LED_BLUE_PIN);
PIT_StopTimer(PIT_PERIPHERAL, PIT_CHANNEL_0);
PIT_SetTimerPeriod(PIT_PERIPHERAL, PIT_CHANNEL_0, _48KHZ_SR);
PIT_StartTimer(PIT_PERIPHERAL, PIT_CHANNEL_0);
break;
}

GPIO_PortClearInterruptFlags(GPIOA, pin_flags); /* Clear pin flags */

/* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F


Store immediate overlapping exception return operation might vector to
incorrect interrupt. */
#if defined _CORTEX_M && (_CORTEX_M == 4U)
__DSB();
#endif
}
//---------------------------------------------------------------------------------
-----------------------------------------------------------

/* PORTC_IRQn interrupt handler


*
* Si el filtro esta activado lo desactiva y si esta desactivado lo activa
*
* */
/* PORTC_IRQn interrupt handler */

void GPIOC_IRQHANDLER(void){
uint32_t pin_flags = GPIO_PortGetInterruptFlags(GPIOC); // Get pin flags
// Place your interrupt code here

if(!transformada)
{
transformada = true;
}
else
{
transformada = false;
}

GPIO_PortClearInterruptFlags(GPIOC, pin_flags);

#if defined _CORTEX_M && (_CORTEX_M == 4U)


__DSB();
#endif
}

/* PORTC_IRQn interrupt handler */

/* PORTD_IRQn interrupt handler */


//PULSADOR CONECTADO EN PULL-UP. FUNCIONAMIENTO: CAMBIAR EL TIPO DE FILTRO
void GPIOD_IRQHANDLER(void) {
/* Get pin flags */
uint32_t pin_flags = GPIO_PortGetInterruptFlags(GPIOD);

/* Place your interrupt code here */

/* Clear pin flags */


GPIO_PortClearInterruptFlags(GPIOD, pin_flags);

/* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F


Store immediate overlapping exception return operation might vector to
incorrect interrupt. */
#if defined _CORTEX_M && (_CORTEX_M == 4U)
__DSB();
#endif
}

//---------------------------------------------------------------------------------
-----------------------------------------------------------
void PIT_CHANNEL_0_IRQHANDLER(void) {
uint32_t intStatus;
/* Reading all interrupt flags of status register */
intStatus = PIT_GetStatusFlags(PIT_PERIPHERAL, PIT_CHANNEL_0);
PIT_ClearStatusFlags(PIT_PERIPHERAL, PIT_CHANNEL_0, intStatus);

ADC16_SetChannelConfig(ADC1, 0 , &ADC1_channelsConfig);

/* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F


Store immediate overlapping exception return operation might vector to
incorrect interrupt. */
#if defined _CORTEX_M && (_CORTEX_M == 4U)
__DSB();
#endif
}

//---------------------------------------------------------------------------------
-----------------------------------------------------------

void ADC1_IRQHANDLER(void){ // ADC0_IRQn interrupt handler


uint32_t result_values[2] = {0}; // Array of result values
static uint16_t indiceBuffer = 0; //Equivale a orden JULI

// Get flags for each group


for ( int i=0; i<2; i++){
uint32_t status = ADC16_GetChannelStatusFlags(ADC0_PERIPHERAL, i);
if ( status == kADC16_ChannelConversionDoneFlag){
result_values[i] = ADC16_GetChannelConversionValue(ADC0_PERIPHERAL, i);
}
}

q15_t valueAdc = (q15_t)(result_values[0]-DC_OFFSET);


buffer[indiceBuffer] = valueAdc;

// Place your code here

DAC_SetBufferValue(DAC0_PERIPHERAL, 0, (valueAdc+DC_OFFSET));

indiceBuffer ++;

if(indiceBuffer>=512)
{
indiceBuffer = 0;
}
//adcValue = result_values[0];
//AdcReady = 1;// flag

#if defined _CORTEX_M && (_CORTEX_M == 4U)


__DSB();
#endif
}

You might also like