博客首页 | 排行榜 |

不死鸟的BLOG

养成良好的习惯比什么都重要!差距是由长时间的习惯拉开的…………

个人档案
博文分类
ADC(5)  2009-02-15 13:36


/myspace/album/image.php?uid=105196&aid=807&pic=3b90d977&ext=png&screen=show
/myspace/album/image.php?uid=105196&aid=807&pic=c2f5b45a&ext=png&screen=show

 

 

本例展示了如何使用双ADC对多路通道进行同时转化。

 

使用ADC1对通道14和通道17进行转化。使用ADC2对通道10和通道11进行转化。通道17与内部参考电压Vref相连。

 

一旦转换开始,ADC1和ADC2对对通道14和10的转换将同时开始,结果存于ADC1的32-bit DR寄存器内,其中高16bit为ADC2对通道10的转换结果,低16bit为ADC1对通道14的转换结果。之后由DMA把DR寄存器内的数据传送到内存中的ADC_DualConvertedValueTab表格。随后,ADC1和ADC2以及DMA会对通道17和通道11进行同样流程的转换和数据传送。

 

这样的转换-传送将会持续进行直到传送的数据数目达到DMA预设的上限。

 

然后利用LCD把通道17上Vref的值,和通道10上分压器VR1上的电压值交替显示在屏幕上作为验证。

 

 

 

#include "stm32f10x_lib.h"

#include "lcd.h"

 

/* Private typedef -----------------------------------------------------------*/

/* Private define ------------------------------------------------------------*/

#define ADC1_DR_Address    ((u32)0x4001244C)

 

/* Private macro -------------------------------------------------------------*/

/* Private variables ---------------------------------------------------------*/

ADC_InitTypeDef ADC_InitStructure;

DMA_InitTypeDef DMA_InitStructure;

vu32 ADC_DualConvertedValueTab[16];

ErrorStatus HSEStartUpStatus;

 

vu16 TempvalueCh10,TempvalueCh17;   

/* Private function prototypes -----------------------------------------------*/

void RCC_Configuration(void);

void GPIO_Configuration(void);

void NVIC_Configuration(void);

 

void LcdShow_Init(void);

void displayconvert(vu16 value); 

/* Private functions ---------------------------------------------------------*/

void delay(void);

void delay()

{

int i,j; j=0;

for (i=0; i<0xfffff; i++) j++;

}

 

char display[4]={0,0};

 

/*******************************************************************************

* 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();

 

  LcdShow_Init();

 

  /* DMA channel1 configuration ----------------------------------------------*/

  DMA_DeInit(DMA_Channel1);

  DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)ADC1_DR_Address;

  DMA_InitStructure.DMA_MemoryBaseAddr = (u32)ADC_DualConvertedValueTab;

  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;

  DMA_InitStructure.DMA_BufferSize = 16;

  DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;

  DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;

  DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word;

  DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Word;

  DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;

  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_RegSimult; //ADC1和ADC2工作在同步规则和同步注入模式

  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 = 2;

  ADC_Init(ADC1, &ADC_InitStructure);

  /* ADC1 regular channels configuration */

  ADC_RegularChannelConfig(ADC1, ADC_Channel_14, 1, ADC_SampleTime_239Cycles5);   

  ADC_RegularChannelConfig(ADC1, ADC_Channel_17, 2, ADC_SampleTime_239Cycles5);

  /* Enable ADC1 DMA */

  ADC_DMACmd(ADC1, ENABLE);

 

  /* ADC2 configuration ------------------------------------------------------*/

  ADC_InitStructure.ADC_Mode = ADC_Mode_RegSimult;  //ADC1和ADC2工作在同步规则和同步注入模式

  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 = 2;

  ADC_Init(ADC2, &ADC_InitStructure);

  /* ADC2 regular channels configuration */

  ADC_RegularChannelConfig(ADC2, ADC_Channel_10, 1, ADC_SampleTime_239Cycles5);

  ADC_RegularChannelConfig(ADC2, ADC_Channel_11, 2, ADC_SampleTime_239Cycles5);

  /* Enable ADC2 external trigger conversion */

  ADC_ExternalTrigConvCmd(ADC2, ENABLE);

 

  /* Enable ADC1 */

  ADC_Cmd(ADC1, ENABLE);

  /* Enable Vrefint channel17 */

  ADC_TempSensorVrefintCmd(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));

 

  /* Enable ADC2 */

  ADC_Cmd(ADC2, ENABLE);

 

  /* Enable ADC2 reset calibaration register */  

  ADC_ResetCalibration(ADC2);

  /* Check the end of ADC2 reset calibration register */

  while(ADC_GetResetCalibrationStatus(ADC2));

 

  /* Start ADC2 calibaration */

  ADC_StartCalibration(ADC2);

  /* Check the end of ADC2 calibration */

  while(ADC_GetCalibrationStatus(ADC2));

 

  /* Start ADC1 Software Conversion */

  ADC_SoftwareStartConvCmd(ADC1, ENABLE);

 

  /* Test on Channel 1 DMA_FLAG_TC flag */

  while(!DMA_GetFlagStatus(DMA_FLAG_TC1));//   DMA_FLAG_TC flag----- 通道1传输完成标志位

  /* Clear Channel 1 DMA_FLAG_TC flag */

  DMA_ClearFlag(DMA_FLAG_TC1);

 

  while (1)//  LCD 上交替显示

  {

    /*Show Channel17 (Vintref) and Channel10 (Vpontentiometer) on LCD*/

    vu16 TempvalueCh17 = ADC_DualConvertedValueTab[7] & 0xffff;

    vu16 TempvalueCh10 = ADC_DualConvertedValueTab[8] >> 16;

    displayconvert(TempvalueCh17);

    write_string(display);

    delay();

    delay();

    displayconvert(TempvalueCh10);

    write_string(display);

    delay();

    delay();

  }

}

 

/*******************************************************************************

* 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);

 

  /* 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);

 

    /* PCLK2 = HCLK */

    RCC_PCLK2Config(RCC_HCLK_Div1);

 

    /* PCLK1 = HCLK/2 */

    RCC_PCLK1Config(RCC_HCLK_Div2);

 

    /* ADCCLK = PCLK2/4 */

    RCC_ADCCLKConfig(RCC_PCLK2_Div4);

 

    /* PLLCLK = 8MHz * 7 = 56 MHz */

    RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_7);

 

    /* Enable PLL */

    RCC_PLLCmd(ENABLE);

 

    /* Wait till PLL is ready */

    while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)

    {

    }

 

    /* Select PLL as system clock source */

    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, ADC2 and GPIOC clock */

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_ADC2 |

                         RCC_APB2Periph_GPIOC, ENABLE);

  /* Enable GPIOE clock for LCD display

     (PC.08 -- PC.11: COM1~4; PE.00 -- PE.15: SEG0~15 )*/

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE, ENABLE);

 

  /* TIM2 clocks enable */  //For LCD display

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, 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 PC.00, PC.01 and PC.04 (ADC Channel10, Channel11 and Channel14)

    as analog input ----------------------------------------------------------*/

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_4;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;

  GPIO_Init(GPIOC, &GPIO_InitStructure);

 

  /* Configure PE.00 -- PE.15 as Output push-pull : SEG0~15 */

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  GPIO_Init(GPIOE, &GPIO_InitStructure);

 

  /* Configure PC.08 -- PC.11 as Output push-pull  : COM1~4 */

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  GPIO_Init(GPIOC, &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

 

  /* enabling interrupt */ // For LCD enable IT from TIM2

  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); 

}

 

/***************************************************************************

* Function Name  : LcdShow_Init

* Description    : Configures TIM2 for LCD display

* Input          : None

* Output         : None

* Return         : None

***************************************************************************/

 

void LcdShow_Init(void)

{

  TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;

  TIM_OCInitTypeDef  TIM_OCInitStructure;

 

  /* Time base configuration */

  TIM_TimeBaseStructure.TIM_Period = 8000;

  TIM_TimeBaseStructure.TIM_Prescaler = 17;

  TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;

  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

  TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);

 

  TIM_ARRPreloadConfig(TIM2,DISABLE);

  /* only counter overflow/underflow generate U interrupt */

  TIM_UpdateRequestConfig(TIM2,TIM_UpdateSource_Regular);

 

  /* Output Compare Timing Mode configuration: Channel1 */

  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Timing;

  TIM_OCInitStructure.TIM_Channel = TIM_Channel_1;

  TIM_OCInitStructure.TIM_Pulse = 4000;

  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;

  TIM_OCInit(TIM2, &TIM_OCInitStructure);

 

  TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Disable);

 

  /* TIM IT enable */

  TIM_ITConfig(TIM2, TIM_IT_CC1 | TIM_IT_Update, ENABLE);

 

  /* TIM2 enable counter */

  TIM_Cmd(TIM2, ENABLE);

}

 

/***************************************************************************

* Function Name  : displayconvert

* Description    : convert data to displayable form

* Input          : None

* Output         : None

* Return         : None

***************************************************************************/

void displayconvert(vu16 value)

{

  u8 num1 = value&0xf;

  u8 num2 = (value&0xff)>>4;

  u8 num3= (value&0xfff)>>8;

  u8 num4 = value>>12;

  if (num1>9)

    display[3]=num1+(65-10);

  else

    display[3]=num1+(48-0);

 

  if (num2>9)

    display[2]=num2+(65-10);

  else

    display[2]=num2+(48-0);

 

  if (num3>9)

    display[1]=num3+(65-10);

  else

    display[1]=num3+(48-0);

 

  if (num4>9)

    display[0]=num4+(65-10);

  else

    display[0]=num4+(48-0);

}

 

#ifdef  DEBUG

/*******************************************************************************

* Function Name  : assert_failed

* Description    : Reports the name of the source file and the source line number

*                  where the assert_param error has occurred.

* Input          : - file: pointer to the source file name

*                  - line: assert_param 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

类别:实验 |
上一篇:EXTI | 下一篇:ADC(4)
以下网友评论只代表其个人观点,不代表本网站的观点或立场