⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 clock.c

📁 一个简单但功能丰富的开发板程序
💻 C
字号:
/************************************************
        应用范例 3-5 
	标题:范例  3-5 
 	版本:1.0// 
 	Target:	    STC89C52RC   
 	程序描述:这个程序说明如何利用单片机做个        
                  电子钟
*************************************************/
#include<reg51.h>
#define uint unsigned int
#define uchar unsigned char
code seven_seg[10] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
code bit_select[6] = {0xfe,0xfd,0xfb,0xf7,0xef,0xdf};
char counter[3] = {0,0,0};
uint x,y,z,a,b,c,d;
sbit key1 = P3^0;
sbit key2 = P3^1;
sbit key3 = P3^2; 
sbit key4 = P3^3;
sbit FMQ = P3^4; 
sbit JDQ = P3^5;
uchar key1_state,key2_state,key3_state,key4_state,key1_flog;
/************************************************
                  延迟子函数 
*************************************************/
void delay(uchar i)
{
	while(i)
	i--;
}
/**********************************************
          初始化T0,每250uS中断一次 
***********************************************/
void timer0_init()
{
	TMOD = 0x02;
	TH0 = 0x06;
	TL0 = 0x06;
	TR0 = 1;
	ET0 = 1;
	EA = 1;
}
/**********************************************
           T0的中断服务子程序 
***********************************************/
void timer0_isr() interrupt 1
{
	x++;
	if(x == 2000)
	{
		x = 0;
		y++;
		z = !z;
		if(y == 2)
		{
			counter[0]++;
			y = 0;
		}
	}
}
/**********************************************
              记时子函数 
***********************************************/
void counter_add1()
{
	if(counter[0] >= 60)
	{
		counter[0] = 0;
		counter[1]++;
	}
	if(counter[1] >= 60)
	{
		counter[1] = 0;
		counter[2]++;
	}
	if(d == 0 && counter[2] > 24)
	counter[2] = 0;
	if(d == 1 && counter[2] > 13)
	counter[2] = counter[2]-12;

}
/**********************************************
            LED显示器显示子函数 
***********************************************/
void display()
{
	uchar i;
	P0 = a|seven_seg[counter[0]%10];
	P2 = bit_select[0];
	delay(100);
	P0 = a|seven_seg[counter[0]/10];
	P2 = bit_select[1];
	delay(100);
   	P0 = b|seven_seg[counter[1]%10];  
	P2 = bit_select[2];
	delay(100);
	P0 = seven_seg[counter[1]/10];
	P2 = b|bit_select[3];
	delay(100);
	i = 0x80*z;
	i = 0x7f|i;
	P0 = c|seven_seg[counter[2]%10]&i;
	P2 = bit_select[4];
	delay(100); 
	P0 = c|seven_seg[counter[2]/10];
	P2 = bit_select[5];
	delay(100); 
}
/********************************************
              按键检测子程序 
********************************************/
uchar gote_key()
{
	if(key1 == 0 ) 				
	{ 	
		key1 = 1;  
		delay(100);
		if(key1 == 0) 
		key1_state = 1;		  //记忆key1按下的状态
	} 
	if(key1 == 1 && key1_state == 1)
	{ 
		key1_state = 0; 
		key1_flog++;
	}	
	if(key1_flog == 4) 
	key1_flog = 0;
	return(key1_flog);
}
/********************************************
              按键设置子程序 
*********************************************/
void key_setup()
{
	uchar i;
	i = gote_key();	
	if(i == 3)
	{
		y =0;	
		if(key2 == 0)	
	    { 
		    key2 = 1;  
			;; 
			if(key2 == 0) 
			key2_state= 1;	
	     } 
		if(key2 == 1 && key2_state == 1) 
		{ 
			key2_state = 0;
			counter[0]++; 
			if(counter[0]>= 60) 
			counter[0] = 0;   
		}	
		if(key3 == 0 )	
		 { 
			key3 = 1;  
			;;
			if(key3 == 0)
			key3_state = 1;
		} 
		if(key3 == 1 && key3_state == 1) 
		{ 
			key3_state = 0;
			counter[0]--; 
			if(counter[0] < 0) 
			counter[0] = 59;   
		}

		a = z*0xff; 
	}		
	else a = 0;	
//////	
	if(i == 2)
	{
		y =0;	
		if(key2 == 0)	
	    { 
		    key2 = 1;  
			;; 
			if(key2 == 0) 
			key2_state= 1;	
	     } 
		if(key2 == 1 && key2_state == 1) 
		{ 
			key2_state = 0;
			counter[1]++; 
			if(counter[1]>= 60) 
			counter[1] = 0;   
		}	
		if(key3 == 0 )	
		 { 
			key3 = 1;  
			;;
			if(key3 == 0)
			key3_state = 1;
		} 
		if(key3 == 1 && key3_state == 1) 
		{ 
			key3_state = 0;
			counter[1]--; 
			if(counter[1] < 0) 
			counter[1] = 59;   
		}

		b = z*0xff; 
	}		
	else b = 0;
	
/////
	if(i == 1)  
	{
		y =0;	
		if(key2 == 0)	
	    { 
		    key2 = 1;  
			;; 
			if(key2 == 0) 
			key2_state= 1;	
	     } 
		if(key2 == 1 && key2_state == 1) 
		{ 
			key2_state = 0;
			counter[2]++; 
			if(counter[2] > 23 && d == 0) 
			counter[2] = 0;
			if(counter[2] > 12 && d == 1) 
			counter[2] = 1;   
		}	
		if(key3 == 0 )	
		 { 
			key3 = 1;  
			;;
			if(key3 == 0)
			key3_state = 1;
		} 
		if(key3 == 1 && key3_state == 1) 
		{ 
			key3_state = 0;
			counter[2]--; 
			if(counter[2] < 0 && d == 0) 
			counter[2] = 23;
			if(counter[2] < 1 && d == 1) 
			counter[2] = 12;   
		}
		c = z*0xff;
	}
	else c = 0;
	////////
	if(key4 == 0)	
	{ 
		key4 = 1;  
		;; 
		if(key4 == 0) 
		key4_state = 1;	
	} 
	if(key4 == 1 && key4_state == 1) 
	{
		d = !d;
		key4_state = 0;

	}
	
}
/*********************************************
                     主函数  
**********************************************/
void main()
{
	timer0_init();
	while(1)
	{
		key_setup();
		display();
		counter_add1();
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -