51单片机利用74HC595驱动数码管
扫描二维码
随时随地手机看文章
基于51单片机利用八个数码管花样显示如下:
xxx11xxx→xx2222xx→x333333x→44444444→x555555x→xx6666xx→x777777x→88888888
每个状态各一秒,显示反复循环,其中x表示对应的数码管熄灭
问题补充:是共阳极的数码管,利用595芯片驱动的。采用C语言。
===================================================
最佳答案:程序设计完毕,采用PROTEUS仿真截图如下。
程序如下:
//=============================================
#include
#define uint unsigned int
#define uchar unsigned char
//HC595芯片
sbit H_ds = P2^0;
sbit H_shcp = P2^1;
sbit H_stcp = P2^2;
char code DISP_8[] = {
0x00, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f};
char code TAB_64[] = {
0,0,0,1,1,0,0,0, 0,0,2,2,2,2,0,0,
0,3,3,3,3,3,3,0, 4,4,4,4,4,4,4,4,
0,5,5,5,5,5,5,0, 0,0,6,6,6,6,0,0,
0,7,7,7,7,7,7,0, 8,8,8,8,8,8,8,8};
char i = 1, j = 0;
char DISP_B[] = {0,0,0,0,0,0,0,0};
//------------------------------------------------------------------
void sendbyte_H(uchar aa) //利用HC595输出
{
char z;
aa = ~aa;
for(z = 0; z < 8; z++) { //循环8次移入数据
H_shcp = 0;
H_ds = aa & 128; //数据位送到HC595数据线
H_shcp = 1; //上升沿输入数据
aa <<= 1;
}
}
//------------------------------------------------------------------
void display()
{
char z;
for(z = 0; z < 8; z++) DISP_B[z] = TAB_64[j * 8 + z];
j++; j %= 8;
for(z = 0; z < 8; z++) sendbyte_H(DISP_8[DISP_B[z]]);
H_stcp = 0; H_stcp = 1; //上升沿使数据并行输出
}
//------------------------------------------------------------------
void main()
{
TMOD = 0x01;
TH0 = (65536 - 46080) / 256;//50ms@11.0592MHz
TL0 = (65536 - 46080) % 256;
TR0 = 1;
ET0 = 1;
EA = 1;
while (1);
}
//------------------------------------------------------------------
void T0_time()interrupt 1
{
TL0 = (65536 - 46080) % 256; //重新设置初始值
TH0 = (65536 - 46080) / 256;//50ms@11.0592MHz
i--;
if (i == 0) {
i = 40;
display(); //花样显示
}
}
//------------------------------------------------------------------