当前位置:网站首页>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。
边栏推荐
- 【LeetCode】5. Valid Palindrome·有效回文
- regular expression
- Summary of binary tree recursive routines
- MySQL replace primary key delete primary key add primary key
- Attacking technology Er - Automation
- Initial experience | purchase and activate typora software
- 成为程序员的你,后悔了吗?
- golang代码检查工具
- Multi view 3D reconstruction
- Calculating the number of daffodils in C language
猜你喜欢
CorelDRAW plug-in -- GMS plug-in development -- new project -- macro recording -- VBA editing -- debugging skills -- CDR plug-in (2)
2: Chapter 1: understanding JVM specification 1: introduction to JVM;
Data analysis - Thinking foreshadowing
Realize reverse proxy client IP transparent transmission
Object detection based on impulse neural network
Creative mode 1 - single case mode
Neural structured learning - Part 2: training with natural graphs
Basic knowledge of database (interview)
CIS基准测试工具kube-bench使用
Alibaba Tianchi SQL training camp task4 learning notes
随机推荐
Commonly used probability distributions: Bernoulli distribution, binomial distribution, polynomial distribution, Gaussian distribution, exponential distribution, Laplace distribution and Dirac delta d
Use of metadata in golang grpc
CorelDRAW plug-in -- GMS plug-in development -- new project -- macro recording -- VBA editing -- debugging skills -- CDR plug-in (2)
UVA11294-Wedding(2-SAT)
Three. Js-01 getting started
Live tiktok shop 2022 latest gameplay card slot overseas live e-commerce new traffic
Expectation, variance and covariance
MySQL replace primary key delete primary key add primary key
Selenium+pytest automated test framework practice
Attacking technology Er - Automation
Southeast Asia e-commerce guide, how do sellers layout the Southeast Asia market?
Detailed explanation of pointer and array written test of C language
二叉树递归套路总结
两数之和、三数之和(排序+双指针)
Neural structured learning - Part 3: training with synthesized graphs
Xinyuan & Lichuang EDA training camp - brushless motor drive
From the perspective of quantitative genetics, why do you get the bride price when you get married
poj 2762 Going from u to v or from v to u? (推断它是否是一个薄弱环节图)
Différence entre hors bande et en bande
Leetcode sword finger offer brush questions - day 21