cancel
Showing results for 
Search instead for 
Did you mean: 

Reading ADC Multichannel from sensor

motorhead_i_t
Associate II
Posted on February 13, 2013 at 18:48

 i am having trouble writing code on

STM32f103 , I want to get value from accelerometer and send to UART

void RCC_setup(void)

{

ErrorStatus HSEStartUpStatus;

RCC_DeInit();

RCC_HSEConfig(RCC_HSE_ON);

HSEStartUpStatus = RCC_WaitForHSEStartUp();

if (HSEStartUpStatus == SUCCESS)

{

RCC_PCLK1Config(RCC_HCLK_Div2);

RCC_PCLK2Config(RCC_HCLK_Div1);

RCC_HCLKConfig(RCC_SYSCLK_Div1);

  RCC_ADCCLKConfig(RCC_PCLK2_Div4);

  RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);

FLASH_SetLatency(FLASH_Latency_2);

FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

RCC_PLLCmd(ENABLE);

while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);

RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

while(RCC_GetSYSCLKSource() != 0x08);

}

}

void GPIO_USART1_setup()

{

GPIO_InitTypeDef GPIO_InitStructure ;

//Enable GPIOA and AFIO clock

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO,ENABLE);

//Configure USART_Tx (PA9) as alternate function push-pull

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz ;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP ;

GPIO_Init(GPIOA, &GPIO_InitStructure);

//Configure USART1_Rx (PA10) as input floating

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 ;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;

GPIO_Init(GPIOA , &GPIO_InitStructure);

}

void GPIO_ADC1_setup()

{

   GPIO_InitTypeDef GPIO_InitStructure;

   // Enable GPIOC clock

   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);

   // Configure PA1 as analog input(ADC Channel) 

   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2;

   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;

   GPIO_Init(GPIOA, &GPIO_InitStructure);

}

void DMA_Config(void)

{

  

__IO uint32_t ADC1ConvertedValue[2];

/* Initialize the DMA1 Channel1 according to the DMA_InitStructure members */

DMA_InitTypeDef DMA_InitStructure;

        RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE);

  

DMA_DeInit(DMA1_Channel1);

DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADC1->DR;

DMA_InitStructure.DMA_MemoryBaseAddr =(uint32_t)&ADC1ConvertedValue[2];

DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;

DMA_InitStructure.DMA_BufferSize = 3;

DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;

DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;

DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;

DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;

DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; 

DMA_InitStructure.DMA_Priority = DMA_Priority_High;

DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;

DMA_Init(DMA1_Channel1, &DMA_InitStructure);

/* Enable DMA1 Channel1 */

DMA_Cmd(DMA1_Channel1, ENABLE);

}

void ADC_setup()

{

   ADC_InitTypeDef ADC_InitStructure;

   // Enable ADC1 clock

   RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE);

   // ADC1 configuration

   ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;

   ADC_InitStructure.ADC_ScanConvMode = ENABLE;

   ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;

   ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;

   ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;

   ADC_InitStructure.ADC_NbrOfChannel = 3;

   ADC_Init(ADC1, &ADC_InitStructure);

   // ADC1 regular channel1 configuration  

   ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_13Cycles5);

ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 2, ADC_SampleTime_13Cycles5);

ADC_RegularChannelConfig(ADC1, ADC_Channel_2, 3, ADC_SampleTime_13Cycles5);

 

   ADC_DMACmd(ADC1, ENABLE);                        

   ADC_Cmd(ADC1, ENABLE);  // Enable ADC1 

   

   ADC_ResetCalibration(ADC1);   // Enable ADC1 reset calibaration register 

   while(ADC_GetResetCalibrationStatus(ADC1));

      

   ADC_StartCalibration(ADC1);   // Start ADC1 calibaration 

   while(ADC_GetCalibrationStatus(ADC1)); // Check the end of ADC1 calibration 

ADC_SoftwareStartConvCmd(ADC1, ENABLE);

}

int main (void) {

 __IO uint32_t ADC1ConvertedValue[2];

 int16_t i;

          uint16_t Valuel[2];

   RCC_setup();

          GPIO_USART1_setup();

          USART1_setup(); 

         DMA_Config();

 ADC_setup();

while(1) {

                 ADC_SoftwareStartConvCmd(ADC1, ENABLE);

 for(i=0;i<3;i++)

{

Value[i] = ADC1ConvertedValue[i]; 

USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET ;

USART_SendData(USART1,Value[i]);

                          }

}

Result:  value not change ,not read 

please give me some guidelines ,Thank You .

1 REPLY 1
Posted on February 13, 2013 at 19:23

Good grief, where to start.

Use a newer version of the STM32F1 library (3.5.0 ?), the clocks and PLL are set up in SystemInit(), called prior to main() in the CMSIS model.

You DMA to a local array in a subroutine, that becomes a random stack location once it exits, use a global.

The array is too small for the data sample length. Need foo[3]

The beginning of the array is &foo[0] not &foo[2]!!

You don't setup the USART.

USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET ; // WTF?

USART_SendData() sends 8-bit binary data, not ASCII decoding, not 12/16-bit words.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..