Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
06/08/11 14:38
Read: times


 
Msg Score: -1
 -1 Message Not Useful
#182537 - See if this code is of any help!
Responding to: ???'s previous message

#ifndef IC2_h
#define IC2_h

/* DEFINATIONS FOR DS1307RTC */

#define	    sda     P0_0                     // Serial data pin
#define	    scl     P0_1                     // Serial clock pin

#define	    set_sda     (P0_0=0x01)          // Set Serial data pin
#define	    set_scl     (P0_1=0x01)          // Set Serial clock pin

#define	    clr_sda     (P0_0=0x00)          // Clear Serial data pin
#define	    clr_scl     (P0_1=0x00)          // Clear Serial clock pin


#define I2C_DELAY 0x0F	                    // For delay i2c bus 

/*------------------------------------------------------------------*/
/*-----------------------------------------------------------------*/
//Function prototypes
/*-----------------------------------------------------------------*/
void I2C_delay(void);
void I2C_clock(void);
void I2C_start(void);
void I2C_stop(void);
bit I2C_write(unsigned char dat);
unsigned char I2C_read(void);

/*------------------------------------------------------------------------
------------------------------------------------------------------------*/


void I2C_delay(void)
{
	unsigned char i;

	for(i=0; i<I2C_DELAY; i++);
}

void I2C_clock(void)
{
	I2C_delay();

	set_scl;		// Start clock 

	I2C_delay();    

	clr_scl;		// Clear SCL 
}

void I2C_start(void)
{
	if(scl)
	clr_scl;		// Clear SCL 

	set_sda;                // Set SDA 
	set_scl;		// Set SCL 

	I2C_delay(); 

	clr_sda;                // Clear SDA 

	I2C_delay(); 

	clr_scl;                // Clear SCL 
}

void I2C_stop(void)
{
	if(scl)	
	clr_scl;			// Clear SCL 

	clr_sda;			// Clear SDA 
	I2C_delay();

	set_scl;			// Set SCL

	I2C_delay();

	set_sda;			// Set SDA 
}

bit I2C_write(unsigned char dat)
{
	bit data_bit;		
	unsigned char i;	

	for(i=0;i<8;i++)		//sending 8 bits of data
	{
	 data_bit = dat & 0x80;		// Filter MSB bit keep to data_bit
	 sda = data_bit;		// Send data_bit to SDA

	I2C_clock();      		// Call for send data to i2c bus 

	dat = dat<<1;  
	}

	set_sda;			

	I2C_delay();	
	
	set_scl;			

	I2C_delay();	

	data_bit = sda;   	// Check acknowledge
	clr_scl;		

	I2C_delay();

	return data_bit;	// If send_bit = 0 i2c is valid 		 	
}

unsigned char I2C_read(void)
{
	bit rd_bit;	
	unsigned char i, dat;

	dat = 0x00;	

	for(i=0;i<8;i++)		//  read 8 bit data 
	{
	 I2C_delay();

	 set_scl;			

	 I2C_delay(); 

	 rd_bit = sda;		// Keep to check acknowledge
	 dat = dat<<1;		
	 dat = dat | rd_bit;	// Keep bit data in dat

	 clr_scl;		
	}
	return dat;
}

#endif

 



List of 13 messages in thread
TopicAuthorDate
Help me ? 89C52 interfacing with EEPROM 24C02            01/01/70 00:00      
   Do *not* use 'for' loops for delays!            01/01/70 00:00      
   100k is the upper limit            01/01/70 00:00      
      HLL delay loops - just don't do it!            01/01/70 00:00      
   that may be difficult            01/01/70 00:00      
      but there is a way            01/01/70 00:00      
   See if this code is of any help!            01/01/70 00:00      
      I doubt it very much            01/01/70 00:00      
      No, it isn't            01/01/70 00:00      
   doubts 1,2,3            01/01/70 00:00      
   code for 24c02 wih 89c52            01/01/70 00:00      
      Do *not* use 'for' loops for delays!            01/01/70 00:00      
      code?            01/01/70 00:00      

Back to Subject List