[ASK} Connecting PIC18F2553 with AD5242 via i2c

Status
Not open for further replies.

arheevap

Newbie level 2
Joined
Aug 4, 2009
Messages
2
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
Eindhoven
Activity points
1,309
Dear All,

I have a problem about i2c connection of PIC18F2553 with AD5242. I want to connecting the pin 21(SDA) and pin 22(SCL) of PIC 18F2553 to pin 8(SDA) and pin 7 (SCL) of AD5242 respectively. Therefore i could adjust the value of digital potentiometer at AD5242. it decreases and if its too low its increased
the value of the digital potentiometer. This is the list of program i have been writing. Please give me some correction about the program listing. if the output signal is saturated,

void i2c_connection(void)
{

//DEFINES
#define SCL TRISB1 //SCL Direction Register Bit
#define SDA TRISB0 //SDA Direction Register Bit
#define SCL_IN RB1
#define SDA_IN RB0


RGAIN = 270;
RWIPPER = 45;
RAB = 10000;
RES_DIGIPOT = 256;
SDA = SCL = 1;
SCL_IN = SDA_IN = 0;

void i2c_dly(void)
{
}

void i2c_start(void)
{
SDA = 1; // i2c start bit sequence
i2c_dly();
SCL = 1;
i2c_dly();
SDA = 0;
i2c_dly();
SCL = 0;
i2c_dly();
}

void i2c_stop(void)
{
SDA = 0; // i2c stop bit sequence
i2c_dly();
SCL = 1;
i2c_dly();
SDA = 1;
i2c_dly();
}

unsigned char i2c_read(char ack)
{
char x, d=0;
SDA = 1;
for(x=0; x<8; x++)
{
d <<= 1;
do
{
SCL = 1;
}
while(SCL_IN==0); // wait for any SCL clock stretching
i2c_dly();
if(SDA_IN) d |= 1;
SCL = 0;
}
if(ack) SDA = 0;
else SDA = 1;
SCL = 1;
i2c_dly(); // send (N)ACK bit
SCL = 0;
SDA = 1;
return d;
}

bit i2c_write(unsigned char d)
{
char x;
static bit b;
for(x=8; x; x--)
{
if(d&0x80) SDA = 1;
else SDA = 0;
SCL = 1;
d <<= 1;
SCL = 0;
}
SDA = 1;
SCL = 1;
i2c_dly();
b = SDA_IN; // possible ACK bit
SCL = 0;
return b;
}


void write_eeprom (unsigned char address, unsigned char data)
{
i2c_start();
i2c_write(0xa0);
i2c_write(address);
i2c_write(data);
i2c_stop();
DelayMs(11);
}

unsigned char read_eeprom(unsigned char address, unsigned char data
{
i2c_start();
i2c_write(0xa0);
i2c_write(address);
i2c_write(0xa1);
data=i2c_read(0);
i2c_stop();
return(data);
}

void i2c_digipot (unsigned char d_resistor)
{
d_resistor=((RGAIN-RWIPER)/RAB)*RES_DIGIPOT;
i2c_senddigipot(0, (int)(d_resistor+0.5));
i2c_senddigipot(1, (int)(d_resistor+0.5));
}

void i2c_senddigipot (unsigned char channel, unsigned char value)
{
char digipot_data[2];
if(channel==0)
{
digipot_data[0]=0xa0; //RDAC1
}
else
{
digipot_data[0]=0xa1; //RDAC2
}

digipot_data[1]=value;

if (value > )
{
value --;
}

if (value < )
{
value ++;
}
}
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…