上面unsigned char code table【】我之前沒有搞注釋,現(xiàn)在解釋一下unsigned char是數(shù)組類型 code是編碼的意識(shí) table是數(shù)組名 【】里的是顯示數(shù)字的控制發(fā)光二極管的編碼從左到右分別是1、2、3、4、5、6、7、8、9、A、B、C、D、E、F。
會(huì)了一個(gè)數(shù)碼管之后就要弄八個(gè)數(shù)碼管或者六個(gè)數(shù)碼管甚至更多 我就舉最有代表的時(shí)鐘六位數(shù)碼管顯示。當(dāng)數(shù)碼一多,就要分別控制每個(gè)數(shù)碼管 這就要用到另一個(gè)I/O控制口P2,控制方法和數(shù)碼管一樣,就是把六個(gè)數(shù)碼管看成是六個(gè)二極管,通過控制共陰或者共陽端的電位高低來控制數(shù)碼管的亮滅!
#include<reg51.h> //51系列單片機(jī)頭文件
#define uchar unsigned char //宏定義
uchar code table []={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
/***************************數(shù)碼管掃描延時(shí)****************************/
void delays(void)
{
uint i,j;
for(i=0;i<2;i++)
for(j=0;j<110;j++);
}
void main(void)
{
P0=table [1];
P2=0xfd;
delays();
P0=table [2];
P2=0xfe;
delays();
P0=table [3];
P2=0xf7;
delays();
P0=table [4];
P2=0xfb;
delays();
P0=table [5];
P2=0xdf;
delays();
P0=table [6];
P2=0xef;
delays();
while(1)
} 顯示的效果就是012345這是靜態(tài)顯示 如果是你想要?jiǎng)討B(tài)顯示在上面的程序里用一個(gè)for循環(huán)語句table【】內(nèi)用字母或簡單運(yùn)算表示
簡單舉例:
#include<reg51.h> //51系列單片機(jī)頭文件
#define uchar unsigned char //宏定義
uchar code table []={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
/***************************數(shù)碼管掃描延時(shí)****************************/
void delays(void)
{
uint i,j;
for(i=0;i<2;i++)
for(j=0;j<110;j++);
}
void main( )
{ uchar i;
while(1)//無限循環(huán)語句
{
For(i=0;i<9;i++)//循環(huán)語句
{ if(i==9) //選擇語句
{
i=0;
}
P0=table [ i];
P2=0xfd;
delays();
P0=table [ i];
P2=0xfe;
delays();
P0=table [ i];
P2=0xf7;
delays();
P0=table [ i];
P2=0xfb;
delays();
P0=table [ i];
P2=0xdf;
delays();
P0=table [ i];
P2=0xef;
delays();
}
}
} 顯示效果是不斷循環(huán)顯示000000、111111、222222、~~~、999999當(dāng)然也可以顯示不一樣的,原理都一樣 參數(shù)改變 顯示效果也不一樣!