专注电子技术学习与研究
当前位置:单片机教程网 >> MCU设计实例 >> 浏览文章

自学单片机编程之数码管显示

作者:佚名   来源:本站原创   点击数:  更新时间:2014年08月18日   【字体:

 对于我以前学习过的东西以周记的形式进行总结整理!

当我对于流水灯和一些基础的C语言知识后,我们就要和数码管打交道了,数码管是显示数字的一种媒介,简单的说就是八个二极管通过亮灭来显示数字符号,是多个二极管更高级的表达方式。
现在我们用的基本都是共阴或者共阳数码管,就是八个发光二极管的阴极或阳极在数码管内部连在一起,我们只需要用控制八个二极管的引脚来控制每个二极管的电压高低来控制二极管的亮灭,从而显示出不同的数字或符号。注意的共阴数码管是高电平点亮,共阳数码管是低电平点亮。(主要是模电知识:发光二极管原理)
首先学会控制一个数码管
P0是控制数码管内部8个发光二极管亮灭的I/O控制口

上面unsigned  char  code   table【】我之前没有搞注释,现在解释一下unsigned  char是数组类型 code是编码的意识  table是数组名    【】里的是显示数字的控制发光二极管的编码从左到右分别是123456789ABCDEF

会了一个数码管之后就要弄八个数码管或者六个数码管甚至更多 我就举最有代表的时钟六位数码管显示。当数码一多,就要分别控制每个数码管  这就要用到另一个I/O控制口P2,控制方法和数码管一样,就是把六个数码管看成是六个二极管,通过控制共阴或者共阳端的电位高低来控制数码管的亮灭!

#include<reg51.h>                                                          //51系列单片机头文件

#define     uchar        unsigned  char                    //宏定义

uchar        code          table []={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};

/***************************数码管扫描延时****************************/

void  delays(void)

{

         uint  i,j;

                   for(i=0;i<2;i++)

                            for(j=0;j<110;j++);

}

void  main(void)

{

P0=table [1];

                   P2=0xfd;

                   delays();                                                                                      

                            P0=table [2];

                            P2=0xfe;

                            delays();                                                                             

 

                                     P0=table [3];

                                     P2=0xf7;

                                     delays();                                                                   

                                              

                                               P0=table [4];

                                               P2=0xfb;

                                               delays();                                                          

                                              

                                                        P0=table [5];

                                                        P2=0xdf;

                                                        delays();                                                                                                         

                                                                 P0=table [6];

                                                                 P2=0xef;

                                                                 delays();  

while1

}              显示的效果就是012345这是静态显示  如果是你想要动态显示在上面的程序里用一个for循环语句table【】内用字母或简单运算表示

   简单举例:

#include<reg51.h>                                                          //51系列单片机头文件

#define     uchar        unsigned  char                    //宏定义

uchar        code          table []={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};

/***************************数码管扫描延时****************************/

void  delays(void)

{

         uint  i,j;

                   for(i=0;i<2;i++)

                            for(j=0;j<110;j++);

}

void  main( )

{   uchar   i

    while1//无限循环语句

{

Fori=0;i<9;i++//循环语句

{       ifi==9  //选择语句

       {

          i=0;

       }

  P0=table [i];

                   P2=0xfd;

                   delays();                                                                                      

                            P0=table [i];

                            P2=0xfe;

                            delays();                                                                             

 

                                     P0=table [i];

                                     P2=0xf7;

                                     delays();                                                                   

                                              

                                               P0=table [i];

                                               P2=0xfb;

                                               delays();                                                          

                                              

                                                        P0=table [i];

                                                        P2=0xdf;

                                                        delays();                                                                                                         

                                                                 P0=table [i];

                                                                 P2=0xef;

                                                                 delays();  

}

}

}      显示效果是不断循环显示000000111111222222~~~999999当然也可以显示不一样的,原理都一样 参数改变   显示效果也不一样!

关闭窗口