Quoting "Laurent FAILLIE" <[email protected]>:
> Hello,
>
> I googled but w/o success ... can someone point me out a tutorial or
> at least an example of I2C slave programming with SDcc on PIC (I'm
> using 16F88) ?
>
I don't know if the 16F88 has a hardware I2C circuit, but if it has,
I've used this (just the relevant functions here):
// Send an I2C start sequence
void I2Cstart()
{
SEN=1;
while(SEN) {
}
SSPIF=0;
}
// Send an I2C STOP sequence
void I2Cstop()
{
PEN=1;
while(PEN) {
}
SSPIF=0;
}
// Send a repeated START
void I2Crestart()
{
RSEN=1;
while(RSEN) {
}
SSPIF=0;
}
These were the control signals, next is what I used for the data,
after calling relevant previous functions (never mind my comments, I
assume an RTC won't complain about any data errors):
// Shift one byte of data to the bus.
// As of this version, no error checing is done. The function
// assumes the receiver sends an "Acknowledged".
void I2Cwrite(unsigned char c)
{
SSPBUF=c;
while(!SSPIF);
SSPIF=0;
}
// Shift in one byte from the bus and then sends and "Acknowledge"
unsigned char I2Cread(unsigned char ack)
{
unsigned char c;
RCEN=1;
while(!BF);
c=SSPBUF;
if(ack) {
ACKDT=0;
}
else {
ACKDT=1;
}
ACKEN=1;
return(c);
}
************************************
Then, the bit-banged version, if you don't have proper I2C hardware.
This is on a 16F690, mapping like this:
SDA=PORTB4
SCL=PORTB6
(You can try without the delays, for me it was just for being on the
safe side)
void I2Cstart(void)
{
SCL=0;
I2Cdelay();
SDA=1;
I2Cdelay();
SCL=1;
I2Cdelay();
SDA=0;
I2Cdelay();
}
void I2Cstop(void)
{
SDA=0;
I2Cdelay();
SCL=1;
I2Cdelay();
SDA=1;
I2Cdelay();
}
void I2Cwrite(unsigned char c)
{
signed char i;
SCL=0;
I2Cdelay();
for(i=7 ; i>=0 ; i--) {
SDA=((c>>i)&0x01);
I2Cdelay();
SCL=1;
I2Cdelay();
while(SCL_RD==0) { // Ensure the clock has been released by the slave
}
SCL=0;
I2Cdelay();
}
I2Cdelay();
while(SDA_RD) { // wait for ack
}
SCL=1;
I2Cdelay();
I2Cdelay();
I2Cdelay();
}
unsigned char I2Cread(unsigned char ack)
{
unsigned char i,byte;
byte=0x00;
SCL=0;
for(i=0;i<8;i++) {
I2Cdelay();
SDA=1;
SCL=1;
while(SCL_RD==0) { // Clock stretch
}
I2Cdelay();
byte=byte << 1;
byte |= (SDA_RD & 0x01);
I2Cdelay();
SCL=0;
}
I2Cdelay();
if(ack==0) {
SDA=1;
I2Cdelay();
SCL=1;
I2Cdelay();
SCL=0;
I2Cdelay();
}
else {
SDA=0;
I2Cdelay();
SCL=1;
I2Cdelay();
SCL=0;
}
return(byte);
}
Note: I haven't bothered with error checking or things like that, you
may want to add some later on...
I hope this helps at all.
Regards, Robert
------------------------------------------------------------------------------
This SF.net email is sponsored by Sprint
What will you do first with EVO, the first 4G phone?
Visit sprint.com/first -- http://p.sf.net/sfu/sprint-com-first
_______________________________________________
Sdcc-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/sdcc-user