專注電子技術(shù)學(xué)習(xí)與研究
當(dāng)前位置:單片機(jī)教程網(wǎng) >> MCU設(shè)計(jì)實(shí)例 >> 瀏覽文章

51單片機(jī):數(shù)碼管c代碼集合

作者:佚名   來源:本站原創(chuàng)   點(diǎn)擊數(shù):  更新時(shí)間:2011年10月29日   【字體:

/*
點(diǎn)亮第一個(gè)數(shù)碼管,因?yàn)榘遄邮亲砸炎龅,到電子城買數(shù)碼管時(shí)說好要共陰的,拿來測時(shí)才發(fā)現(xiàn)是共陽的。
*/
//------------------------------------------------------------
  /*
#include <reg52.h>
#define uchar unsigned char
sbit duan=P2^5 ;             //注意,有分號
sbit wei=P2^6;              //注意,有分號+P是大寫的,若你寫成小寫的則會提示說找不到

const unsigned char table[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E};                //共陽極數(shù)碼管數(shù)組,請注意,我使用的共陽極的數(shù)碼管,如果你是用買的現(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;  //共陽極數(shù)碼管  顯示1的編碼值是F9,如果你是買的開發(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;  //共陽極數(shù)碼管  顯示1的編碼值是F9,如果你是買的開發(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 ;             //注意,有分號+P是大寫的,若你寫成小寫的則會提示說找不到
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)槲业氖枪碴柕,其?shù)碼管選中得高電平,如果你是共低的則為0x
   wei=0;
   delay(600);    //一定要加延時(shí)否則看起來亂碼實(shí)際上是閃爍太快了有余光
  }

 }

 }

//-----------------------------------------------------------------

/*
數(shù)碼管從0開始到9變化,同時(shí)LED燈正流+倒流.
*/
#include<reg52.h>
#include <intrins.h>      //LED燈用到移動關(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); //移動
 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); //移動
 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();
   }
}
關(guān)閉窗口

相關(guān)文章