|
|
View previous topic :: View next topic |
Author |
Message |
dirtymouse
Joined: 10 Apr 2007 Posts: 1 Location: manchester, uk
|
I2C with adc and |
Posted: Tue Apr 10, 2007 8:46 am |
|
|
Hi
i'm (trying) to connect my pic (16f876a) to a ADC (max127); but i'm a bit (or a lot) lost.
Hope somebody can help me.....
On my research i came to here:
http://www.ccsinfo.com/forum/viewtopic.php?t=21456
i made some litle changes, but it doesn't works:
Code: | #define SCL_C3 PIN_C4
#define SDA_C4 PIN_C4
#define end_ADC 0x60
#use i2c (MASTER, SCL=SCL_C3, SDA=SDA_C4, ADDRESS=end_ADC,SLOW,FORCE_HW)
void initI2C(){
output_float(SCL_C3);
output_float(SDA_C4);
}
void writeI2C(INT16 comando){
i2c_start(); // Start condition
i2c_write(end_ADC); // Address + R/W
i2c_write(comando & 0x00ff); // comando 1
i2c_write((comando & 0xff00) >> 8); // comando 2
i2c_stop(); // Stop condition
}
INT16 readI2C(){
BYTE b1=0, b2=0;
i2c_start(); // "restart" condition
i2c_write((end_ADC | 0x0001)); // address + R/W
b1 = i2c_read(1); // read 1ºbyte
b2 = i2c_read(0); // read 2ºbyte
i2c_stop(); // Stop condition
return make16(b2, b1);
}
int16 getdata(){
unsigned int16 value;
value=0;
//write cycle
writeI2C(0x0080);
delay_us(8);
//read cycle
value=readI2C();
return valor;
}
|
First of all, i really don't know if the problem is on the pic's code or in the adc, but assuming the ADC is ok (it's new), the problem is with the code.
I'm selecting channel 0, and using the regular schematic.
Now, isn't it necessary to initialize some registers?
The example has a small init.....
i believe it's necessary to control SSPCON SSPCON2 and SSPSTAT...is that correct?
and what about TRIS?
with i2c, SDA works like input and output, is it supposed that i affect them in the middle?
Is there any way to test the code, without connecting it with another device?
Tks for your time and attention
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Apr 10, 2007 12:27 pm |
|
|
Try the code shown in this post. It's not written in the form of a
driver. It's just inline code.
http://www.ccsinfo.com/forum/viewtopic.php?t=18886&highlight=max127
Your code is completely wrong. Here, you've put SCL and SDA on
the same pin. They should be on pins C3 and C4, respectively.
Quote: | #define SCL_C3 PIN_C4
#define SDA_C4 PIN_C4 |
Here, you're setting an address in a #use i2c() statement that declares
an i2c Master. Address is only used for i2c Slaves.
Quote: | #use i2c (MASTER, SCL=SCL_C3, SDA=SDA_C4, ADDRESS=end_ADC,SLOW,FORCE_HW) |
My advice is, use the code in the link. |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|