welcom ! Handel home

2014年7月2日 星期三

ST32 M3 DMA ADC Transfer to Buffer

#define ADC1_DR_addr    ((u32)0x4001244C) //AD轉換結果存放位址定義
void DMA_ADC_Transfer_Reset(void){
    //開始DMA傳輸
     DMA_Channel1->CCR &= ~(1<<0 span="">禁用DMA_Channel,ENCCR1寄存器的0
     DMA->IFCR |= 0x0000000F;//清除CHANNEL14個標誌
     DMA_Channel1->CNDTR = (u16)SampleDepth;//重新設置要設置的DMA傳輸資料量
     DMA_Channel1->CCR |= (1<<0 span="">重新使能DMA_channel1
     while(!(DMA->ISR & DMA_FLAG_TC1));
     DMA_Channel1->CCR &= ~(1<<0 span="">禁用DMA_Channel,ENCCR1寄存器的0
}
void DMA_ADC_Transfer_Init(void){
DMA_InitTypeDef         DMA_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); 
  DMA_DeInit(DMA_Channel1);
  DMA_InitStructure.DMA_PeripheralBaseAddr = ADC_DR_Address;//ADC資料寄存器位址
  DMA_InitStructure.DMA_MemoryBaseAddr = (u32 )Data_Buffer[0];//目標緩衝區位址
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;//外設是源
  DMA_InitStructure.DMA_BufferSize = 0;//設置DMA讀取長度為0 啟動前再重設
  DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;//外設地址不遞增
  DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;//目標緩衝區位址遞增
  DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word;//外設資料寬度 32bit
  DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Word;//目標緩衝區資料寬度32bit
  DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;//DMA模式:Circular迴圈模式/Normal普通模式
  DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;//優先順序
  DMA_InitStructure.DMA_M2M = DMA_M2M_Enable;//記憶體到記憶體模式使能
  DMA_Init(DMA_Channel1, &DMA_InitStructure);  
  //DMA_Cmd(DMA_Channel1, ENABLE);//實作時再啟動
}

void ADC_Initial(void){
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB| RCC_APB2Periph_GPIOC |RCC_APB2Periph_ADC1, ENABLE );   //使能ADC通道時鐘
ADC_InitTypeDef        ADC_InitStructure;
  ADC_InitStructure.ADC_Mode = ADC_Mode_RegSimult;//ADC1/ADC2同時並行採樣模式
  ADC_InitStructure.ADC_ScanConvMode = DISABLE;//多通道掃描模式
  ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;//單次轉換模式(轉換後即停止)
  ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T2_CC2;//觸發模式
  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;//數據右對齊
  ADC_InitStructure.ADC_NbrOfChannel = 1;
  ADC_Init(ADC1 , &ADC_InitStructure);
  ADC_RegularChannelConfig(ADC1, ADC_Channel_14, 1, ADC_SampleTime_1Cycles5);
  ADC_DMACmd(ADC1, ENABLE); 
 ADC_InitStructure.ADC_Mode = ADC_Mode_RegSimult;
  ADC_InitStructure.ADC_ScanConvMode = DISABLE;
  ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
  ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T2_CC2;
  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
  ADC_InitStructure.ADC_NbrOfChannel = 1;
  ADC_Init(ADC2, &ADC_InitStructure);
  ADC_RegularChannelConfig(ADC2, ADC_Channel_11, 1, ADC_SampleTime_1Cycles5);
  ADC_ExternalTrigConvCmd(ADC2, ENABLE);
  ADC_Cmd(ADC1, ENABLE);
  ADC_ResetCalibration(ADC1); 
  while(ADC_GetResetCalibrationStatus(ADC1));
  ADC_StartCalibration(ADC1);
  while(ADC_GetCalibrationStatus(ADC1));
  ADC_Cmd(ADC2, ENABLE);
  ADC_ResetCalibration(ADC2);
  while(ADC_GetResetCalibrationStatus(ADC2));
  ADC_StartCalibration(ADC2);
  while(ADC_GetCalibrationStatus(ADC2));
  //while(!DMA_GetFlagStatus(DMA_FLAG_TC1));
  //DMA_ClearFlag(DMA_FLAG_TC1); 
    ADC_ExternalTrigConvCmd( ADC1, ENABLE);
}

void ADC_Sample_Frequency_Set(u32 Frequency){
   TIM_OCInitTypeDef        TIM_OCInitStructure;
   TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure; 
   RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE );
   TIM_Cmd(TIM2, DISABLE);//先停止TIM2時鐘,以準備下的設置
 TIM_TimeBaseStructure.TIM_Period = 35; //APR寄存器         
  TIM_TimeBaseStructure.TIM_Prescaler = 1000000/Frequency-1; //預分頻值,用來調整頻率,分頻係數=1000khz/(prescaler+1)     
  TIM_TimeBaseStructure.TIM_ClockDivision = 0;    
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; 
  TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1 ;//TIM_OCMode_Toggle,TIM_OCMode_PWM1 
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;                
TIM_OCInitStructure.TIM_Pulse = TIM_TimeBaseStructure.TIM_Period; //CCR2寄存器
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;         
TIM_OC2Init(TIM2, &TIM_OCInitStructure);
  TIM_Cmd(TIM2, ENABLE); 
  TIM_CtrlPWMOutputs(TIM2, ENABLE); 
}


  1. /******************************************************************************/   
  2. /* SETUP.C: Setup Functions                                                   */   
  3. /******************************************************************************/   
  4. /* This file is part of the uVision/ARM development tools.                    */   
  5. /* Copyright (c) 2005-2007 Keil Software. All rights reserved.                */   
  6. /* This software may only be used under the terms of a valid, current,        */   
  7. /* end user licence from KEIL for a compatible version of KEIL software       */   
  8. /* development tools. Nothing else gives you the right to use this software.  */   
  9. /******************************************************************************/   
  10.    
  11. #include               /* STM32F10x Library Definitions      */   
  12. //#include "UET_GPIO.h"   
  13.    
  14.    
  15. #define ADC1_DR_Address    ((u32)0x4001244C) //統蕉晤最忒聊菴媼梒摯眈壽敵湔痄華硊腕堤   
  16.    
  17.    
  18. unsigned short int ADC_ConvertedValue[9]={0};   
  19. ADC_InitTypeDef   ADC_InitStructure;   
  20. DMA_InitTypeDef   DMA_InitStructure;   
  21.    
  22. void SetupADC (void)    
  23. {   
  24.      
  25. //  /* Configure PC0-PC5 (ADC Channel10-Channel5) as analog input                             */   
  26. //  GPIO_InitStructure.GPIO_Pin  = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5;   
  27. //  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;   
  28. //  GPIO_Init(GPIOC, &GPIO_InitStructure);   
  29.    
  30.   /* DMA Channel1 Configuration ----------------------------------------------*/   
  31.   DMA_DeInit(DMA_Channel1); //蔚籵耋1腔眈壽敵湔葩弇峈蘇硉   
  32.   DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address;//俋扢華硊   
  33.   DMA_InitStructure.DMA_MemoryBaseAddr     = (u32)&ADC_ConvertedValue;//囀湔華硊   
  34.   DMA_InitStructure.DMA_DIR                = DMA_DIR_PeripheralSRC;//dma換怀源砃等砃   
  35.   DMA_InitStructure.DMA_BufferSize         = 9;//扢离DMA婓換怀奀遣喳腔酗僅 word   
  36.   DMA_InitStructure.DMA_PeripheralInc      = DMA_PeripheralInc_Disable;//扢离DMA腔俋扢菰崝耀宒ㄛ珨跺俋扢   
  37.     
  38.   DMA_InitStructure.DMA_MemoryInc          = DMA_MemoryInc_Enable;//扢离DMA腔囀湔菰崝耀宒   
  39.   DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;//俋扢杅擂趼酗 16bits   
  40.   DMA_InitStructure.DMA_MemoryDataSize     = DMA_MemoryDataSize_HalfWord;//囀湔杅擂趼酗    16bits   
  41.   DMA_InitStructure.DMA_Mode               = DMA_Mode_Circular;//扢离DMA腔換怀耀宒ㄩ蟀哿祥剿腔悜遠耀宒   
  42.   DMA_InitStructure.DMA_Priority           = DMA_Priority_High;//扢离DMA腔蚥珂撰梗   
  43.   DMA_InitStructure.DMA_M2M                = DMA_M2M_Disable;//扢离DMA腔2跺memory笢腔曹講誑眈溼恀   
  44.   DMA_Init(DMA_Channel1, &DMA_InitStructure);//扢离陔腔籵耋敵湔硉   
  45.      
  46.   DMA_Cmd(DMA_Channel1, ENABLE);        /* Enable DMA Channel1                */   
  47.      
  48.   /* ADC1 Configuration (ADC1CLK = 12 MHz) -----------------------------------*/   
  49.   ADC_InitStructure.ADC_Mode               = ADC_Mode_Independent;   
  50.   ADC_InitStructure.ADC_ScanConvMode       = ENABLE;   
  51.   ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;                             
  52.   ADC_InitStructure.ADC_ExternalTrigConv   = ADC_ExternalTrigConv_None;   
  53.   ADC_InitStructure.ADC_DataAlign          = ADC_DataAlign_Right;   
  54.   ADC_InitStructure.ADC_NbrOfChannel       = 9;//9跺籵耋   
  55.   ADC_Init(ADC1, &ADC_InitStructure);   
  56.    
  57.   /* ADC1 Regular Channel Configuration                                      */   
  58.   ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 1, ADC_SampleTime_55Cycles5);   
  59.   ADC_RegularChannelConfig(ADC1, ADC_Channel_8, 2, ADC_SampleTime_55Cycles5);   
  60.   ADC_RegularChannelConfig(ADC1, ADC_Channel_9, 3, ADC_SampleTime_55Cycles5);   
  61.   ADC_RegularChannelConfig(ADC1, ADC_Channel_10, 4, ADC_SampleTime_55Cycles5);   
  62.   ADC_RegularChannelConfig(ADC1, ADC_Channel_11, 5, ADC_SampleTime_55Cycles5);   
  63.   ADC_RegularChannelConfig(ADC1, ADC_Channel_12, 6, ADC_SampleTime_55Cycles5);   
  64.   ADC_RegularChannelConfig(ADC1, ADC_Channel_13, 7, ADC_SampleTime_55Cycles5);   
  65.   ADC_RegularChannelConfig(ADC1, ADC_Channel_14, 8, ADC_SampleTime_55Cycles5);   
  66.   ADC_RegularChannelConfig(ADC1, ADC_Channel_15, 9, ADC_SampleTime_55Cycles5);   
  67. //  ADC_RegularChannelConfig(ADC1, ADC_Channel_9, 10, ADC_SampleTime_55Cycles5);   
  68. //  ADC_RegularChannelConfig(ADC1, ADC_Channel_10, 11, ADC_SampleTime_55Cycles5);   
  69. //  ADC_RegularChannelConfig(ADC1, ADC_Channel_11, 12, ADC_SampleTime_55Cycles5);   
  70. //  ADC_RegularChannelConfig(ADC1, ADC_Channel_12, 13, ADC_SampleTime_55Cycles5);   
  71. //  ADC_RegularChannelConfig(ADC1, ADC_Channel_13, 14, ADC_SampleTime_55Cycles5);   
  72. //  ADC_RegularChannelConfig(ADC1, ADC_Channel_14, 15, ADC_SampleTime_55Cycles5);   
  73. //  ADC_RegularChannelConfig(ADC1, ADC_Channel_15, 16, ADC_SampleTime_55Cycles5);   
  74.    
  75.    /* Enable JEOC interupt */   
  76.   ADC_ITConfig(ADC1, ADC_IT_EOC, ENABLE);   
  77.    
  78.   ADC_DMACmd(ADC1, ENABLE);             /* Enable ADC1's DMA interface        */   
  79.   ADC_Cmd   (ADC1, ENABLE);             /* Enable ADC1                        */   
  80.    
  81.   /* Enable ADC1 reset calibaration register */      
  82.   ADC_ResetCalibration(ADC1);   
  83.   /* Check the end of ADC1 reset calibration register */   
  84.   while(ADC_GetResetCalibrationStatus(ADC1)){};   
  85.    
  86.   /* Start ADC1 calibaration */   
  87.   ADC_StartCalibration(ADC1);   
  88.   /* Check the end of ADC1 calibration */   
  89.   while(ADC_GetCalibrationStatus(ADC1)){};   
  90.    
  91.    
  92.   ADC_SoftwareStartConvCmd(ADC1,ENABLE);/* Start ADC1 Software Conversion     */    
  93. }   

沒有留言: