继续像昨天一样, 今天也用TX-B开发板参照模数转换ADC0804的应用中的实验要求和源代码, 自己也改进了一些代码.
ADC0804与单片机的连接电路图:
![](https://www.cnblogs.com/images/cnblogs_com/fengmk2/ADC0804.jpg)
6位数码管与单片机的连接电路图
![](https://www.cnblogs.com/images/cnblogs_com/fengmk2/scm2.gif)
数码管动态扫描显示的原理:就六位数码管显示123456举例说明如下:先让第一个数码管显示1,其余的全部不亮,1大约亮几毫秒,然后熄灭,紧接着立即让第二个数码管显示2,其余的全部不亮,2同样亮几毫秒,依次这样亮到第六个数码管,然后再回来显示1,如此这样以很快的速度不断循环下去,由于人眼的视觉暂留时间大约为20毫秒左右,所以是感觉不出有不亮的数码管存在的,看见的是六个数码管同时在显示,数值是123456,如果我们把这个过程一点点放慢,看见的是从第一个数码管显1,然后移到第二个再显2,。。。也就是说在任一时刻只有一位数码管是亮的。
ADC0804: ADC0804是8位全MOS中速A/D 转换器、它是逐次逼近式A/D 转换器,片内有三态数据输出锁存器,可以和单片机直接接口。单通道输入,转换时间大约为100us。ADC0804 转换时序是:当CS=0 许可进行A/D 转换。WR由低到高时,A/D开始转换,一次转换一共需要66-73 个时钟周期。CS与WR同时有效时启动A/D转换,转换结束产生INTR 信号(低电平有效),可供查询或者中断信号。在CS和RD 的控制下可以读取数据结果。本实验没有使用INTR信号。
源代码:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
ADC0804
1
#include <reg51.H>
2
//读取通过ADC0804将模拟量转换成数字量, 并显示相应的数值
3
#include <intrins.H> //要使用到_nop_();函数, 延时一个空操作时间
4![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
#define uchar unsigned char
6![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
7
unsigned char j, k;
8
//延时函数, 例i=10,则大概延时10ms.
9
void delay(unsigned char i)
10![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
11
for(j = i; j > 0; j--)
12![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
13
for(k = 125; k > 0; k--);
14
}
15
}
16![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
17![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
unsigned char code table[]=
{0x3f,0x06,0x5b,0x4f,0x66,
18
0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};//0-F数码管的编码(共阴极)
19![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
20
//控制数码管段选的锁存器锁存端 下降沿过后锁存,即先令dula = 1,再令dula=0,完成锁存
21
sbit dula=P2^6;
22
//控制数码管位选的锁存器锁存端 过程如dula
23
sbit wela=P2^7;
24
sbit adrd = P3^7; //一个上升沿脉冲, 打开读取ADC
25
sbit adwr = P3^6; //一个上升沿脉冲, 启动ADC
26![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
uchar ds[] =
{0, 0, 0}; //存放转换结果
27
sbit adcs = P0^7; //ADC的片选, 低电平有效
28![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
29
//datas[0]百位数字,datas[1]十位, datas[2]个位
30
void display(uchar datas[])
31![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
32
uchar count;
33
for(count = 0; count < 3; count++)
34![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
35
//片选
36
wela = 0;
37
P0 = ((0xfe << count) | (0xfe >> (8 - count))) & 0x7f; //选择第(count + 1) 个数码管
38
wela = 1; //打开锁存, 给它一个下降沿量
39
wela = 0;
40
//段选
41
dula = 0;
42
P0 = table[datas[count]]; //显示数字
43
dula = 1; //打开锁存, 给它一个下降沿量
44
dula = 0;
45
delay(5); //延时5ms, 即亮5ms
46![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
47
//清除段先, 让数码管灭, 去除对下一位的影响, 去掉高位对低位重影
48
//若想知道影响效果如何, 可自行去掉此段代码
49
//因为数码管是共阴极的, 所有灭的代码为: 00H
50
dula = 0;
51
P0 = 0x00; //显示数字
52
dula = 1; //打开锁存, 给它一个下降沿量
53
dula = 0;
54
}
55
}
56![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
57
//启动ADC
58
void ADStart()
59![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
60
adcs = 0; //选中ADC
61
_nop_();
62
adwr = 0;
63
_nop_();
64
adwr = 1; //一个上升沿脉冲
65
_nop_();
66
adcs = 1;
67
}
68![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
69
//读ADC
70
uchar ADRead()
71![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
72
uchar tmp;
73
adcs = 0; //选中ADC
74
_nop_();
75
adrd = 0; //准备读取
76
_nop_();
77
_nop_();
78
tmp = P1; //读取ADC数据
79
adrd = 1;
80
_nop_();
81
adcs = 1;
82
return tmp;
83
}
84![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
85
void main()
86![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
87
uchar advalue, icount;
88
89
while(1)
90![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
91
//启动AD转换
92
ADStart();
93
//读AD
94
advalue = ADRead();
95
96
ds[0] = advalue / 100; //百位
97
ds[1] = (advalue % 100) / 10; //十位
98
ds[2] = advalue % 10; //个位
99
//循环显示五十次, 再去采样电压, 留足够的时间让ADC0804完成模数转换
100
for(icount = 0; icount < 50; icount++)
101![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
102
display(ds);
103
}
104
}
105
}
特别对于数码管显示函数display()
1
//datas[0]百位数字,datas[1]十位, datas[2]个位
2
void display(uchar datas[])
3![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
4
uchar count;
5
for(count = 0; count < 3; count++)
6![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
7
//片选
8
wela = 0;
9
P0 = ((0xfe << count) | (0xfe >> (8 - count))) & 0x7f; //选择第(count + 1) 个数码管
10
wela = 1; //打开锁存, 给它一个下降沿量
11
wela = 0;
12
//段选
13
dula = 0;
14
P0 = table[datas[count]]; //显示数字
15
dula = 1; //打开锁存, 给它一个下降沿量
16
dula = 0;
17
delay(5); //延时5ms, 即亮5ms
18![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
//清除段先, 让数码管灭, 去除对下一位的影响, 去掉高位对低位重影
20
//若想知道影响效果如何, 可自行去掉此段代码
21
//因为数码管是共阴极的, 所有灭的代码为: 00H
22
dula = 0;
23
P0 = 0x00; //显示数字
24
dula = 1; //打开锁存, 给它一个下降沿量
25
dula = 0;
26
}
27
}
若去掉将数码灭掉的那4行代码, 显示效果将如下:
![](https://www.cnblogs.com/images/cnblogs_com/fengmk2/adc0804Result1.jpg)
而最理想的效果应如下:
![](https://www.cnblogs.com/images/cnblogs_com/fengmk2/adc0804result2.jpg)
这是因为74HC573的锁存, 上一位的结果被锁存了, 在片选后, 也被显示在选中的位上.
只要在片选前将锁存的结果清除, 即可消除这个影响.