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

📄 1.c

📁 液晶时钟
💻 C
字号:
   #include <REG52.H>
#include <ABSACC.H>   
#define uchar unsigned char
#define uint  unsigned int
/*******12864液晶配置************/
   sbit RS=P2^4;
   sbit RW=P2^5;
   sbit  E=P2^6;
   sbit RST=P2^3;
/*******1302配置*************/
   sbit  T_RST=P1^2;   
   sbit   T_IO=P1^1;  
   sbit T_SCLK=P1^0;  
   uchar bdata AB;
   uchar bdata AD;	 
   sbit AB0=AB^0;
   sbit AB7=AB^7;
   sbit AD0=AD^0;
   sbit AD7=AD^7;
/************************
    定义全局变量
*************************/
sbit key1=P3^0;
sbit key2=P3^1;
sbit key3=P3^2;
sbit key4=P3^3;
sbit beep=P3^0;

uchar key;    //按键值
uchar set=0;  // 功能键值
/**************************
   定义1302时间数据部分
***************************/ //秒 分 时 日 月 星期 年
//用于保存从1302中取得的时间数据
uchar data time[7]={0x00,0x50,0x18,0x23,0x04,0x03,0x08};     
uchar code shuzi[15]={0x30,0x31,0x32,0x33,0x34,0x35,
                      0x36,0x37,0x38,0x39,0x20,0x2e,
					  0x2b,0x2d,0x3a}; //空格 . + - :
uchar data alarm[2]; //用于存储闹钟时间: 时 分
/********************************
        模块数据定义部分
********************************/
uchar code Address[]=
{
0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,      /*第一行汉字位置*/
0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,      /*第二行汉字位置*/
0x88,0x89,0x8a,0x8b,0x8c,0x8d,0x8e,0x8f,      /*第三行汉字位置*/
0x98,0x99,0x9a,0x9b,0x9c,0x9d,0x9e,0x9f,      /*第四行汉字位置*/
};

uchar code Chinese01[]={"日期:"};
uchar code Chinese02[]={"时间:"};
uchar code Chinese03[]={"星期:"};
uchar code Chinese04[]={"时钟"};
uchar code Chinese05[]={"调时"};
uchar code Chinese06[]={"闹钟"};
/************ 延时n*1ms ******************/
void delay(uint k)
{  uint i,j;
   for(i=0;i<k;i++)
    {  for(j=0;j<0x60;j++)
	     {;}
	 }
}

/******数据转换***********
	  BCD转换为十进制 
**************************/
turnbcd(unsigned char bcd)      
{ 
      unsigned char n_n;
      n_n=(bcd&0xf0)/16*10+(bcd&0x0f);
      return(n_n); 
} 

/*--------------------------------------RT-12864 --------------------------------------------*/
void write_com(unsigned char com_byte)    //写命令到LCD	0000  RST    E    RW     RS 
{														 // 1    1     0    0
    delay(1);  
    RS=0; RW=0;
    E=1;  RST=1;
    P0=com_byte;
    E=0;
}
void write_data(unsigned char dat_byte)  //写数据到LCD   0000 // 1    1      0   1
{ 
    delay(1);
    RS=1; RW=0;
    E=1;  RST=1;
    P0=dat_byte;
    E=0;
 }	
/**********************
写汉字功能a地址和要写
第c个数字就可以连续写完 b个数
***********************/
void Inhanzi(uchar *str,uchar a,uchar b,uchar c)
{
   delay(1);
   write_com(0x30);
   while(c)
   {
     delay(1);
     write_com(Address[a]);
	 write_data(str[b]);
	 b++;
	 write_data(str[b]);
	 b++;
	 a++;
	 c--;
	  }
}

/***********************
  a写数据的地址 
  b 、c为要写的数据数字(0-9)
  显示0-9功能
***********************/
void xianshi(unsigned char a,unsigned char b,unsigned char c) 
{
  write_com(Address[a]);
  write_data(shuzi[b]);
  write_data(shuzi[c]);
}

/*----------------------------------------------------1302程序------------------------------------------------*/
/*********************
    写数据   低位开始 
**********************/
 void inputbyte(unsigned char a_a)    
 {   
    unsigned char  b_b;
      AB=a_a;
	  for(b_b=8; b_b>0; b_b--)
     {
      T_IO = AB0;
      T_SCLK = 1;
      T_SCLK = 0;
      AB = AB >> 1; 
     } 
 }
/********************
  读取字节有返回值
*********************/
outputbyte()  
{  
	unsigned char  b_b;
    for(b_b=7; b_b>0; b_b--)
    {   
        AD7=T_IO;
        T_SCLK = 1; 
        T_SCLK = 0;
       	AD = AD>>1; 
    } 
    return(AD); 
}
/*********************
往1302里写数据参数分别为
     地址 和 数据
**********************/
void w1302(unsigned char add,unsigned char dat)
{	
    T_RST=0; 
    T_SCLK=0; 
    T_RST=1; 
    inputbyte(add); 
    inputbyte(dat);   
    T_SCLK = 1; 
    T_RST = 0;
} 
/**********************
读取1302某地址的数据   
返回值为该地址的数据
**********************/	
unsigned char r1302(unsigned char add)     
{
    unsigned char ucdata;
    T_RST=0; 
    T_SCLK=0; 
    T_RST=1; 
    inputbyte(add);  
    ucdata = outputbyte();  
    T_SCLK = 1; 
    T_RST = 0;
    return(ucdata);
}
/********************
设置初始时间 秒 分 时
     日 月 星期 年
**********************/
void set1302()   
{   
    unsigned char  q_q=0;
	unsigned char  b_b;
    unsigned char add = 0x80;
    w1302(0x8e,0x00);
    for(b_b =7; b_b>0; b_b--)
    { 	
	    w1302(add,time[q_q]);  
        add +=2;              
		q_q++;
    }
}
/********************
保存当前时间格式为: 
秒 分 时 日 月 星期 年 
********************/
void get1302()  
{	
  unsigned char  b_b;
  unsigned char ducaddr = 0x81;//读数据 连续读数  放到数组里
  for (b_b=0; b_b<7; b_b++)
  {
        time[b_b]=turnbcd(r1302(ducaddr)); //格式为: 秒 分 时 日 月 星期 年 
        ducaddr += 2;
  }
}
/*-------------------------------------------------调时间部分-------------------------------------------------*/

void timexianshi(void)	   //显示时间 
{
	get1302();
	xianshi(11,time[6]/10,time[6]%10); //年
	xianshi(12,13,time[4]/10);         //月
	xianshi(13,time[4]%10,13);         //月
	xianshi(14,time[3]/10,time[3]%10); //日
	xianshi(19,time[2]/10,time[2]%10);//时
	xianshi(20,14,time[1]/10);        //分
	xianshi(21,time[1]%10,14);	      //分
	xianshi(22,time[0]/10,time[0]%10);//秒
	xianshi(27,10,time[5]);
}
/******************************
          调时间函数
*******************************/
void tiaoshi_year(void)		 //对年的调整 
{  
   Inhanzi(Chinese05,3,0,2); //调时界面
   Inhanzi(Chinese01,8,0,3);
   Inhanzi(Chinese02,16,0,3); 
   Inhanzi(Chinese03,24,0,3);
   Inhanzi(Chinese03,24,0,3); 
   xianshi(11,time[6]/10,time[6]%10);
   key=time[6];
    if(key2 == 0) { delay(80); if(key2==0)
				        {
					  key++;
					  time[6]=key;      		 
					  xianshi(11,time[6]/10,time[6]%10); 
					     }
					}
	if(key1 == 0) { delay(80); if(key1==0)
					    { 
					  key--;  
					  time[6]=key;      		
					  xianshi(11,time[6]/10,time[6]%10);  
						   }
				   }
}

void tiaoshi_month(void)	  //对月的调整
{  
   Inhanzi(Chinese05,3,0,2); //调时界面
   Inhanzi(Chinese01,8,0,3);
   Inhanzi(Chinese02,16,0,3); 
   Inhanzi(Chinese03,24,0,3);
   Inhanzi(Chinese03,24,0,3); 
   xianshi(12,13,time[4]/10);        
   xianshi(13,time[4]%10,13);     
   key=time[4];
    if(key2 == 0) { delay(80); if(key2==0)
				        {
					  key++;
					  time[4]=key;      		
					  xianshi(12,13,time[4]/10);        
       	              xianshi(13,time[4]%10,13);     
					     }
					}
	if(key1 == 0) { delay(80); if(key1==0)
					    { 
					  key--;  
					  time[4]=key;      		
					   xianshi(12,13,time[4]/10);        
       	               xianshi(13,time[4]%10,13);  
						   }
				   }
} 

void tiaoshi_day(void)	      //对日的调整
{  
   Inhanzi(Chinese05,3,0,2); //调时界面
   Inhanzi(Chinese01,8,0,3);
   Inhanzi(Chinese02,16,0,3); 
   Inhanzi(Chinese03,24,0,3);
   Inhanzi(Chinese03,24,0,3);
   xianshi(14,time[3]/10,time[3]%10); 
   key=time[3];
    if(key2 == 0) { delay(80); if(key2==0)
				        {
					  key++;
					  time[3]=key;      		  
					  xianshi(14,time[3]/10,time[3]%10);
					     }
					}
	if(key1 == 0) { delay(80); if(key1==0)
					    { 
					  key--;  
					  time[3]=key;      		 
					  xianshi(14,time[3]/10,time[3]%10);
						   }
				   }
} 

void tiaoshi_hour(void)		 //对时的调整
{  
   Inhanzi(Chinese05,3,0,2); //调时界面
   Inhanzi(Chinese01,8,0,3);
   Inhanzi(Chinese02,16,0,3); 
   Inhanzi(Chinese03,24,0,3);
   Inhanzi(Chinese03,24,0,3); 
   xianshi(19,time[2]/10,time[2]%10);
   key=time[2];
    if(key2 == 0) { delay(80); if(key2==0)
				        {
					  key++;
					  time[2]=key;      		  
					  xianshi(19,time[2]/10,time[2]%10); 
					     }
					}
	if(key1 == 0) { delay(80); if(key1==0)
					    { 
					  key--;  
					  time[2]=key;      		
					  xianshi(19,time[2]/10,time[2]%10); 
						   }
				   }
}

void tiaoshi_minute(void)	 //对分的调整
{ 
   Inhanzi(Chinese05,3,0,2); //调时界面
   Inhanzi(Chinese01,8,0,3);
   Inhanzi(Chinese02,16,0,3); 
   Inhanzi(Chinese03,24,0,3);
   Inhanzi(Chinese03,24,0,3);
   xianshi(20,14,time[1]/10);       
   xianshi(21,time[1]%10,14);
   key=time[1];
    if(key2 == 0) { delay(80); if(key2==0)
				        {
					  key++;
					  time[1]=key;      		
					  xianshi(20,14,time[1]/10);       
	                  xianshi(21,time[1]%10,14);	   
					     }
					}
	if(key1 == 0) { delay(80); if(key1==0)
					    { 
					  key--;  
					  time[1]=key;      		 	 
					  xianshi(20,14,time[1]/10);        
	                  xianshi(21,time[1]%10,14);	     
						   }
				   }
}

void tiaoshi_week(void)	      //对星期的调整 
{ 
   Inhanzi(Chinese05,3,0,2); //调时界面
   Inhanzi(Chinese01,8,0,3);
   Inhanzi(Chinese02,16,0,3); 
   Inhanzi(Chinese03,24,0,3);
   Inhanzi(Chinese03,24,0,3);
   xianshi(27,10,time[5]);
   key=time[5];
    if(key2 == 0) { delay(80); if(key2==0)
				        {
					  key++;
					  time[5]=key;      		 
					  xianshi(27,10,time[5]); 
					     }
					}
	if(key1 == 0) { delay(80); if(key1==0)
					    { 
					  key--;  
					  time[5]=key;      		 
					  xianshi(27,10,time[5]); 
						   }
				   }
} 
/*******************
      闹钟调节 
*******************/  
void alarm_clock()
{
   Inhanzi(Chinese06,3,0,2); //闹钟界面
   Inhanzi(Chinese01,8,0,3);
   Inhanzi(Chinese02,16,0,3); 
   Inhanzi(Chinese03,24,0,3); 
   xianshi(19,alarm[0]/10,alarm[0]%10);
   xianshi(20,14,alarm[1]/10);       
   xianshi(21,alarm[1]%10,14); 
   if(key2 == 0) { delay(80); if(key2==0)
				     {
				    key++; if(key==24) key=0; //调节时
					alarm[0]=key;  //存储时    		  
					xianshi(19,alarm[0]/10,alarm[0]%10); //显示调节
					     }
				   }
   if(key1 == 0) { delay(80); if(key2==0)
				     {
				    key++; if(key==60) key=0; //调节分
					alarm[1]=key;  //存储分    		
					xianshi(20,14,alarm[1]/10); //显示调节      
	                xianshi(21,alarm[1]%10,14);	   
					    }
					}

}

/*-------------------------------------------待机状态--------------------------------------------------------*/
void daiji(void)
{ 
  Inhanzi(Chinese04,3,0,2); //待机界面
  Inhanzi(Chinese01,8,0,3);
  Inhanzi(Chinese02,16,0,3); 
  Inhanzi(Chinese03,24,0,3); 
  timexianshi();   //时间显示
  //检测闹钟
  if((time[2]==alarm[0])&&(time[1]==alarm[1]))
    beep=0;	              //闹钟响
  if(time[1]==alarm[1]+1) 
    beep=1;               //闹钟响1分钟后停
}

//外中断1 子程序
void INT1_set() interrupt 2 using 0
{
  delay(80);
  if(key4==0) set++;		//功能键值递增
  if(set>=9)set=0;		//键值的范围0~8
again:if(key4==0)goto again;	//若按键未释放,则等待
}

/*-----------------------------------------------初始化部分----------------------------------------------------*/
void initial(void)
{
   key1=key2=key3=key4=1;
   beep=1;
   write_com(0x30); //8BitMCU,基本指令集合
   write_com(0x03); //AC归0,不改变DDRAM内容
   write_com(0x0c);	//显示ON,游标OFF,游标位反白OFF
   write_com(0x01);	//清屏,AC归0
   write_com(0x06);	//写入时,游标右移动
   set1302();       //初始化对1302进行设置
}
																													
void main(void)
{
	initial();  //初始化
	EA=1;		//开启全局中断
	PX1=1;		//外中断1 设为最高优先级
	EX1=1;		//开启外中断1
    while(1)
    { 
	  switch(set)  //判断 set 的值
	  {
	    case 0: daiji();          break;   //待机界面

		case 1: tiaoshi_year();   break;   //调时  年
		case 2: tiaoshi_month();  break;   //      月    
		case 3: tiaoshi_day();    break;   //	   日
		case 4: tiaoshi_hour();   break;   //	   时
		case 5: tiaoshi_minute(); break;   //	   分
		case 6: tiaoshi_week();   break;   //	   秒
		case 7: alarm_clock();    break;   //调节闹钟
		case 8: set1302();        break;   //保存时间
		default: break;
	   }
     }		 
} 

⌨️ 快捷键说明

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