Jump to content
OppaErich

STM32 ADC1, multichannel & single conversion ?

Recommended Posts

I'm trying to read the output of a 3-axis accelerometer. X,Y and Z output are connected to ADC1 channels 10 to 12 of a STM32F103RB.

 

How can I do three channel A2D in single conversion mode ? There are lots of examples using continuous mode but none for single conversion mode. Here's my naive approach that did not work.

------------Edit----------------------

int main(void){

uint16_t ADCBuffer[] = {0xAAAA, 0xAAAA, 0xAAAA, 0xAAAA};

 

This works only using DMA, I found some demo code but the first line sends the MCU into an exception trap. How come ? If I change this to uint32_t then no exception occurs but I don't get sane readings. ADCBuffer[0] gets a large number, [1] is set to 1 and [2] stays at AAAA. Hmmmpf :unsure:

 


#include "stm32f10x_conf.h"
#include "ITDB02_display.h"


int main(void){

uint16_t ADCBuffer[] = {0xAAAA, 0xAAAA, 0xAAAA, 0xAAAA};

InitLCD(LANDSCAPE, ASPECT_16x9);
fontSize(FONT_LARGE);
clrScr();
setColor(200,200,200);

GPIO_InitTypeDef GPIO_InitStructure;

ADC_InitTypeDef ADC_InitStructure;

DMA_InitTypeDef DMA_InitStructure;

SystemInit();

RCC_ADCCLKConfig(RCC_PCLK2_Div6);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO | RCC_APB2Periph_ADC1, ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);

DMA_InitStructure.DMA_BufferSize = 3;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)ADCBuffer;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&ADC1->DR;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_Init(DMA1_Channel1, &DMA_InitStructure);

DMA_Cmd(DMA1_Channel1, ENABLE);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);

ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_InitStructure.ADC_NbrOfChannel = 3;
ADC_InitStructure.ADC_ScanConvMode = ENABLE;
ADC_Init(ADC1, &ADC_InitStructure);

ADC_RegularChannelConfig(ADC1, ADC_Channel_10, 1, ADC_SampleTime_1Cycles5);
ADC_RegularChannelConfig(ADC1, ADC_Channel_11, 2, ADC_SampleTime_1Cycles5);
ADC_RegularChannelConfig(ADC1, ADC_Channel_12, 1, ADC_SampleTime_1Cycles5);

ADC_Cmd(ADC1, ENABLE);
ADC_DMACmd(ADC1, ENABLE);

ADC_ResetCalibration(ADC1);
while(ADC_GetResetCalibrationStatus(ADC1));
ADC_StartCalibration(ADC1);
while(ADC_GetCalibrationStatus(ADC1));


while(1){
ADC_SoftwareStartConvCmd(ADC1, ENABLE);

printNumI(ADCBuffer[0], 0,5);
printNumI(ADCBuffer[1], 0,20);
printNumI(ADCBuffer[2], 0,35);

if(ADCBuffer[3] != 0xAAAA)
print("Sumthin went WRONG HERE !",10,80,0);
}
for(;;
}

Edited by Oppa

Share this post


Link to post
Share on other sites

HAH I'm getting somewhere. I've changed the line

uint16_t ADCBuffer[] = {0xAAAA, 0xAAAA, 0xAAAA, 0xAAAA}; 

to

uint16_t ADCBuffer[4];
ADCBuffer[3] = 0xAAAA; 

and I get sane readings. B) Strange though, why does the compiler build something that sends the CPU into a trap ?

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...