74hc595驅(qū)動串行l(wèi)ed顯示
串行驅(qū)動LED顯示
//一個74hc595位移寄存器驅(qū)動三極管驅(qū)動led位 ,
//兩個74hc595驅(qū)動led段,方式位5位x8段x2=10個數(shù)碼管
//5分頻,每次掃描時間位1.25ms
//定義特殊符號
#define nul 0xf
#define qc 0xc
#define qb 0xb
#define q_ 0xa
#define q__ 0xd
#define q___ 0xe
#define qp 0x10
#define qe 0x11
#define qj 0x12
#define qn 0x13
#define qf 0x14
#define qa 0x15
#define qr 0x16
#define qd 0x17
#define qu 0x18
#define ql 0x19
#define qh 0x1a
#define qwen 0x1b
#define qt 0x1c
#define qla 0x1d
#define qlb 0x1e
#define qlc 0x1f
#define qld 0x20
#define qle 0x21
#define qlf 0x22
#define qlg 0x23
#define qldp 0x24
//顯示段信息,不同led排列組合的段信息只需更改8個數(shù)值即可。
//因此,該定義具有通用性。
// 顯示
// -d 20
// |c 40 |e 10
// - g 80
// |b 2 |f 4
// _a1 .dp 8
#define pa 1
#define pb 2
#define pc 0x40
#define pd 0x20
#define pe 0x10
#define pf 4
#define pg 0x80
#define PDP 8
//--------------
#define l0 PDP+pg
#define l1 255-pf-pe
#define l2 PDP+pc+pf
#define l3 pdp+pc+pb
#define l4 pdp+pa+pb+pd
#define l5 pdp+pb+pe
#define l6 pdp+pe
#define l7 pdp+pc+pg+pb+pa
#define l8 pdp
#define l9 pdp+pb
#define la pdp+pa
#define lb pdp+pd+pe
#define lc pdp+pg+pe+pf
#define ld pdp+pc+pd
#define le pdp+pe+pf
#define lf pdp+pe+pf+pa
#define l_ 255-pg
#define lnul 255
#define ll pdp+pg+pd+pf+pe
#define lp pdp+pa+pf
#define lt pdp+pd+pe+pf
#define lr pdp+pe+pf+pg+pa
#define ln pdp+pg+pa
#define lh pdp+pd+pe+pa
#define ly pdp+pb+pd
#define lu pdp+pg+pd
#define l__ pdp+pg+pb+pc+pe+pf
#define l___ l__-pg
#define l_1 255-pa
#define l_2 255-pa-pg
#define lj 255-(pe+pf+pa)
#define lwen 255-(pd+pe+pg+pb)
#define lall 0
#define lla 255-pa
#define llb 255-pb
#define llc 255-pc
#define lld 255-pd
#define lle 255-pe
#define llf 255-pf
#define llg 255-pg
#define lldp 255-pdp
//串行送出的位信息,目前是10位led顯示。
unsigned char code un_dig[]={0x7f,0xbf,0xdf,0xef,0xf7,0xfb};
//串行送出的短信息。
unsigned char code un_disp[]={l0,l1,l2,l3,l4,l5,l6,l7,l8,l9,l_,lb,lc,l__,l___,lnul,lp,le,lj,ln,lf,la,lr,ld,lu,
ll,lh,lwen,lt,lla,llb,llc,lld,lle,llf,llg,lldp, lnul };
sbit d_clk=P0^0; //移位時鐘
sbit d_dat=P0^1; //移位數(shù)據(jù)
sbit d_st=P0^2; //移位鎖定
unsigned char dig; //位掃描計(jì)數(shù)器
unsigned char d[10]; //顯示緩沖
//送出8位串行數(shù)據(jù)
void out_disp(unsigned char dd) {
unsigned char i;
for (i=0;i<8;i++) {
if (dd&1) d_dat=1; else d_dat=0;
d_clk=0;
dd>>=1;
d_clk=1;
}
}
//控制小數(shù)點(diǎn)和閃爍,顯示數(shù)據(jù)|0x040表示有小數(shù)點(diǎn);顯示數(shù)據(jù)|0x80表示閃爍。
void out_displ(unsigned char dd) {
if (dd>=0x80) {
if (s001>flash_time) {out_disp(0xff);return;}
}
dd&=0x7f;
if (dd>=0x40) {
dd=un_disp[dd&0x3f]^pdp;
} else dd=un_disp[dd];
out_disp(dd);
}
unsigned int s001; //閃爍時間參考
void int_t0(void) interrupt 1 {
unsigned char dd;
TL0=TL0+30;TH0=0xfb; //800
time++;
if ((++s001)>=800) s001=0;
// 顯示
if ((++dig)>4) dig=0;
d_st=0;
dd=d[dig+5];
out_displ(dd);
dd=d[dig];
out_displ(dd);
out_disp(un_dig[dig]);
d_st=1;
}
void main(void) {
unsigned char i;
TMOD=0x1;
TR0=ET0=1;
EA=1;
for (i=0;i<10;i++) d[i]=i; //display test
while (1) {}
}