串行口驅(qū)動(dòng)led顯示程序
//本文介紹一個(gè)串行口驅(qū)動(dòng)led顯示的c語(yǔ)言源程序
//一個(gè)74hc595位移寄存器驅(qū)動(dòng)三極管驅(qū)動(dòng)led位,
//兩個(gè)74hc595驅(qū)動(dòng)led段,方式位5位x8段x2=10個(gè)數(shù)碼管
//5分頻,每次掃描時(shí)間位1.25ms
//定義特殊符號(hào)
#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個(gè)數(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
//串行口驅(qū)動(dòng)led顯示程序定義結(jié)束
//串行送出的位信息,目前是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; //移位時(shí)鐘
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;i8;i++) {
if (dd1) 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[dd0x3f]^pdp;
} else dd=un_disp[dd];
out_disp(dd);
}
unsigned int s001; //閃爍時(shí)間參考
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;
}
//串行口驅(qū)動(dòng)led顯示程序主程序開(kāi)始
void main(void) {
unsigned char i;
TMOD=0x1;
TR0=ET0=1;
EA=1;
for (i=0;i10;i++) d[i]=i; //display test
while (1) {}
}
手機(jī)電池相關(guān)文章:手機(jī)電池修復(fù)
c語(yǔ)言相關(guān)文章:c語(yǔ)言教程
評(píng)論