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

📄 calender.c

📁 C8051F020的全面应用,包括两个UART口、SPI、I2C
💻 C
字号:
extern void delayus(uchar delayvalue);
//------------------------------------------------------------------------------
// I2C Functions - Master
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
// 	Routine:	i2c_init
//	Inputs:		none
//	Outputs:	none
//	Purpose:	Initialize I2C for the ADu812C
//------------------------------------------------------------------------------
void i2c_init (void)
{
											// To initialize we need the output
											// data high, the clock high, and
											// the I2C system in Master mode

	SMBusSDA = HIGH;						// Set the data bit to HIGH
	SMBusSCL = HIGH;						// Set the clock bit to HIGH
}
//------------------------------------------------------------------------------
// 	Routine:	i2c_start
//	Inputs:		none
//	Outputs:	none
//	Purpose:	Sends I2C Start Trasfer - State "B"
//------------------------------------------------------------------------------
void i2c_start (void)
{
	SMBusSDA = HIGH;
	SMBusSCL = HIGH;						// An I2C start sequence is defined as
	delayus(2);								// a High to Low Transistino on the data
	SMBusSDA = LOW;							// line as the CLK pin is high
	delayus(2);/*delay 2 us*/
	
}

//------------------------------------------------------------------------------
// 	Routine:	i2c_stop
//	Inputs:		none
//	Outputs:	none
//	Purpose:	Sends I2C Stop Trasfer - State "C"
//------------------------------------------------------------------------------
void i2c_stop (void)
{
											// An I2C start sequence is defined as
											// a Low to High Transistino on the data
											// line as the CLK pin is high
	SMBusSDA = LOW;
	SMBusSCL = HIGH;
	delayus(2);
	SMBusSDA = HIGH;
	delayus(2);

}

//------------------------------------------------------------------------------
// 	Routine:	i2c_waitACK
//	Inputs:		none
//	Outputs:	BOOL
//	Purpose:	wait slave to send ACK
//------------------------------------------------------------------------------
BOOL i2c_waitACK(void)
{
	BOOL idata MDI;
	SMBusSCL = LOW;
	delayus(2);
	SMBusSCL = HIGH;
	delayus(2);
	SMBusSDA = HIGH;
	MDI	= SMBusSDA;
	SMBusSCL = LOW;
	return MDI;
}

//------------------------------------------------------------------------------
// 	Routine:	i2c_waitACK
//	Inputs:		send_ack 
//	Outputs:	BOOL
//	Purpose:	wait slave to send ACK
//------------------------------------------------------------------------------
void i2c_sendACK(BOOL send_ack)
{
	SMBusSCL = LOW;
	
	if (send_ack == YES)
		SMBusSDA = LOW;
	else 
		SMBusSDA = HIGH;
	delayus(2);

	SMBusSCL = HIGH;
	delayus(2);

	SMBusSCL = LOW;
	SMBusSDA = LOW;
}

//------------------------------------------------------------------------------
// 	Routine:	i2c_write
//	Inputs:		output byte
//	Outputs:	none
//	Purpose:	Writes data over the I2C bus
//------------------------------------------------------------------------------
BOOL i2c_write (BYTE output_data)
{
	uchar idata i;

	for(i=0;i<8;i++)
	{
		SMBusSCL = LOW;
		SMBusSDA = ((output_data & 0x80) ? 1 : 0);
		delayus(2);
		SMBusSCL = HIGH;
		delayus(2);
		
		output_data <<= 1;
	}

	return(i2c_waitACK());
}

//------------------------------------------------------------------------------
// 	Routine:	i2c_read
//	Inputs:		send_ack (if TRUE send the ACK signal)
//	Outputs:	input byte
//	Purpose:	Reads data from the I2C bus
//------------------------------------------------------------------------------
BYTE i2c_read (BOOL send_ack)
{
	BYTE data i, input_data;
	
	input_data = 0x00;

	for(i = 0; i < 8; i++)  	// Get 8 bits from the device
	{
		SMBusSCL = LOW;
		delayus(2);
		SMBusSCL = HIGH;
		delayus(2);
		SMBusSDA = HIGH;
		input_data |= SMBusSDA;
		SMBusSCL = LOW;						// Clock the data into the register
     	input_data <<= 1;					// Shift the data right 1 bit
	}

	i2c_sendACK(send_ack);					// Send ACK or NACK.
   	return input_data;
}

//------------------------------------------------------------------------------
// 	Routine:	CalenderRead
//	Inputs:		register address
//	Outputs:	register value
//	Purpose:	read register value of Calender IC
//------------------------------------------------------------------------------
uchar CalenderRead(uchar RegAddr)
{
	uchar idata temp;

	i2c_start();
	while(i2c_write(CalenderWR));
	while(i2c_write(RegAddr));
	i2c_start();
	while(i2c_write(CalenderRD));
	temp = i2c_read(NO);
	i2c_stop();

	return temp;
}

//------------------------------------------------------------------------------
// 	Routine:	CalenderWrite
//	Inputs:		register address, value written to register
//	Outputs:	none
//	Purpose:	set register value of Calender IC
//------------------------------------------------------------------------------
void CalenderWrite(uchar RegAddr, uchar value)
{
	i2c_start();
	while(i2c_write(CalenderWR));
	while(i2c_write(RegAddr));
	while(i2c_write(value));
	i2c_stop();
}
//------------------------------------------------------------------------------
// 	Routine:	VolLowDect
//	Inputs:		none
//	Outputs:	status of VOLTAGE, HIGH is low-voltage; LOW is normal-voltage
//	Purpose:	Writes data over the I2C bus
//------------------------------------------------------------------------------
BOOL VolLowDect(void)
{
	uchar idata temp;
	BOOL idata VolResult;

	temp = CalenderRead(Seconds);
	VolResult = temp>>7;
	return VolResult;
}

⌨️ 快捷键说明

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