Hi,
Is there any way to interface I2C device like 20c16 with Flowcode?
I2C with Flowcode
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
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
-
- Posts: 110
- Joined: Thu Sep 29, 2005 10:53 am
- Location: Matrix Multimedia
- Been thanked: 1 time
- Contact:
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;
}
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;
}
-
- Posts: 112
- Joined: Wed Oct 12, 2005 6:29 pm
- Location: USA
- Been thanked: 1 time