Author Topic: trigger external TIM use for ADC conversion  (Read 11535 times)

Offline stm32

  • Newbie
  • *
  • Posts: 1
    • View Profile
trigger external TIM use for ADC conversion
« on: June 01, 2011, 10:08:39 AM »
i have a project of STM32f103 , i need to conversion a signal audio use ADC of STM32 , i use TIM1 for signal external trigger conversion for ADC , i config TIM , ADC and DMA to sample signal 8khz . i use DAM to tranfer data from ADC1_DR_Address(0x4001244C)  to SRAM ( first address  : 0x20000000 ) , and finally i use USART to tranfer data -> PC . but, my project not working ?
atthough , PC recevei data , and display , but the value of bytes 0x00 . it point to that ADC not working , someone who can help me ? PLEASE !

#define SRAM_Address  ((uint32_t)0x20000000)
#define ADC1_DR_Address  ((uint32_t)0x4001244C)


#ifndef f32
#define   f32      float
#endif


GPIO_InitTypeDef GPIO_InitStructure;
ADC_InitTypeDef ADC_InitStructure;
USART_InitTypeDef USART_InitStructure;
DMA_InitTypeDef           DMA_InitStructure;
TIM_TimeBaseInitTypeDef   TIM_TimeBaseStructure;
TIM_OCInitTypeDef         TIM_OCInitStructure;



void RCC_Configuration(void)
{
   
   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
}


void Delay(__IO uint32_t num)
{
   __IO uint32_t index = 0;

   /* default system clock is 72MHz */
   for(index = (72 * num); index != 0; index--)
   {
   }
}
int main(void)
{
   u8 i;
   
   /* Configure the system clocks */
   RCC_Configuration();   

   /* Configure PC.00 and PC.01 as input pull_up */
     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1;
     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
     GPIO_Init(GPIOC, &GPIO_InitStructure);

   

   /* config PB.10-> PB.13 */
   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10|GPIO_Pin_11|GPIO_Pin_12|GPIO_Pin_13;
   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
     GPIO_Init(GPIOB, &GPIO_InitStructure);
   
   
   
   /*
     *   configure UART pins   
     */   
   
   /* Enable UART clock, if using USART2 or USART3 ... we must use RCC_APB1PeriphClockCmd(RCC_APB1Periph_USARTx, ENABLE) */
   RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);




   

   /* Configure PA9 for USART Tx as alternate function push-pull */
   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
   GPIO_Init(GPIOA, &GPIO_InitStructure);

   /* Configure PA10 for USART Rx as input floating */
   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
   GPIO_Init(GPIOA, &GPIO_InitStructure);   
   
   /* USARTx configured as follow:
        - BaudRate = 115200 baud 
        - Word Length = 8 Bits
        - One Stop Bit
        - No parity
        - Hardware flow control disabled (RTS and CTS signals)
        - Receive and transmit enabled
     */
   USART_InitStructure.USART_BaudRate = 38400;
   USART_InitStructure.USART_WordLength = USART_WordLength_8b;
   USART_InitStructure.USART_StopBits = USART_StopBits_1;
   USART_InitStructure.USART_Parity = USART_Parity_No;
   USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
   USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;   

   /* USART configuration */
   USART_Init(USART1, &USART_InitStructure);

   /* Enable USART */
   USART_Cmd(USART1, ENABLE);   




   

   /* TIM1 configuration ------------------------------------------------------*/

   RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);


        /* Time Base configuration */
   TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
     TIM_TimeBaseStructure.TIM_Period = 1499;      // tan so lay mau la 8 khz  (12MHz : 1500 = 8KHz)   
     TIM_TimeBaseStructure.TIM_Prescaler = 0x5;     // 72MHz : 6 = 12MHz
     TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;   
     TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;   // dem len
     
     TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);

   TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
   TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;               
   TIM_OCInitStructure.TIM_Pulse = 749;
     TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;         
     TIM_OC1Init(TIM1, &TIM_OCInitStructure);
   
   
      


   /* DMA1 Channel1 Configuration ----------------------------------------------*/

      RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
   
   
        DMA_DeInit(DMA1_Channel1);
        DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address;
        DMA_InitStructure.DMA_MemoryBaseAddr = (u32)SRAM_Address;
        DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
        DMA_InitStructure.DMA_BufferSize = 16000;          /*lay 10000 mau */       
        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);

      
      
 
        /* cho phep DMA1 channel1 */
        DMA_Cmd(DMA1_Channel1, ENABLE);

      

   /*
     *   configure ADC pins   
     */

   /* Configure PA.01 (ADC Channel1) as analog input -------------------------*/
     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
     GPIO_Init(GPIOA, &GPIO_InitStructure);

   /* Configure clocks for ADC and GPIO PORT */
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);

   /* ADCx configuration ------------------------------------------------------*/
     ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
     ADC_InitStructure.ADC_ScanConvMode = DISABLE;
     ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
     ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1;
     ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
     ADC_InitStructure.ADC_NbrOfChannel = 1;
     ADC_Init(ADC1, &ADC_InitStructure);   


   
   /* ADC1 Regular Channel 1 Configuration */
   ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 1, ADC_SampleTime_55Cycles5);      

   ADC_DMACmd(ADC1, ENABLE);

   ADC_ExternalTrigConvCmd(ADC1, ENABLE);
   
     /* Enable ADC1 */
     ADC_Cmd(ADC1, ENABLE);   

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

    /* Check the end of ADC1 reset calibration register */
    while(ADC_GetResetCalibrationStatus(ADC1));

    /* Start ADC1 calibaration */
    ADC_StartCalibration(ADC1);
    /* Check the end of ADC1 calibration */
    while(ADC_GetCalibrationStatus(ADC1));
   /*start convertion*/ 
   //ADC_SoftwareStartConvCmd(ADC1,ENABLE);
   

   
   /* start main program */
   while (1)   
   {   
      GPIO_WriteBit(GPIOB,GPIO_Pin_13, (BitAction)(1 - GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_13)));
      Delay(50000);
      while(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_0)== 0)
         {
         Delay(50000);
         while(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_0) == 0)
            {
               TIM_Cmd(TIM1, ENABLE);                    // lay mau khi bam nut
            }
         }
      TIM_Cmd(TIM1,DISABLE);
      while(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_1) == 0)
         {
            Delay(1000);
            while(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_1) == 0)
            {
               GPIO_WriteBit(GPIOB,GPIO_Pin_10, (BitAction) (1));
               for(i=0;i<=40000;i++)
                  {
                     USART_SendData(USART1,*(uint8_t *)(SRAM_Address + i));
                     Delay(80);
                  }
               GPIO_WriteBit(GPIOB,GPIO_Pin_10,( BitAction) (0));
            }
         }
   }
}




what thing i wrong ?