⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 i2cbus.cpp

📁 并行口仿真i2c总线
💻 CPP
📖 第 1 页 / 共 5 页
字号:
  // On passe TOUT a '0' !!!
  SetBitI2C(CNST_BITN_LPTP_WSCL);
  delay(CNST_DATA_TIME_TEMPO);
}

/*--- START FUNCTION HEADER --------------------------------------------------*/
/* Name : SendI2C                                                             */
/* Role : Send 8 bits on the I2C bus                                          */
/* Interface :  IN Data2Send                                                  */
/* Pre-condition : None                                                       */
/* Constraints : None                                                         */
/* Behavior :                                                                 */
/*   DO                                                                       */
/*     [Send 8 bits, from MSB to LSB]                                         */
/*   OD                                                                       */
/*----------------------------------------------------------------------------*/
/* PROC SendI2C                                                               */
/* (                                                                          */
/* )                                                                          */
/* DATA                                                                       */
/* ATAD                                                                       */
/*                                                                            */
/* DO                                                                         */
/*   [Send 8 bits, from MSB to LSB] =                                         */
/*   DO                                                                       */
/*     WHILE [Counter = 7 to 0]                                               */
/*       DO                                                                   */
/*         [Set SDA to the bit number 'Counter' state]                        */
/*         [Validate with a complete clock cycle, SCL to '1', then '0']       */
/*       OD                                                                   */
/*     [At the end, set SDA and SCL to '0']                                   */
/*   OD                                                                       */
/* OD                                                                         */
/*--- END FUNCTION HEADER ----------------------------------------------------*/
void FluxI2C::SendI2C(char Data2Send)      // Envoie 8bits
{
  int LoopaSend;                           // Compteur Boucle

  // En Emission, le PREMIER bit envoye est le MSB -> le LSB
  for(LoopaSend=7; LoopaSend>=0; LoopaSend--)
  {
    // On positionne SDA a la valeur Correspondante
    if((Data2Send>>LoopaSend)&1)
      ClearBitI2C(CNST_BITN_LPTP_WSDA);
    else
      SetBitI2C(CNST_BITN_LPTP_WSDA);
    delay(CNST_DATA_TIME_TEMPO);

    // On passe SCL a '1'
    ClearBitI2C(CNST_BITN_LPTP_WSCL);
    delay(CNST_DATA_TIME_TEMPO);

    // On passe SCL a '0'
    SetBitI2C(CNST_BITN_LPTP_WSCL);
    delay(CNST_DATA_TIME_TEMPO);
  }

  // On remet TOUT a '0'
  SetBitI2C(CNST_BITN_LPTP_WSDA);
  delay(CNST_DATA_TIME_TEMPO);
}

/*--- START FUNCTION HEADER --------------------------------------------------*/
/* Name : ReceiveI2C                                                          */
/* Role : Receive 8 bits on the I2C bus                                       */
/* Interface :  OUT ReceiveI2C                                                */
/* Pre-condition : None                                                       */
/* Constraints : None                                                         */
/* Behavior :                                                                 */
/*   DO                                                                       */
/*     [Receive 8 bits, from MSB to LSB]                                      */
/*   OD                                                                       */
/*----------------------------------------------------------------------------*/
/* PROC ReceiveI2C                                                            */
/* (                                                                          */
/* )                                                                          */
/* DATA                                                                       */
/* ATAD                                                                       */
/*                                                                            */
/* DO                                                                         */
/*   [Receive 8 bits, from MSB to LSB] =                                      */
/*   DO                                                                       */
/*     WHILE [Counter = 7 to 0]                                               */
/*       DO                                                                   */
/*         [Set SCL to '1', rising edge of SCL]                               */
/*         [Set the buffetr bit number 'Counter' to SDA state]                */
/*         [Set SCL to '0', falling edge of SCL]                              */
/*         [Clear SDA and let the Slave set its state at the next loop]       */
/*       OD                                                                   */
/*   OD                                                                       */
/* OD                                                                         */
/*--- END FUNCTION HEADER ----------------------------------------------------*/

// WARNING, I NEVER TRIED THIS FUNCTION, BUT IT SHOULD WORKS FINE !!!

char FluxI2C::ReceiveI2C(void)
{
  char TempoReceive=0;            // Le Buffer de reception, vide au depart...
  int LoopaReceive;               // Le Compteur de Boucle

  // En Reception, le PREMIER bit recu est le MSB -> le LSB
  for(LoopaReceive=7; LoopaReceive>=0; LoopaReceive--)
  {
    // On passe SCL a '1'
    ClearBitI2C(CNST_BITN_LPTP_WSCL);
    delay(CNST_DATA_TIME_TEMPO);

    // On place SDA au bon endroit dans TempoReceive
    TempoReceive=TempoReceive|(TestBitI2C(CNST_BITN_LPTP_RSDA)<<LoopaReceive);

    // On redescend SCL a '0' -> TOUT a '0'
    SetBitI2C(CNST_BITN_LPTP_WSCL);
    delay(CNST_DATA_TIME_TEMPO);


    // ATTENTION : Je n'ai JAMAIS essaye si ca marche. Je pense qu'en fait,
    //             plutot que de redescendre SDA a la fin de chaque BIT, il
    //             sagisse en fait de laisser l'Esclave tout faire jusqu'a la
    //             fin, les 8 bits transmit, puis ENFIN redescendre SDA.
    //           > Donc deplacez les deux lignes suivantes juste apres
    //             l'accolade, en-dehors de la boucle FOR, avant le RETURN !!!
    //           > A ESSAYER SI CETTE VERSION NE FONCTIONNE PAS !!!
    SetBitI2C(CNST_BITN_LPTP_WSDA);
    delay(CNST_DATA_TIME_TEMPO);
  }

  return TempoReceive;
}

/*--- START FUNCTION HEADER --------------------------------------------------*/
/* Name : SendI2CFlux                                                         */
/* Role : Send a complete frame                                               */
/* Interface :  IN SendFlux's address (using pre-defined tables, see 'main')  */
/*             OUT SendI2CFlux                                                */
/*                 0 - Error                                                  */
/*                 1 - OK                                                     */
/* Pre-condition : None                                                       */
/* Constraints : None                                                         */
/* Behavior :                                                                 */
/*   DO                                                                       */
/*     [Send a complete frame on the I2C bus]                                 */
/*   OD                                                                       */
/*----------------------------------------------------------------------------*/
/* PROC SendI2CFlux                                                           */
/* (                                                                          */
/* )                                                                          */
/* DATA                                                                       */
/* ATAD                                                                       */
/*                                                                            */
/* DO                                                                         */
/*   [Send a complete frame on the I2C bus] =                                 */
/*   DO                                                                       */
/*     [Set the I2C bus in START mode]                                        */
/*     [Print the selcted LPT data port on which the data will be sent]       */
/*     [Print the I2C module address and the number of parameters to send]    */
/*     WHILE [Counter = 1 to SizeOfFrame+1] (skip number of params to send)   */
/*       DO                                                                   */
/*         [Clear the Error Counter]                                          */
/*           DO                                                               */
/*             [Send the byte number 'Counter' on the I2C bus]                */
/*             [Increase the Error Counter / Number of Try Counter]           */
/*           OD                                                               */
/*         WHILE [(NO Ack from Slave) AND (Error Counter < Number of try)]    */
/*         [Print the byte number sent, its value, and the number of try]     */
/*         IF [Error Counter = Number of Try] THEN                            */
/*           [Exit loop and finish the routine with a final test]             */
/*         ELSE                                                               */
/*           [Increase the number of bytes really sent]                       */
/*         FI                                                                 */
/*       OD                                                                   */
/*     [Set the I2C bus in STOP mode]                                         */
/*     IF [Number of bytes really sent = Number of byte to send] THEN         */
/*       [Return '1', OK]                                                     */
/*     ELSE                                                                   */
/*       [Return '0', error]                                                  */
/*     FI                                                                     */
/*   OD                                                                       */
/* OD                                                                         */
/*--- END FUNCTION HEADER ----------------------------------------------------*/
int FluxI2C::SendI2CFlux(char SendFlux[])
{
  int NbReallySent=0;               // Compteur d'Octet envoyes
  int LoopaSend;                    // Compteur de boucle
  int ErrorSend;

  StartI2C();
  
  // ATTENTION : sur l'ecran du PC, une description pr閏ise de ce qui est
  //             envoye et/ou recus sur le port I2c est affiche
  printf("Sending on LPT%d : ", FI2C_LPT_PORT+1);
  // {'Adresse du module I2C'['Nombre d'octets de parametre 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -