環(huán)境:
主機(jī):XP
開發(fā)環(huán)境:MDK4.23
MCU:STM32F103CBT6
說明:
通過腳PA1,PA2采集AD。每路AD采集10次。
#include "ad_driver.h"
//全局變量
//AD采樣存放空間
__IO uint16_t ADCConvertedValue[20];
//函數(shù)
//初始化AD
void init_ad(void)
{
ADC_InitTypeDef ADC_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
//---------------------充電AD初始化--------------------
//啟動DMA時鐘
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
//啟動ADC1時鐘
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
//采樣腳設(shè)置
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//DMA1通道1配置
DMA_DeInit(DMA1_Channel1);
//外設(shè)地址
DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_ADDRESS;
//內(nèi)存地址
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)ADCConvertedValue;
//dma傳輸方向單向
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
//設(shè)置DMA在傳輸時緩沖區(qū)的長度
DMA_InitStructure.DMA_BufferSize = 20;
//設(shè)置DMA的外設(shè)遞增模式,一個外設(shè)
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
//設(shè)置DMA的內(nèi)存遞增模式
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
//外設(shè)數(shù)據(jù)字長
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
//內(nèi)存數(shù)據(jù)字長
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
//設(shè)置DMA的傳輸模式:連續(xù)不斷的循環(huán)模式
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
//設(shè)置DMA的優(yōu)先級別
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
//設(shè)置DMA的2個memory中的變量互相訪問
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel1, &DMA_InitStructure);
//使能通道1
DMA_Cmd(DMA1_Channel1, ENABLE);
//ADC1配置
//獨立工作模式
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
//掃描方式
ADC_InitStructure.ADC_ScanConvMode = ENABLE;
//連續(xù)轉(zhuǎn)換
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
//外部觸發(fā)禁止
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
//數(shù)據(jù)右對齊
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
//用于轉(zhuǎn)換的通道數(shù)
ADC_InitStructure.ADC_NbrOfChannel = 2;
ADC_Init(ADC1, &ADC_InitStructure);
//規(guī)則模式通道配置
ADC_RegularChannelConfig(ADC1, ADC_Channel_1 , 1, ADC_SampleTime_239Cycles5);
ADC_RegularChannelConfig(ADC1, ADC_Channel_2 , 2, ADC_SampleTime_239Cycles5);
//使能ADC1的DMA
ADC_DMACmd(ADC1, ENABLE);
//使能ADC1
ADC_Cmd(ADC1, ENABLE);
//使能ADC1復(fù)位校準(zhǔn)寄存器
ADC_ResetCalibration(ADC1);
//檢查校準(zhǔn)寄存器是否復(fù)位完畢
while(ADC_GetResetCalibrationStatus(ADC1));
//開始校準(zhǔn)
ADC_StartCalibration(ADC1);
//檢測是否校準(zhǔn)完畢
while(ADC_GetCalibrationStatus(ADC1));
//開啟ADC1的軟件轉(zhuǎn)換
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
}
//獲得充電電壓
float voltage_charge(void)
{
uint8_t i = 0;
uint16_t sum = 0;
float v = 0;
//取得10次充電電壓平均值
for (i = 0;i < 10;i++)
{
sum += ADCConvertedValue[i * 2];
}
sum /= 10;
//變換成電壓值
v = 0.002991 * sum;
return v;
}
//獲得放電電壓
float voltage_discharge(void)
{
uint8_t i = 0;
uint16_t sum = 0;
float v = 0;
//取得10次充電電壓平均值
for (i = 0;i < 10;i++)
{
sum += ADCConvertedValue[i * 2 + 1];
}
sum /= 10;
//變換成電壓值
v = 0.002991 * sum;
return v;
}
上海意泓電子科技有限責(zé)任公司 版權(quán)所有 未經(jīng)授權(quán)禁止復(fù)制或鏡像
CopyRight 2020-2025 www.jlodreman.com All rights reserved 滬ICP備2021005866號