51單片機:數碼管c代碼集合
/*
點(diǎn)亮第一個(gè)數碼管,因為板子是自已做的,到電子城買(mǎi)數碼管時(shí)說(shuō)好要共陰的,拿來(lái)測時(shí)才發(fā)現是共陽(yáng)的。
*/
//------------------------------------------------------------
/*
#include reg52.h>
#define uchar unsigned char
sbit duan=P2^5 ; //注意,有分號
sbit wei=P2^6; //注意,有分號+P是大寫(xiě)的,若你寫(xiě)成小寫(xiě)的則會(huì )提示說(shuō)找不到
const unsigned char table[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0x88,0x83,
0xC6,0xA1,0x86,0x8E}; //共陽(yáng)極數碼管數組,請注意,我使用的共陽(yáng)極的數碼管,如果你是用買(mǎi)的現成板,一般是使用共陰極的
void main()
{
duan=1;
P0=table[1];
duan=0;
wei=1;
P0=0x01;
wei=0;
while(1);
}
*/
//-----------------------------------------------------------------
/*
//靜態(tài)顯示,第一個(gè)數碼管顯示1
#include reg52.h>
sbit duan=P2^5;
sbit wei=P2^6;
void main()
{
duan=1;
P0=0xF9; //共陽(yáng)極數碼管 顯示1的編碼值是F9,如果你是買(mǎi)的開(kāi)發(fā)板(共陰的數碼管)則為0x06;
duan=0;
wei=1;
P0=0x01; //選中第1個(gè)數碼管
wei=0;
while(1); //一直顯示,以便我們觀(guān)察
}
*/
//-----------------------------------------------------------------
/*
//靜態(tài)顯示,全為1
#include reg52.h>
sbit duan=P2^5;
sbit wei=P2^6;
void main()
{
duan=1;
P0=0xF9; //共陽(yáng)極數碼管 顯示1的編碼值是F9,如果你是買(mǎi)的開(kāi)發(fā)板(共陰的數碼管)則為0x06;
duan=0;
wei=1;
P0=0xff; //選中所有的數碼管
wei=0;
while(1); //一直顯示,以便我們觀(guān)察
}
*/
//-----------------------------------------------------------------
//靜態(tài)顯示:從0到F (所有的數碼管)
#include reg52.h>
#define uchar unsigned char
sbit duan=P2^5 ; //注意,有分號+P是大寫(xiě)的,若你寫(xiě)成小寫(xiě)的則會(huì )提示說(shuō)找不到
sbit wei=P2^6;
const unsigned char table[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0x88,0x83,
0xC6,0xA1,0x86,0x8E}; //數碼管數組
void delay(int x)
{
int a,b;
for(a=x;a>0;a--)
for(b=110;b>0;b--);
}
void main()
{
while(1)
{
uchar n=0;
for(n=0;n=9;n++)
{
duan=1;
P0=table[n];
duan=0;
wei=1;
P0=0xff; //因為我的是共陽(yáng)的,其數碼管選中得高電平,如果你是共低的則為0x
wei=0;
delay(600); //一定要加延時(shí)否則看起來(lái)亂碼實(shí)際上是閃爍太快了有余光
}
}
}
//-----------------------------------------------------------------
/*
數碼管從0開(kāi)始到9變化,同時(shí)LED燈正流+倒流.
*/
#includereg52.h>
#include intrins.h> //LED燈用到移動(dòng)關(guān)鍵字crol,調用此關(guān)鍵字
#define uchar unsigned char
const unsigned char table[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0x88,0x83,
0xC6,0xA1,0x86,0x8E}; //數碼管數組
sbit wei=P2^6;
sbit duan=P2^5;
void delay(uchar x)
{
uchar a,b;
for(a=x;a>0;a--)
for(b=110;b>0;b--);
}
void LED()
{
uchar a,temp;
temp=0xfe;
for (a=8;a>0;a--) //循環(huán)8次,即流水燈8個(gè)循環(huán)8次即可點(diǎn)亮8個(gè)
{
P1=temp;
temp=_crol_(temp,1); //移動(dòng)
delay(200);
}
delay(5);
temp=0x7f;
for (a=8;a>0;a--) //循環(huán)8次,即流水燈8個(gè)循環(huán)8次即可點(diǎn)亮8個(gè)
{
P1=temp;
temp=_crol_(temp,-1); //移動(dòng)
delay(170);
}
}
void scan()
{
uchar n=0;
for(n=0;n=9;n++)
{
duan=1;
P0=table[n];
duan=0;
wei=1;
P0=0xff;
wei=0;
delay(1000);
LED();
}
if(n==9){delay(100000);}
}
void main()
{
while(1)
{
// LED();
scan();
}
}
評論