51單片機(jī)+74HC595驅(qū)動(dòng)數(shù)碼管程序
完整的源碼和圖紙下載地址:http://www.51hei.com/bbs/dpj-20392-1.html
下面是51單片機(jī)驅(qū)動(dòng)74hc595芯片的程序:
#include //包含51單片機(jī)的頭文件
#include
#define uint unsigned int
#define uchar unsigned char
#include
#define uint unsigned int
#define uchar unsigned char
//sbit sin_595 =P1^0;
//sbit rclk_595 =P1^1;
//sbit sclk_595 =P1^2;
//sbit s_clr =P1^3;
sbit a_cp_595 =P2^4; //串行移位時(shí)鐘脈沖 sh_cp_595
sbit b_cp_595 =P2^3; //輸出鎖存器控制脈沖 st_cp_595
//sbit _oe_595 =P1^5; //8位并行輸出使能/禁止(高阻態(tài))
sbit ds_595 =P2^2; //串行數(shù)據(jù)輸入
extern uchar datas[6]; //存放6個(gè)數(shù)碼管的顯示數(shù)字
uchar ledcode[]={
0xC0,// 0
0xF9,// 1
0xA4,// 2
0xB0,// 3
0x99,// 4
0x92,// 5
0x82,// 6
0xF8,// 7
0x80,// 8
0x90,// 9
0x88,// A
0x83,// B
0xC6,// C
0xA1,// D
0x86,// E
0x8E// F
};
void delay(uint z)
{
uint t1,y;
for(t1=z;t1>0;t1--)
for(y=110;y>0;y--);
}
sbit b_cp_595 =P2^3; //輸出鎖存器控制脈沖 st_cp_595
//sbit _oe_595 =P1^5; //8位并行輸出使能/禁止(高阻態(tài))
sbit ds_595 =P2^2; //串行數(shù)據(jù)輸入
extern uchar datas[6]; //存放6個(gè)數(shù)碼管的顯示數(shù)字
uchar ledcode[]={
0xC0,// 0
0xF9,// 1
0xA4,// 2
0xB0,// 3
0x99,// 4
0x92,// 5
0x82,// 6
0xF8,// 7
0x80,// 8
0x90,// 9
0x88,// A
0x83,// B
0xC6,// C
0xA1,// D
0x86,// E
0x8E// F
};
void delay(uint z)
{
uint t1,y;
for(t1=z;t1>0;t1--)
for(y=110;y>0;y--);
}
void led_display(void)
{
uchar i,j;
bit testb;
uchar bdata movebit[6];
uchar bdata test;
//_oe_595=0; //選中數(shù)碼管
for(i=0;i<6;i++)
movebit[i]=ledcode[datas[i]];
// P1=0;
delay(1);
for(i=0;i<6;i++) //數(shù)據(jù)移位
{
test=movebit[i];
for(j=0;j<8;j++)
{
testb=test&0x80;
test=test<<1;
if(testb)
{
ds_595=1;
}
else
{
ds_595=0;
}
a_cp_595=1;
a_cp_595=0;
} //數(shù)據(jù)移位
}
b_cp_595=0;
b_cp_595=1;
b_cp_595=0;
}
{
uchar i,j;
bit testb;
uchar bdata movebit[6];
uchar bdata test;
//_oe_595=0; //選中數(shù)碼管
for(i=0;i<6;i++)
movebit[i]=ledcode[datas[i]];
// P1=0;
delay(1);
for(i=0;i<6;i++) //數(shù)據(jù)移位
{
test=movebit[i];
for(j=0;j<8;j++)
{
testb=test&0x80;
test=test<<1;
if(testb)
{
ds_595=1;
}
else
{
ds_595=0;
}
a_cp_595=1;
a_cp_595=0;
} //數(shù)據(jù)移位
}
b_cp_595=0;
b_cp_595=1;
b_cp_595=0;
}
評(píng)論