Chinaunix首页 | 论坛 | 博客
  • 博客访问: 313256
  • 博文数量: 169
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 12
  • 用 户 组: 普通用户
  • 注册时间: 2013-07-10 12:00
文章分类

全部博文(169)

文章存档

2014年(38)

2013年(131)

我的朋友

分类: C/C++

2014-02-24 22:07:05

原文地址:stm 32f4的 ad采集代码 作者:M甲壳虫M

这是我开始设计的时候从discover上改过来的,测试是能成功的,希望对新研究m4的朋友有帮助;
自己感觉对于M4和M3的区别就在于,他的资源跟丰富,但是在初始化设置的时候吧M3中的一些设置更加人性化了,但是到现在st还没有一个固件库的文档,所以对于初学者
还是建议从m3开始学起,这样资料多一点,(我是属于对其内部结果有点了解,主要采用固件库编写)的那种人,跟那些直接采用寄存器的大神没法比啊,还得努力学习啊,对于外设的配置无非是,时钟配置,管脚配置,结构体配置,根据你的需要,在就是中断配置,在m4中多了一个端口服用的设置,这个如果要用到复用的同学可以看看,还有就是如果很多人写一个代码(团队合作的方式)尽量采用固件库方式这样可以得到更好的效果,想想看着一大堆的寄存器地址你的感觉是(⊙_⊙)。。。。。。。。。。。
/* Private define ------------------------------------------------------------*/
#define ADC1_DR_Address    ((u32)0x4001204C)
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/


/* Private function prototypes -----------------------------------------------*/
void USART_Configuration(void);
void ADC_Configuration(void);


/* Private variables ---------------------------------------------------------*/
float AD_value_1,AD_value_2,AD_value_3,AD_value_4,AD_value_5,AD_value_6,AD_value_7,AD_value_8;
vu16 ADC_ConvertedValue[8];




int main(void)
{
USART_Configuration();

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2 | RCC_AHB1Periph_GPIOA, ENABLE);//????GPIOA???±????DMA2
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);//????ADC1???è?±??

ADC1_DMA_Config();//DMA????
ADC1_GPIO_Init();//GPIO????
ADC1_Init();//AD??????????????
TIM6_Config();

ADC_SoftwareStartConv(ADC1);


while(1)
{
/* Printf message with AD value to serial port every 1 second */
printf("\r\n****************************************************************\r\n");
AD_value_1 = ADC_ConvertedValue[0];
AD_value_1 = (AD_value_1/4096)*3.3;
printf("The current AD1 value = %4.5fV \r\n", AD_value_1);

AD_value_2 = ADC_ConvertedValue[1];
AD_value_2 = (AD_value_2/4096)*3.3;
printf("The current AD2 value = %4.5fV \r\n", AD_value_2);

printf("\r\n****************************************************************\r\n");
AD_value_3 = ADC_ConvertedValue[2];
AD_value_3 = (AD_value_3/4096)*3.3;
printf("The current AD1 value = %4.5fV \r\n", AD_value_3);

printf("\r\n****************************************************************\r\n");
AD_value_4 = ADC_ConvertedValue[3];
AD_value_4 = (AD_value_4/4096)*3.3;
printf("The current AD1 value = %4.5fV \r\n", AD_value_4);


printf("\r\n****************************************************************\r\n");
AD_value_5 = ADC_ConvertedValue[4];
AD_value_5 = (AD_value_5/4096)*3.3;
printf("The current AD1 value = %4.5fV \r\n", AD_value_5);

printf("\r\n****************************************************************\r\n");
AD_value_6 = ADC_ConvertedValue[5];
AD_value_6 = (AD_value_6/4096)*3.3;
printf("The current AD1 value = %4.5fV \r\n", AD_value_6);

printf("\r\n****************************************************************\r\n");
AD_value_7 = ADC_ConvertedValue[6];
AD_value_7 = (AD_value_7/4096)*3.3;
printf("The current AD1 value = %4.5fV \r\n", AD_value_7);

printf("\r\n****************************************************************\r\n");
AD_value_8 = ADC_ConvertedValue[7];
AD_value_8 = (AD_value_8/4096)*3.3;
printf("The current AD1 value = %4.5fV \r\n", AD_value_8);

Delay(0x3ffffff); 
}
}
/**
  * @brief  ADC1 channel6 with DMA configuration
  * @param  None
  * @retval None
  */






/**
  * @brief  Delay Function.
  * @param  nCount:specifies the Delay time length.
  * @retval None
  */
void Delay(__IO uint32_t nCount)
{
  while(nCount--)
  {


  }
}
////////////////////////////////////////////////////////
void ADC1_Init(void)//AD??????????????
{
ADC_InitTypeDef       ADC_InitStructure;
  ADC_CommonInitTypeDef ADC_CommonInitStructure;

 /* ADC Common Init **********************************************************/
  ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
  ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2;
  ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
  ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
  ADC_CommonInit(&ADC_CommonInitStructure);


  /* ADC1 Init ****************************************************************/
  ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
  ADC_InitStructure.ADC_ScanConvMode = ENABLE;
  ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
  ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
  ADC_InitStructure.ADC_NbrOfConversion = 2;
  ADC_Init(ADC1, &ADC_InitStructure);


  /* ADC1 regular channe6 configuration *************************************/
  ADC_RegularChannelConfig(ADC1, ADC_Channel_6, 1, ADC_SampleTime_3Cycles);
  ADC_RegularChannelConfig(ADC1, ADC_Channel_7, 2, ADC_SampleTime_3Cycles);
 /* Enable DMA request after last transfer (Single-ADC mode) */
  ADC_DMARequestAfterLastTransferCmd(ADC1, ENABLE);


  /* Enable ADC3 DMA */
  ADC_DMACmd(ADC1, ENABLE);


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

}
static ADC1_DMA_Config(void)//DMA????
{
DMA_InitTypeDef       DMA_InitStructure;
//  DMA_DeInit(DMA2_Stream0);
  /* DMA2 Stream0 channe0 configuration **************************************/
  DMA_InitStructure.DMA_Channel = DMA_Channel_0; //?????¨??0  
  DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADC1_DR_Address; //???è?ù???·
  DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&ADC_ConvertedValue;//?????ù???·
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory; //????·??ò?????è??????
  DMA_InitStructure.DMA_BufferSize = 8; //DMA????8
  DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //???è???·??????
  DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //???????·????
  DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;//????°?×?  16bit
  DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; //????°?×?  16bit
  DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; //???·????
  DMA_InitStructure.DMA_Priority = DMA_Priority_High; //????????
  DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;         
  DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
  DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
  DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
  DMA_Init(DMA2_Stream0, &DMA_InitStructure);
  DMA_Cmd(DMA2_Stream0, ENABLE);
}
static ADC1_GPIO_Init(void)//GPIO????
{
GPIO_InitTypeDef      GPIO_InitStructure;

/* Configure ADC1 Channel6 pin as analog input ******************************/
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_3 | GPIO_Pin_6; //PA0 PA3 PA6
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; //????????
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; //???è????????
  GPIO_Init(GPIOA, &GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1; //PB0 PB1
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; //????????
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; //???è????????
  GPIO_Init(GPIOB, &GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_2 | GPIO_Pin_3; //PC0 PC2 PC3
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; //????????
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; //???è????????
  GPIO_Init(GPIOC, &GPIO_InitStructure);

}

阅读(779) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~