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

📄 rtc.c

📁 实时钟RTC芯片DS1302的全部C语言驱动程序
💻 C
字号:
/*
*********************************************************************************************************
*                                          RTC driver                                     
*
*                                   (c) Copyright 2004, JinTong, BeiJing
*                                          All Rights Reserved
*
*
* File         : 
* Description  : 
* Originally by: Mafy
*********************************************************************************************************
*/

#include "Includes.h"
									   		
									   		
static void SetRstHigh(void)
{
	*PIO_SODR = 0x000000400;
}

static void SetRstLow(void)
{
	*PIO_CODR = 0x000000400;
}

static void Delay(void)
{
	int i;
	for(i = 0; i < 10; i++);
}

/*
*******************************************************************************************************
* Shift Data from ARM into DS1302 
*******************************************************************************************************
*/
void DS_Shift_In(unsigned char bIn)
{
	unsigned char i;

	for( i = 0; i < 8; i++ )
	{
		*PIO_CODR = 0x10000000; 			/* low logic level 										*/
		
		if( bIn & 0x01 ) 					/* change data 											*/
		{
			*PIO_SODR = 0x20000000;  			
		}
		else
		{
			*PIO_CODR = 0x20000000; 
		}
		 									/* LSB first 											*/
		bIn = bIn >> 1; 					/* next bit 											*/					
		
		
		*PIO_SODR = 0x10000000;				/* raising edge in 										*/
		
	} 
}

/*
*******************************************************************************************************
* Shift Data from DS1302 to ARM 
*******************************************************************************************************
*/
unsigned char DS_Shift_Out(void)
{
	unsigned char i;
	unsigned int  bData;
	unsigned char Dt = 0;

	*PIO_ODR = 0x20000000;		    		/* release DS_IO 										*/ 
	
	for( i = 0; i < 8; i++ )
	{
		*PIO_SODR = 0x10000000;	   			/* high logic level 									*/
		Delay(); 				   			/* delay 												*/
		
		*PIO_CODR = 0x10000000;	   			/* failing edge data removed 							*/
		Delay(); 				   			/* delay 												*/
		
		bData = *PIO_PDSR;
		bData = (bData >> 29) & 0x01;
		Dt |=  (bData << i);
	} 
	
	*PIO_OER = 0x20000000;
	
	return (Dt);
}


/*
*******************************************************************************************************
* Read One Byte from DS1302 to MCU 
* Written in 25/10,2000. HIT 
* cr: Read Command 
* 0x81-Read Second: CH XXX,XXXX(BCD) 
* 0X83-Read Minute: 0 XXX,XXXX(BCD) 
* 0X85-Read Hour: 12/24 0 A/P x,xxxx  
* 0X87-Read Date: 00 XX,XXXX(BCD)  
* 0X89-Read Month: 000 X,XXXX(BCD)  
* 0X8B-Read Week: 00000 XXX(BCD)  
* 0X8D-Read Year: xxxx XXX(BCD) 
*******************************************************************************************************
*/
unsigned char DS_Read(unsigned char cr)
{
	unsigned char dd=0;

	SetRstLow(); 						/* initializing 											*/
	Delay(); 
	
	*PIO_CODR = 0x10000000;				/* SCLK low logic level 									*/
	Delay();
	
	SetRstHigh(); 						/* Enabled 													*/

	DS_Shift_In( cr );	 				/* Write Command Byte 										*/
	dd = DS_Shift_Out(); 				/* Read Data 												*/

	SetRstLow();						/* Disabled 												*/
	
	*PIO_SODR = 0x10000000; 
	
	return ( dd );
}

/*
*******************************************************************************************************
* Read One Byte from DS1302 to MCU 
* Written in 25/10,2000. HIT 
* ord: Write Command,dd-data for writting
* 0x80-Write Second: CH XXX,XXXX(BCD) 
* 0X82-Write Minute: 0 XXX,XXXX(BCD) 
* 0X84-Write Hour: 12/24 0 A/P x,xxxx  
* 0X85-Write Date: 00 XX,XXXX(BCD)  
* 0X86-Write Month: 000 X,XXXX(BCD)  
* 0X8a-Write Week: 00000 XXX(BCD)  
* 0X8c-Write Year: xxxx XXX(BCD)  
*******************************************************************************************************
*/
void DS_Write(unsigned char ord,unsigned char dd)
{
	SetRstLow(); 
	Delay();
	
	*PIO_CODR = 0x10000000; 
	Delay();
	
	SetRstHigh(); 						/* Enable 													*/
	
	DS_Shift_In( ord ); 				/* Write Command 											*/
	DS_Shift_In( dd ); 					/* Write Data 												*/
	
	SetRstLow();
	
	*PIO_SODR = 0x10000000; 
}



/*
*******************************************************************************************************
* Set DS1302 Clock Value *
* Written in 25/10,2000. HIT *
* dt[6]-dt[0]: Week,Year,Month,Date * 
* Hour,Minute,Second * 
*******************************************************************************************************
*/
void DS_SetClock(unsigned char dt[7])
{
	DS_Write(0x8E, 0); 					/* Disabled Write Protect 									*/
	DS_Write(0x80, 0x80); 				/* Disabled Clock 											*/

	DS_Write(0x8a, dt[6]); 				/* Week Day BCD 											*/
	DS_Write(0x80, dt[5] & 0x7F);  		/* Second : BCD 											*/
	DS_Write(0x82, dt[4]); 				/* Minute : BCD 											*/  
	DS_Write(0x84, dt[3]); 				/* Hour(24) BCD 											*/ 
	DS_Write(0x86, dt[2]); 				/* Date : BCD 												*/ 
	DS_Write(0x88, dt[1]); 				/* Month : BCD 												*/ 
	DS_Write(0x8c, dt[0]); 				/* Year : BCD 												*/ 
	
										/* MSB:0 Enabled Clock 										*/ 
	DS_Write(0x8E,0x80); 				/* Enabled Write Protect 									*/
}



/*
*******************************************************************************************************
* Read DS1302 Clock Value 
* Written in 25/10,2000. HIT 
* dt[6]-dt[0]: Week,Year,Month,Date  
* Hour,Minute,Second 
*******************************************************************************************************
*/
void DS_ReadClock(unsigned char dt[7])
{
	dt[0] = DS_Read(0x8d); 				/* Year 													*/ 
	dt[1] = DS_Read(0x89); 				/* Month 													*/ 
	dt[2] = DS_Read(0x87); 				/* Date 													*/ 
	dt[3] = DS_Read(0x85) & 0x3F; 		/* Hour 													*/ 
	dt[4] = DS_Read(0x83) & 0x7F; 		/* Minute 													*/ 
	dt[5] = DS_Read(0x81) & 0x7F; 		/* Second 													*/
	dt[6] = DS_Read(0x8b); 				/* Week 													*/ 
}



/*
*******************************************************************************************************
* convert time to milisecond
*******************************************************************************************************
*/

unsigned int g_uw_Tod[2];
unsigned char g_uc_ClkBase[7] = {0x5, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00};

void CalTod(void)
{ 	
	int i;
	unsigned char t[7];
	unsigned char Tmp = 0;
	
	g_uw_Tod[0] = 0;
 	g_uw_Tod[1] = 0;
 	
	DS_ReadClock(t);
	
	//BCD to Hex
	for (i = 0; i < 7; i++)
	{
		Tmp =	t[i] ;
		t[i] = (t[i] >> 4) * 10 ;
		Tmp  = Tmp & 0xF;
		t[i] = t[i] + Tmp;
	}
	
	//
	for(i = 0; i < 7; i++)
	{
		t[i] = t[i] - g_uc_ClkBase[i];
	}
	
	//
 	g_uw_Tod[1] = t[0] * 12 + t[1];
 	g_uw_Tod[1] = g_uw_Tod[1] * 365 + t[2];
 	g_uw_Tod[1] = g_uw_Tod[1] * 24 + t[3];
 	g_uw_Tod[1] = g_uw_Tod[1] * 60 + t[4];
 	g_uw_Tod[1] = g_uw_Tod[1] * 60 + t[5];
 	g_uw_Tod[1] = g_uw_Tod[1] * 1000; 	
}


/*
*******************************************************************************************************
* RTC test
*******************************************************************************************************
*/
void testRtc(void)
{ 
	unsigned char tt[7] = {0x06, 0x01, 0x08, 0x09, 0x00, 0x00, 0x03};
	unsigned char tm[7];
	unsigned char Tmp = 0;
	int i ;	

	DS_SetClock(tt);
	
	while(1)
	{
		DS_ReadClock(tm);
		
		for (i = 0; i < 7; i++)
		{
			Tmp =	tm[i] ;
			tm[i] = (tm[i] >> 4) * 10 ;
			Tmp  = Tmp & 0xF;
			tm[i] = tm[i] + Tmp;
			
		}
	}
}


/*
*******************************************************************************************************
* 											the file end
*******************************************************************************************************
*/




























⌨️ 快捷键说明

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