51單片機(jī):數(shù)碼管c代碼集合
/*
點(diǎn)亮第一個(gè)數(shù)碼管,因?yàn)榘遄邮亲砸炎龅模诫娮映琴I(mǎi)數(shù)碼管時(shí)說(shuō)好要共陰的,拿來(lái)測(cè)時(shí)才發(fā)現(xiàn)是共陽(yáng)的。
*/
//------------------------------------------------------------
/*
#include reg52.h>
#define uchar unsigned char
sbit duan=P2^5 ; //注意,有分號(hào)
sbit wei=P2^6; //注意,有分號(hào)+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)極數(shù)碼管數(shù)組,請(qǐng)注意,我使用的共陽(yáng)極的數(shù)碼管,如果你是用買(mǎi)的現(xiàn)成板,一般是使用共陰極的
void main()
{
duan=1;
P0=table[1];
duan=0;
wei=1;
P0=0x01;
wei=0;
while(1);
}
*/
//-----------------------------------------------------------------
/*
//靜態(tài)顯示,第一個(gè)數(shù)碼管顯示1
#include reg52.h>
sbit duan=P2^5;
sbit wei=P2^6;
void main()
{
duan=1;
P0=0xF9; //共陽(yáng)極數(shù)碼管 顯示1的編碼值是F9,如果你是買(mǎi)的開(kāi)發(fā)板(共陰的數(shù)碼管)則為0x06;
duan=0;
wei=1;
P0=0x01; //選中第1個(gè)數(shù)碼管
wei=0;
while(1); //一直顯示,以便我們觀察
}
*/
//-----------------------------------------------------------------
/*
//靜態(tài)顯示,全為1
#include reg52.h>
sbit duan=P2^5;
sbit wei=P2^6;
void main()
{
duan=1;
P0=0xF9; //共陽(yáng)極數(shù)碼管 顯示1的編碼值是F9,如果你是買(mǎi)的開(kāi)發(fā)板(共陰的數(shù)碼管)則為0x06;
duan=0;
wei=1;
P0=0xff; //選中所有的數(shù)碼管
wei=0;
while(1); //一直顯示,以便我們觀察
}
*/
//-----------------------------------------------------------------
//靜態(tài)顯示:從0到F (所有的數(shù)碼管)
#include reg52.h>
#define uchar unsigned char
sbit duan=P2^5 ; //注意,有分號(hào)+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}; //數(shù)碼管數(shù)組
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àn)槲业氖枪碴?yáng)的,其數(shù)碼管選中得高電平,如果你是共低的則為0x
wei=0;
delay(600); //一定要加延時(shí)否則看起來(lái)亂碼實(shí)際上是閃爍太快了有余光
}
}
}
//-----------------------------------------------------------------
/*
數(shù)碼管從0開(kāi)始到9變化,同時(shí)LED燈正流+倒流.
*/
#includereg52.h>
#include intrins.h> //LED燈用到移動(dòng)關(guān)鍵字crol,調(diào)用此關(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}; //數(shù)碼管數(shù)組
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();
}
}
評(píng)論