原创 ADC 程序 for mini board

2008-4-10 16:23 3074 6 6 分类: MCU/ 嵌入式

根据ST提供的代码修改,从ADC通过DMA到memory的变量。


原来的代码与mini板的pin不符,作了一点修改:


1. RCC initialization,注意开GPIOA、DMA和ADC的clock


2. GPIOA pin11 (ADC_IN1)的初始化,设置其为analog input


3. DMA和ADC 的初始化


Note:设置IAR工作在Simulation模式ADC_GetCalibrationStatus(ADC1)会始终为SET。


还有些地方没有搞懂:


#define ADC1_DR_Address    ((u32)0x4001244C) 这个地址在其手册没有没有搜索到?


LPFQ48的Pin11 是个可复用的pin,不需要remap吗?系统提供的remap函数里面都没有ADC的参数,是否只要把该pin设为analog input系统就认为它是AD input 了?


希望各位指定一下


/******************** (C) COPYRIGHT 2007 STMicroelectronics ********************
* File Name          : main.c
* Author             : MCD Application Team
* Date First Issued  : 02/05/2007, Modified by Ray 04/10/2008
* Description        : Main program body
********************************************************************************


// 1 ADC inside & 16 pins can act as AD input. For 48LQFN, Pin 11 is ADC_IN1


/* Includes ------------------------------------------------------------------*/
#include "stm32f10x_lib.h"


/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define ADC1_DR_Address    ((u32)0x4001244C)


/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
ADC_InitTypeDef ADC_InitStructure;
/*typedef struct
{ u32 ADC_Mode;       
  FunctionalState ADC_ScanConvMode;      whether the conversion is performed in multi or single channel mode
  FunctionalState ADC_ContinuousConvMode;                continuous or single mode
  u32 ADC_ExternalTrigConv;                              define external trigger
  u32 ADC_DataAlign;                                     specify the align is right or left
  u8 ADC_NbrOfChannel;                                   specify the number of ADC channels
} ADC_InitTypeDef  */
DMA_InitTypeDef DMA_InitStructure;
/*typedef struct
{ u32 DMA_PeripheralBaseAddr;          the number defines the peripheral base address for DMA channelx
  u32 DMA_MemoryBaseAddr;                                     memory base address for DMA channelx
  u32 DMA_DIR;                         specify the peripheral is source or destination
  u32 DMA_BufferSize;
  u32 DMA_PeripheralInc;               whether the peripheral address is incremented or not
  u32 DMA_MemoryInc;                   whether the memory address is incremented or not
  u32 DMA_PeripheralDataSize;          configures the peripheral data width
  u32 DMA_MemoryDataSize;
  u32 DMA_Mode;                        circular of normal mode
  u32 DMA_Priority;
  u32 DMA_M2M;                         whether enable memory to memory transfer
} DMA_InitTypeDef;
*/
vu16 ADC_ConvertedValue;               // typedef volatile unsigned short vu16; (stm32f10x_type.h) *
ErrorStatus HSEStartUpStatus;
   
/* Private function prototypes -----------------------------------------------*/
void RCC_Configuration(void);
void GPIO_Configuration(void);
void NVIC_Configuration(void);
void SysTick_Config(void); 
/* Private functions ---------------------------------------------------------*/


/*******************************************************************************
* Function Name  : main
* Description    : Main program
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
int main(void)
{
#ifdef DEBUG
  debug();
#endif


  /* System clocks configuration ---------------------------------------------*/
  RCC_Configuration();


  /* NVIC configuration ------------------------------------------------------*/
  NVIC_Configuration();


  /* GPIO configuration ------------------------------------------------------*/
  GPIO_Configuration();


  /* Configure the systick */
  //SysTick_Config();
  //LcdShow_Init();


  /* DMA channel1 configuration ----------------------------------------------*/
  DMA_DeInit(DMA_Channel1);     // reset DMA registers to default values
  DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address;       //(u32)0x4001244C
  DMA_InitStructure.DMA_MemoryBaseAddr = (u32)&ADC_ConvertedValue;
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
  DMA_InitStructure.DMA_BufferSize = 1;
  DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable;      
  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(DMA_Channel1, &DMA_InitStructure);


  /* Enable DMA channel1 */
  DMA_Cmd(DMA_Channel1, 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 = 1;
  ADC_Init(ADC1, &ADC_InitStructure);


  /* ADC1 regular channel_1 configuration */
  ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 1, ADC_SampleTime_55Cycles5);


  /* Enable ADC1 DMA */
  ADC_DMACmd(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));
  //In simulation, ADC_GetResetCalibrationStatus(ADC1) always is SET
 
  /* Start ADC1 calibaration */
  ADC_StartCalibration(ADC1);
  /* Check the end of ADC1 calibration */
  while(ADC_GetCalibrationStatus(ADC1));


  /* Start ADC1 Software Conversion */
  ADC_SoftwareStartConvCmd(ADC1, ENABLE);


  while(1)
  {
  }
}


/*******************************************************************************
* Function Name  : RCC_Configuration
* Description    : Configures the different system clocks.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void RCC_Configuration(void)
{
  /* RCC system reset(for debug purpose) */
  RCC_DeInit();


  /* Enable HSE */
  RCC_HSEConfig(RCC_HSE_ON);      //enable high speed external oscillator


  /* Wait till HSE is ready */
  HSEStartUpStatus = RCC_WaitForHSEStartUp();


  if(HSEStartUpStatus == SUCCESS)
  {
    /* Enable Prefetch Buffer */
    FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);


    /* Flash 2 wait state */
    FLASH_SetLatency(FLASH_Latency_2); 


    /* HCLK = SYSCLK */
    RCC_HCLKConfig(RCC_SYSCLK_Div1);  //config AHB clock, it can be SYSCLK/1 to /512


    /* PCLK2 = HCLK */
    RCC_PCLK2Config(RCC_HCLK_Div1);   //config high speed AHB clock


    /* PCLK1 = HCLK/2 */
    RCC_PCLK1Config(RCC_HCLK_Div2);   //config low speed AHB clock


    /* ADCCLK = PCLK2/4 */
    RCC_ADCCLKConfig(RCC_PCLK2_Div4); //config ADC clock from PCLK2/2 ~ /8


    /* PLLCLK = HSE * 14 = 56 MHz */   //config PLL source and multiplication factor
    RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_14);  //HSE is source, factor is 14


    /* Enable PLL */
    RCC_PLLCmd(ENABLE);


    /* Wait till PLL is ready */
    while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
    {
    }


    /* Select PLL as system clock source, other options are HSE and HSI */
    RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);


    /* Wait till PLL is used as system clock source */
    while(RCC_GetSYSCLKSource() != 0x08)
    {
    }
  }


/* Enable peripheral clocks --------------------------------------------------*/
  /* Enable DMA clock */
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA, ENABLE);


  /* Enable ADC1 and GPIOC clock */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOA, ENABLE);


}


/*******************************************************************************
* Function Name  : GPIO_Configuration
* Description    : Configures the different GPIO ports.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void GPIO_Configuration(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;


  /* Configure GPIOA.1 Pin11 (ADC Channel 1) as analog input -------------------------*/
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
 
}


/*******************************************************************************
* Function Name  : NVIC_Configuration
* Description    : Configures Vector Table base location.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void NVIC_Configuration(void)
{
  NVIC_InitTypeDef NVIC_InitStructure;


#ifdef  VECT_TAB_RAM
  /* Set the Vector Table base location at 0x20000000 */
  NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else  /* VECT_TAB_FLASH  */
  /* Set the Vector Table base location at 0x08000000 */
  NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
#endif


    /* Configure the Priority Group to 2 bits */
  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);


  /* enabling interrupt */
  NVIC_InitStructure.NVIC_IRQChannel=USB_LP_CAN_RX0_IRQChannel;  //USB lower than CAN or RX
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);


  /* enabling interrupt */
  NVIC_InitStructure.NVIC_IRQChannel=TIM2_IRQChannel;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);


  /* Configure the SysTick handler priority */
  NVIC_SystemHandlerPriorityConfig(SystemHandler_SysTick, 2, 0);
}


#ifdef  DEBUG
/*******************************************************************************
* Function Name  : assert_failed
* Description    : Reports the name of the source file and the source line number
*                  where the assert error has occurred.
* Input          : - file: pointer to the source file name
*                  - line: assert error line source number
* Output         : None
* Return         : None
*******************************************************************************/
void assert_failed(u8* file, u32 line)
{
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */


  /* Infinite loop */
  while (1)
  {
  }
}


#endif 


/******************* (C) COPYRIGHT 2007 STMicroelectronics *****END OF FILE****/
/* NOTE
volatile 影响编译器编译的结果,指出,volatile 变量是随时可能发生变化的,与volatile变量有关的运算,不
要进行编译优化,以免出错,(VC++ 在产生release版可执行码时会进行编译优化,加volatile关键字的变量有关的运算,
将不进行编译优化。)。
例如:
volatile int i="10";
int j = i;
...
int k = i;
volatile 告诉编译器i是随时可能发生变化的,每次使用它的时候必须从i的地址中读取,因而编译器生成的可执行码会重新从
i的地址读取数据放在k中。
而优化做法是,由于编译器发现两次从i读数据的代码之间的代码没有对i进行过操作,它会自动把上次读的数据放在k中。
而不是重新从i里面读。这样以来,如果i是一个寄存器变量或者表示一个端口数据就容易出错,所以说volatile可以保证对特
殊地址的稳定访问,不会出错。                     */


 


 

文章评论0条评论)

登录后参与讨论
我要评论
0
6
关闭 站长推荐上一条 /2 下一条