htujun
3 years ago
23 changed files with 2160 additions and 2245 deletions
@ -1,109 +1,86 @@
@@ -1,109 +1,86 @@
|
||||
#include "adc.h" |
||||
#include "delay.h" |
||||
/*
|
||||
* @Author: your name |
||||
* @Date: 2022-04-23 14:47:47 |
||||
* @LastEditTime: 2022-04-26 21:25:14 |
||||
* @LastEditors: your name |
||||
* @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
|
||||
* @FilePath: \USERe:\项目\兼职项目\20220423-IndoorEnvSystem\stm32\HARDWARE\ADC\adc.c |
||||
*/ |
||||
#include "ADC.h" |
||||
|
||||
u32 ADC_SUM_BUF = 0; |
||||
|
||||
#define ADC1_DR_Address ((u32)0x40012400+0x4c) |
||||
uint16_t ADC_ConvertedValue[3]; |
||||
/*******************************************************************************
|
||||
* 函 数 名 : ADCx_Init |
||||
* 函数功能 : ADC初始化
|
||||
* 输 入 : 无 |
||||
* 输 出 : 无 |
||||
*******************************************************************************/ |
||||
void ADCx_Init(void) |
||||
u16 ADC_AVG = 0; |
||||
|
||||
void ADC_Init_MY(void) |
||||
{ |
||||
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);
|
||||
GPIO_InitTypeDef GPIO_InitStructure; |
||||
ADC_InitTypeDef ADC_InitStructure; |
||||
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO | RCC_APB2Periph_ADC1,ENABLE); |
||||
|
||||
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初始化
|
||||
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; |
||||
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; |
||||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;//模拟输入
|
||||
GPIO_Init(GPIOA, &GPIO_InitStructure); |
||||
|
||||
ADC_DMACmd(ADC1, ENABLE); |
||||
ADC_Cmd(ADC1, ENABLE);//开启AD转换器
|
||||
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; |
||||
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; |
||||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;//模拟输入
|
||||
GPIO_Init(GPIOA, &GPIO_InitStructure); |
||||
|
||||
|
||||
ADC_ResetCalibration(ADC1);//重置指定的ADC的校准寄存器
|
||||
while(ADC_GetResetCalibrationStatus(ADC1));//获取ADC重置校准寄存器的状态
|
||||
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;//adc模式独立模式
|
||||
ADC_InitStructure.ADC_ScanConvMode = DISABLE;//扫描模式
|
||||
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;//连续模式
|
||||
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;//配置规则组触发模式软件出发
|
||||
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;//数据对其方式16位寄存器12位数据左对齐右对齐
|
||||
ADC_InitStructure.ADC_NbrOfChannel = 1;//规则通道序列长度
|
||||
ADC_InjectedChannelConfig(ADC1,ADC_Channel_0,1,ADC_SampleTime_239Cycles5); |
||||
ADC_InjectedChannelConfig(ADC1,ADC_Channel_1,1,ADC_SampleTime_239Cycles5); |
||||
ADC_Init(ADC1, &ADC_InitStructure); |
||||
|
||||
ADC_StartCalibration(ADC1);//开始指定ADC的校准状态
|
||||
while(ADC_GetCalibrationStatus(ADC1));//获取指定ADC的校准程序
|
||||
ADC_Cmd(ADC1,ENABLE); |
||||
//下面四个校准函数
|
||||
ADC_ResetCalibration(ADC1);//重置ADC校准寄存器
|
||||
while(ADC_GetResetCalibrationStatus(ADC1));//等待重置结束
|
||||
ADC_StartCalibration(ADC1);//开始校准ADC
|
||||
while(ADC_GetCalibrationStatus(ADC1));//等待校准结束
|
||||
|
||||
ADC_SoftwareStartConvCmd(ADC1, ENABLE);//使能或者失能指定的ADC的软件转换启动功能
|
||||
} |
||||
|
||||
/*******************************************************************************
|
||||
* 函 数 名 : 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++) |
||||
{ |
||||
//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 ADC_Get_Data(void)//获取adc某一通道一次转换结果
|
||||
{
|
||||
ADC_ExternalTrigInjectedConvConfig(ADC1,ADC_ExternalTrigInjecConv_None);//配置注入组触发模式
|
||||
ADC_InjectedSequencerLengthConfig(ADC1, 1);//注入组长度
|
||||
ADC_InjectedChannelConfig(ADC1,ADC_Channel_0,1,ADC_SampleTime_239Cycles5);//配置规则通道
|
||||
ADC_SoftwareStartInjectedConvCmd(ADC1, ENABLE);//开启软件转换
|
||||
|
||||
while(!ADC_GetFlagStatus(ADC1,ADC_FLAG_JEOC)); // 清除转换结束标志位等待转换结束
|
||||
return(ADC_GetInjectedConversionValue(ADC1,ADC_InjectedChannel_1));//得到转换结果
|
||||
|
||||
} |
||||
|
||||
u16 Get_ADC1_Value(u8 ch,u8 times) |
||||
u16 ADC_Get_AVG_Data(void) |
||||
{ |
||||
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; |
||||
}
|
||||
u16 i = 0; |
||||
for(i = 0;i < 500;i++) |
||||
{ |
||||
ADC_SUM_BUF = ADC_SUM_BUF + ADC_Get_Data(); |
||||
} |
||||
|
||||
ADC_AVG = ADC_SUM_BUF /500; |
||||
ADC_SUM_BUF = 0; |
||||
return(ADC_AVG); |
||||
|
||||
} |
||||
u16 ADC_Get_Data1(void)//获取adc某一通道一次转换结果
|
||||
{
|
||||
ADC_ExternalTrigInjectedConvConfig(ADC1,ADC_ExternalTrigInjecConv_None);//配置注入组触发模式
|
||||
ADC_InjectedSequencerLengthConfig(ADC1, 1);//注入组长度
|
||||
ADC_InjectedChannelConfig(ADC1,ADC_Channel_1,1,ADC_SampleTime_239Cycles5);//配置规则通道
|
||||
ADC_SoftwareStartInjectedConvCmd(ADC1, ENABLE);//开启软件转换
|
||||
|
||||
while(!ADC_GetFlagStatus(ADC1,ADC_FLAG_JEOC)); // 清除转换结束标志位等待转换结束
|
||||
return(ADC_GetInjectedConversionValue(ADC1,ADC_InjectedChannel_1));//得到转换结果
|
||||
|
||||
} |
||||
|
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.
@ -1,13 +1,2 @@
@@ -1,13 +1,2 @@
|
||||
*** 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 |
||||
Load "..\\OBJ\\Template.axf" |
||||
Erase Done.Programming Done.Verify OK.Flash Load finished at 21:29:01 |
||||
|
@ -1 +1 @@
@@ -1 +1 @@
|
||||
2022/4/25 23:07:52 |
||||
2022/4/26 21:29:01 |
File diff suppressed because one or more lines are too long
Loading…
Reference in new issue