基于51单片机的8位数码管时钟设计—按键修改时间

Posted perseverance52

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了基于51单片机的8位数码管时钟设计—按键修改时间相关的知识,希望对你有一定的参考价值。

基于51单片机的8位数码管时钟设计—按键修改时间

  • 仿真图演示
    在这里插入图片描述
共阳极数码管程序源码
#include<reg51.h>

typedef unsigned char uchar;
typedef unsigned int uint;

//按键定义
sbit key_stop = P2^0;
sbit key_star = P2^1;
sbit key_fen = P2^2;
sbit key_shi = P2^3;

//0~9的十六进制数码
uchar code num[10] = {0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8, 0x80, 0x90};  // 0~9 7SegCA共阳数码管
 //unsigned char code num[]  = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
					  //共阴0-F段码,加code是存在rom里面
//秒、分、时标志
uchar miao = 0, fen = 0, shi = 0;  

//秒、分、时高位低位
uchar miao_L, miao_H, fen_L, fen_H, shi_L, shi_H; 

//计数
uint counter = 0;  


void delay(uint x){  //毫秒延时函数
	    unsigned int i=0,j=0;
    for(i=0;i<x;i++)
        for(j=0;j<123;j++);
		;
}//delay

void key_delay(int xms){  //按键延时函数 x ms
	unsigned int i, j;
	for(i=0; i<xms; ++i)
		for(j=0; j<110; ++j)
			;
}//key_delay

void T0_Init(){  //定时器初始化
	
	TMOD = 0x01; 
	TH0 = 0x3c;  
	TL0 = 0xb0;	 
    EA = 1;  //开总中断
	ET0 = 1;     
	TR0 = 1;    
}//T0_Init

void display(){  //显示函数
	
	P1 = 0x20;  //0010 0000
	P0 = 0xbf;	
	delay(5);
		
	P1 = 0x04; //0100 
	P0 = 0xbf;
	delay(5);

	P1 = 0x80;  // 1000 0000
	P0 = num[miao_L];  
	delay(5);

	P1 = 0x40;	// 0100 0000
	P0 = num[miao_H]; 
	delay(5);

	P1 = 0x10;	// 0001 0000
	P0 = num[fen_L];  
	delay(5);

	P1 = 0x08;	// 0000 1000
	P0 = num[fen_H];  
	delay(5);

	P1 = 0x02;	// 0000 0010	
	P0 = num[shi_L];  
	delay(5);
	
	P1 = 0x01;	// 0000 0001
	P0 = num[shi_H]; 
	delay(5);

}//display

void keyscan(){  //按键函数

	if(key_stop == 0){
		key_delay(10);
		if(key_stop == 0){
			EA = 0;  //关总中断,即停止
			while(!key_stop);
		}
	}//key_stop
	
	if(key_star == 0){
		key_delay(10);
		if(key_star == 0){
			EA = 1;  //开总中断
			while(!key_star);
		}
	}//key_star	
	
	if(key_fen == 0){
		key_delay(10);
		if(key_fen == 0){
			fen++;
			while(!key_fen);
		}
	}//key_fen++
	
	if(key_shi == 0){
		key_delay(10);
		if(key_shi == 0){
			shi++;
			while(!key_shi);
		}
	}//key_shi++
}

void main(){

	T0_Init(); 
	
	while(1){

		display();
		
        keyscan();		
   }

}//main

void timer0_Init() interrupt 1{  //中断函数 

	TL0 =(65536-50000)%256;//50ms预装载值
	TH0 =(65536-50000)/256;
	 counter++;
//1秒钟=50ms*20=1000ms,20次计数为1秒钟
	 if(counter == 20){
	 	counter = 0;  
	 	miao++;	//i = 100 

		if(miao == 60){
			miao = 0;  
			fen++;

			if(fen == 60){
				fen = 0;  
				shi++;

				if(shi == 24){
					shi = 0;
					fen = 0;
					miao = 0;
				}
			}
		}

		miao_L = miao%10;  
		miao_H = miao/10; 

		fen_L = fen%10; 
		fen_H = fen/10;  

		shi_L = shi%10;  
		shi_H = shi/10;

	 }
 }//timer0_Init

  • 仿真文件
链接:https://pan.baidu.com/s/1gt6bAAXCftSrNFayG7qSGQ 
提取码:klq8 
共阴极数码管程序代码
#include<reg51.h>

typedef unsigned char uchar;
typedef unsigned int uint;

//按键定义
sbit key_stop = P2^0;
sbit key_star = P2^1;
sbit key_fen = P2^2;
sbit key_shi = P2^3;

//0~9的十六进制数码
//uchar code num[10] = {0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8, 0x80, 0x90};  // 0~9 7SegCA共阳数码管
//1100 0000,1001 1111,
unsigned char code num[]  = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
					  //共阴0-F段码,加code是存在rom里面
//秒、分、时标志
uchar miao = 0, fen = 0, shi = 0;  

//秒、分、时高位低位
uchar miao_L, miao_H, fen_L, fen_H, shi_L, shi_H; 

//计数
uint counter = 0;  


void delay(uint x){  //毫秒延时函数
	    unsigned int i=0,j=0;
    for(i=0;i<x;i++)
        for(j=0;j<123;j++);
		;
}//delay

void key_delay(int xms){  //按键延时函数 x ms
	unsigned int i, j;
	for(i=0; i<xms; ++i)
		for(j=0; j<110; ++j)
			;
}//key_delay

void T0_Init(){  //定时器初始化
	
	TMOD = 0x01; 
	TH0 = 0x3c;  
	TL0 = 0xb0;	 
    EA = 1;  //开总中断
	ET0 = 1;     
	TR0 = 1;    
}//T0_Init

void display(){  //显示函数
	
	//P1 = 0x20;  //0010 0000
	P1 =0xdf;
	P0 = 0x40;	//段选显示-
	delay(2);

//	P1 = 0x04; //位选0000 0100 
		P1 =0xfb;
	P0 = 0x40;//段选显示-
	delay(2);

	//P1 = 0x40;  // 1000 0000
	P1=0X7F;
	P0 = num[miao_L];  
	delay(2);

//	P1 = 0x80;	// 0100 0000
		P1=0XBF;
	P0 = num[miao_H]; 
	delay(2);

//	P1 = 0x08;	// 0001 0000
	P1=0XEF;
	P0 = num[fen_L];  
	delay(2);

//	P1 = 0x10;	// 0000 1000
	P1=0XF7;
	P0 = num[fen_H];  
	delay(2);

//	P1 = 0x01;	// 0000 0010	
	P1=0XFD;
	P0 = num[shi_L];  
	delay(5);
	
//	P1 = 0x02;	// 0000 0001
	P1=0XFE;
	P0 = num[shi_H]; 
	delay(5);

}//display

void keyscan(){  //按键函数

	if(key_stop == 0){
		key_delay(10);
		if(key_stop == 0){
			EA = 0;  //关总中断,即停止
			while(!key_stop);
		}
	}//key_stop
	
	if(key_star == 0){
		key_delay(10);
		if(key_star == 0){
			EA = 1;  //开总中断
			while(!key_star);
		}
	}//key_star	
	
	if(key_fen == 0){
		key_delay(10);
		if(key_fen == 0){
			fen++;
			while(!key_fen);
		}
	}//key_fen++
	
	if(key_shi == 0){
		key_delay(10);
		if(key_shi == 0){
			shi++;
			while(!key_shi);
		}
	}//key_shi++
}

void main(){

	T0_Init(); 
	
	while(1){

		display();
		keyscan();		
   }

}

void timer0_Init() interrupt 1{  //中断函数 

	TL0 =(65536-50000)%256;//50ms预装载值
	TH0 =(65536-50000)/256;
	 counter++;
//1秒钟=50ms*20=1000ms,20次计数为1秒钟
	 if(counter == 20){
	 	counter = 0;  
	 	miao++;	//i = 100 

		if(miao == 60){
			miao = 0;  
			fen++;

			if(fen == 60){
				fen = 0;  
				shi++;

				if(shi == 24){
					shi = 0;
					fen = 0;
					miao = 0;
				}
			}
		}

		miao_L = miao%10;  
		miao_H = miao/10; 

		fen_L = fen%10; 
		fen_H = fen/10;  

		shi_L = shi%10;  
		shi_H = shi/10;

	 }
 }//timer0_Init

以上是关于基于51单片机的8位数码管时钟设计—按键修改时间的主要内容,如果未能解决你的问题,请参考以下文章

基于51单片机定时器计数+2片74HC595联级+8位数码管时钟+按键修改时间

基于51单片机+74HC595移位寄存器+8位共阴数码管时钟设计—按键修改时间

基于51单片机的按键计数设计,4位,0到9999

基于51单片机定时器计数+74HC595移位寄存器+8位数码管时钟

基于51单片机的电子秒表

基于51单片机的电子秒表