#include"AD_DA.H"////////////AD的配置函數(shù)/////////////////////////////
#include "stm32f10x.h"
#include "stm32f10x_dac.h"
#include "stm32f10x_adc.h"
#include "stm32f10x_dma.h"
u16 ADC_ConvertedValue[BufferSize];
本文引用地址:http://butianyuan.cn/article/201611/321632.htm////////////AD濾波函數(shù)/////////////////////////////
////////////AD的配置函數(shù)/////////////////////////////
////////////AD函數(shù)/////////////////////////////
voidADC_configuration()
{
ADC_InitTypeDef ADC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
//對(duì)應(yīng)的引腳配置:通道0~15依次對(duì)應(yīng):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;//指定用于轉(zhuǎn)換的通道數(shù)
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的校準(zhǔn)寄存器被設(shè)置完畢
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觸發(fā)方式為軟件控制
DAC_InitStructure.DAC_WaveGeneration=DAC_WaveGeneration_None;
//不從DAC端口產(chǎn)生波形
DAC_InitStructure.DAC_LFSRUnmask_TriangleAmplitude=DAC_LFSRUnmask_Bits8_0; //8位DA波形生成模式
DAC_InitStructure.DAC_OutputBuffer=DAC_OutputBuffer_Enable;
//使能DAC輸出緩沖器
//用上面參數(shù)初始化DAC通道1
DAC_Init(DAC_Channel_2, &DAC_InitStructure);//用上面參數(shù)初始化DAC通道1
DAC_Cmd(DAC_Channel_2, ENABLE);//使能DAC通道1
DAC_Init(DAC_Channel_1, &DAC_InitStructure);//用上面參數(shù)初始化DAC通道1
DAC_Cmd(DAC_Channel_1, ENABLE);//使能DAC通道1
//DAC_SetChannel2Data(DAC_Align_12b_L, DAC_data);//設(shè)置DAC通道1為12位且數(shù)據(jù)左對(duì)齊模式
//DAC_SoftwareTriggerCmd(DAC_Channel_2, ENABLE);//使能DAC通道1的軟件觸發(fā)方式,轉(zhuǎn)換一次
//DAC_SetChannel1Data(DAC_Align_12b_L, DAC_data);//設(shè)置DAC通道1為12位且數(shù)據(jù)左對(duì)齊模式
//DAC_SoftwareTriggerCmd(DAC_Channel_1, ENABLE);//使能DAC通道1的軟件觸發(fā)方式,轉(zhuǎn)換一次
}
void DMA_Configuration(void)
{
DMA_InitTypeDef DMA_InitStructure;
DMA_DeInit(DMA1_Channel1);
DMA_InitStructure.DMA_PeripheralBaseAddr=ADC1_DR_Address;//定義DMA外設(shè)基地址
DMA_InitStructure.DMA_MemoryBaseAddr=(u32)&ADC_ConvertedValue; // 定義DMA內(nèi)存基地址
DMA_InitStructure.DMA_DIR=DMA_DIR_PeripheralSRC;//外設(shè)作為數(shù)據(jù)傳輸?shù)膩碓矗ㄍ庠O(shè)傳到內(nèi)存) DMA_DIR_PeripheralDST——內(nèi)存?zhèn)鞯酵庠O(shè)
DMA_InitStructure.DMA_BufferSize=3;//連續(xù)轉(zhuǎn)化3個(gè)AD通道值
DMA_InitStructure.DMA_PeripheralInc=DMA_PeripheralInc_Disable; //外設(shè)寄存器地址不變
DMA_InitStructure.DMA_MemoryInc=DMA_MemoryInc_Enable;//內(nèi)存寄存器地址遞增
DMA_InitStructure.DMA_PeripheralDataSize=DMA_PeripheralDataSize_Word; //外設(shè)數(shù)據(jù)寬度32bit
DMA_InitStructure.DMA_MemoryDataSize=DMA_MemoryDataSize_Word;//內(nèi)存數(shù)據(jù)寬度32bit
DMA_InitStructure.DMA_Mode=DMA_Mode_Circular;//DMA模式是循環(huán)
DMA_InitStructure.DMA_Priority=DMA_Priority_High; //DMA優(yōu)先級(jí)
DMA_InitStructure.DMA_M2M=DMA_M2M_Disable;//DMA沒有設(shè)置內(nèi)存到內(nèi)存?zhèn)鬏?br />DMA_Init(DMA1_Channel1,&DMA_InitStructure);
DMA_Cmd(DMA1_Channel1,ENABLE);
}
評(píng)論