当前位置:网站首页>(16) ADC conversion experiment
(16) ADC conversion experiment
2022-07-01 17:10:00 【I love notes】
This section is mainly about reviewing ADC The corresponding content of , Our chapter adopts a AD chip xpt2046 To read the change of external voltage , Display the digital quantity of voltage on the nixie tube .
About ADC:
We all know that there are digital quantities inside the MCU , Namely 1 perhaps 0, And our current and voltage are analog quantities when they are transmitted , That is, the analog quantity is likely to be a dynamic continuous voltage , If we directly put this voltage on the MCU , Through our internal circuit transformation , More than how much voltage value is 1, Less than how much voltage is 0, But this does not indicate the specific size of the voltage value , So there is ADC To convert analog voltage value into multi digit digital voltage value , The more bits of voltage you convert , Explain that the more accurate your transformation . So for a ADC For example, there are several important indicators .1 It's resolution : Resolution is how many bits I use to express the output voltage ,ADC The resolution of is generally 8 Bits or 12 position , So 8 Bit and 12 Bit examples are converted at the same time 10v Voltage example , When we are 12 When a :
We say its resolution is 12 position , Each scale of it is 2.4mV, And when we are 8 When a :
At this time, we say its resolution is 8 position , Each scale of it is 39mV, It clearly verifies the previous conclusion ,12 Bit resolution is much higher .2 Is the conversion error , The conversion error we are talking about is half of our minimum scale , such as 12 Bit conversion error in conversion 10V The conversion error of time is 1.2mV.3 Is the conversion rate , That is, the number of conversions per second , And finish once A/D Time required for conversion , Is the reciprocal of the conversion rate .
About ADC transformation
ADC The conversion is mainly divided into three steps , They are sampling , Quantization and coding . Sampling is mainly about explaining the sampling principle , The sampling principle mainly means that the sampling frequency must be of the signal frequency 2 times , But the sampling frequency should not be too large , Too large will lead to too many sampling points , The sampling time is too long , The ideal sampling frequency should be the signal frequency 3 To 5 times . And about quantification , We all know that changes in numbers are not continuous , So the size of any numerical quantity , Are expressed as integer multiples of a minimum unit , Therefore, when using digital quantity to express voltage , It must also be converted into an integral multiple of the smallest unit , This process is quantification . We express each sampling value of a continuous voltage as an integral multiple of the smallest unit , And the integer multiple value is encoded in binary , Then it's coding , This binary signal is us AD Output value .
Hardware :
XPT2046 It is a four wire resistive touch screen , It contains 12 Bit resolution AD converter ,XPT2046 There are many functions , But in this section, we mainly focus on the use of voltage detection , my 51 There is a sliding rheostat on the MCU , We can change the input voltage by adjusting the voltage value of the sliding rheostat , The figure is shown below :
Among them AIN0 That is, with our XPT2046 The input pin of is connected , About XPT2046 The hardware diagram of is as follows
X,Y,Z,VBAT,AUX Enter after selecting through the on-chip control register ADC,ADC It can be configured as single ended or differential mode , choice VBAT,AUX perhaps X,Z Should be configured in single ended mode . As a touch screen application , You should choose differential mode , We can get it through the manual. We need to put A2,A1,A0 Configure to 001, Whole XPT2046 The sequence diagram of is as follows :
Among them 7 The meaning of is as follows :
We choose bit 7 by 1
From the above list, we can know that the configuration signal we want to send is 0x94.
Software :
It's about XPT2046 The procedure is as follows , Although our voltage value is 5V, But we don't need to calculate the actual value of the voltage , We just need to get the actual number of copies , As for the follow-up treatment, we won't deal with it for the time being . It's about XPT2046 The reading and writing procedures are as follows :
#include "xpt2046.h"
#include <intrins.h>
void delay_us(unsigned int xus){
while(xus--);
}
unsigned int spi_read(){
int i;
unsigned int dat = 0;
DCLK = 0;
for(i = 0;i < 12;i++){
dat <<= 1;
DCLK = 1;
DCLK = 0;
dat |= DOUT;
}
return dat;
}
void spi_write(unsigned char dat){
int i;
DCLK = 0;
for(i = 0;i < 8;i++){
DIN= (dat >> 7) & 0x01;
dat <<= 1;
DCLK = 0;
DCLK = 1;
}
}
unsigned int read_data(unsigned char cmd){
unsigned int val;
int i;
DCLK = 0;
CS= 0;
spi_write(cmd);
for(i = 0;i < 6;i++);
DCLK = 1;
_nop_();
_nop_();
DCLK = 0;
_nop_();
_nop_();
val = spi_read();
CS = 1;
return val;
}
The procedure about the nixie tube is as follows :
#include "key.h"
unsigned char smgduan[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
void key_scan(unsigned int val){
int i;
unsigned char table[4];
for(i = 0;i < 4;i++){
table[i] = val % 10;
val /= 10;
}
for(i = 0;i < 4;i++){
switch(i){
case 0:
LSC = 0; LSB = 0; LSA = 0;
break;
case 1:
LSC = 0; LSB = 0; LSA = 1;
break;
case 2:
LSC = 0; LSB = 1; LSA = 0;
break;
case 3:
LSC = 0; LSB = 1; LSA = 1;
break;
}
P0 = smgduan[table[i]];
delay_us(100);
}
}
The procedure of the main function is as follows :
#include "reg52.h"
#include "xpt2046.h"
#include "key.h"
void main(){
unsigned char cmd =0x94;
unsigned int val;
int i = 0;
while(1){
if(i==50){
val = read_data(cmd);
i = 0;
}
key_scan(val);
i++;
}
}
The code of the two header functions is as follows :
#ifndef _XPT_2046_H_
#define _XPT_2046_H_
#include "reg52.h"
sbit DIN = P3^4;
sbit DOUT = P3^7;
sbit CS = P3^5;
sbit DCLK = P3^6;
unsigned int spi_read();
void delay_us(unsigned int xus);
unsigned int read_data(unsigned char cmd);
#endif
#ifndef _KEY_H_
#define _KEY_H_
#include "reg52.h"
#include "xpt2046.h"
sbit LSA = P2^2;
sbit LSB = P2^3;
sbit LSC = P2^4;
void key_scan(unsigned int val);
#endif
This completes XPT2046 Programming for .
边栏推荐
- 判断二叉树是否为二叉搜索树
- Research and investment strategy report of China's sodium sulfate industry (2022 Edition)
- Introduction to software engineering - Chapter 6 - detailed design
- 换掉UUID,NanoID更快更安全!
- 中国一次性卫生用品生产设备行业深度调研报告(2022版)
- 判断一棵二叉树是否为平衡二叉树
- Basic usage of Frida
- In aks, use secret in CSI driver mount key vault
- vulnhub靶场-hacksudo - Thor
- ShenYu 网关开发:在本地启用运行
猜你喜欢
多线程并发之CountDownLatch阻塞等待
Why should you consider using prism
走进微信小程序
Leetcode records - sort -215, 347, 451, 75
[mathematical modeling] [matlab] implementation of two-dimensional rectangular packing code
Internet News: "20220222" get together to get licenses; Many products of Jimi have been affirmed by consumers; Starbucks was fined for using expired ingredients in two stores
Redis 分布式锁
[live broadcast appointment] database obcp certification comprehensive upgrade open class
National Security Agency (NSA) "sour Fox" vulnerability attack weapon platform technical analysis report
Oom caused by improper use of multithreading
随机推荐
如何使用 etcd 实现分布式 /etc 目录
Cookies and session keeping technology
[Supplément linguistique c] déterminer quel jour est demain (date de demain)
存在安全隐患 起亚召回部分K3新能源
Today, at 14:00, 15 ICLR speakers from Hong Kong University, Beihang, Yale, Tsinghua University, Canada, etc. continue!
SystemVerilog structure (II)
中国冰淇淋市场深度评估及发展趋势预测报告(2022版)
【Try to Hack】vulnhub DC4
多线程并发之CountDownLatch阻塞等待
GameFramework食用指南
整形数组合并【JS】
判断二叉树是否为二叉搜索树
There is a new breakthrough in quantum field: the duration of quantum state can exceed 5 seconds
中国酶制剂市场预测与投资战略研究报告(2022版)
(12) About time-consuming printing
Pytest learning notes (13) -allure of allure Description () and @allure title()
拼接字符串,得到字典序最小的结果
智能运维实战:银行业务流程及单笔交易追踪
阿里云、追一科技抢滩对话式AI
Mysql database - Advanced SQL statement (2)