当前位置:网站首页>STM32__06—单通道ADC
STM32__06—单通道ADC
2022-07-05 23:09:00 【c语言的神】
一,ADC(模数转化)
STM32芯片集成了12位逐次逼近型ADC模块,什么是逐次逼近型,简单来说就是内置了一个DAC模块用于输出一个电压与输入电压进行二分比较,通过DAC的数字量来确定输入电压的数值量。输入电压范围为0~3.3,对应数字量0~4095。我使用的是103c8t6,有2个ADC模块,分别为ADC1与ADC2,每个ADC模块有10个通道。接下来是对下面ADC模块进行简单的介绍。
我把本节所要学习的部分,分成3个模块为
1,输入模块
输入引脚对于的IO口可以参考下图,该引脚应用图来自江科大
这张图很重要,后面会多次用到。输入模块的配置比较简单配置成GPIO_Mode_AIN(GPIO的模拟输入模式)
2,连接通道模块
这一部分主要是配置输入通道,顺序,转化周期。
同时涉及到模式选择:注入模式与规则模式
我们这里涉及到的是规则模式,简单解释一下就是把个个输入通道排序,然后按照一定的规则进行转化有单次转化,连续转化,扫描转化,非扫描转化。具体功能参照数据手册。
3,ADC转换器模块(重点)
在使用ADC转换器是需要对预分频器进行设置,该预分频器是对72MHz的内部时钟进行分频
虽然可以进行/2,/4,/6,/8分频,但是ADCCLK最大只能14MHz,所以只能使用/6,/8分频。
触发源可以是定时器或者是软件。
在转换完成后的数值需要去寄存器读取,但是寄存器为16位,但是读取的数值为12位,这里需要对12位的数值进行右对齐处理。
二,代码部分
进行ADC的几个步骤,基本是按照上面那3大模块进行的编程
1,开启RCC,包括ADC与GPIO
2,GPIO初始化,配置为模拟输入模式
3,配置连接模块
4,ADC转换器
5,开启ADC
6,校准,减少误差
校准不需要深入了解,知道怎么校准就可以了。
AD.c
#include "stm32f10x.h" // Device header
void AD_Init(void)
{
//开启时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE);//开启ADC1时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);//开启GPIOA时钟
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AIN;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
ADC_RegularChannelConfig(ADC1,ADC_Channel_0,1,ADC_SampleTime_55Cycles5);//选择规则组的输入通道
//初始化ADC
ADC_InitTypeDef ADC_InitStructure;
ADC_InitStructure.ADC_Mode=ADC_Mode_Independent;//选择独立工作模式
ADC_InitStructure.ADC_DataAlign=ADC_DataAlign_Right;//数据对齐方式:右对齐
ADC_InitStructure.ADC_ExternalTrigConv=ADC_ExternalTrigConv_None;//外部触发源选择软件触发
ADC_InitStructure.ADC_ContinuousConvMode=DISABLE;//连续转换模式或者单次模式
ADC_InitStructure.ADC_ScanConvMode=DISABLE;// 扫描模式或者非扫描模式
ADC_InitStructure.ADC_NbrOfChannel=1;//通道数目1~16;
ADC_Init(ADC1,&ADC_InitStructure);
RCC_ADCCLKConfig(RCC_PCLK2_Div6);//配置ADCCLK 6分频 72/6=12MHz
//开启ADC
ADC_Cmd(ADC1,ENABLE);
//校准
ADC_ResetCalibration(ADC1);//复位校准
while(ADC_GetResetCalibrationStatus(ADC1)==SET);//等待复位校准完成
ADC_StartCalibration(ADC1);//开始校准
while(ADC_GetCalibrationStatus(ADC1)==SET);//等待校准完成
}
uint16_t AD_GetValue(void)
{
ADC_SoftwareStartConvCmd(ADC1,ENABLE);
while(ADC_GetFlagStatus(ADC1,ADC_FLAG_EOC)==RESET);//规则组或注入组转换完成标志位
return ADC_GetConversionValue(ADC1);
}
main.c
#include "stm32f10x.h" // Device header
#include "Delay.h"
#include "LED.h"
#include "Key.h"
#include "Buzzer.h"
#include "OLED.h "
#include "AD.h"
uint16_t ADValue;
float Volatge;
int main()
{
OLED_Init();
AD_Init();
OLED_ShowString(1,1,"ADValue");
OLED_ShowString(2,1,"Volatge:0.00V");
while(1)
{
ADValue = AD_GetValue();
Volatge = (float)ADValue / 1241;
OLED_ShowNum(1,9,ADValue,4);
OLED_ShowNum(2,9,Volatge,1);//显示整数的部分
OLED_ShowNum(2,11,(uint16_t)(Volatge*100)%100,2);
Delay_ms(100);
}
}
三,总结
32算是小入门了,学起来轻松了不少。过几天更新电赛单片机MSP430。
边栏推荐
- Multi camera stereo calibration
- UVA11294-Wedding(2-SAT)
- 二叉树递归套路总结
- Sum of two numbers, sum of three numbers (sort + double pointer)
- Common static methods of math class
- UVA – 11637 garbage remembering exam (combination + possibility)
- Selenium+pytest automated test framework practice
- Registration and skills of hoisting machinery command examination in 2022
- Data analysis - Thinking foreshadowing
- 派对的最大快乐值
猜你喜欢
TVS管 与 稳压二极管参数对比
There are 14 God note taking methods. Just choose one move to improve your learning and work efficiency by 100 times!
Go语言实现原理——Map实现原理
Non rigid / flexible point cloud ICP registration
Live tiktok shop 2022 latest gameplay card slot overseas live e-commerce new traffic
February 13, 2022-4-symmetric binary tree
Brushless drive design -- on MOS drive circuit
Using LNMP to build WordPress sites
Three. JS VR house viewing
基于脉冲神经网络的物体检测
随机推荐
Spécifications techniques et lignes directrices pour la sélection des tubes TVS et ESD - Recommandation de jialichuang
698. 划分为k个相等的子集 ●●
Alibaba Tianchi SQL training camp task4 learning notes
11gR2 Database Services for "Policy" and "Administrator" Managed Databases (文件 I
Basic knowledge of database (interview)
February 13, 2022 -5- maximum depth of binary tree
[original] what is the core of programmer team management?
UVA – 11637 garbage remembering exam (combination + possibility)
Switching power supply buck circuit CCM and DCM working mode
Technical specifications and model selection guidelines for TVs tubes and ESD tubes - recommended by jialichuang
Hainan Nuanshen tea recruits warmhearted people: recruitment of the product experience recommender of Nuanshen multi bubble honey orchid single cluster
Why use weak pointers for delegation- Why use weak pointer for delegation?
npm ELECTRON_ Mirror is set as domestic source (npmmirror China mirror)
Three. JS VR house viewing
yate. conf
Rethinking about MySQL query optimization
MySQL (2) -- simple query, conditional query
数据库基础知识(面试)
How to design API return code (error code)?
Summary of binary tree recursive routines