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

📄 rtc41t81.c

📁 dm270 source code
💻 C
📖 第 1 页 / 共 2 页
字号:

#include <system/uart270.h>

#include <user/rtc41T81.h>  

enum {I2C_SUCCESS, I2C_ERROR};
#define	I2C_SCL			GIO32   // output_____I2C Bus SCL
#define I2C_SDA			GIO31   // out/input__I2C Bus SDA

/* ------------------------------------
    Macro Definitions
   ------------------------------------ */ 	
#define 	M41T81_SLAVE_ADDR 		0x68

// Regiast address
#define 	M41T81_seconds_fraction 0x00
#define 	M41T81_seconds   		0x01
#define 	M41T81_Minutes   		0x02
#define 	M41T81_Hours  			0x03
#define 	M41T81_Week   			0x04
#define 	M41T81_Day   			0x05
#define 	M41T81_Month   			0x06
#define 	M41T81_Year  			0x07
#define 	M41T81_AlarmMonth  		0x0a
#define 	M41T81_AlarmDate 		0x0b
#define 	M41T81_AlarmHour 		0x0c
#define 	M41T81_AlarmMinutes		0x0d
#define 	M41T81_AlarmSeconds		0x0e
#define 	M41T81_ST   			0x01
#define 	M41T81_HT  				0x0c
#define 	M41T81_AFE   			0x0A
#define 	M41T81_ABE   			0x0A
#define 	M41T81_AF   			0x0F
#define 	M41T81_PRT1				0x0e
#define 	M41T81_PRT2				0x0d
#define 	M41T81_PRT3				0x0c
#define 	M41T81_PRT4				0x0b
#define 	M41T81_PRT5				0x0b

// signal  position
#define 	M41T81_positionST   	0x80
#define 	M41T81_positionHT  		0x40
#define 	M41T81_positionAFE   	0x80
#define 	M41T81_positionABE 		0x20
#define 	M41T81_positionAF 		0x40
#define 	M41T81_positionPRT1		0x80
#define 	M41T81_positionPRT2		0x80
#define 	M41T81_positionPRT3		0x80
#define 	M41T81_positionPRT4		0x80
#define 	M41T81_positionPRT5		0x40
/* ------------------------------------
    Type Definitions
   ------------------------------------ */

/* ------------------------------------
    Variables Definitions
   ------------------------------------ */
Uint8 		PwrSavingBase;
/* ------------------------------------
    Function Prototypes         
   ------------------------------------ */

/*=========================================================================
SubRoutine: static Uint8 M41T81_Hex2Dec( Uint8 Hex , Uint8 LimitUp, Uint8 LimitDown )

Function  : Char hexadecimal number change to decimal number. (0x00 ~ 0x63 ==> 0 ~ 99)

Parameter : Uchar(8bit) hex.
			LimitUp - Limit for max number.
			LimitDown - Limit for min number

Return    : char decimal number. if return 0xff the converter is fail.

Reference : [2][1]

=========================================================================*/
static Uint8 M41T81_Hex2Dec( Uint8 Hex , Uint8 LimitUp, Uint8 LimitDown )
{
	Uint8 	Dec;

	if( ( LimitDown <= Hex ) && ( Hex <= LimitUp  ))
	{
		Dec = Hex % 10;
		Dec = ( ( Hex / 10 ) << 4 ) + Dec;
		return Dec;
	}
	else
		return (Uint8)0xff;
}	
/* M41T81_Hex2Dec - */

/*=========================================================================
SubRoutine: static Uint8 M41T81_Dec2Hex( Uint8 Dec )

Function  : Char decimalnumber change to hexadecimal number. ( 0 ~ 99 ==> 0x00 ~ 0x63)

Parameter : Uchar(8bit) decimal.

Return    : char hex number.

Reference : [2][1]

=========================================================================*/
static Uint8 M41T81_Dec2Hex( Uint8 Dec )
{
	Uint8 	Hex, Dec_Temp = Dec;
	
	Hex = (((Dec_Temp >> 4) & 0x0F )* 10) + (Dec & 0x0F);
		
	return Hex;
}	
/* M41T81_Dec2Hex - */

/*=========================================================================
SubRoutine: STATUS M41T81_I2cSendCmd( Uint16 cmd )

Function  : write a register to M41T81.	

Parameter : Uint16 cmd ( 8 bit register add and 8 bit data.) 

Return    : STATUS ( E_PASS or E_INVALID_INPUT)

Reference : [2][1]

=========================================================================*/
STATUS M41T81_I2cSendCmd( Uint16 cmd ) 
{
	STATUS 	status=E_PASS;
 	Uchar 	success;

	i2cWriteReg((Uchar)M41T81_SLAVE_ADDR<<1, (Uchar)(cmd >> 8), (Uchar)(cmd & 0xFF ), &success);
 	if(success)
		{
			UART_sendString( UART0, "\r\n M41T81 control fail.");
			status=E_INVALID_INPUT;
		}
	return status;
}
/* M41T81_I2cSendCmd - */

/*=========================================================================
SubRoutine: Uint32 M41T81_ReadRegister( STATUS *status )

Function  : Reading M41T81 register data.	

Parameter : The point address for 20 registers of M41T81.

Return    : status

Reference : [2][1]

=========================================================================*/
static void M41T81_ReadRegister( STATUS *status, Uint8 *RegisterAdd )
{	
	BOOL	GetAck = TRUE;
	Uint16 	counter;
	Uint8	*M41T81_Register;

	M41T81_Register = RegisterAdd;
	
   	i2cStart();
   	i2cWriteByte( ((Uchar)M41T81_SLAVE_ADDR<<1) | 0x00 );      
   	GetAck &= i2cGetAck();
   	i2cWriteByte( 0x00 );      
   	GetAck &= i2cGetAck();
   	i2cStart();
   	i2cWriteByte( ((Uchar)M41T81_SLAVE_ADDR<<1) | 0x01 );      
   	GetAck &= i2cGetAck();
	for( counter = 0 ; counter < 0x14 ; counter++)
	{
		makeSdaInput();
		*M41T81_Register = i2cReadByte();
		makeSdaOutput();		
		if( counter < 0x13)
		{
			i2cSendAck();
			M41T81_Register++;
		}
		else
			i2cSendNack();
	}
   	i2cStop();
	
   *status = (GetAck) ? (STATUS)I2C_SUCCESS : (STATUS)I2C_ERROR;
}
/* M41T81_ReadRegister - */

/*=========================================================================
SubRoutine: STATUS M41T81_StopEnable( Uint8 *StopRegister )

Function  : write stop bit to enable for M41T81.	// emil : Disable RTC

Parameter : Uint8 *StopRegister

Return    : status

Reference : [2][1]

=========================================================================*/
STATUS M41T81_StopEnable( Uint8 *StopRegister )
{
	Uint16 	cmd;
	Uint8	temp;
	
	cmd = M41T81_ST << 8;
	temp = ( 0x80 | *StopRegister );
	cmd = ( cmd & 0xFF00 ) |( temp & 0x00FF );
	return M41T81_I2cSendCmd( cmd );	
}
/* M41T81_StopEable - */

/*=========================================================================
SubRoutine: STATUS M41T81_StopDisable( Uint8 *StopRegister )

Function  : write stop bit to desable for M41T81.	// emil : Enable RTC

Parameter : Uint8 *StopRegister

Return    : status

Reference : [2][1]

=========================================================================*/
STATUS M41T81_StopDisable( Uint8 *StopRegister )
{
	Uint16 	cmd;
	Uint8	temp;
	
	cmd = M41T81_ST << 8;
	temp = ( ~(0x80) & *StopRegister );
	cmd = ( cmd & 0xFF00 ) |( temp & 0x00FF );
	return M41T81_I2cSendCmd( cmd );	
}
/* M41T81_StopDisable - */

/*=========================================================================
SubRoutine: STATUS M41T81_HaltEable( Uint8 *HaltRegister )

Function  : write HT(halt update ) bit to enable for M41T81.	

Parameter : nil

Return    : status

Reference : [2][1]

=========================================================================*/
STATUS M41T81_HaltEable( Uint8 *HaltRegister )
{
	Uint16 	cmd;
	Uint8	temp;
	
	cmd = M41T81_HT << 8;
	temp = ( 0x40 | *HaltRegister);
	cmd = ( cmd & 0xFF00 ) |( temp & 0x00FF );
	return M41T81_I2cSendCmd( cmd );	
}
/* M41T81_HaltEable - */

/*=========================================================================
SubRoutine: STATUS M41T81_HaltDisable( Uint8 *HaltRegister )

Function  : write HT(halt update ) bit to desable for M41T81.	

Parameter : nil

Return    : status

Reference : [2][1]

=========================================================================*/
STATUS M41T81_HaltDisable( Uint8 *HaltRegister )
{
	Uint16 	cmd;
	Uint8	temp;
	
	cmd = M41T81_HT << 8;
	temp = ( ~(0x40) & *HaltRegister);
	cmd = ( cmd & 0xFF00 ) |( temp & 0x00FF );
	return M41T81_I2cSendCmd( cmd );	
}
/* M41T81_HaltDesable - */

/*=========================================================================
SubRoutine: STATUS M41T81_AlarmEnable( void )

Function  : Write Alarm Enable register to M41T81.	

Parameter : nil

Return    : status

Reference : [2][1]

=========================================================================*/
STATUS M41T81_AlarmEnable( void )
{
	STATUS			status;
	static Uint8	Register[20];
	Uint8			*M41T81_Register;
	Uint16 			cmd;
	Uint8			temp;
	
	M41T81_Register = Register;
	i2cInit(); 
	M41T81_ReadRegister(&status, &(*M41T81_Register));
	if(status == I2C_SUCCESS)
	{
	cmd = M41T81_AFE << 8;
	temp = (  0xA0 | *(M41T81_Register + 0x0A));
	cmd = ( cmd & 0xFF00 ) |( temp & 0x00FF );
	status = M41T81_I2cSendCmd( cmd );	
	}
	else
	{
		status = E_INVALID_INPUT;
	}

	return status;	
}
/* M41T81_AlarmEnable - */

/*=========================================================================
SubRoutine: STATUS M41T81_AlarmDisable( void )

Function  : Write Alarm Disable register to M41T81.	

Parameter : nil

Return    : status

Reference : [2][1]

=========================================================================*/
STATUS M41T81_AlarmDisable( void )
{
	STATUS			status;
	static Uint8	Register[20];
	Uint8			*M41T81_Register;
	Uint16 			cmd;
	Uint8			temp;
	
	M41T81_Register = Register;
	i2cInit(); 
	M41T81_ReadRegister(&status, &(*M41T81_Register));
	if(status == I2C_SUCCESS)
	{
	cmd = M41T81_AFE << 8;
	temp = ( (~0xA0) & *(M41T81_Register + 0x0A));
	cmd = ( cmd & 0xFF00 ) |( temp & 0x00FF );
	status = M41T81_I2cSendCmd( cmd );	
	}
	else
	{
		status = E_INVALID_INPUT;
	}
	
	return status;	
}
/* M41T81_AlarmDisable - */

/*=========================================================================
SubRoutine: STATUS M41T81_TimeInput( M41T81_TM *TimeInput )

Function  : set time successive to M41T81.	

Parameter : *TimeInput - time struct for input.

Return    : STATUS

Reference : [2][1]

=========================================================================*/
STATUS M41T81_TimeInput( M41T81_TM *TimeInput )
{
	STATUS			status;
	static Uint8	Register[20];
	Uint8			*M41T81_Register;
	Uint8			Buffer;
	M41T81_TM		TimeBuff;
	Uint16			counter;
	
	TimeBuff = *TimeInput;
	M41T81_Register = Register;
	i2cInit(); 
	M41T81_ReadRegister(&status, &(*M41T81_Register));
	if(status != I2C_SUCCESS)

⌨️ 快捷键说明

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