一 电路
二 数码管静态显示工作原理
三 代码
代码1 实现第一个数码管显示数字6的功能
#include "reg52.h"unsigned char code SMG_Duanma[18] = {0xc0,0xf9.0xa4,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x80,0xc0,0x86,0x8e,0xbf,0x7f};void Delay(unsignde int t)
{while(t--);while(t--);
}//实现延时功能的函数void InitHC138(unsigned char n)
{switch (n){ case 4;HC138_C = 1;HC138_B = 0;HC138_A = 0;//输出Y4break;case 5;HC138_C = 1;HC138_B = 0;HC138_A = 1;//输出Y5break;
case 4;HC138_C = 1;HC138_B = 1;HC138_A = 0;//输出Y6break;case 5;HC138_C = 1;HC138_B = 1;HC138_A = 1;//输出Y7break;}
}void shumaguan()
{InitHC138(6);P0 = 0x01;InitHC138(7);P0 = 0x82;
}//实现第一个数码管,显示数字6的函数功能void main()
{shumaguan();while(1){}
}
代码2:实现数码管由数字0显示到数字9,并逐个进行。然后,数码管的内容,由0到f循环显示的功能。
#include "reg52.h"unsigned char code SMG_Duanma[18] = {0xc0,0xf9.0xa4,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x80,0xc0,0x86,0x8e,0xbf,0x7f};void Delay(unsignde int t)
{while(t--);while(t--);
}//实现延时功能的函数void InitHC138(unsigned char n)
{switch (n){ case 4;HC138_C = 1;HC138_B = 0;HC138_A = 0;//输出Y4break;case 5;HC138_C = 1;HC138_B = 0;HC138_A = 1;//输出Y5break;
case 4;HC138_C = 1;HC138_B = 1;HC138_A = 0;//输出Y6break;case 5;HC138_C = 1;HC138_B = 1;HC138_A = 1;//输出Y7break;}
}void shumaguan_bit(unsigned char dat, unsigned pos)
{InitHC138(6);P0 = 0x01<<pos;//数码管的位置InitHC138(7);P0 = dat;//数码管的内容
}void shumaguan()
{unsigned char i;for(i = 0 ; i < 8; i++){for(j = 0; j < 10 ; j++){shumaguan_bit(SMG_Duanma[j],i);Delay(60000);}}//实现了数码管由数字0显示到数字9,并逐个进行的功能for(j = 0; j < 16 ; j++){InitHC138(6);P0 = 0xff;//数码管全选InitHC138(7);P0 = SMG_Duanma[j];//数码管的内容,由0到f循环显示}
}void main()
{while(1){shumaguan();}
}