#include"AD_DA.H"////////////AD的配置函數/////////////////////////////
#include "stm32f10x.h"
#include "stm32f10x_dac.h"
#include "stm32f10x_adc.h"
#include "stm32f10x_dma.h"
u16 ADC_ConvertedValue[BufferSize];
本文引用地址:http://www.ex-cimer.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觸發(fā)方式為軟件控制
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的軟件觸發(fā)方式,轉換一次
//DAC_SetChannel1Data(DAC_Align_12b_L, DAC_data);//設置DAC通道1為12位且數據左對齊模式
//DAC_SoftwareTriggerCmd(DAC_Channel_1, ENABLE);//使能DAC通道1的軟件觸發(fā)方式,轉換一次
}
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——內存?zhèn)鞯酵庠O
DMA_InitStructure.DMA_BufferSize=3;//連續(xù)轉化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模式是循環(huán)
DMA_InitStructure.DMA_Priority=DMA_Priority_High; //DMA優(yōu)先級
DMA_InitStructure.DMA_M2M=DMA_M2M_Disable;//DMA沒有設置內存到內存?zhèn)鬏?br />DMA_Init(DMA1_Channel1,&DMA_InitStructure);
DMA_Cmd(DMA1_Channel1,ENABLE);
}
評論