一个74hc595位移寄存器驱动三极管驱动led位,
//两个74hc595驱动led段,方式位5位x8段x2=10个数码管
//5分频,每次扫描时间位1.25ms
//定义特殊符号
#definenul0xf
#defineqc0xc
#defineqb0xb
#defineq_0xa
#defineq__0xd
#defineq___0xe
#defineqp0x10
#defineqe0x11
#defineqj0x12
#defineqn0x13
#defineqf0x14
#defineqa0x15
#defineqr0x16
#defineqd0x17
#definequ0x18
#defineql0x19
#defineqh0x1a
#defineqwen0x1b
#defineqt0x1c
#defineqla0x1d
#defineqlb0x1e
#defineqlc0x1f
#defineqld0x20
#defineqle0x21
#defineqlf0x22
#defineqlg0x23
#defineqldp0x24
//显示段信息,不同led排列组合的段信息只需更改8个数值即可。
//因此,该定义具有通用性。
//显示
//-d20
//|c40|e10
//-g80
//|b2|f4
//_a1.dp8
#definepa1
#definepb2
#definepc0x40
#definepd0x20
#definepe0x10
#definepf4
#definepg0x80
#definepdp8
//--------------
#definel0pdp+pg
#definel1255-pf-pe
#definel2pdp+pc+pf
#definel3pdp+pc+pb
#definel4pdp+pa+pb+pd
#definel5pdp+pb+pe
#definel6pdp+pe
#definel7pdp+pc+pg+pb+pa
#definel8pdp
#definel9pdp+pb
#definelapdp+pa
#definelbpdp+pd+pe
#definelcpdp+pg+pe+pf
#defineldpdp+pc+pd
#definelepdp+pe+pf
#definelfpdp+pe+pf+pa
#definel_255-pg
#definelnul255
#definellpdp+pg+pd+pf+pe
#definelppdp+pa+pf
#defineltpdp+pd+pe+pf
#definelrpdp+pe+pf+pg+pa
#definelnpdp+pg+pa
#definelhpdp+pd+pe+pa
#definelypdp+pb+pd
#definelupdp+pg+pd
#definel__pdp+pg+pb+pc+pe+pf
#definel___l__-pg
#definel_1255-pa
#definel_2255-pa-pg
#definelj255-(pe+pf+pa)
#definelwen255-(pd+pe+pg+pb)
#definelall0
#definella255-pa
#definellb255-pb
#definellc255-pc
#definelld255-pd
#definelle255-pe
#definellf255-pf
#definellg255-pg
#definelldp255-pdp
//串行送出的位信息,目前是10位led显示。
unsignedcharcodeun_dig[]={0x7f,0xbf,0xdf,0xef,0xf7,0xfb};
//串行送出的短信息。
unsignedcharcodeun_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};
sbitd_clk=P0^0;//移位时钟
sbitd_dat=P0^1;//移位数据
sbitd_st=P0^2;//移位锁定
unsignedchardig;//位扫描计数器
unsignedchard[10];//显示缓冲
//送出8位串行数据
voidout_disp(unsignedchardd){
unsignedchari;
for(i=0;i<8;i++){
if(dd&1)d_dat=1;elsed_dat=0;
d_clk=0;
dd>>=1;
d_clk=1;
}
}
//控制小数点和闪烁,显示数据|0x040表示有小数点;显示数据|0x80表示闪烁。
voidout_displ(unsignedchardd){
if(dd>=0x80){
if(s001>flash_time){out_disp(0xff);return;}
}
dd&=0x7f;
if(dd>=0x40){
dd=un_disp[dd&0x3f]^pdp;
}elsedd=un_disp[dd];
out_disp(dd);
}
unsignedints001;//闪烁时间参考
voidint_t0(void)interrupt1{
unsignedchardd;
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;
}
voidmain(void){
unsignedchari;
TMOD=0x1;
TR0=ET0=1;
EA=1;
for(i=0;i<10;i++)d[i]=i;//displaytest
while(1){}
}