LOGIN
student
 · BeedIndia
Share
Report
Get Link
TASK
Open
Reward › Contribution Review

this is i2c subroutin called from main

#include<Intrins.h>
#include<reg52.h>

sbit ADC_SDIO=P1^2;
sbit ADC_CLK=P1^3; //sbit dready=P3^5;

char dec;
#define ADC_W 0x54
#define ADC_R 0x55
/*****************************************************************************/

/*void Delay(int val)
{
unsigned char j;
unsigned int i;
for(i=0;i<val;i++)
for(j=0;j<250;j++);
}
/*****************************************************************************/

void delay_ms(int count)
{
int j,i;
for(j=0;j<count;j++)
{
for(i=0;i<1104;i++);
}
}
/*****************************************************************************/

void i2c_start(void) //send START condition to I2C
{
ADC_SDIO=1;
ADC_CLK=1;
nop(); //I2C_delay(185);//__delay_us(HalfBitDelay); //keep delay of maximum 300ns.
nop();
nop();
nop();
nop();
nop();

ADC_SDIO=0;
_nop_();                                          //I2C_delay(185);//__delay_us(HalfBitDelay);         //keep delay of minimum 600ns
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();

}

/********************************************************************************/

bit i2c_send(unsigned char value) //send device address
{
int i;
for(i=0;i<8;i++)
{
ADC_CLK=0;
nop(); //I2C_delay(92);//__delay_us(HalfBitDelay/2); //keep delay of maximum 600ns
nop();
nop();
nop();

	if((value)&0x80)
		ADC_SDIO=1;
	else
		ADC_SDIO=0;
	                                                      //I2C_delay(92);//__delay_us(HalfBitDelay/2);      //keep delay of minimum 100 ns
	ADC_CLK=1;
	_nop_();                                                //I2C_delay(185);//__delay_us(HalfBitDelay);     //keep delay of minimum 600ns
	value<<=1;
}
ADC_CLK =0;
  _nop_();
ADC_SDIO =1;					

  _nop_();
  ADC_CLK =1;
  _nop_();
  ADC_CLK =0;

return ADC_SDIO;

}

/*****************************************************************************/

unsigned char i2c_recieve(void)
{
unsigned char data_r;
int i;
for(i=0;i<8;i++)
{
ADC_CLK=0;
dec=15; //I2C_delay(92);//__delay_us(HalfBitDelay/2); //keep delay of maximum 600ns
while((dec–)!=0)
nop();

	data_r <<=1;                                        //I2C_delay(92);//__delay_us(HalfBitDelay/2);      //keep delay of minimum 100 ns
	ADC_CLK=1;
	_nop_();                                                //I2C_delay(185);//__delay_us(HalfBitDelay);     //keep delay of minimum 600ns		
	data_r|=ADC_SDIO;
}

return(data_r);

}

/******************************************************************************/

void i2c_send_ack(void)
{
ADC_CLK =0;
nop();
nop();
ADC_SDIO =0;
nop();
ADC_CLK =1;
/* Make the SCLK duty near 50% */
dec=18;
while((dec–)!=0)
nop();
ADC_CLK =0;
ADC_SDIO =1;
}

/******************************************************************************/

void i2c_send_nack(void)
{
ADC_CLK =0;
nop();
nop();
ADC_SDIO =1;
nop();
nop();
nop();
nop();
ADC_CLK =1;
/* Make the SCLK duty near 50% */
dec=18;
while((dec–)!=0)
nop();
ADC_CLK =0;
nop();
nop();
ADC_SDIO =0;
nop();
nop();
}

/*****************************************************************************/

void streaming_i2c_mode(void)
{
ADC_SDIO =0;
nop();
nop();

ADC_CLK =0;
                                                                                         /* Delay 240 Machine cycles */
dec=22;
while((dec--)!=0)
_nop_();

}

/*************************************************************************/

void i2c_stop(void) //send START condition to I2C
{
ADC_CLK=0;
nop(); //I2C_delay(92);//__delay_us(HalfBitDelay/2); //keep delay of maximum 150ns
ADC_SDIO=0;
nop(); //I2C_delay(92);//__delay_us(HalfBitDelay/2); //keep delay of maximum 150ns
nop();
ADC_CLK=1;
nop(); //I2C_delay(92);//__delay_us(HalfBitDelay/2);//keep delay of minimum 600ns
ADC_SDIO=1;
nop(); //I2C_delay(92);//__delay_us(HalfBitDelay/2);
}

/*****************************************************************************/

void i2c_send_byte(unsigned char reg_add,unsigned char DATA)
{
unsigned char temp;

i2c_start();
temp=ADC_W;					
i2c_send(temp);
i2c_send(reg_add);
i2c_send(DATA);
i2c_stop();

}

/*****************************************************************************/

void function(void)
{

	i2c_send_byte(0x00,0x01);	//register reset
	delay_ms(10);

    i2c_send_byte(0x00,0x02);	//power up digital ckt
	delay_ms(10);
     
	i2c_send_byte(0x00,0x04);	//power up regr
	delay_ms(10);
	
	i2c_send_byte(0x00,0x96);	  //cntrol -10010110
	delay_ms(10);
    
	i2c_send_byte(0x01,0x07);	//128 gain select
	delay_ms(10);
    
	i2c_send_byte(0x02,0x01);	//reserved
	delay_ms(10);
	
	i2c_send_byte(0x11,0x80);  	  //i2c control -enable crsd  sdio=0
	delay_ms(10);
 	
	i2c_send_byte(0x15,0x30);	   //  turn off
	delay_ms(10);
    
	i2c_send_byte(0x1c,0x80);	//power control gain
	delay_ms(10);

}

/*****************************************************************************/

void init_cpu(void)
{
ADC_CLK=1;
ADC_SDIO=1;
function();
}

/*****************************************************************************/

unsigned long read7802(void)
{
long count=0;
unsigned int K;

streaming_i2c_mode();

K=ADC_W;
i2c_send(K);

K=0x12;
i2c_send(K);

_nop_();
_nop_();

i2c_start();

K=ADC_R;
i2c_send(K);

K=i2c_recieve();
i2c_send_ack();
count |= ((long)K<<16);

delay_ms(15);

K=i2c_recieve();
i2c_send_ack();
count |= ((long)K<<8);

K=i2c_recieve();
i2c_send_nack();

K=K&0xF0;
count |=K;

i2c_stop();

if (count>0x7FFFFF)           // if MSB=1, 
	{
    
	count|=0xFF800000;

	}	   

return((count/10)+10000);		

}

0 like 
Like
Award Contribution
JOIN TASK
LEAVE A REPLY
DISCOVER
CHAT
HIRE
ACTIVITY
FEED
New Post
Help
Start Project
Online Users
Share Link
Write something before you submit it!
Photo updated
Request Sent!
Updated
Copied to Clipboard