程序一利用動(dòng)態(tài)掃描方法在六位數(shù)碼管上顯示出穩(wěn)定的654321.
時(shí)鐘頻率為11.0592M
本文引用地址:http://m.butianyuan.cn/article/201611/321416.htm#include //52單片機(jī)頭文件
#include //包含有左右循環(huán)移位子函數(shù)的庫
#define uint unsigned int //宏定義
#define uchar unsigned char //宏定義
sbit dula=P2^6; //數(shù)碼管段選鎖存端
sbit wela=P2^7; ////數(shù)碼管位選鎖存端
uchar code table[]={ //數(shù)碼管顯示編碼
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
void display(uchar,uchar,uchar,uchar,uchar,uchar); //函數(shù)聲明
void delay(uint);
void main()
{
while(1)
{
display(6,5,4,3,2,1); //始終顯示
}
}
void display(uchar one,uchar two,uchar three,uchar four,uchar five,uchar six)
{
dula=1;
P0=table[one]; //送段數(shù)據(jù)
dula=0;
P0=0xff;//送位數(shù)據(jù)前關(guān)閉所有顯示,防止打開位選鎖存后段選數(shù)據(jù)通過位選鎖存器
wela=1;
P0=0xfe;
wela=0;
delay(1);
dula=1;
P0=table[two];
dula=0;
P0=0xff;
wela=1;
P0=0xfd;
wela=0;
delay(1);
dula=1;
P0=table[three];
dula=0;
P0=0xff;
wela=1;
P0=0xfb;
wela=0;
delay(1);
dula=1;
P0=table[four];
dula=0;
P0=0xff;
wela=1;
P0=0xf7;
wela=0;
delay(1);
dula=1;
P0=table[five];
dula=0;
P0=0xff;
wela=1;
P0=0xef;
wela=0;
delay(1);
dula=1;
P0=table[six];
dula=0;
P0=0xff;
wela=1;
P0=0xdf;
wela=0;
delay(1);
}
void delay(uint z) //延時(shí)子函數(shù)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
程序二
用動(dòng)態(tài)掃描方法和定時(shí)器1在數(shù)碼管的前三位顯示出秒表,
精確到1%秒,即最后一位顯示1%秒,一直循環(huán)下去
#include //52單片機(jī)頭文件
#include //包含有左右循環(huán)移位子函數(shù)的庫
#define uint unsigned int //宏定義
#define uchar unsigned char //宏定義
sbit dula=P2^6; //數(shù)碼管段選鎖存端
sbit wela=P2^7; ////數(shù)碼管位選鎖存端
uchar ge,shi,bai;
uint tt;
uchar code table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
void display(uchar,uchar,uchar); //函數(shù)聲明
void delay(uint);
void main()
{
TMOD=0x10;//設(shè)置定時(shí)器1為工作方式1
TH1=(65536-10000)/256;
TL1=(65536-10000)%256;
EA=1;//開總中斷
ET1=1;//開定時(shí)器1中斷
TR1=1;//啟動(dòng)定時(shí)器1
while(1)
{
display(bai,shi,ge);
}
}
void exter0() interrupt 3 // 定時(shí)器1中斷
{
TH1=(65536-10000)/256;
TL1=(65536-10000)%256;
tt++;
if(tt==1000)
tt=0;
bai=tt/100;
shi=tt%100/10;
ge=tt%10;
}
void display(uchar one,uchar two,uchar three)
{
dula=1;
P0=table[one]; //送段數(shù)據(jù)
dula=0;
P0=0xff;//送位數(shù)據(jù)前關(guān)閉所有顯示,防止打開位選鎖存后段選數(shù)據(jù)通過位選鎖存器
wela=1;
P0=0xfe;
wela=0;
delay(1);
dula=1;
P0=table[two];
dula=0;
P0=0xff;
wela=1;
P0=0xfd;
wela=0;
delay(1);
dula=1;
P0=table[three];
dula=0;
P0=0xff;
wela=1;
P0=0xfb;
wela=0;
delay(1);
}
void delay(uint z) //延時(shí)子函數(shù)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
評論