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

📄 1302

📁 ds1302与数码管时间可调
💻
字号:
#include <reg51.h>//头文件
#include <intrins.h>
#include "DS1302.h"
#define uchar unsigned char
#define uint  unsigned int
code duanma[11] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf};
code weima[8] = {0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
uint  jishu;
uchar dot,flash,a,b,c,d;//
uchar key1_state,key2_state,key3_state,key4_state;
static uchar key1_flag=0;		//定义静态变量
sbit    key1 = P3^0;     			//定义4个按键的端口
sbit    key2 = P3^1;
sbit    key3 = P3^2; 
sbit    key4 = P3^3;
/************时间延迟函数**************/
//void delay(int delaytime)
//{
//	while(delaytime--)
//	{
//	   _nop_();
//	}
//}
// 毫秒延时
void delayms(unsigned int delaytime)
{
	uint i,j;
		for( i = 0;i < delaytime;i++ )
			for(j = 0;j < 120;j++);
}
//消隐函数		  
void xiaoying()
{
  delayms(1);
  P0=0;
  delayms(1);
}
/***********获取按键状态函数***************/
uchar get_key()
{
   if(key1 == 0 ) 				
	{ 	
		key1 = 1;  
		delayms(1);
		if(key1 == 0) 
		key1_state = 1;		  //记忆key1按下的状态
	} 
if(key1 == 1 && key1_state == 1)
	{ 
		key1_state = 0; 
		key1_flag++;
	}	
	if(key1_flag == 4) 
	key1_flag = 0;
	return(key1_flag);
}
/************* T0的中断服务函数****************/
void timer0_isr(void) interrupt 1
{
  // uchar i;
  TR0 = 0;//给T0重新置初值 
  TH0 = (65536-5000)/256;//
  TL0 = (65536-5000)%256;// 
  TR0 = 1;
  jishu++;//进入中断的次数 
  if(jishu == 100)
     {
	  dot =! dot;
		jishu = 0;
	
     } 
}
/************* T0初始化函数,每5毫秒进一次中断************/
void timer0_init()
{
	  EA = 0;
	  TR0 = 0;
	  TMOD &= 0xF0;
	  TMOD |= 0x01;
	  TL0 = (65536-5000)%256;
	  TH0 = (65536-5000)/256;
      PT0 = 0;
	  ET0 = 1;
	  TR0 = 1;
	  EA = 1;
}
/*************单片机初始化函数*****************/
void Init_mcu(void)
{
	timer0_init();//中断T0初始化
	init1302();	//
	key1_flag = 0;
}
/***********按键和时间设置函数*************/
void key_setup()
{
	uchar i;
	i = get_key();	
	if(i == 3)//i=3;adjust second ;调整秒
	{
		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 = dot*0xff; 
	}		
else a = 0;	
	if(i == 2)//i=2;adjust the minute;调整分钟
	{
		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 = dot*0xff; 
	}		
	else b = 0;
	if(i == 1)  //i=1;adjust the hour;调整小时
	{
		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 = dot*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 display()
{
	uchar i;
	for(i=0;i<8;i++)
	{
	P0 = 0x00;
	P2 = weima[7];
	P0 = a|~duanma[counter[0]%10];
	delayms(1);
	P0 = 0x00;
	P2 = weima[6];
    P0 = a|~duanma[counter[0]/10];
    delayms(1);


	P0 = 0x00;
	P2 = weima[5];
	P0=~duanma[10];
	delayms(1);


	P0 = 0x00;
	P2 = weima[4];
    P0 = b|~duanma[counter[1]%10];  
	delayms(1);
	 P0 = 0x00;
	P2 = weima[3];
	P0 = b|~duanma[counter[1]/10];
	delayms(1);
	//i = 0x80*dot;
	//i = 0x7f|i;

	
	P0 = 0x00;
	P2 = weima[2];
    P0=~duanma[10];
    delayms(1);


	P0 = 0x00;
    P2 = weima[1];
	P0 = c&~duanma[counter[2]%10];
	delayms(1); 
	P0 = 0x00;
	P2 = weima[0];
	P0 = c&~duanma[counter[2]/10];
    delayms(1);	
	}

}
void display1()
{
	uchar i;
	for(i=0;i<8;i++)
	{
	P0 = 0x00;
	P2 = weima[7];
	P0 = a|~duanma[counter[0]%10];
	delayms(1);
	P0 = 0x00;
	P2 = weima[6];
    P0 = a|~duanma[counter[0]/10];
    delayms(1);


	P0 = 0x00;
	P2 = weima[5];
	P0=~duanma[10];
	delayms(1);


	P0 = 0x00;
	P2 = weima[4];
    P0 = b&~duanma[counter[1]%10];  
	delayms(1);
	 P0 = 0x00;
	P2 = weima[3];
	P0 = b&~duanma[counter[1]/10];
	delayms(1);
	//i = 0x80*dot;
	//i = 0x7f|i;

	
	P0 = 0x00;
	P2 = weima[2];
    P0=~duanma[10];
    delayms(1);


	P0 = 0x00;
    P2 = weima[1];
	P0 = c|~duanma[counter[2]%10];
	delayms(1); 
	P0 = 0x00;
	P2 = weima[0];
	P0 = c|~duanma[counter[2]/10];
    delayms(1);	
	}

}

void display2()
{
	uchar i;
	for(i=0;i<8;i++)
	{
	P0 = 0x00;
	P2 = weima[7];
	P0 = a&~duanma[counter[0]%10];
	delayms(1);
	P0 = 0x00;
	P2 = weima[6];
    P0 = a&~duanma[counter[0]/10];
    delayms(1);


	P0 = 0x00;
	P2 = weima[5];
	P0=~duanma[10];
	delayms(1);


	P0 = 0x00;
	P2 = weima[4];
    P0 = b|~duanma[counter[1]%10];  
	delayms(1);
	 P0 = 0x00;
	P2 = weima[3];
	P0 = b|~duanma[counter[1]/10];
	delayms(1);
	//i = 0x80*dot;
	//i = 0x7f|i;

	
	P0 = 0x00;
	P2 = weima[2];
    P0=~duanma[10];
    delayms(1);


	P0 = 0x00;
    P2 = weima[1];
	P0 = c|~duanma[counter[2]%10];
	delayms(1); 
	P0 = 0x00;
	P2 = weima[0];
	P0 = c|~duanma[counter[2]/10];
    delayms(1);	
	}

}
/**************主函数****************/
void main()
{
    uchar i;
	Init_mcu(); 
while(1)    //无限循环
	{
	key_setup();
	if(key1_flag == 1)
		{
			display();
		}
	if(key1_flag==2)
	{
	  display1();
	}
	if(key1_flag == 3 )
	{
	 display2();
	}
	if(key1_flag ==4)
		{
			TR0=1;
			key1_flag=0;
			sec=(counter[0]/10)*16+counter[0]%10; // BCD 码转换成 HEX
			min=(counter[1]/10)*16+counter[1]%10; // BCD 码转换成 HEX
			hou=(counter[2]/10)*16+counter[2]%10; // BCD 码转换成 HEX
			set_time();
			TR0=1;
		}

    if(key1_flag == 0)
   {		
       get_time();
		flash = 0x7f*dot;
		flash = flash|0x80;
		P0 = 0x00;
		P2 = weima[i];
		if(i == 2 || i == 5)
		P0 = ~duanma[time1[i]]&flash;
		else
		P0 = ~duanma[time1[i]];
	  }
	i++;
	if(i == 8)i = 0; 
	xiaoying();
	}
}

⌨️ 快捷键说明

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