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

📄 datatime.c

📁 c++开发的DS1302时钟芯片的程序,完全正确,可直接用
💻 C
字号:
/***************************************************************************
			HMDP HM1801 电池检测分析仪	DataTime.c	
									
  Copyright (c) 2003-2004 by Shenzhen HuaMei Digital Power Technology Co.,Ltd(HMDP).
  All Rights Reserved.

  Developer: L4157
  Last update date: 2.16/2004
  Function:HM1801实时时钟(DS1302)服务函数.  
  Note: 1. RTC初始化;
  	2. 写RTC;
  	3. 读RTC;
	4.读日期时间;
	5.写日期时间.
*******************************************************************************/

 #include "D:\hm1801\hm1801soft\hm1801\header\hm1801.h"


#define RTCCS		4
#define UNCSRTC()	PORTB &= ~(1<<RTCCS)
#define CSRTC()		PORTB |=  (1<<RTCCS)
#define TSDA		5
#define TSCL 		6

struct rtc_bcd {								/* BCD formats */
   unsigned char sec;
   unsigned char min;  
   unsigned char hour;   
   unsigned char date;
    
   unsigned char  month;
   unsigned char  day;
   unsigned char  year;      
   unsigned char  control;     
   unsigned char  tc;
} rtc;

void Send_RTC(unsigned char sdata);
char Receive_RTC();
void us_delay(char);

void us_delay(char us){
	unsigned char i, clk;
	
	for(clk=0;clk<us;clk++){
		for(i=0;i<SYSTEM_OS_CLK;i++)
			;
	}

}


unsigned char BCDTOCHAR(unsigned char c)
{unsigned char i;

 i=(c&0xf0);
 i>>=4;
 i*=10;
 i+=(c&0x0f);
 return i;

}

unsigned char CHARTOBCD(unsigned char c)
{unsigned char i,j;
 
 i=c%10;
 j=c/10;
 j<<=4;
 i+=j;
 
 return i;
}

void init_rtc(void)
{     
	
	
	write_rtc(0x8E,0);       //Clear the write protect bit
        write_rtc(0x80,0);   //Enable the clock  
        write_rtc(0x90,0xab);    //Enable the TC & the TC current is 0.06mA
        
}


/****************************************************************************/
/*	Name:										*/
/*		get_date_time								*/
/*											*/
/*	Function:									*/
/*		Reads the RTC clock chip and returns the time in the structure		*/
/*		pointed to by time.							*/
/*											*/
/*	Synopsis:									*/
/*		UINT get_date_time(struct sys_time *, struct sys_date *)		*/
/*											*/
/*	In:										*/
/*		struct sys_time	*theTime; pointer to the time structure.		*/
/*		struct sys_date	*theDate; pointer to the date structure.		*/
/*											*/
/*	Returns:									*/
/*		Nothing									*/
/****************************************************************************/

UINT get_date_time(struct sys_time *theTime, struct sys_date *theDate)
{ 
	if (read_rtc(0xFD)==0x55) { 						/* clock appears to be operational	*/
		read_rtc(0xBF);	
		if(rtc.hour&0x80)						/* read the clock chip			*/
		{    if(rtc.hour&0x20) 
		          theTime->hour    = 	12;
		     else
		          theTime->hour    =     0;
		   	
		     theTime->hour  +=BCDTOCHAR(rtc.hour&0x1f);

		}			
		else
		      theTime->hour    = BCDTOCHAR(rtc.hour);                 	/* extract the time 			*/
		theTime->minute  = BCDTOCHAR(rtc.min);
		theTime->second  = BCDTOCHAR(rtc.sec);
		theTime->elapsed = (theTime->hour*60) + theTime->minute;
		theDate->year	 = BCDTOCHAR(rtc.year) + 2000;
		theDate->month	 = BCDTOCHAR(rtc.month);
		theDate->day	 = BCDTOCHAR(rtc.date);
		theDate->dow	 = rtc.day;
    }
    else {	
        									/* clock not initialized and/or not running		*/
    	theTime->hour    = 8;							/* use a default date							*/
        theTime->minute  = 0;							/* 2004/10/01 8:00AM							*/
        theTime->second  = 0;
        theTime->elapsed = (8 * 60);
        theDate->year    = 2004;
        theDate->month   = 10;
        theDate->day     = 1;
        theDate->dow     = 3;
       	write_rtc(0xFC,0x55); 
        set_date_time( theTime,  theDate);
        
    }
	return 1;
}


/****************************************************************************/
/*	Name:											*/
/*		set_date_time									*/
/*												*/
/*	Function:										*/
/*		Updates the time parameters of the RTC clock chip.				*/
/*												*/
/*	Synopsis:										*/
/*		UINT set_date_time(struct sys_time *, struct sys_date *)			*/
/*												*/
/*	In:											*/
/*		struct sys_time	*theTime; pointer to the time structure.			*/
/*		struct sys_date	*theDate; pointer to the date structure.			*/
/*												*/
/*	Returns:										*/
/*		Nothing										*/
/****************************************************************************/

UINT set_date_time(struct sys_time *theTime, struct sys_date *theDate)

{
	char i;

	time = *theTime;
	date = *theDate;

	if (read_rtc(0xFD)==0x55) {
	   	memset(&rtc,0,sizeof(rtc));
	   	
		rtc.hour      = CHARTOBCD(theTime->hour);										/* update the time parameters					*/
		rtc.min       = CHARTOBCD(theTime->minute);
		rtc.sec       = CHARTOBCD(theTime->second);
		i             = theDate->year % 100;
		rtc.year      = CHARTOBCD( i );
		rtc.month     = CHARTOBCD(theDate->month);
    	        rtc.date      = CHARTOBCD(theDate->day );
    	        rtc.day       = theDate->dow;
	        write_rtc(0xBE,0);
        }
	return 1;
}

#if HARDWARE == 1

/****************************************************************************/
/*	Name:									*/
/*		read_rtc							*/
/*										*/
/*	Function:								*/
/*		Reads the time parameters of the RTC clock chip.		*/
/*										*/
/*	Synopsis:								*/
/*		static void read_rtc(void)					*/
/*										*/
/*	In:									*/
/*		Nothing.							*/
/*										*/
/*	Returns:								*/
/*		Nothing.							*/
/****************************************************************************/

char read_rtc(char command)

{
	char i,byte;
	char *ptr;


	if (command == 0xBF) {					/* burst time read command */
		i          = 8;
		byte       = 1;
		ptr        = (char *)(&rtc);
	}
	else {
		i          = 1;
		byte       = 0;
		ptr        = &byte;
	}
	
        PORTB&= ~(1<<TSCL);
        DDRB&=~(1<<TSDA);
        us_delay(4);
       	CSRTC();
        Send_RTC( command);				/* write command */

	do {
             *ptr++     = Receive_RTC();		/* read it */
    	} while(--i );
    	PORTB&=~(1<<TSCL);
    	us_delay(8);
	UNCSRTC();
    	return(byte);
}


void write_rtc(char command,char byte)

{
	char i;
	char *ptr;

	if (command == 0xBE) {					/* burst time write command */
		i   = 8;
		ptr = (char *)(&rtc);
	}
	else {
		i   = 1;
		ptr = &byte;
	}
	                             
        PORTB&= ~(1<<TSCL);
        us_delay(4);
        CSRTC(); 
        Send_RTC( command );				/* write command */

       do {
		Send_RTC(*ptr++); 			/* send the next one */

        } while(--i);
        
        PORTB&=~(1<<TSCL);
        UNCSRTC();
        us_delay(8);

}



void Send_RTC(unsigned char sdata)
{
	unsigned char i,j;
	
	
	j=sdata;
	us_delay(4);
	
	DDRB|=(1<<TSDA);			//turn SDA to output
	for(i=0;i<8;i++){
		PORTB&=~(1<<TSCL);		//SCK=0
		
		if(j&0x01)			//Send the bit to pin mosi
			PORTB|=(1<<TSDA);	//send 1
		else
			PORTB&=~(1<<TSDA);	//send 0
			
		us_delay(4);
		j>>=1;				//Turn the next bit
		PORTB|=(1<<TSCL);		//SCK=1
		us_delay(4);
	
	}
	DDRB&=~(1<<TSDA);			//turn  SDA to input
}

char Receive_RTC(){
	unsigned char i;
	char ctemp;
	
	DDRB&=~(1<<TSDA);			//turn  SDA to input
	ctemp=0;
	for(i=0;i<8;i++)
	{	PORTB|=(1<<TSCL);	//SCK=1
		us_delay(4);
		PORTB&=~(1<<TSCL);	//SCK=0
		us_delay(3);
		ctemp>>=1;
		if(PINB&(1<<TSDA))
			ctemp|=0x80;

		us_delay(1);

	}

	return ctemp;
}



#else



/****************************************************************************/
/*	Name:									*/
/*		read_rtc							*/
/*										*/
/*	Function:								*/
/*		Reads the time parameters of the RTC clock chip.		*/
/*										*/
/*	Synopsis:								*/
/*		static void read_rtc(void)					*/
/*										*/
/*	In:									*/
/*		Nothing.							*/
/*										*/
/*	Returns:								*/
/*		Nothing.							*/
/****************************************************************************/

char read_rtc(char command)

{
	char i,byte;
	char *ptr;


	if (command == 0xBF) {					/* burst time read command */
		i          = 8;
		byte       = 1;
		ptr        = (char *)(&rtc);
	}
	else {
		i          = 1;
		byte       = 0;
		ptr        = &byte;
	}
	
	
        system.spi_busy   = TRUE;
        SPCR|=0x22;                                             /*DS1302 is LSB first transfer &the sclk<250K*/
 	SPCR &= ~(1<<CPHA);					/* send data on leading edge		*/
       
	SPIPage    = RTC;		                        /* select SPI to the clock module */
        _NOP();
        SPI_MasterTransmit( command );				/* write command */
					
	do {
             *ptr++     = SPI_MasterReceive(0xff);		/* read it */
    	} while(--i );


	SPIPage    =  0;  
	SPCR&=0xdd;                                     	/* make sure MOSI set for None */
	SPCR |= (1<<CPHA);					/* send data on leading edge		*/
	SPIPage = 0;
	system.spi_busy = FALSE;				

    return(byte);
}


void write_rtc(char command,char byte)

{
	char i;
	char *ptr;

	if (command == 0xBE) {					/* burst time write command */
		i   = 8;
		ptr = (char *)(&rtc);
	}
	else {
		i   = 1;
		ptr = &byte;
	}
	
        system.spi_busy = TRUE;                                
        SPCR|=0x22;               				/*DS1302 is LSB first transfer &the sclk<250K*/     
	SPCR &= ~(1<<CPHA);					/* send data on leading edge		*/
                        
	SPIPage    = RTC;	 				/* select SPI to the clock module DS1302*/
	_NOP();
		                        
        SPI_MasterTransmit( command );				/* write command */

       do {
		SPI_MasterTransmit(*ptr++); 			/* send the next one */

        } while(--i);

	SPIPage    =  0;      
	SPCR&=0xdd;                               		/* make sure MOSI set for None */
 	SPCR |= (1<<CPHA);					/* send data on leading edge		*/
	SPIPage = 0;
	system.spi_busy = FALSE;
	
}

#endif

⌨️ 快捷键说明

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