C51 COMPILER V9.51   TWI                                                                   11/20/2017 11:41:54 PAGE 1   


C51 COMPILER V9.51, COMPILATION OF MODULE TWI
OBJECT MODULE PLACED IN .\OBJ\TWI.obj
COMPILER INVOKED BY: C:\Keil4\C51\BIN\C51.EXE ..\code\TWI.C BROWSE DEBUG OBJECTEXTEND PRINT(.\LST\TWI.lst) OBJECT(.\OBJ\
                    -TWI.obj)

line level    source

   1          /*
   2          */      
   3          #define _TWI_C
   4          
   5          #include        "Incfile.h"
   6          
   7          void TWI0_ISR () interrupt  INT_VECTOR_TWI0
   8          {
   9   1              switch ( SISTA )
  10   1              {
  11   2                      case 0x08:      // a start has been transmitted.
  12   2                              CLR_TWI_STA();
  13   2                              SIDAT = SlaveAddress; //SLA + W
  14   2                              break;
  15   2                      case 0x20:      // SLA+W transmited, NOT ACK received
  16   2                              TWI0Status = TWI_RW_FAIL;
  17   2                              break;
  18   2                      case 0x18:      // SLA+W transmited, ACK received 
  19   2                              SIDAT = SlaveRegAddr;
  20   2                              break;
  21   2                      case 0x28:      // DATA transmited, ACK received
  22   2                              SEND_TWI_STA();         //restart
  23   2                              break;
  24   2                      case 0x30:      // DATA transmited, NOT ACK received
  25   2                              TWI0Status = TWI_RW_FAIL;
  26   2                              break;
  27   2                      case 0x10:      // restart transmited
  28   2                              CLR_TWI_STA();
  29   2                              SIDAT = SlaveAddress | 0x01;    //SLA + R
  30   2                              break;
  31   2                              
  32   2                      case 0x40:      // SLA+R transmited, ACK received 
  33   2                              SlaveDataCnt=0;
  34   2                              if ( SlaveDataLen > 1 )
  35   2                              {
  36   3                                      SEND_TWI_ACK();
  37   3                              }
  38   2                              else
  39   2                              {
  40   3                                      SEND_TWI_NOACK();
  41   3                              }
  42   2                              break;
  43   2                      case 0x50:      // Data received
  44   2                              SlaveData = SIDAT;
  45   2                              SlaveDataCnt++;
  46   2                              if(SlaveDataCnt<(SlaveDataLen-1))
  47   2                              {
  48   3                                      SEND_TWI_ACK();
  49   3                              }
  50   2                              else
  51   2                              {
  52   3                                      SEND_TWI_NOACK();
  53   3                              }
  54   2                              break;
C51 COMPILER V9.51   TWI                                                                   11/20/2017 11:41:54 PAGE 2   

  55   2                              
  56   2                      case 0x58:      // All data received
  57   2                              SlaveData = SIDAT;
  58   2                              TWI0Status = TWI_RW_SUCCESS;
  59   2                              SEND_TWI_STO();                 // 发送停止位
  60   2                              break;
  61   2                      case 0x38:      // Arbitration lost in SLA+W    OR Data bytes
  62   2                              TWI0Status = TWI_RW_FAIL;
  63   2                              break;
  64   2                      case 0x48:      // SLA+R transmited, NOT ACK received
  65   2                              TWI0Status = TWI_RW_FAIL;
  66   2                              break;
  67   2                      default:        //通讯有错误，重新处理
  68   2                              TWI0Status = TWI_RW_FAIL;
  69   2                              break;
  70   2              }
  71   1              if(TWI0Status==TWI_RW_FAIL)
  72   1              {
  73   2                      SEND_TWI_STO();                 // 发送停止位
  74   2              }
  75   1              CLR_TWI_SI();           // 清 SI
  76   1              
  77   1      }
  78          
  79          /***********************************************************************************
  80          函数名称:   void ReStartTWI0(void)
  81          功能描述:   重启TWI0 
  82          输入参数:   
  83          返回参数:     
  84          *************************************************************************************/
  85          void ReStartTWI0(void)
  86          {
  87   1              SICON = CR2 | CR0;              // SYSCLK = 24M, SYSCLK / 256 = 93.75k
  88   1              nop();
  89   1              nop();
  90   1              nop();
  91   1              SICON |= ENSI;                  // 打开TWI模块
  92   1      }
  93          
  94          void TWI0Proc(void)
  95          {
  96   1              switch(TWI0Status)
  97   1              {
  98   2                      case TWI_RW_IDLE:
  99   2                              if(TWI0Runtime==0)
 100   2                              {
 101   3                                      LED_RUN=0;
 102   3                                      CLR_TWI_I();
 103   3                                      ReStartTWI0();
 104   3                                      SlaveAddress=0xA0;
 105   3                                      SlaveDataLen=1;
 106   3                                      SlaveRegAddr++;
 107   3                                      TWI0Ovtime=TWI_OV_TIME_MAX;
 108   3                                      TWI0FailCnt=0;
 109   3                                      TestOutStr("\nTest ");
 110   3                                      TestOutHex(SlaveRegAddr);
 111   3                                      SEND_TWI_STA();
 112   3                                      CLR_TWI_SI();
 113   3                                      SET_TWI_I();
 114   3                                      TWI0Status=TWI_RW_WORKING;
 115   3                              }
 116   2                              break;
C51 COMPILER V9.51   TWI                                                                   11/20/2017 11:41:54 PAGE 3   

 117   2                      case TWI_RW_WORKING:
 118   2                              if(TWI0Ovtime==0)
 119   2                              {
 120   3                                      LED_RUN=1;
 121   3                                      SEND_TWI_STO();                 // 发送停止位
 122   3                                      CLR_TWI_SI();
 123   3                                      TWI0Runtime=TWI_RUN_TIME_MAX;
 124   3                                      TWI0Status=TWI_RW_IDLE;
 125   3                              }
 126   2                              break;
 127   2                      case TWI_RW_SUCCESS:
 128   2                              TestOutHex(SlaveData);
 129   2                              CLR_TWI_I();
 130   2                              TWI0Runtime=TWI_RUN_TIME_MAX;
 131   2                              TWI0Status=TWI_RW_IDLE;
 132   2                              LED_RUN=1;
 133   2                              break;
 134   2                      case TWI_RW_FAIL:
 135   2                              TWI0FailCnt++;
 136   2                              if(TWI0FailCnt<TWI_FAIL_CNT_MAX)
 137   2                              {
 138   3                                      ReStartTWI0();
 139   3                                      SEND_TWI_STA();
 140   3                                      CLR_TWI_SI();
 141   3                                      SET_TWI_I();
 142   3                                      TWI0Status=TWI_RW_WORKING;
 143   3                              }
 144   2                              else
 145   2                              {
 146   3                                      TWI0Runtime=TWI_RUN_TIME_MAX;
 147   3                                      TWI0Status=TWI_RW_IDLE;
 148   3                                      LED_RUN=1;
 149   3                              }
 150   2                              break;
 151   2                      default:
 152   2                              TWI0Status=TWI_RW_FAIL;
 153   2                              break;
 154   2              }
 155   1      }
 156          
 157          


MODULE INFORMATION:   STATIC OVERLAYABLE
   CODE SIZE        =    294    ----
   CONSTANT SIZE    =      7    ----
   XDATA SIZE       =   ----    ----
   PDATA SIZE       =   ----    ----
   DATA SIZE        =      9    ----
   IDATA SIZE       =   ----    ----
   BIT SIZE         =   ----    ----
END OF MODULE INFORMATION.


C51 COMPILATION COMPLETE.  0 WARNING(S),  0 ERROR(S)
