Page 1 of 1
I2C with Flowcode
Posted: Wed Nov 30, 2005 2:09 pm
by manio1
Hi,
Is there any way to interface I2C device like 20c16 with Flowcode?
Posted: Mon Dec 05, 2005 12:24 pm
by Ingelsw
Hi All,
I think this is a question for Ian. I have the same request as Manio. I want to hook up some I2C components (like PCF8574) and i know this should be possible because the webserver E-blocks is using I2C. I suppose that i could go in the C code of the webserver E-block but i'm not such an expert in C. The beauty of flowcode is that in normal case, you don't have to do this...
Is a macro available to use I2C?
Many thanks
Wilbert
Posted: Mon Dec 05, 2005 2:35 pm
by Ian
We don't have a Flowcode component for I2C yet I'm afraid.
We are looking at doing one at some point soon, but with a busy schedule we can't say when.
However I2C comms is possible using C code.
there is a lot of detail on I2C on the Microchip website, and in the PIC datasheets.
Below is a bit of code that is taken from the Interent component, which utilizes I2C, that you can use as a basis for some custom code.
I haven't tried it however, and it will no doubt need a bit of tweaking for use in Flowcode, but it might help you out with your own experimentation.
//common implementations
char MaskFromBit(char bit)
{
char retVal = 0;
switch (bit)
{
case 0:
retVal = 0x01;
break;
case 1:
retVal = 0x02;
break;
case 2:
retVal = 0x04;
break;
case 3:
retVal = 0x08;
break;
case 4:
retVal = 0x10;
break;
case 5:
retVal = 0x20;
break;
case 6:
retVal = 0x40;
break;
case 7:
retVal = 0x80;
break;
}
return (retVal);
}
char ReadI2CByte(char lastbyte)
{
char retVal = 0;
char mask;
#ifndef MX_MI2C
asm error "Chip does not have master I2C capability"
#endif
//wait until the bus is idle...
mask = MaskFromBit(R_W);
while (sspstat & mask); // tx in progress?
mask = MaskFromBit(ACKEN);
mask = mask | MaskFromBit(RCEN);
mask = mask | MaskFromBit(PEN);
mask = mask | MaskFromBit(RSEN);
mask = mask | MaskFromBit(SEN);
while (sspcon2 & mask); // bus busy?
//set RCEN and wait for it to clear
mask = MaskFromBit(RCEN);
sspcon2 |= mask;
while ((sspcon2 & mask) == mask);
if (lastbyte == 0)
{
clear_bit(sspcon2, ACKDT);
} else {
set_bit(sspcon2, ACKDT);
}
//set ACKEN and wait for it to clear
mask = MaskFromBit(ACKEN);
sspcon2 |= mask;
while ((sspcon2 & mask) == mask);
retVal = sspbuf;
return (retVal);
}
void SendI2CByte(char val)
{
char rw_mask = MaskFromBit(R_W);
char mask;
#ifndef MX_MI2C
asm error "Chip does not have master I2C capability"
#endif
//wait until the bus is idle...
while (sspstat & rw_mask); // tx in progress?
mask = MaskFromBit(ACKEN);
mask = mask | MaskFromBit(RCEN);
mask = mask | MaskFromBit(PEN);
mask = mask | MaskFromBit(RSEN);
mask = mask | MaskFromBit(SEN);
while (sspcon2 & mask); // bus busy?
mask = MaskFromBit(BF);
while (sspstat & mask); // buffer still full?
sspbuf = val;
while (sspstat & rw_mask); // tx in progress?
//mask = MaskFromBit(ACKSTAT);
//while (sspcon2 & mask); // ack?
return;
}
void MAC_SendI2CStart()
{
#ifndef MX_MI2C
asm error "Chip does not have master I2C capability"
#endif
//set SEN and wait for it to clear
char mask = MaskFromBit(SEN);
sspcon2 |= mask;
while ((sspcon2 & mask) == mask);
return;
}
void MAC_SendI2CStop()
{
#ifndef MX_MI2C
asm error "Chip does not have master I2C capability"
#endif
//set PEN and wait for it to clear
char mask = MaskFromBit(PEN);
sspcon2 |= mask;
while ((sspcon2 & mask) == mask);
return;
}
Posted: Mon Dec 05, 2005 9:25 pm
by Ingelsw
Hi Ian,
Thanks a lot for your response and help. I will give it a try this weekend (i'm not sure with my knowledge of C that it will work).
I hope the I2C component will be available soon!!
Best regards
Wilbert
Posted: Tue Dec 06, 2005 9:21 am
by manio1
Thanks Ian,
I have no clue programing in C, but hopefuly with some reading i'll be able to understand your example.
Posted: Tue Jan 10, 2006 8:00 am
by jimhumphries
I too can use an I2C module. Could this be made "peripheral independent" so that any available pin/part can be used? Would I2C components be available for "generic" EEPROM, "generic" DAC, "genaric" clock, etc.?
Jim