導航:首頁 > 編程語言 > 單片機七段數碼管顯示程序

單片機七段數碼管顯示程序

發布時間:2025-09-01 15:07:37

❶ 用51單片機程序顯示3位7段數碼管,求具體程序

假設為共陰極數碼管,驅動輸入端接單片機P1口,共陰極接P2口的0.1.2.腳。我的程序如下,當前顯示數字123
#include <reg52.h>
typedef unsigned char uint8;
typedef unsigned int uint16;
typedef long int uint32;
code uint8 number[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
void delay_ms(uint16 x)//1ms延時
{
uint8 i = 121;
while(x > 0)
{
i = 121;
while(i > 0)
i --;
x --;
}
}
void xianshi(uint16 x)
{
uint8 i=0;
for(i=0;i<=2;i++)
{
P2=~(1<<i);
switch(i)
{
case 0:P1=number[(x/1)%10];break;
case 1:P1=number[(x/10)%10];break;
case 2:P1=number[(x/100)%10];break;
default:break;
}
delay_ms(10);
}
}
main()
{
while(1)
{
xianshi(123);
}
}

❷ at89c51單片機的七段顯示數碼管和流水燈的編程

這個可以借鑒網上的一些資料,具體的看你的原理圖,可以仿寫
最重要的是自己掌握方法
可以到一些貼吧論壇討論,相互學習

#include <reg52.h>
typedef unsigned int uint;
typedef unsigned char uchar;
sbit wela = P2^7; //數碼管位聲明
sbit la = P2^6; //數碼管段選聲明
sbit FM = P2^3;//蜂鳴器聲明
uchar counter = 0, counter1 = 0, j = 0, flag = 0, flag1 = 0;
uchar num = 6, num1 = 0;
//數碼管段選編碼
uchar code table_ [] = {
0x3F, //"0"
0x06, //"1"
0x5B, //"2"
0x4F, //"3"
0x66, //"4"
0x6D, //"5"
0x7D, //"6"
0x07, //"7"
0x7F, //"8"
0x6F, //"9"
0x77, //"A"
0x7C, //"B"
0x39, //"C"
0x5E, //"D"
0x79, //"E"
0x71, //"F"
0x76, //"H"
0x38, //"L"
0x37, //"n"
0x3E, //"u"
0x73, //"P"
0x5C, //"o"
0x40, //"-"
0x00, //熄滅
0x00 //自定義
};
//數碼管位選編碼
uchar code table_we[] = {
0xfe,
0xfd,
0xfb,
0xf7,
0xef,
0xdf
};
void init();
void display();
void main()
{

init();
while(1)
{
if(counter1 == 30)
{
counter1 = 0;
display();
num--;
num1++;
if(num == 0)
{
num = 6;
}
if(num1 == 6)
{
num1 = 0;
}
}

if(counter == 200)//定時到4秒後,流水燈向右流動
{
counter = 0;//counter清零,
//關閉定時器0
TR0 = 0;
//ET0 = 0;
flag = 1; //標志變數置1後,定時器重新初始化
TH0 = 0xB8; //定時器0定時20毫秒
TL0 = 0x00;
TR0 = 1; //重新打開定時器0
//ET0 = 1;
j = 0;
}
}
}
//定時器初始化程序
void init()
{
//定時器0和定時器1初始化函數
TMOD = 0x11;//定義定時器1和0在工作方式1下
TH0 = 0xB8; //定時器0定時20毫秒
TL0 = 0x00;
TH1 = 0xFC; //定時器1定時20毫秒
TL1 = 0x67;
TR0 = 1; //打開定時器0
TR1 = 1; //打開定時器1
EA = 1; //打開總中斷
ET0 = 1; //打開定時器0中斷
ET1 = 1; //打開定時器1中斷
}
//數碼管顯示子程序
void display()
{
la = 1;
P0 = table_[num];
la = 0;
P0 = 0xff;
wela = 1;
P0 = table_we[num1];
wela = 0;
la = 1;
P0 = table_[num];
la = 0;
P0 = 0xff;
wela = 1;
P0 = table_we[num1];
wela = 0;

la = 1;
P0 = table_[num];
la = 0;
P0 = 0xff;
wela = 1;
P0 = table_we[num1];
wela = 0;

la = 1;
P0 = table_[num];
la = 0;
P0 = 0xff;
wela = 1;
P0 = table_we[num1];
wela = 0;

la = 1;
P0 = table_[num];
la = 0;
P0 = 0xff;
wela = 1;
P0 = table_we[num1];
wela = 0;

la = 1;
P0 = table_[num];
la = 0;
P0 = 0xff;
wela = 1;
P0 = table_we[num1];
wela = 0;

}
//中斷定時器0
void int_time0() interrupt 1
{
TH0 = 0xB8;
TL0 = 0x00;
counter++;
if(flag == 0)
{
if(counter % 10 == 0)//定時200毫秒
{
P1 = ~(1 << j++);//流水燈左移流動
if(j == 8)
{
j = 0;
}
}
}
else
{
if(counter % 5 == 0)
{
//counter = 0;
//流水燈右移流動
P1 = ~(0x80 >> j++);
FM = ~FM;
if(j == 8)
j = 0;
}
}

}
//中斷定時器1
void int_time1() interrupt 3
{
TH1 = 0xFC;
TL1 = 0x67;
counter1++;

}
參考

閱讀全文

與單片機七段數碼管顯示程序相關的資料

熱點內容
百度地圖歷史版本大全 瀏覽:71
安卓圖片拼接代碼 瀏覽:939
微信網店源碼 瀏覽:278
換系統盤還原微軟備份文件 瀏覽:265
保存配置文件到nbram 瀏覽:593
配置文件加密後 瀏覽:360
delphiword自動編號 瀏覽:793
蘋果手機拷貝文件到百度 瀏覽:405
單片機七段數碼管顯示程序 瀏覽:683
紅頭文件內容如何製作 瀏覽:69
輸入壓縮文件密碼忘了怎麼辦 瀏覽:89
內核配置文件加解密 瀏覽:507
該文件已被損壞的原因 瀏覽:809
mysql每月備份資料庫 瀏覽:453
javaawt布局 瀏覽:303
三星自帶壁紙在哪個文件夾里 瀏覽:565
eclipse中如何修改文件名 瀏覽:778
2014版高洛峰php視頻教程打包下載 瀏覽:765
ad文件名後邊的數字怎麼去掉 瀏覽:560
w7無線網路沒有了 瀏覽:222

友情鏈接