打印
[技术讨论]

51单片机初级-数码管 这里有你不知道的知识

[复制链接]
1388|1
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
感动|  楼主 | 2018-6-6 11:47 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 感动 于 2018-6-6 13:17 编辑

数码管的一种是半导体发光器件,数码管可分为七段数码管和八段数码管,区别在于八段数码管比七段数码管多一个用于显示小数点的发光二极管单元DP(decimal point),其基本单元是发光二极管。数码管是一类价格便宜使用简单,通过对其不同的管脚输入相对的电流,使其发亮,从而显示出数字能够显示 时间、日期、温度等所有可用数字表示的参数的器件。
在电器特别是家电领域应用极为广泛,如显示屏、空调、热水器、冰箱等等。绝大多数热水器用的都是数码管,其他家电也用液晶屏与荧光屏。
驱动方式

直流驱动
是指每个数码管的每一个段码都由一个单片机的I/O端口进行驱动,或者使用如BCD码二-十进制译码器译码进行驱动。优点是编程简单,显示亮度高,缺点是占用I/O端口多。
显示驱动
是将所有数码管通过分时轮流控制各个数码管的的COM端,就使各个数码管轮流受控显示。将所有数码管的8个显示笔划"a,b,c,d,e,f,g,dp"的同名端连在一起,另外为每个数码管的公共极COM增加位选通控制电路,位选通由各自独立的I/O线控制,当单片机输出字形码时,所有数码管都接收到相同的字形码,但究竟是那个数码管会显示出字形,取决于单片机对位选通COM端电路的控制,所以我们只要将需要显示的数码管的选通控制打开,该位就显示出字形,没有选通的数码管就不会亮。


我们经常要用多只数码管,如果每个数码管都要单独直接连到单片机,单片机管教数量会不够用,实际工程中常把公共端单独连接,其余管教并联。尽管如此还是会占用不少管脚,所以工程上用串并变换芯片扩展单片机IO,如下图所示

file:///C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\msohtmlclip1\01\clip_image002.jpg 4HC595D芯片引脚图与功能

file:///C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\msohtmlclip1\01\clip_image003.png74HC595的数据端:
QA--QH: 八位并行输出端,可以直接控制数码管的8个段。
QH': 级联输出端。我将它接下一个595的SI端。
SI: 串行数据输入端。74hc595的控制端说明: /SCLR(10脚): 低电平时将移位寄存器的数据清零。通常我将它接Vcc。 SCK(11脚):上升沿时数据寄存器的数据移位。QA-->QB-->QC-->...-->QH;下降沿移位寄存器数据不变。(脉冲宽度:5V时,大于几十纳秒就行了。我通常都选微秒级)
   控制移位寄存器        SCK 上升沿 数据  移位       SCK 下降沿 数据  保持  RCK(12脚):上升沿时移位寄存器的数据进入存储寄存器,下降沿时存储寄存器数据不变。通常我将RCK置为低电平,当移位结束后,在RCK端产生一个正脉冲(5V时,大于几十纳秒就行了。我通常都选微秒级),更新显示数据。
   控制存储寄存器       RCK 上升沿 移位寄存器 的数据进入 存储寄存器       RCK  下降沿 存储寄存器数据不变   /G(13脚): 高电平时禁止输出(高阻态)。如果单片机的引脚不紧张,用一个引脚控制它,可以方便地产生闪烁和熄灭效果。比通过数据端移位控制要省时省力。 注:74164和74595功能相仿,都是8位串行输入转并行输出移位寄存器。74164的驱动电流(25mA)比74595(35mA)的要小,14脚封装,体积也小一些。 74595的主要优点是具有数据存储寄存器,在移位的过程中,输出端的数据可以保持不变。这在串行速度慢的场合很有用处,数码管没有闪烁感。
程序.rar (748 Bytes)
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
sbit Input_Clock=P2^2;//上升沿有效-将SER上的数据移入74HC595D的移位寄存器中
sbit Output_Clock=P2^1;//上升沿有效-将移位寄存器中的数据送入到输出锁存器中
sbit Input_Data=P2^0;//串行数据输入
sbit P35=P3^5;
void delay_ns(uint ns)
{
        uint a,b;
        for(a=0;a<ns;a++)
                for(b=0;b<3000;b++);
}

void main()
{
        //1
        uint Reference_Level=0x6666;        
        uint i,aa,bb;
        Output_Clock=0;
               
        while(1)
                {
                for(aa=0;aa<5;aa++)
                        {        
                        Reference_Level=0x9F80;
                for(i=0;i<16;i++)
                        {
                                if(Reference_Level&0x0001==0x0001)
                                        {
                                                Input_Clock=0;
                                                delay_ns(1);
                                                Input_Data=1;
                                                Input_Clock=1;
                                                P35=0;
                                                delay_ns(1);
                                                
                                                
                                        }else{
                                                Input_Clock=0;
                                                delay_ns(1);
                                                Input_Data=0;
                                                Input_Clock=1;
                                                delay_ns(1);
                                                P35=1;
                                                
                                        }
                                Reference_Level=Reference_Level>>1;                        
                        }
                Output_Clock=1;
                delay_ns(1);
                Output_Clock=0;
                        
                //2
                 Reference_Level=0x2540;
                for(i=0;i<16;i++)
                        {
                                if(Reference_Level&0x0001==0x0001)
                                        {
                                                Input_Clock=0;
                                                delay_ns(1);
                                                Input_Data=1;
                                                Input_Clock=1;
                                                P35=0;
                                                delay_ns(1);
                                        }else{
                                                Input_Clock=0;
                                                delay_ns(1);
                                                Input_Data=0;
                                                Input_Clock=1;
                                                P35=1;
                                                delay_ns(1);                        
                                        }
                                Reference_Level=Reference_Level>>1;                        
                        }
                        Output_Clock=1;
                        delay_ns(1);
                        Output_Clock=0;
               
                //3
                 Reference_Level=0x0D20;
                for(i=0;i<16;i++)
                        {
                                if(Reference_Level&0x0001==0x0001)
                                        {
                                                Input_Clock=0;
                                                delay_ns(1);
                                                Input_Data=1;
                                                Input_Clock=1;
                                                P35=0;
                                                delay_ns(1);
                                        }else{
                                                Input_Clock=0;
                                                delay_ns(1);
                                                Input_Data=0;
                                                Input_Clock=1;
                                                P35=1;
                                                delay_ns(1);                        
                                        }
                                Reference_Level=Reference_Level>>1;                        
                        }
                        Output_Clock=1;
                        delay_ns(1);
                        Output_Clock=0;
                        
                        //4
                 Reference_Level=0x9910;
                for(i=0;i<16;i++)
                        {
                                if(Reference_Level&0x0001==0x0001)
                                        {
                                                Input_Clock=0;
                                                delay_ns(1);
                                                Input_Data=1;
                                                Input_Clock=1;
                                                P35=0;
                                                delay_ns(1);
                                        }else{
                                                Input_Clock=0;
                                                delay_ns(0);
                                                Input_Data=0;
                                                Input_Clock=1;
                                                P35=1;
                                                delay_ns(1);                        
                                        }
                                Reference_Level=Reference_Level>>1;                        
                        }
                        Output_Clock=1;
                        delay_ns(1);
                        Output_Clock=0;
                        
                //5
                 Reference_Level=0x4908;
                for(i=0;i<16;i++)
                        {
                                if(Reference_Level&0x0001==0x0001)
                                        {
                                                Input_Clock=0;
                                                delay_ns(1);
                                                Input_Data=1;
                                                Input_Clock=1;
                                                P35=0;
                                                delay_ns(1);
                                        }else{
                                                Input_Clock=0;
                                                delay_ns(1);
                                                Input_Data=0;
                                                Input_Clock=1;
                                                P35=1;
                                                delay_ns(1);                        
                                        }
                                Reference_Level=Reference_Level>>1;                        
                        }
                        Output_Clock=1;
                        delay_ns(1);
                        Output_Clock=0;        
                        
                //6
                Reference_Level=0x4104;
                for(i=0;i<16;i++)
                        {
                                if(Reference_Level&0x0001==0x0001)
                                        {
                                                Input_Clock=0;
                                                delay_ns(1);
                                                Input_Data=1;
                                                Input_Clock=1;
                                                P35=0;
                                                delay_ns(1);
                                        }else{
                                                Input_Clock=0;
                                                delay_ns(1);
                                                Input_Data=0;
                                                Input_Clock=1;
                                                P35=1;
                                                delay_ns(1);                        
                                        }
                                Reference_Level=Reference_Level>>1;                        
                        }
                        Output_Clock=1;
                        delay_ns(1);
                        Output_Clock=0;        
                        
                //7
                Reference_Level=0x1F02;
                for(i=0;i<16;i++)
                        {
                                if(Reference_Level&0x0001==0x0001)
                                        {
                                                Input_Clock=0;
                                                delay_ns(1);
                                                Input_Data=1;
                                                Input_Clock=1;
                                                P35=0;
                                                delay_ns(1);
                                        }else{
                                                Input_Clock=0;
                                                delay_ns(1);
                                                Input_Data=0;
                                                Input_Clock=1;
                                                P35=1;
                                                delay_ns(1);                        
                                        }
                                Reference_Level=Reference_Level>>1;                        
                        }
                        Output_Clock=1;
                        delay_ns(1);
                        Output_Clock=0;        
                        
                //8
                 Reference_Level=0x0101;
                for(i=0;i<16;i++)
                        {
                                if(Reference_Level&0x0001==0x0001)
                                        {
                                                Input_Clock=0;
                                                delay_ns(1);
                                                Input_Data=1;
                                                Input_Clock=1;
                                                P35=0;
                                                delay_ns(1);
                                        }else{
                                                Input_Clock=0;
                                                delay_ns(1);
                                                Input_Data=0;
                                                Input_Clock=1;
                                                P35=1;
                                                delay_ns(1);                        
                                        }
                                Reference_Level=Reference_Level>>1;                        
                        }
                        Output_Clock=1;
                        delay_ns(1);
                        Output_Clock=0;        
                        }
                        
                                Reference_Level=0x0000;
                for(i=0;i<16;i++)
                        {
                                if(Reference_Level&0x0001==0x0001)
                                        {
                                                Input_Clock=0;
                                                delay_ns(1);
                                                Input_Data=1;
                                                Input_Clock=1;
                                                P35=0;
                                                delay_ns(1);
                                        }else{
                                                Input_Clock=0;
                                                delay_ns(1);
                                                Input_Data=0;
                                                Input_Clock=1;
                                                P35=1;
                                                delay_ns(1);                        
                                        }
                                Reference_Level=Reference_Level>>1;                        
                        }
                        Output_Clock=1;
                        delay_ns(1);
                        Output_Clock=0;        
                        
                        
                        for(aa=0;aa<10;aa++)
                                        {
                                                P35=0;
                                                P0=0xfe;
                                                for(bb=0;bb<=7;bb++)
                                                        {
                                                                P35=1;
                                                                if(bb==8)
                                                                {
                                                                        P0=0xfe;
                                                                }
                                                                delay_ns(10);
                                                                P0=P0<<1;
                                                               
                                                        }
                                        }
                                        P0=0xff;
                }
}
这是完整程序欢迎下载

相关帖子

沙发
感动|  楼主 | 2018-6-10 15:29 | 只看该作者
最近发现很多人对595不是很清楚,看不懂数据手册说的是什么意思。确实,有些东西无法用文字描述清楚,能看懂的都是有些基础的。比如用文字描述圆形,看似简单,让所有人都明白也是很难的,不信你自己就试试吧,记住是让所以人明白,智商正常的人。    看不懂没关系,也许找张纸画一下就明白了,一张图说明很多问题,也许是理科出身的原因吧。言归正传,看不懂595的说明没关系,上图吧!

输入有5各脚,其中10脚复位可以不理他,给他固定电平就好。13脚是输出使能,我们使能,给他固定电平。剩下三个脚有用了,14脚是数据输入,11脚是上升沿数据移位,12脚是锁存输出。第9脚可以做级联,给下一个芯片的数据输入。

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

35

主题

57

帖子

4

粉丝