htujun
3 years ago
30 changed files with 2511 additions and 2292 deletions
@ -1,98 +1,109 @@
@@ -1,98 +1,109 @@
|
||||
#include "adc.h" |
||||
#include "delay.h" |
||||
|
||||
|
||||
|
||||
|
||||
//初始化ADC
|
||||
//这里我们仅以规则通道为例
|
||||
//我们默认将开启通道0~3
|
||||
void Adc_Init(void) |
||||
{
|
||||
ADC_InitTypeDef ADC_InitStructure;
|
||||
GPIO_InitTypeDef GPIO_InitStructure; |
||||
|
||||
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB |RCC_APB2Periph_ADC1 , ENABLE ); //使能ADC1通道时钟
|
||||
|
||||
|
||||
RCC_ADCCLKConfig(RCC_PCLK2_Div6); //设置ADC分频因子6 72M/6=12,ADC最大时间不能超过14M
|
||||
|
||||
//PA1 作为模拟通道输入引脚
|
||||
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1; |
||||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; //模拟输入引脚
|
||||
GPIO_Init(GPIOB, &GPIO_InitStructure);
|
||||
|
||||
ADC_DeInit(ADC1); //复位ADC1
|
||||
|
||||
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; //ADC工作模式:ADC1和ADC2工作在独立模式
|
||||
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数据右对齐
|
||||
ADC_InitStructure.ADC_NbrOfChannel = 1; //顺序进行规则转换的ADC通道的数目
|
||||
ADC_Init(ADC1, &ADC_InitStructure); //根据ADC_InitStruct中指定的参数初始化外设ADCx的寄存器
|
||||
|
||||
ADC_Cmd(ADC1, ENABLE); //使能指定的ADC1
|
||||
|
||||
ADC_ResetCalibration(ADC1); //使能复位校准
|
||||
while(ADC_GetResetCalibrationStatus(ADC1)); |
||||
ADC_StartCalibration(ADC1); |
||||
while(ADC_GetCalibrationStatus(ADC1)); |
||||
|
||||
}
|
||||
//获得ADC值
|
||||
//ch:通道值 0~3
|
||||
u16 Get_Adc(u8 ch)
|
||||
#include "adc.h" |
||||
#include "delay.h" |
||||
|
||||
|
||||
#define ADC1_DR_Address ((u32)0x40012400+0x4c) |
||||
uint16_t ADC_ConvertedValue[3]; |
||||
/*******************************************************************************
|
||||
* 函 数 名 : ADCx_Init |
||||
* 函数功能 : ADC初始化
|
||||
* 输 入 : 无 |
||||
* 输 出 : 无 |
||||
*******************************************************************************/ |
||||
void ADCx_Init(void) |
||||
{ |
||||
//设置指定ADC的规则组通道,一个序列,采样时间
|
||||
ADC_RegularChannelConfig(ADC1, ch, 1, ADC_SampleTime_239Cycles5 ); //ADC1,ADC通道,采样时间为239.5周期
|
||||
|
||||
ADC_SoftwareStartConvCmd(ADC1, ENABLE); //使能指定的ADC1的软件转换启动功能
|
||||
|
||||
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC ));//等待转换结束
|
||||
GPIO_InitTypeDef GPIO_InitStructure; //定义结构体变量
|
||||
ADC_InitTypeDef ADC_InitStructure; |
||||
DMA_InitTypeDef DMA_InitStructure; |
||||
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOA|RCC_APB2Periph_ADC1|RCC_APB2Periph_AFIO,ENABLE);
|
||||
RCC_ADCCLKConfig(RCC_PCLK2_Div6);//设置ADC分频因子6 72M/6=12,ADC最大时间不能超过14M
|
||||
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); |
||||
|
||||
DMA_DeInit(DMA1_Channel1); |
||||
DMA_InitStructure.DMA_PeripheralBaseAddr =(u32)&ADC1->DR; |
||||
DMA_InitStructure.DMA_MemoryBaseAddr =(u32)&ADC_ConvertedValue; |
||||
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; |
||||
DMA_InitStructure.DMA_BufferSize = 2;
|
||||
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; |
||||
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
|
||||
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; |
||||
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; |
||||
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; |
||||
DMA_InitStructure.DMA_Priority = DMA_Priority_High; |
||||
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; |
||||
DMA_Init(DMA1_Channel1, &DMA_InitStructure); |
||||
DMA_Cmd(DMA1_Channel1, ENABLE); |
||||
|
||||
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_1;//ADC
|
||||
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AIN; //模拟输入
|
||||
//GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
|
||||
GPIO_Init(GPIOA,&GPIO_InitStructure); |
||||
|
||||
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_1;//ADC
|
||||
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AIN; //模拟输入
|
||||
//GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
|
||||
GPIO_Init(GPIOB,&GPIO_InitStructure);
|
||||
|
||||
ADC_DeInit(ADC1);//?? ADC1,?? ADC1,
|
||||
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 = 2;//1个转换在规则序列中 也就是只转换规则序列1
|
||||
ADC_Init(ADC1, &ADC_InitStructure);//ADC初始化
|
||||
|
||||
ADC_DMACmd(ADC1, ENABLE); |
||||
ADC_Cmd(ADC1, ENABLE);//开启AD转换器
|
||||
|
||||
ADC_ResetCalibration(ADC1);//重置指定的ADC的校准寄存器
|
||||
while(ADC_GetResetCalibrationStatus(ADC1));//获取ADC重置校准寄存器的状态
|
||||
|
||||
ADC_StartCalibration(ADC1);//开始指定ADC的校准状态
|
||||
while(ADC_GetCalibrationStatus(ADC1));//获取指定ADC的校准程序
|
||||
|
||||
return ADC_GetConversionValue(ADC1); //返回最近一次ADC1规则组的转换结果
|
||||
ADC_SoftwareStartConvCmd(ADC1, ENABLE);//使能或者失能指定的ADC的软件转换启动功能
|
||||
} |
||||
|
||||
|
||||
u16 Get_Adc_Average(u8 ch,u8 times) |
||||
/*******************************************************************************
|
||||
* 函 数 名 : Get_ADC_Value |
||||
* 函数功能 : 获取通道ch的转换值,取times次,然后平均
|
||||
* 输 入 : ch:通道编号 |
||||
times:获取次数 |
||||
* 输 出 : 通道ch的times次转换结果平均值 |
||||
*******************************************************************************/ |
||||
u16 Get_ADC_Value(u8 ch,u8 times) |
||||
{ |
||||
u32 temp_val=0; |
||||
u8 t; |
||||
//设置指定ADC的规则组通道,一个序列,采样时间
|
||||
ADC_RegularChannelConfig(ADC1, ch, 1, ADC_SampleTime_239Cycles5); //ADC1,ADC通道,239.5个周期,提高采样时间可以提高精确度
|
||||
|
||||
for(t=0;t<times;t++) |
||||
{ |
||||
temp_val+=Get_Adc(ch); |
||||
//ADC_SoftwareStartConvCmd(ADC1, ENABLE);//使能指定的ADC1的软件转换启动功能
|
||||
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC ));//等待转换结束
|
||||
//temp_val+=ADC_GetConversionValue(ADC1);
|
||||
temp_val+=ADC_ConvertedValue[0]; |
||||
delay_ms(5); |
||||
} |
||||
return temp_val/times; |
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
u16 Get_ADC1_Value(u8 ch,u8 times) |
||||
{ |
||||
u32 temp_val=0; |
||||
u8 t; |
||||
//设置指定ADC的规则组通道,一个序列,采样时间
|
||||
ADC_RegularChannelConfig(ADC1, ch, 2, ADC_SampleTime_239Cycles5); //ADC1,ADC通道,239.5个周期,提高采样时间可以提高精确度
|
||||
|
||||
for(t=0;t<times;t++) |
||||
{ |
||||
//ADC_SoftwareStartConvCmd(ADC1, ENABLE);//使能指定的ADC1的软件转换启动功能
|
||||
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC ));//等待转换结束
|
||||
//temp_val+=ADC_GetConversionValue(ADC1);
|
||||
temp_val+=ADC_ConvertedValue[1]; |
||||
delay_ms(5); |
||||
} |
||||
return temp_val/times; |
||||
}
|
||||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -0,0 +1,31 @@
@@ -0,0 +1,31 @@
|
||||
..\obj\stm32f10x_dma.o: ..\STM32F10x_FWLib\src\stm32f10x_dma.c |
||||
..\obj\stm32f10x_dma.o: ..\STM32F10x_FWLib\inc\stm32f10x_dma.h |
||||
..\obj\stm32f10x_dma.o: ..\USER\stm32f10x.h |
||||
..\obj\stm32f10x_dma.o: ..\CORE\core_cm3.h |
||||
..\obj\stm32f10x_dma.o: D:\Keil_v5\ARM\ARMCC\Bin\..\include\stdint.h |
||||
..\obj\stm32f10x_dma.o: ..\USER\system_stm32f10x.h |
||||
..\obj\stm32f10x_dma.o: ..\USER\stm32f10x_conf.h |
||||
..\obj\stm32f10x_dma.o: ..\STM32F10x_FWLib\inc\stm32f10x_adc.h |
||||
..\obj\stm32f10x_dma.o: ..\USER\stm32f10x.h |
||||
..\obj\stm32f10x_dma.o: ..\STM32F10x_FWLib\inc\stm32f10x_bkp.h |
||||
..\obj\stm32f10x_dma.o: ..\STM32F10x_FWLib\inc\stm32f10x_can.h |
||||
..\obj\stm32f10x_dma.o: ..\STM32F10x_FWLib\inc\stm32f10x_cec.h |
||||
..\obj\stm32f10x_dma.o: ..\STM32F10x_FWLib\inc\stm32f10x_crc.h |
||||
..\obj\stm32f10x_dma.o: ..\STM32F10x_FWLib\inc\stm32f10x_dac.h |
||||
..\obj\stm32f10x_dma.o: ..\STM32F10x_FWLib\inc\stm32f10x_dbgmcu.h |
||||
..\obj\stm32f10x_dma.o: ..\STM32F10x_FWLib\inc\stm32f10x_dma.h |
||||
..\obj\stm32f10x_dma.o: ..\STM32F10x_FWLib\inc\stm32f10x_exti.h |
||||
..\obj\stm32f10x_dma.o: ..\STM32F10x_FWLib\inc\stm32f10x_flash.h |
||||
..\obj\stm32f10x_dma.o: ..\STM32F10x_FWLib\inc\stm32f10x_fsmc.h |
||||
..\obj\stm32f10x_dma.o: ..\STM32F10x_FWLib\inc\stm32f10x_gpio.h |
||||
..\obj\stm32f10x_dma.o: ..\STM32F10x_FWLib\inc\stm32f10x_i2c.h |
||||
..\obj\stm32f10x_dma.o: ..\STM32F10x_FWLib\inc\stm32f10x_iwdg.h |
||||
..\obj\stm32f10x_dma.o: ..\STM32F10x_FWLib\inc\stm32f10x_pwr.h |
||||
..\obj\stm32f10x_dma.o: ..\STM32F10x_FWLib\inc\stm32f10x_rcc.h |
||||
..\obj\stm32f10x_dma.o: ..\STM32F10x_FWLib\inc\stm32f10x_rtc.h |
||||
..\obj\stm32f10x_dma.o: ..\STM32F10x_FWLib\inc\stm32f10x_sdio.h |
||||
..\obj\stm32f10x_dma.o: ..\STM32F10x_FWLib\inc\stm32f10x_spi.h |
||||
..\obj\stm32f10x_dma.o: ..\STM32F10x_FWLib\inc\stm32f10x_tim.h |
||||
..\obj\stm32f10x_dma.o: ..\STM32F10x_FWLib\inc\stm32f10x_usart.h |
||||
..\obj\stm32f10x_dma.o: ..\STM32F10x_FWLib\inc\stm32f10x_wwdg.h |
||||
..\obj\stm32f10x_dma.o: ..\STM32F10x_FWLib\inc\misc.h |
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -1,2 +1,13 @@
@@ -1,2 +1,13 @@
|
||||
Load "..\\OBJ\\Template.axf" |
||||
Erase Done.Programming Done.Verify OK.Flash Load finished at 12:58:02 |
||||
*** Using Compiler 'V5.06 update 6 (build 750)', folder: 'D:\Keil_v5\ARM\ARMCC\Bin' |
||||
Build target 'Target 1' |
||||
compiling adc.c... |
||||
linking... |
||||
..\OBJ\Template.axf: Error: L6218E: Undefined symbol DMA_Cmd (referred from adc.o). |
||||
..\OBJ\Template.axf: Error: L6218E: Undefined symbol DMA_DeInit (referred from adc.o). |
||||
..\OBJ\Template.axf: Error: L6218E: Undefined symbol DMA_Init (referred from adc.o). |
||||
Not enough information to list image symbols. |
||||
Not enough information to list load addresses in the image map. |
||||
Finished: 2 information, 0 warning and 3 error messages. |
||||
"..\OBJ\Template.axf" - 3 Error(s), 0 Warning(s). |
||||
Target not created. |
||||
Build Time Elapsed: 00:00:01 |
||||
|
@ -1 +1 @@
@@ -1 +1 @@
|
||||
2022/4/17 12:58:02 |
||||
2022/4/25 23:07:52 |
File diff suppressed because one or more lines are too long
Loading…
Reference in new issue