当前位置:网站首页>(十七)51单片机——AD/DA转换
(十七)51单片机——AD/DA转换
2022-08-05 00:52:00 【Meursault639】
目录
学习目标
今天我们需要学习的是AD/DA转换,也就是数字信号与模拟信号之间的转换。模拟信号转换为数字信号我们使用可调电阻、光敏电阻和热敏电阻来实现,在光敏电阻小于100时(较暗),D1灯会自动亮;而数字信号转换为模拟信号我们使用实现DA呼吸灯来展示。
成果展示
AD
DA
硬件知识
介绍
- AD(Analog to Digital):模拟-数字转换,将模拟信号转换为计算机可操作的数字信号。
- DA(Digital to Analog):数字-模拟转换,将计算机输出的数字信号转换为模拟信号。
- AD/DA转换打开了计算机与模拟信号的大门,极大的提高了计算机系统的应用范围,也为模拟信号数字化处理提供了可能。
硬件电路模型
AD:就是将电压量转换为数字量。
DA:其实有点类似于PWM,最后经过一个低通滤波器,就能实现效果了。
- AD转换通常有多个输入通道,用多路选择开关连接至AD转换器,以实现AD多路复用的目的,提高硬件利用率
- AD/DA与单片机数据传送可使用并口(速度快、原理简单),也可使用串口(接线少、使用方便)
- 可将AD/DA模块直接集成在单片机内,这样直接写入/读出寄存器就可进行AD/DA转换,单片机的IO口可直接复用为AD/DA的通道
电路
AD
这是一个简单的AD芯片(ADC0809),首先选择模拟开关,之后进行AD转换,再通过缓存器输出,就可以了。
DA
这是一个简单的DA芯片(ADC0832),首先输入数据,通过控制电路控制寄存器,最后通过一个DA转换器输出就成了。
运算放大器
- 运算放大器(简称“运放”)是具有很高放大倍数的放大电路单元。内部集成了差分放大器、电压放大器、功率放大器三级放大电路,是一个性能完备、功能强大的通用放大电路单元,由于其应用十分广泛,现已作为基本的电路元件出现在电路图中
- 运算放大器可构成的电路有:电压比较器、反相放大器、同相放大器、电压跟随器、加法器、积分器、微分器等
- 运算放大器电路的分析方法:虚短、虚断(负反馈条件下)
运放电路
电压比较器
这是电压比较器的作用,当+大于-的时候,输出VCC,当-大于+的时候,输出GDN。
反向放大器
通过分析虚短和虚段可以得到这个结论。
同向放大器
电压跟随器
DA原理
T型电阻网络DA转换器
这个的具体过程就不做详细介绍,我们只需要知道每个开关相当于每个位,到时候只需要知道哪些开关闭合了就能知道放大了多少倍。
PWM型DA转换器
这个的作用就是将交流滤掉,变成直流电压。
AD原理
逐次逼近型AD转换器
逐次逼近型AD转换器的原理就是通过DAC不断变换去和输入的电压进行比较,直到找到一个合适的电压,输出对应数字量就行。
AD/DA性能指标
- 分辨率:指AD/DA数字量的精细程度,通常用位数表示。例如,对于5V电源系统来说,8位的AD可将5V等分为256份,即数字量变化最小一个单位时,模拟量变化5V/256=0.01953125V,所以,8位AD的电压分辨率为0.01953125V,AD/DA的位数越高,分辨率就越高
- 转换速度:表示AD/DA的最大采样/建立频率,通常用转换频率或者转换时间来表示,对于采样/输出高速信号,应注意AD/DA的转换速度
XPT2046
时序
CS作为片选,用来选择不同的设备,然后是DCLK上升沿为发送,下降沿为读出,DOUT是读出的值。
代码
AD
// XRT2046.c
#include <REGX52.H>
#include "Delay.h"
//引脚定义
sbit XPY2046_DIN=P3^4;
sbit XPY2046_CS=P3^5;
sbit XPY2046_DCLK=P3^6;
sbit XPY2046_DOUT=P3^7;
/**
* @brief ZPT2046读取AD值
* @param Command 命令字,范围:头文件内定义的宏,结尾的数字表示转换的位数
* @retval AD转换后的数字量,范围:8位为0~255,12位为0~4095
*/
unsigned int XPT2046_ReadAD(unsigned char Command)
{
unsigned int ADValue = 0;
unsigned char i;
XPY2046_DCLK = 0;
XPY2046_CS = 0;
for(i=0;i<8;i++)
{
XPY2046_DIN=Command&(0x80>>i);
XPY2046_DCLK=1;
Delay(1);
XPY2046_DCLK=0;
}
for(i=0;i<16;i++)
{
XPY2046_DCLK=1;
Delay(1);
XPY2046_DCLK=0;
if(XPY2046_DOUT){ADValue|=(0x8000>>i);}
}
XPY2046_CS=1;
if(Command & 0x08){
return ADValue>>8;
}else
return ADValue>>4;
}
// main.c
#include <REGX52.H>
#include "LCD1602.h"
#include "Delay.h"
#include "XPT2046.h"
unsigned int ADValue1 = 0,ADValue2 = 0,ADValue3 = 0;
sbit LED = P2^0;
void main(){
LCD_Init();
LCD_ShowString(1,1,"ADD NTC GR");
while(1){
ADValue1 = XPT2046_ReadAD(XPT2046_XP_8);//读取AIN0,可调电阻
LCD_ShowNum(2,1 ,ADValue1,3);
ADValue2 = XPT2046_ReadAD(XPT2046_YP_8);//读取AIN1,热敏电阻
LCD_ShowNum(2,6 ,ADValue2,3);
ADValue3 = XPT2046_ReadAD(XPT2046_VBAT_8);//读取AIN2,光敏电阻
LCD_ShowNum(2,11 ,ADValue3,3);
if(ADValue3 < 100){// 光敏电阻小于100(较暗)就发光
LED = 0;
}else{
LED = 1;
}
}
}
DA
//main.c
#include <REGX52.H>
#include "Delay.h"
#include "Time0.h"
sbit DA = P2^1;
unsigned char Counter,Compare;//计数值和比较值,用于输出PWM
unsigned char i;
void main(){
Timer0_Init();
while(1){
for(i=0;i<90;i++)
{
Compare=i; //设置比较值,改变PWM占空比
Delay(10);
}
for(i=90;i>0;i--)
{
Compare=i; //设置比较值,改变PWM占空比
Delay(10);
}
}
}
void Timer0_Routine() interrupt 1
{
static unsigned int T0Count;
TL0 = 0xAD; //设置定时初始值
TH0 = 0xFF;
Counter++;
Counter%=90;
if(Counter < Compare){
DA = 1;
}else
DA = 0;
}
总结
本节学习的是DA和AD,其中DA比较重要,而AD一般都能用PWM来实现,希望对大家有所帮助,如果有错误也希望能及时指出,谢谢大家。
边栏推荐
- Binary tree [full solution] (C language)
- E - Many Operations (bitwise consideration + dp thought to record the result after the operation
- leetcode: 269. The Martian Dictionary
- 软件测试面试题:BIOS, Fat, IDE, Sata, SCSI, Ntfs windows NT?
- Software testing interview questions: the difference and connection between black box testing, white box testing, and unit testing, integration testing, system testing, and acceptance testing?
- Knowledge Points for Network Planning Designers' Morning Questions in November 2021 (Part 2)
- 2022 The Third J Question Journey
- 2022 Hangzhou Electric Power Multi-School Session 3 Question L Two Permutations
- Pytorch usage and tricks
- 软件测试面试题:软件都有多少种分类?
猜你喜欢
快速批量修改VOC格式数据集标签的文件名,即快速批量修改.xml文件名
Dynamic Programming/Knapsack Problem Summary/Summary - 01 Knapsack, Complete Knapsack
金九银十面试跳槽季;你准备好了吗?
Gartner Hype Cycle:超融合技术将在2年内到达“生产力成熟期”
JUC线程池(一): FutureTask使用
node uses redis
码率vs.分辨率,哪一个更重要?
深度学习训练前快速批量修改数据集中的图片名
Activity Recommendation | Kuaishou StreamLake Brand Launch Conference, witness together on August 10!
### Error querying database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionExcep
随机推荐
Software testing interview questions: test life cycle, the test process is divided into several stages, and the meaning of each stage and the method used?
活动推荐 | 快手StreamLake品牌发布会,8月10日一起见证!
ORA-01105 ORA-03175
2022 Multi-school Second Session K Question Link with Bracket Sequence I
FSAWS 的全球基础设施和网络
2022 Nioke Multi-School Training Session 2 J Question Link with Arithmetic Progression
2022牛客多校训练第二场 J题 Link with Arithmetic Progression
Software Testing Interview Questions: What do test cases usually include?
Lattice PCIe Learning 1
JWT简单介绍
软件基础的理论
软件测试面试题:软件都有多少种分类?
After the staged testing is complete, have you performed defect analysis?
Software test interview questions: BIOS, Fat, IDE, Sata, SCSI, Ntfs windows NT?
Software Testing Interview Questions: What aspects should be considered when designing test cases, i.e. what aspects should different test cases test against?
E - Many Operations (bitwise consideration + dp thought to record the result after the operation
Software testing interview questions: What is the difference between load testing, capacity testing, and strength testing?
2022 Hangzhou Electric Power Multi-School Session 3 K Question Taxi
Software Testing Interview Questions: About Automated Testing Tools?
"WEB Security Penetration Testing" (28) Burp Collaborator-dnslog out-band technology