2021-08-24 11:08 AM
Hi everyone
Thanks for read my topic
I tried to record the sound, I managed to record the sound at a sample rate of 8 kHz.
this is my code it works great. But I need more sample rate like 11kHz or 16 kHz, I do not know how I can increase my sample rate to this rate.i use STM32F103
I got this code in the source
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_ADC1_Init(void);
static void MX_TIM3_Init(void);
static void MX_USART1_UART_Init(void);
/* USER CODE END 0 */
#define SAMP 8000
int main(void)
{
int k;
uint8_t buf[40];
uint16_t dat[SAMP]; // store ADC value 'SAMP'variable is 8000 1 sec for store audio
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_ADC1_Init();
MX_TIM3_Init();
MX_USART1_UART_Init();
HAL_TIM_Base_Start(&htim3);
while (1)
{
for(k=0;k<SAMP;k++)
{
while((__HAL_TIM_GET_COUNTER(&htim3))<124);
HAL_ADC_Start(&hadc1);
HAL_ADC_PollForConversion(&hadc1, HAL_MAX_DELAY);
dat[k] = HAL_ADC_GetValue(&hadc1); //GET ADC Value
}
// for loop to print sammples to computer screen with 1 msec pause between samples
for (k=0;k<SAMP;k++)
{
sprintf((char*)buf,"%d\r\n", dat[k]);
HAL_UART_Transmit(&huart1, buf, strlen((char*)buf), HAL_MAX_DELAY);
HAL_Delay(1);
}
HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_13);
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13,GPIO_PIN_SET);
HAL_Delay(3000);
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13,GPIO_PIN_RESET);
HAL_Delay(3000);
}
}
htim3.Instance = TIM3;
htim3.Init.Prescaler = 16-1;
htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
htim3.Init.Period = 125-1;
htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_Base_Init(&htim3) != HAL_OK)
{
Error_Handler();
}
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig) != HAL_OK)
{
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
{
Error_Handler();
}
2021-08-25 12:22 AM
You could try reducing the prescaler from 16-1 to 8-1. May or may not work, depending on optimization settings and other things.
A better way would be to use a timer to trigger the ADC and store that data into a buffer. Much higher rates and much more accurate timings are possible that way.
2021-08-25 01:33 AM
Hi @TDK
thank you for your attention Can you more explain about "trigger the ADC" please ?
2021-08-25 01:36 AM
2021-08-25 09:13 AM
Excuse me , is there example ? i did read about "Conversion on external trigger"
2021-08-25 12:45 PM