新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設計應用 > 51單片機-數(shù)碼管

51單片機-數(shù)碼管

作者: 時間:2016-12-02 來源:網絡 收藏


#include <reg52.h>

void delay(){
int i,j;
for(i = 0; i < 0xff; i++)
for(j = 0; j < 0xff; j++)
;
}
unsigned char code duan[]={
0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,
0x77,0x7C,0x39,0x5E,0x79,0x71};
unsigned char code wei[]={
0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07};

void show(unsigned char we,unsigned char du){
P1 = wei[we];
P2 = duan[du];
delay();
}
void main(){
while(1){
show(1,5);
}
}



動態(tài)顯示: 3個數(shù)碼管從0"999計數(shù)。





#include <reg52.h>

unsigned int code duan[]={
0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7f,0x6f};

unsigned int code wei[]={
0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfe,0xff};

unsigned int num,sum;

void delay(int k){
int i,j;
for(i = 0; i < k; i++)
for(j = 0; j < 0xff; j++)
;
}

void display(unsigned int bai, unsigned int shi, unsigned int ge){
P1 = 0x0;
P2 = duan[bai];
delay(1); //少延時一些數(shù)碼管更亮,如果延時很大就
P1 = 0x1; //變成靜態(tài)的了
P2 = duan[shi];
delay(1);
P1 = 0x2;
P2 = duan[ge];
delay(1);
}
void main(){
EA = 1;
ET1 = 1;
TMOD = 0x10;
TR1 = 1;
num = 0;
sum = 0;
TH1 = (65535 - 50000) / 256;
TL1 = (65535 - 50000) % 256;
while(1){
if(num == 20){ //每一秒計數(shù)增加1
num = 0;
if(sum == 1000)
sum = 0; //用3位數(shù)碼管顯示,當達到1000時從頭顯示
sum++;
}
display(sum/100,sum%100/10,sum%10);
}
}

void time1() interrupt 3{
TH1 = (65535 - 50000) / 256;
TL1 = (65535 - 50000) % 256;
num++;
}








#include <reg52.h>

unsigned char code duan[]={
0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D};

void delay(int k){
int i,j;
for(i = 0; i < k; i++)
for(j = 0; j < 0xff; j++)
;
}

void display(){
int i;
for(i = 1; i < 7; i++){
P1 = i-1;
P2 = duan[7-i];
delay(1);
}
}

void main(){
while(1){
display();
}
}







#include <reg52.h>

unsigned int num,sum;

unsigned int code duan[]={
0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7f,0x6f};

void delay(int m){
int i,j;
for(i = 0; i < m; i++)
for(j = 0; j < 0xff; j++)
;
}

void display(int n1,int n2,int n3){
P1 = 0;
P2 = duan[n1];
delay(1);
P1 = 1;
P2 = duan[n2];
delay(1);
P1 = 2;
P2 = duan[n3];
delay(1);
}

void main(){
EA = 1;
ET1 = 1;
TMOD = 0x10;
TR1 = 1;
sum = 0;
num = 0;
TH1 = (65535 - 50000) / 256;
TL1 = (65535 - 50000) % 256;
while(1){
if(num == 1){
num = 0;
sum++;
}
if(sum == 1000)
sum = 0;
display(sum/100,sum%100/10,sum%10);
}
}

void time1() interrupt 3{
TH1 = (65535 - 10000) / 256;
TL1 = (65535 - 10000) % 256;
num++;
}

上一頁 1 2 下一頁

評論


技術專區(qū)

關閉