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

📄 main.c

📁 基于LPC21xx的PCF8563(I2C RTC)实时时钟程序
💻 C
📖 第 1 页 / 共 2 页
字号:
     CurTime.min  = (Timebuf[3]-0x30)*10+(Timebuf[4]-0x30);
     CurTime.sec  = (Timebuf[6]-0x30)*10+(Timebuf[7]-0x30);
     RTCInit(CurTime);
}	 

/********************************************
写时间修改值
********************************************/
void PRTC_settime()
{
      uchar c;
      if((CurTime.year/1000)>=2) 
         c=0;
      else 
         c=1;//世纪标志
      
      PRTCsetBuf[0] = 0x00;
      PRTCsetBuf[1] = 0x10;      
      PRTCsetBuf[2] = ((CurTime.sec / 10)<<4)      | (CurTime.sec % 10);
      PRTCsetBuf[3] = ((CurTime.min / 10)<<4)      | (CurTime.min % 10);
      PRTCsetBuf[4] = ((CurTime.hour/ 10)<<4)      | (CurTime.hour% 10);
      PRTCsetBuf[5] = ((CurTime.day / 10)<<4)      | (CurTime.day % 10);
      PRTCsetBuf[6] =  (CurTime.week);
      PRTCsetBuf[7] = ((CurTime.month / 10)<<4)    | (CurTime.month% 10)| ( c<<8 );
      PRTCsetBuf[8] = (((CurTime.year%100)/10)<<4) | ((CurTime.year%100) % 10);
      PRTCsetBuf[9] = 0x00;
      PRTCsetBuf[10]= 0x00;
      PRTCsetBuf[11]= 0x00;
      PRTCsetBuf[12]= 0x00;
      PRTCsetBuf[13]= 0x00;
      PRTCsetBuf[14]= 0x03;//减少功耗考虑; 
      PRTCsetBuf[15]= 0x00;

      I2C_WriteNByte(PRTC,ONE_BYTE_SUBA,0x00,PRTCsetBuf,16);
                   
}

//////////////////////////////////////////
void ReTimeDate(void)    
{   
    TDBuf[0]=(CurTime.hour)/10+0x30;
	TDBuf[1]=(CurTime.hour)%10+0x30;
	TDBuf[3]=(CurTime.min)/10+0x30;
	TDBuf[4]=(CurTime.min)%10+0x30;
	TDBuf[6]=(CurTime.sec)/10+0x30;
	TDBuf[7]=(CurTime.sec)%10+0x30;
	TDBuf[8]='-';
	TDBuf[9]=(CurTime.week)+0x61; //星期用a~g表示,a为周日
	TDBuf[10]=((CurTime.year)%100)/10+0x30;
	TDBuf[11]=((CurTime.year)%100)%10+0x30;
	TDBuf[12]=(CurTime.month)/10+0x30;
	TDBuf[13]=(CurTime.month)%10+0x30;	
	TDBuf[14]=(CurTime.day)/10+0x30;
	TDBuf[15]=(CurTime.day)%10+0x30;	
	
}	
//////////////////Set the time to adjust to the present time!!/////////////
void settime(void)
{   
    extern void RTCInit(TimeStruct TimeDate);   
    extern void ReTimeDate(void);//精简时间日期
    
    uchar key_num,i=1;
    
    CCR = 0x00;	//时钟禁止
    
	ReTimeDate();
	lcd_disp(TDBuf,0);
	lcd_disp(TimeScreen[1],1);
	
	while(1)
	{
		lcd_dispCursor(0,i);
		while((key_num=getkey())==0){;}
		switch(key_num)
		{
			case 1:
				if (i<=6)
				i=i+3;
				//else if (i==7)
				//i+=4;
				else if (i<15)
				i+=2;
				else 
				i=1;
				lcd_dispCursor(0,i);
				break;
			case 2:
				if(i==1)
				{
					CurTime.hour+=1;
					if(CurTime.hour>=24)
						CurTime.hour=0;
				}
				else if(i==4)
				{
					CurTime.min+=1;
					if(CurTime.min>=60)
						CurTime.min=0;
				}
				else if (i==7)
				{
					CurTime.sec+=1;
					if(CurTime.sec>=60)
						CurTime.sec=0;
				
				}
				else if (i==9)
				{
					CurTime.week+=1;
					if(CurTime.week>=7)
						CurTime.week=0;
				
				}
				else if (i==11)
				{
					CurTime.year+=1;
					if(CurTime.year>=100)
						CurTime.year=0;
				
				}
				else if (i==13)
				{
					CurTime.month+=1;
					if(CurTime.month>=13)
						CurTime.month=1;
				
				}
				else if (i==15)
				{
					CurTime.day+=1;
					if(CurTime.day>=31)
						CurTime.day=1;
				
				}
				ReTimeDate();
				lcd_disp(TDBuf,0);
				break;
			case 4:
			    TDBuf[8]=0x7E;
			    lcd_disp(TDBuf,0);
			    PRTC_settime();
			    DelayNS(4000);
			    TDBuf[8]='-';
			    lcd_disp(TDBuf,0);
			    break; 	
			case 3:
				goto EndSet;
				
		}
	}
EndSet:	RTCInit(CurTime); //重新设置并启动时钟
	
}

/*
*******************************************************************************************************
** 函数名称 :GetTimeRtc()
** 函数功能 :读取RTC的时间值。
** 入口参数 :无
** 出口参数 :无
*******************************************************************************************************
*/
void GetTimeRTC (void)
{
	uint32 datas;
	uint32 times;
	uint32 bak;
		 
	times = CTIME0;			// 读取完整的时钟寄存器
	datas = CTIME1;
	
	bak = (datas >> 16) & 0xfff;	// 获取 年
	Datebuf[0] = bak / 1000 +'0';
	bak = bak % 1000;
	Datebuf[1] = bak / 100 +'0';
	bak = bak % 100;
	Datebuf[2] = bak / 10 +'0';
	Datebuf[3] = bak % 10 +'0';
	Datebuf[4] = '-';
	
	
	bak = (datas >> 8) & 0x0f;		// 获取 月
	Datebuf[5] = bak / 10 +'0';
	Datebuf[6] = bak % 10 +'0';
	Datebuf[7] = '-';
	
	bak = datas & 0x1f;				// 获取 日
	Datebuf[8] = bak / 10 +'0';
	Datebuf[9] = bak % 10 +'0';
	Datebuf[10] = ' ';
	Datebuf[11] = ' ';
	Datebuf[15] = '.';
		
	bak = (times >> 24) & 0x07;		// 获取 星期
	switch(bak)
	  { case 0:
	       Datebuf[12] = 'S';
	       Datebuf[13] = 'u';
	       Datebuf[14] = 'n';
	       break;
	    case 1:
	       Datebuf[12] = 'M';
	       Datebuf[13] = 'o';
	       Datebuf[14] = 'n';
	       break;
	    case 2:
	       Datebuf[12] = 'T';
	       Datebuf[13] = 'u';
	       Datebuf[14] = 'e';
	       break;   
	    case 3:
	       Datebuf[12] = 'W';
	       Datebuf[13] = 'e';
	       Datebuf[14] = 'd';
	       break;    
	    case 4:
	       Datebuf[12] = 'T';
	       Datebuf[13] = 'h';
	       Datebuf[14] = 'u';
	       break;
	    case 5:
	       Datebuf[12] = 'F';
	       Datebuf[13] = 'r';
	       Datebuf[14] = 'i';
	       break;     
	    case 6:
	       Datebuf[12] = 'S';
	       Datebuf[13] = 'a';
	       Datebuf[14] = 't';        
	  }
	  
	bak = (times >> 16) & 0x1f;		// 获取 小时
	Timebuf[0] = bak / 10 +'0';
	Timebuf[1] = bak % 10 +'0';
	Timebuf[2] = ':';
	
	bak = (times >> 8) & 0x3f;		// 获取 分钟
    Timebuf[3] = bak / 10 +'0';
	Timebuf[4] = bak % 10 +'0';
	Timebuf[5] = ':';
		
	bak = times & 0x3f;				// 获取 秒钟
	Timebuf[6] = bak / 10 +'0';
	Timebuf[7] = bak % 10 +'0';
	
}

void timeshow(void)
{   
    GetTimeRTC();
    Timebuf[8]='S';
    lcd_disp(Datebuf,0);
    lcd_disp(Timebuf,1);
}
/*
*******************************************************************************************************
** 函数名称 :RTCInit()
** 函数功能 :初始化实时时钟
** 入口参数 :无
** 出口参数 :无
*******************************************************************************************************
*/
void RTCInit (TimeStruct TimeDate)
{
    TimeStruct timestruct = TimeDate; 
    
	PREINT = Fpclk / 32768 - 1;	// 设置基准时钟分频器
	PREFRAC = Fpclk - (Fpclk / 32768) * 32768;
	
	CCR   = 0x00;	// 禁止时间计数器
	
	YEAR  = timestruct.year;
	MONTH = timestruct.month;
	DOM   = timestruct.day;
	DOW   = timestruct.week;
	HOUR  = timestruct.hour;
	MIN   = timestruct.min;
	SEC   = timestruct.sec;
	
	//CIIR = 0x01;	// 设置秒值的增量产生1次中断
	CCR  = 0x01;	// 启动RTC
}


/*
*********************************************************************************************************
** 函数名称 :main()
** 函数功能 :Uart1试验。
** 调试说明 :
*********************************************************************************************************
*/
int main (void)
{   
    uchar key_num;   
   
    PINSEL0 = 0x00000000;	
    PINSEL1 = 0x00000000;		// 设置管脚P0[16:31]连接GPIO
	PINSEL2 = PINSEL2 & (~0x08);// P1[25:16]连接GPIO
	IO1DIR = LCD8|LCDEN|LCDOFF; // 设置控制口为输出
	IO0DIR = LCDRS|LCDRW;	
    IO1CLR = LCDEN;    
    lcd_init();
    
    CurTime.year = 2007;
    CurTime.month = 8;
    CurTime.day = 21;
    CurTime.week = 2;
    CurTime.hour = 12;
    CurTime.min = 0;
    CurTime.sec = 0;
    RTCInit (CurTime);  
  
	IRQEnable();							// 打开中断					
	I2cInit(400000);
    //PRTC_settime();
    /*while(1)
    {
    PRTC_gettime(); 
    lcd_disp(Datebuf,0);
	lcd_disp(Timebuf,1);  
    }*/
    
    Start:  
  	                            for(;;)
								{													
									while((key_num=getkey())==0){
									PRTC_gettime(); 
									Timebuf[8]='P';
                                    lcd_disp(Datebuf,0);
	                                lcd_disp(Timebuf,1);}
									switch(key_num)
									{	
										case 1:
										    while((key_num=getkey())==0){timeshow();}  
									    	break;
										case 2: settime();											
											break;
										case 4: timesyn();
										    break;  	
										case 3: goto P_break;
										
										default: break;
									}
								}
								
P_break: goto Start;
    return 0;

}


/*********************************************************************************************************
**                            End Of File
********************************************************************************************************/

⌨️ 快捷键说明

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