-
Notifications
You must be signed in to change notification settings - Fork 973
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sampling at 16KHz using ADC prescaler? #747
Comments
Hi @Bambofy ADC is limited on this core to
Else use directly HAL/LL to play with ADC. FYI, an issue is already opened to have an advanced ADC library with convenient API to use them #5 |
ok thank you |
I'm trying to use the HAL api to set the prescaler of the ADC like this:
The status code returned is 1 (HAL_StatusTypeDef). And it still samples at 9KHz? The analog pin is A0 / PA0? |
which board ? |
NUCLEO-L452RE-P |
and how you read value ? |
|
Using |
Ok is this setup ok? i'm not sure how to actually read the ADC now though:
|
when i poll for conversion, it never receives HAL_OK |
i guess i need to set up all the other pin information, see https://visualgdb.com/tutorials/arm/stm32/adc/ |
you have to call start and poll for conversion, I guess. |
Ah! Also call first So, in void setup()
{
Serial.begin(9600);
analogRead(CONFIG_MIC_PIN);
<your init code, do not use pinMode> |
how do i find out what channel my analog pin is? |
In the datasheet or in the PeripheralsPin.c |
Ok so both ADC init functions return OK but the code get stuck polling for a value?
|
Could you try this, I'm at home and I've not the board. const int CONFIG_MIC_PIN = PA0;
ADC_HandleTypeDef adcConfiguration = {};
ADC_ChannelConfTypeDef AdcChannelConf = {};
void setup()
{
Serial.begin(9600);
// put your setup code here, to run once:
analogRead(CONFIG_MIC_PIN);
adcConfiguration.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV2;
adcConfiguration.Init.Resolution = ADC_RESOLUTION_12B;
adcConfiguration.Init.ScanConvMode = DISABLE;
adcConfiguration.Init.ContinuousConvMode = ENABLE;
adcConfiguration.Init.DiscontinuousConvMode = DISABLE;
adcConfiguration.Init.NbrOfDiscConversion = 0;
adcConfiguration.Init.ExternalTrigConv = ADC_EXTERNALTRIG_T1_CC1;
adcConfiguration.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
adcConfiguration.Init.DataAlign = ADC_DATAALIGN_RIGHT;
adcConfiguration.Init.NbrOfConversion = 1;
adcConfiguration.Init.DMAContinuousRequests = ENABLE;
adcConfiguration.Init.EOCSelection = DISABLE;
if ( HAL_ADC_Init(&adcConfiguration) != HAL_OK) {
Serial.println("HAL_ADC_Init failed");
while (1);
}
AdcChannelConf.Channel = ADC_CHANNEL_5; /* Specifies the channel to configure into ADC */
AdcChannelConf.Rank = ADC_REGULAR_RANK_1; /* Specifies the rank in the regular group sequencer */
/*##-2- Configure ADC regular channel ######################################*/
if (HAL_ADC_ConfigChannel(&adcConfiguration, &AdcChannelConf) != HAL_OK) {
/* Channel Configuration Error */
Serial.println("HAL_ADC_ConfigChan failed");
while (1);
}
if (HAL_ADCEx_Calibration_Start(&adcConfiguration, ADC_SINGLE_ENDED) != HAL_OK) {
Serial.println("HAL_ADCEx_Calibration_Start failed");
while (1);
}
}
void loop()
{
if (HAL_ADC_Start(&adcConfiguration) != HAL_OK) {
/* Start Conversation Error */
Serial.println("HAL_ADC_Start failed");
while (1);
}
if (HAL_ADC_PollForConversion(&adcConfiguration, 10) == HAL_OK)
{
if ((HAL_ADC_GetState(&adcConfiguration) & HAL_ADC_STATE_REG_EOC) == HAL_ADC_STATE_REG_EOC) {
Serial.println(HAL_ADC_GetValue(&adcConfiguration));
}
}
} |
It has "HAL_ADC_Init failed" |
This code is working but when i do HAL_ADC_GetValue i get no value
|
I use your config, so maybe something is wrong. |
|
|
ok i'm still testing, how do i define which pin i want to use? |
ok heres my current code:
|
Well, I will test on monday and check why it does not work. |
it's alive!! you have to call Start() then Poll() then Stop() for the process to get a new value :) 👍 here's a working sketch (analog input on pin A4/PC0)
|
Great! |
Figured it out :) The answer is to change which "Channel" the channel configuration is listening to. For the NUCLEO-L452RE-P it is here https://www.st.com/content/ccc/resource/technical/document/user_manual/group0/ff/5d/51/50/db/12/47/98/DM00387966/files/DM00387966.pdf/jcr:content/translations/en.DM00387966.pdf at page 36. Looking at a row in the table: |
Right, that's why in my example code i've set |
Hey, sorry to bring this issue up again but i'm struggling to change the pin that the data in being input to, see this code: I set the GPIO C pin 1 to analog and the ADC_CHANNEL to 2. to match this table A weird note is that if you:
it correctly receives the values at the channel, and it completely ignores which pin you put into analogRead()?
|
@Bambofy |
@Bambofy |
Hi, I need to sample the ADC at 16KHz to create an audio file. How can I do this using stm32duino?
Many thanks,
Richard
The text was updated successfully, but these errors were encountered: