Question
Storing ADC values into an array
Posted on February 05, 2014 at 13:41
Hello,
I'm currently getting ADC values and storing them into an array, incrementing the array after storing each value. I want to make the array quite large, but the program crashes if i go above array[500]. During debug the program jumps to a hard fault handler. Am i not allowed to create arrays this big? Here is my code:/**************************************************************************
RCC configuration for ADCs and GPIO
***************************************************************************/
void RCC_Configuration(void)
{
/* Enable peripheral clocks */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_ADC2, ENABLE);
}
/**************************************************************************
GPIO configuration
***************************************************************************/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Configure ADC Channel 12 pin as analog input */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
GPIO_Init(GPIOC, &GPIO_InitStructure);
}
/**************************************************************************
Initialize the ADC, ADCs interface clock, pin
***************************************************************************/
void config_ADC(void)
{
/* Strucutre definitions */
ADC_InitTypeDef ADC_InitStructure;
ADC_CommonInitTypeDef ADC_CommonInitStructure;
RCC_Configuration();
GPIO_Configuration();
/* ADC Common Init */
ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
/* the RCC clock uses HCclk/2 = 168MHz/2 = 84MHz/prescaler */
ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div8;
ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
ADC_CommonInit(&ADC_CommonInitStructure);
/* ADC1 regular channel 12 configuration */
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfConversion = 1;
ADC_Init(ADC1, &ADC_InitStructure);
ADC_Init(ADC2, &ADC_InitStructure);
/* Set up the channel for ADC1 PC2*/
ADC_RegularChannelConfig(ADC1, ADC_Channel_12, 1, ADC_SampleTime_480Cycles);
/* Set up the channel for ADC 2 PC1 */
ADC_RegularChannelConfig(ADC2, ADC_Channel_11, 1, ADC_SampleTime_3Cycles);
/* Enable interrupt */
//ADC1->CR1 |= ADC_CR1_EOCIE;
/* enable the interrupt */
//NVIC_EnableIRQ(ADC_IRQn);
/* Enable ADC1 and ADC2*/
ADC_Cmd(ADC1, ENABLE);
ADC_Cmd(ADC2, ENABLE);
}
/**************************************************************************
Read the ADC 1 value
***************************************************************************/
int read_ADC1(void)
{
float ADC1ConvertedValue = 0;
/* Start ADC3 Software Conversion */
ADC_SoftwareStartConv(ADC1);
/* Wait unti lthe conversion is done */
while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);
/* Get the converted value */
ADC1ConvertedValue = ADC_GetConversionValue(ADC1);
return ADC1ConvertedValue;
}
In main i then call a function called measure_acoustic();
/********************************************************************************
Function to measure acoustics
*********************************************************************************/
#define BUFFERSIZE 900
void measure_acoustic(void) {
int i;
int noConversions = BUFFERSIZE;
uint16_t acoustic_sample[BUFFERSIZE];
for(i = 0; i < noConversions; i++){
acoustic_sample[i] = read_ADC1();
}
printf(''SUCCESS\r\n'');
}
Can anyone help me, or know why i can't use large arrays. Or is there a different way to store a large number of values. Thanks