鎖存晶片的作用:儲存上一狀態值先給他賦值1,【使能端為高】,讓我們要顯示的數值通過,再給他賦值0,【使能端為低】,就可以鎖住數值
在BSTV51微控制器中 P2^6 這個串列埠是段選,控制那一個數位的具體顯示,可根據字形表對應輸出 P2^7 是位選,選擇控制要哪一位亮燈
#include<reg52.h>
sbit DUAN = P2^6;
sbit WEI = P2^7;
void main()
{
P0 = 0x7d; // 1111 1110
DUAN = 1;
DUAN = 0;
P0 = 0x00;
WEI = 1;
WEI = 0;
/* P0 = 0x7c; // 1111 1110
DUAN = 1;
DUAN = 0;
P0 = 0xfd;
WEI = 1;
WEI = 0; */
}
用延遲函數
#include<reg52.h>
sbit DUAN = P2^6;
sbit WEI = P2^7;
void delay(unsigned int xms)
{
unsigned int i,j;
for(i=xms;i>0;i--)
for(j=112;j>0;j--);
}
unsigned code sz[17] = { 0x3f,0x06,0x5b,0x4f,0x66,0x6d,
0x7d,0x07,0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71,0x00
};
void main()
{
while(1)
{
P0 = sz[1]; // 數位1
DUAN = 1; // 一端上電
DUAN = 0; // 斷開
P0 = 0xfe;
WEI = 1; // 另一端上高電平
WEI = 0; // 另一端斷開
delay(200);
P0 = sz[2]; // 數位2
DUAN = 1;
DUAN = 0;
P0 = 0xfd;
WEI = 1;
WEI = 0;
delay(200);
P0 = sz[3]; // 數位3
DUAN = 1;
DUAN = 0;
P0 = 0xfb;
WEI = 1;
WEI = 0;
delay(200);
P0 = sz[4]; // 數位4
DUAN = 1;
DUAN = 0;
P0 = 0xf7;
WEI = 1;
WEI = 0;
delay(200);
P0 = sz[5]; // 數位5
DUAN = 1;
DUAN = 0;
P0 = 0xef;
WEI = 1;
WEI = 0;
delay(200);
P0 = sz[6]; // 數位6
DUAN = 1;
DUAN = 0;
P0 = 0xdf;
WEI = 1;
WEI = 0;
delay(200);
P0 = sz[7]; // 數位7
DUAN = 1;
DUAN = 0;
P0 = 0xbf;
WEI = 1;
WEI = 0;
delay(200);
P0 = sz[8]; // 數位8
DUAN = 1;
DUAN = 0;
P0 = 0x7f;
WEI = 1;
WEI = 0;
delay(200);
P0 = sz[8]; // 數位8
DUAN = 1;
DUAN = 0;
P0 = 0x7f;
WEI = 1;
WEI = 0;
delay(200);
P0 = sz[7]; // 數位7
DUAN = 1;
DUAN = 0;
P0 = 0xbf;
WEI = 1;
WEI = 0;
delay(200);
P0 = sz[6]; // 數位6
DUAN = 1;
DUAN = 0;
P0 = 0xdf;
WEI = 1;
WEI = 0;
delay(200);
P0 = sz[5]; // 數位5
DUAN = 1;
DUAN = 0;
P0 = 0xef;
WEI = 1;
WEI = 0;
delay(200);
P0 = sz[4]; // 數位4
DUAN = 1;
DUAN = 0;
P0 = 0xf7;
WEI = 1;
WEI = 0;
delay(200);
P0 = sz[3]; // 數位3
DUAN = 1;
DUAN = 0;
P0 = 0xfb;
WEI = 1;
WEI = 0;
delay(200);
P0 = sz[2]; // 數位2
DUAN = 1;
DUAN = 0;
P0 = 0xfd;
WEI = 1;
WEI = 0;
delay(200);
P0 = sz[1]; // 數位1
DUAN = 1;
DUAN = 0;
P0 = 0xfe;
WEI = 1;
WEI = 0;
delay(200);
}
}
不用延遲函數
#include<reg52.h>
sbit DUAN = P2^6;
sbit WEI = P2^7;
unsigned code sz[17] = { 0x3f,0x06,0x5b,0x4f,0x66,0x6d,
0x7d,0x07,0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71,0x00
};
void main()
{
while(1)
{
P0 = sz[1]; // 數位1
DUAN = 1; // 一端上電
DUAN = 0; // 斷開
P0 = 0xfe;
WEI = 1; // 另一端上高電平
WEI = 0; // 另一端斷開
P0 = 0xff; // 不用延遲函數需要開一個關一個才能保證不亂碼
WEI = 1;
WEI = 0;
P0 = sz[2]; // 數位2
DUAN = 1;
DUAN = 0;
P0 = 0xfd;
WEI = 1;
WEI = 0;
P0 = 0xff;
WEI = 1;
WEI = 0;
P0 = sz[3]; // 數位3
DUAN = 1;
DUAN = 0;
P0 = 0xfb;
WEI = 1;
WEI = 0;
P0 = 0xff;
WEI = 1;
WEI = 0;
P0 = sz[4]; // 數位4
DUAN = 1;
DUAN = 0;
P0 = 0xf7;
WEI = 1;
WEI = 0;
P0 = 0xff;
WEI = 1;
WEI = 0;
P0 = sz[5]; // 數位5
DUAN = 1;
DUAN = 0;
P0 = 0xef;
WEI = 1;
WEI = 0;
P0 = 0xff;
WEI = 1;
WEI = 0;
P0 = sz[6]; // 數位6
DUAN = 1;
DUAN = 0;
P0 = 0xdf;
WEI = 1;
WEI = 0;
P0 = 0xff;
WEI = 1;
WEI = 0;
P0 = sz[7]; // 數位7
DUAN = 1;
DUAN = 0;
P0 = 0xbf;
WEI = 1;
WEI = 0;
P0 = 0xff;
WEI = 1;
WEI = 0;
P0 = sz[8]; // 數位8
DUAN = 1;
DUAN = 0;
P0 = 0x7f;
WEI = 1;
WEI = 0;
P0 = 0xff;
WEI = 1;
WEI = 0;
}
}