View previous topic :: View next topic |
Author |
Message |
deniska_gus
Joined: 11 Jul 2006 Posts: 42 Location: Minden, Germany
|
Help! PIC16F877A and SM Bus |
Posted: Wed Feb 28, 2007 12:55 pm |
|
|
Hello!
Can anyone help me to find an answer, why my code is wrong. I'm reading out an IC BQ2085 and i cann't see anything, if i use Hyper Terminal.
[code]
#include <16F877A.h>
#fuses XT,NOWDT,NOPROTECT,PUT,BROWNOUT,NOLVP,NOWRT
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_c6, rcv=PIN_c7,parity=n,bits=8,ERRORS)
#use i2c(Master, sda=PIN_C4, scl=PIN_C3, SMBUS)
int8 datal, datah;
int16 data;
void read()
{
i2c_start(); // Start
i2c_write(0x16); //Battery Adress + R/W=0
i2c_write(0x17);// Command Word ( CycleCount)
i2c_start(); // Start
i2c_write(0x17);// Battery Address + R/W=1
datal=i2c_read();
i2c_write(0);
datah=i2c_read();
i2c_write(1);
i2c_stop(); //Stop
data=make16(datah, datal);
}
void send()
{
printf("%2ld\r\n", data);
}
void main()
{
while(1)
{
read();
send();
delay_ms(500);
}
}
[code]
I cann't post the datasheet, sorry.
[/img][/code] |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Feb 28, 2007 12:59 pm |
|
|
What is this ?
Quote: |
datah=i2c_read();
i2c_write(1);
|
Are you trying to do a NAK on the last read ?
You don't do it like that. Do it like this, with a 0 parameter:
Code: | datah = i2c_read(0); |
Also the same thing here. Are you trying to do an ACK ?
You need to study the CCS manual and look at a few i2c
drivers in the CCS drivers directory on your hard disk.
Look at how they do things. Don't invent.
Quote: | datal=i2c_read();
i2c_write(0); |
|
|
|
deniska_gus
Joined: 11 Jul 2006 Posts: 42 Location: Minden, Germany
|
|
Posted: Wed Feb 28, 2007 1:05 pm |
|
|
But in the datasheet i can see just NAK after the last read !? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Feb 28, 2007 1:31 pm |
|
|
This is how you do it in CCS:
Code: |
datal=i2c_read();
datah=i2c_read(0); // NAK on last read
i2c_stop();
data=make16(datah, datal);
|
|
|
|
deniska_gus
Joined: 11 Jul 2006 Posts: 42 Location: Minden, Germany
|
|
Posted: Wed Feb 28, 2007 1:46 pm |
|
|
PCM programmer, you are the King. Thank you very much. |
|
|
|