Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Reading Data using I2C

Status
Not open for further replies.

Micro Lover

Member level 2
Joined
Jul 22, 2009
Messages
44
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Location
pakistan
Activity points
1,614
hi
i m using 89C2051

i want to read the temperature using i2c in Keil uVision4

is there any simplest code ? // In C

i mean just send address and read data
no more
 

karthikkrv85

Full Member level 3
Joined
Nov 9, 2009
Messages
173
Helped
17
Reputation
34
Reaction score
17
Trophy points
1,298
Location
bangalore
Activity points
2,356
Hai

Please use this following code

Code:
//------------------------------------------------------------------------------
// I2C Functions - Bit Banged
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
// 	Routine:	i2c_start
//	Inputs:		none
//	Outputs:	none
//	Purpose:	Sends I2C Start Trasfer - State "B"
//------------------------------------------------------------------------------
void i2c_start (void)
{
	SDATA = HIGH;							// Set data line high
	SCLK = HIGH;							// Set clock line high
	SDATA = LOW;							// Set data line low (START SIGNAL)
	SCLK = LOW;								// Set clock line low
}

//------------------------------------------------------------------------------
// 	Routine:	i2c_stop
//	Inputs:		none
//	Outputs:	none
//	Purpose:	Sends I2C Stop Trasfer - State "C"
//------------------------------------------------------------------------------
void i2c_stop (void)
{
	unsigned char input_var;

	SCLK = LOW;								// Set clock line low
	SDATA = LOW;							// Set data line low
	SCLK = HIGH;							// Set clock line high
	SDATA = HIGH;							// Set data line high (STOP SIGNAL)
	input_var = SDATA;						// Put port pin into HiZ
}

//------------------------------------------------------------------------------
// 	Routine:	i2c_write
//	Inputs:		output byte
//	Outputs:	none
//	Purpose:	Writes data over the I2C bus
//------------------------------------------------------------------------------
void i2c_write (unsigned char output_data)
{
	unsigned char index;

	for(index = 0; index < 8; index++)  	// Send 8 bits to the I2C Bus
	{
                                 			// Output the data bit to the I2C Bus
		SDATA = ((output_data & 0x80) ? 1 : 0);
      	output_data  <<= 1;            		// Shift the byte by one bit
		SCLK = HIGH;   		        		// Clock the data into the I2C Bus
		SCLK = LOW;					
	}

	index = SDATA;							// Put data pin into read mode
	SCLK = HIGH;   		        			// Clock the ACK from the I2C Bus
	SCLK = LOW;					
}

//------------------------------------------------------------------------------
// 	Routine:	i2c_read
//	Inputs:		none
//	Outputs:	input byte
//	Purpose:	Reads data from the I2C bus
//------------------------------------------------------------------------------
unsigned char i2c_read (void)
{
	unsigned char index, input_data;

   	index = SDATA;							// Put data pin into read mode

	input_data = 0x00;
	for(index = 0; index < 8; index++)  	// Send 8 bits to the I2C Bus
	{
		input_data <<= 1;					// Shift the byte by one bit
		SCLK = HIGH;           				// Clock the data into the I2C Bus
      	input_data |= SDATA; 		   		// Input the data from the I2C Bus
		SCLK = LOW;					
	}

   return input_data;
}

I hope you will be knowing how to assign the port pins to the SDA and SCL and also the transferring ways

All the best
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top