新闻中心

EEPW首页>嵌入式系统>设计应用> AD与DA的完整配置

AD与DA的完整配置

作者: 时间:2016-11-26 来源:网络 收藏
#include"AD_DA.H"

////////////AD的配置函数/////////////////////////////
#include "stm32f10x.h"
#include "stm32f10x_dac.h"
#include "stm32f10x_adc.h"
#include "stm32f10x_dma.h"
u16 ADC_ConvertedValue[BufferSize];

本文引用地址://m.amcfsurvey.com/article/201611/321632.htm

////////////AD滤波函数/////////////////////////////


////////////AD的配置函数/////////////////////////////
////////////AD函数/////////////////////////////
voidADC_configuration()
{
ADC_InitTypeDef ADC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;


//对应的引脚配置:通道0~15依次对应:PA(0~7),PB(0~1),PC(0~5)
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);


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

ADC_RegularChannelConfig(ADC1, ADC_Channel_14, 1, ADC_SampleTime_55Cycles5);


ADC_Cmd(ADC1, ENABLE);

ADC_ResetCalibration(ADC1);

while(ADC_GetResetCalibrationStatus(ADC1));//等待ADC的校准寄存器被设置完毕

ADC_StartCalibration(ADC1);

while(ADC_GetCalibrationStatus(ADC1));

ADC_SoftwareStartConvCmd(ADC1, ENABLE);


//ADC_ConvertedValue[cnt];
}

void DAC_Configuration(void)
{
DAC_InitTypeDef DAC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;


GPIO_InitStructure.GPIO_Pin= GPIO_Pin_4|GPIO_Pin_5;//IO端口的第4位
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AIN;//端口模式为模拟输入方式
GPIO_Init(GPIOA, &GPIO_InitStructure);

/
DAC_InitStructure.DAC_Trigger=DAC_Trigger_Software;//DAC触发方式为软件控制
DAC_InitStructure.DAC_WaveGeneration=DAC_WaveGeneration_None;
//不从DAC端口产生波形
DAC_InitStructure.DAC_LFSRUnmask_TriangleAmplitude=DAC_LFSRUnmask_Bits8_0; //8位DA波形生成模式
DAC_InitStructure.DAC_OutputBuffer=DAC_OutputBuffer_Enable;
//使能DAC输出缓冲器
//用上面参数初始化DAC通道1
DAC_Init(DAC_Channel_2, &DAC_InitStructure);//用上面参数初始化DAC通道1
DAC_Cmd(DAC_Channel_2, ENABLE);//使能DAC通道1
DAC_Init(DAC_Channel_1, &DAC_InitStructure);//用上面参数初始化DAC通道1
DAC_Cmd(DAC_Channel_1, ENABLE);//使能DAC通道1

//DAC_SetChannel2Data(DAC_Align_12b_L, DAC_data);//设置DAC通道1为12位且数据左对齐模式
//DAC_SoftwareTriggerCmd(DAC_Channel_2, ENABLE);//使能DAC通道1的软件触发方式,转换一次
//DAC_SetChannel1Data(DAC_Align_12b_L, DAC_data);//设置DAC通道1为12位且数据左对齐模式
//DAC_SoftwareTriggerCmd(DAC_Channel_1, ENABLE);//使能DAC通道1的软件触发方式,转换一次
}

void DMA_Configuration(void)
{
DMA_InitTypeDef DMA_InitStructure;
DMA_DeInit(DMA1_Channel1);
DMA_InitStructure.DMA_PeripheralBaseAddr=ADC1_DR_Address;//定义DMA外设基地址
DMA_InitStructure.DMA_MemoryBaseAddr=(u32)&ADC_ConvertedValue; // 定义DMA内存基地址
DMA_InitStructure.DMA_DIR=DMA_DIR_PeripheralSRC;//外设作为数据传输的来源(外设传到内存) DMA_DIR_PeripheralDST——内存传到外设
DMA_InitStructure.DMA_BufferSize=3;//连续转化3个AD通道值
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_Circular;//DMA模式是循环
DMA_InitStructure.DMA_Priority=DMA_Priority_High; //DMA优先级
DMA_InitStructure.DMA_M2M=DMA_M2M_Disable;//DMA没有设置内存到内存传输
DMA_Init(DMA1_Channel1,&DMA_InitStructure);
DMA_Cmd(DMA1_Channel1,ENABLE);
}



关键词:ADDA完整配

评论


技术专区

关闭