i2c en g

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 2 and 3.

Moderators: Benj, Mods

Simon ph
Posts: 1
Joined: Thu Feb 16, 2006 10:26 am
Location: Belgium, Français
Contact:

i2c en g

Postby Simon ph » Thu Feb 16, 2006 10:33 am

comment faire pour crΓ©er un flowcode en communication i2c?

N' existe t'il pas une macro toute faite?

Merci.

User avatar
Steve
Matrix Staff
Posts: 3073
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 87 times
Been thanked: 387 times
Contact:

Postby Steve » Fri Feb 17, 2006 2:13 pm

Bonjour Simon,

We hope to create a component for I2C in the next few weeks.

In the meantime, the following code may help - it is suitable for a 16F877a chip, but should work with any PICmicro with master-mode I2C capability:

Code: Select all

//prototypes
char I2C_Initialize();
char MAC_MaskFromBit(char bit);
void MAC_SendI2CByte(char val);
void MAC_SendI2CStart();
void MAC_SendI2CStop();

char I2C_Initialize()
{
    //initialise i2c
    #define I2C_TRIS trisc
    #define I2C_SDA  4
    #define I2C_SCL  3

    set_bit(I2C_TRIS, MX_I2C_SDA);
    set_bit(I2C_TRIS, MX_I2C_SCL);

    sspadd=48;      //48 = 100kHz, 11 = 400kHz (@ 19.6608MHz)

    sspstat = 0;
    set_bit(sspstat,SMP);

    sspcon2 = 0;

    //i2c master mode, SPEN
    sspcon = 0;
    set_bit(sspcon,SSPM3);
    set_bit(sspcon,CKP);
    set_bit(sspcon,SSPEN);
}


char I2C_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 I2C_ReadByte(char lastbyte)
{
    char retVal = 0;
    char mask;

    //wait until the bus is idle...
    mask = I2C_MaskFromBit(R_W);
    while (sspstat & mask);     // tx in progress?

    mask = I2C_MaskFromBit(ACKEN);
    mask = mask | I2C_MaskFromBit(RCEN);
    mask = mask | I2C_MaskFromBit(PEN);
    mask = mask | I2C_MaskFromBit(RSEN);
    mask = mask | I2C_MaskFromBit(SEN);
    while (sspcon2 & mask);     // bus busy?

    //set RCEN and wait for it to clear
    mask = I2C_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 = I2C_MaskFromBit(ACKEN);
    sspcon2 |= mask;
    while ((sspcon2 & mask) == mask);

    retVal = sspbuf;

    return (retVal);
}


void I2C_SendByte(char val)
{

    char rw_mask = I2C_MaskFromBit(R_W);
    char mask;

    //wait until the bus is idle...
    while (sspstat & rw_mask);     // tx in progress?

    mask = I2C_MaskFromBit(ACKEN);
    mask = mask | I2C_MaskFromBit(RCEN);
    mask = mask | I2C_MaskFromBit(PEN);
    mask = mask | I2C_MaskFromBit(RSEN);
    mask = mask | I2C_MaskFromBit(SEN);
    while (sspcon2 & mask);        // bus busy?

    mask = I2C_MaskFromBit(BF);
    while (sspstat & mask);        // buffer still full?

    sspbuf = val;

    while (sspstat & rw_mask);     // tx in progress?

    return;
}


void I2C_SendStart()
{
    //set SEN and wait for it to clear
    char mask = I2C_MaskFromBit(SEN);
    sspcon2 |= mask;
    while ((sspcon2 & mask) == mask);

    return;
}


void I2C_SendStop()
{
    //set PEN and wait for it to clear
    char mask = I2C_MaskFromBit(PEN);
    sspcon2 |= mask;
    while ((sspcon2 & mask) == mask);

    return;
}