ADC config with DMA in STM32F103

Discussion in 'Embedded Systems and Microcontrollers' started by leily, May 19, 2015.

  1. leily

    Thread Starter New Member

    May 19, 2015
    1
    0
    Hi,

    I want to program STM32F103 in which define an array that is completed with ADC value. I need to trigger the ADC with timer and also use DMA. It means that when the array complete, the DMA_IRQ Handler send this array by USART channel. I wrote this code but it doesn't work.

    I would be appreciate if you help me to correct this program.

    Code (Text):
    1. #include "stm32f10x.h"
    2.  
    3. #define ADC1_DR_Address    ((uint32_t)0x4001244C)
    4. uint16_t array [32];
    5. DMA_InitTypeDef DMA_InitStructure;
    6.  
    7. int main(void)
    8. {
    9.  
    10.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_AFIO, ENABLE);
    11.   RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
    12.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1,ENABLE);
    13.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC , ENABLE);
    14.   RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);                                            
    15.    
    16.   GPIO_InitTypeDef GPIO_InitStructure;
    17.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;            
    18.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    19.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    20.   GPIO_Init(GPIOA, &GPIO_InitStructure);
    21.  
    22.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;          
    23.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    24.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    25.   GPIO_Init(GPIOA, &GPIO_InitStructure);
    26.  
    27.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
    28.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
    29.   GPIO_Init(GPIOA, &GPIO_InitStructure);
    30.  
    31.    NVIC_InitTypeDef sara3;
    32.   sara3.NVIC_IRQChannel = DMA1_Channel1_IRQn;
    33.   sara3.NVIC_IRQChannelPreemptionPriority = 0;
    34.   sara3.NVIC_IRQChannelSubPriority = 0;
    35.   sara3.NVIC_IRQChannelCmd = ENABLE;
    36.   NVIC_Init(&sara3);
    37.   TIM_TimeBaseInitTypeDef Tim_TimeBaseStructure;
    38.   TIM_TimeBaseStructInit(&Tim_TimeBaseStructure);
    39.   Tim_TimeBaseStructure.TIM_Period = 9000;        
    40.   Tim_TimeBaseStructure.TIM_Prescaler = 0x0;      
    41.   Tim_TimeBaseStructure.TIM_ClockDivision = 0x0;  
    42.   Tim_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
    43.   TIM_TimeBaseInit(TIM2, &Tim_TimeBaseStructure);
    44.   TIM_SelectOutputTrigger(TIM2, TIM_TRGOSource_Update);
    45.  
    46. TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
    47.  
    48.   /*
    49.   TIM_OCInitTypeDef  TIM_OCInitStructure;
    50.   TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
    51.   TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;              
    52.   TIM_OCInitStructure.TIM_Pulse = 0x7F;
    53.   TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;        
    54.   TIM_OC1Init(TIM2, &TIM_OCInitStructure);
    55.   */
    56.    
    57.  
    58.   ADC_InitTypeDef ADC_InitStructure;
    59.   ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;      
    60.   ADC_InitStructure.ADC_ScanConvMode = DISABLE;              
    61.   ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;    
    62.   ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T2_CC2;
    63.   ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;            
    64.   ADC_InitStructure.ADC_NbrOfChannel = 1;                      
    65.   ADC_Init(ADC1, &ADC_InitStructure);
    66.  
    67.   ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 1, ADC_SampleTime_55Cycles5);    
    68.                                        
    69.   //ADC_SoftwareStartConvCmd(ADC1,ENABLE);
    70.  
    71.  
    72.  
    73.  
    74.  
    75.  
    76.   DMA_DeInit(DMA1_Channel1);
    77.   DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address;
    78.   DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)array;
    79.   DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
    80.   DMA_InitStructure.DMA_BufferSize = 32;
    81.   DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
    82.   DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
    83.   DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
    84.   DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
    85.   DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
    86.   DMA_InitStructure.DMA_Priority = DMA_Priority_High;
    87.   DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
    88.   DMA_Init(DMA1_Channel1, &DMA_InitStructure);
    89.  
    90.     USART_InitTypeDef USART_InitStructure;
    91.   USART_InitStructure.USART_BaudRate = 115200;
    92.   USART_InitStructure.USART_WordLength = USART_WordLength_8b;
    93.   USART_InitStructure.USART_StopBits = USART_StopBits_1;
    94.   USART_InitStructure.USART_Parity = USART_Parity_No;
    95.   USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    96.   USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
    97.   USART_Init(USART1, &USART_InitStructure);
    98.   USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
    99.   USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
    100.   USART_ClearFlag(USART1,USART_FLAG_TC);
    101.  
    102.   DMA_ITConfig (DMA1_Channel1, DMA_IT_TC,ENABLE);
    103.   DMA_Cmd(DMA1_Channel1, ENABLE);
    104.  
    105.    
    106.   ADC_Cmd(ADC1, ENABLE);
    107.   ADC_DMACmd(ADC1, ENABLE);
    108.   USART_Cmd(USART1, ENABLE);
    109.   TIM_Cmd(TIM2, ENABLE);
    110.  
    111.  
    112.   ADC_ResetCalibration(ADC1);
    113.   while(ADC_GetResetCalibrationStatus(ADC1));
    114.   ADC_StartCalibration(ADC1);
    115.   while(ADC_GetCalibrationStatus(ADC1));
    116.  
    117.  
    118.  
    119.    while (1);
    120.  
    121. }
    122.  
    123.  
    124.  
    125. void DMA1_Channel1_IRQHandler(void)
    126. {
    127.  
    128.   uint8_t i = 0;
    129.   if( DMA_GetITStatus(DMA1_FLAG_TC1) )
    130.   {
    131.     for(i = 0;i < 32 ; i++){
    132.      
    133.       USART_SendData(USART1, (uint8_t) array[i]);
    134.       while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET) {}
    135.      
    136.       USART_SendData(USART1, (uint8_t) array[i]>> 8);
    137.       while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET) {}
    138.       }
    139.  
    140.    
    141.   DMA_DeInit(DMA1_Channel1);
    142.   DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address;
    143.   DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)array;
    144.   DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
    145.   DMA_InitStructure.DMA_BufferSize = 32;
    146.   DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
    147.   DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable;
    148.   DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
    149.   DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
    150.   DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
    151.   DMA_InitStructure.DMA_Priority = DMA_Priority_High;
    152.   DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
    153.   DMA_Init(DMA1_Channel1, &DMA_InitStructure);
    154.  
    155.  
    156.  
    157.   ADC_DMACmd(ADC1, ENABLE);
    158. DMA_Cmd(DMA1_Channel1, ENABLE);
    159.  
    160.     DMA_ClearITPendingBit(DMA1_IT_TC1);
    161.     DMA_ClearITPendingBit(DMA1_IT_GL1);
    162.   }
    163. }
    164.  
     
Loading...