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

📄 spi.lst

📁 用于Slave I2C得读写,此代码针对特定得芯片,但对其他得芯片也有借鉴作用.
💻 LST
字号:
C51 COMPILER V7.02b   SPI                                                                  08/28/2006 18:59:06 PAGE 1   


C51 COMPILER V7.02b, COMPILATION OF MODULE SPI
OBJECT MODULE PLACED IN .\output\spi.obj
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE spi.c LARGE OPTIMIZE(9,SPEED) BROWSE DEBUG OBJECTEXTEND PRINT(.\output\spi.
                    -lst) OBJECT(.\output\spi.obj)

stmt level    source

   1          /*
   2          **********************************************************************************************
   3          * Project:      T8Lib
   4          * File:         spi.c
   5          * Contents: 
   6          *           The spi module c file(software spi)
   7          * $Date: 10/13/05    Michal    T8R01LIB v0.1  initial
   8          * $Date: 11/29/05    Michal    T8R01LIB v0.2  test finish  both 51 and MCF5249
   9          * $Date: 11/30/05    Michal    T8R01LIB v0.3  make lib OK  on  MCF5249
  10          * $Date: 12/01/05    Michal    T8R01LIB v0.4  Add HW_IIC ,HW_SPI macro  
  11          * $Date: 12/02/05    Michal    T8R01LIB v0.5  Add  function below 
  12          *                 static void DoXX(BYTE* out ,BYTE* in,BYTE len,BOOL mode)
  13          *                 static BOOL DoComunication(BYTE* array, BYTE len,BOOL mode)
  14          *
  15          *                Moidfy some *Cmd Fuction and Reply() function
  16          * $Date: 12/05/05    Michal    T8R02LIB v0.1  Add errcode1 to identify T8 off i2c bus state 
  17                  * $Date: 12/29/05 Michal T8R02LIB v0.2
  18                               Modify InitezT8(BOOL mode ) to BOOL  InitezT8Lib(BOOL ComMod,BYTE I2cSlvAdr,BOOL I2cIsHigh,B
             -YTE SpiCLkMod,WORD SpiRate,BYTE *key);
  19                                  void I2c_Init( ); void I2c_Sel( BYTE  SlvAdr, BOOL isHigh)  to void I2c_Init(BYTE 
             - SlvAdr, BOOL isHigh);
  20                                  void  Spi_Init();BOOL  Spi_ClkSel(BYTE mode, WORD rate);  To void  Spi_Init(BYTE m
             -ode, WORD rate);
  21                           Delete void SpiClkSelOpr(BYTE mode, WORD rate) ;
  22                                  void I2c_Sel( BYTE  SlvAdr, BOOL isHigh);  
  23                                  void SetXXKey(BYTE* key); 
  24          * $Date: 12/29/05 Michal T8R02LIB v0.5 test communiction at 400khz speed  is OK  
  25          * Copyright (c) 2005 Fameg, Inc. All rights reserved
  26          ***********************************************************************************************
  27          */
  28          
  29          #include "ezT8.h"
  30          #include "spi.h"
  31          
  32          
  33          #if  SW_SPI 
  34          /*******************************************************
  35          *     local variable and function
  36          *******************************************************/
  37          static BYTE ClkMode=0;           /*  =0 CPOL=CPHA=0
  38                                               =3 CPOL=CPHA=1 
  39                                           */
  40          static WORD DelayCnt;
  41          
  42          static void  StartSPI();
  43          static void  EndSPI();
  44          static void  Spi_SendByte(BYTE dat);
  45          static BYTE  Spi_RecByte();
  46           void ToSpiModeOpr();
  47          
  48          
  49          
  50          #if SW_IIC     //is defined in I2c 
  51          
C51 COMPILER V7.02b   SPI                                                                  08/28/2006 18:59:06 PAGE 2   

  52              BOOL  ReadPinSDOUT()  { return(SDOUT) ;  } 
  53                  extern void  Delay_0_5us(BYTE  cnt) ; 
  54                  extern void  WritePinSCL();
  55                  extern void ClearPinSCL();
  56                  extern void WritePinSDA();
  57                  extern void ClearPinSDA();
  58          
  59          
  60          #else //!SW_IIC
                      void  WritePinSCL(){SCL=1;      }
              
                      void ClearPinSCL(){SCL=0;}
                              
                      void  setSDAout(){} 
              
                      void WritePinSDA(){SDA=1;}
                      void ClearPinSDA(){SDA=0;}
               
                  BOOL  ReadPinSDOUT()  { return(SDOUT) ;  } 
              
                                              
                 void Delay_0_5us(unsigned char cnt)
                 {      
                  unsigned char ct=50; 
                      while(cnt--)
                      {
                              while(ct--)
                      //      asm("nop");
                              ct=50;  
                              
                                      
                      }
                 }            
              #endif
  86          
  87                  void WritePinSS_N(){SS_N=1;}
  88                  void ClearPinSS_N() {SS_N=0;}
  89          
  90          
  91          
  92          void Spi_Init(BYTE mode,WORD rate)
  93          {
  94   1              /* SDA */
  95   1                      
  96   1          ClearPinSS_N();     
  97   1          Delay_0_5us(8);
  98   1          WritePinSS_N();      
  99   1          Delay_0_5us(8);
 100   1          ClearPinSS_N();      
 101   1          Delay_0_5us(8);
 102   1          WritePinSS_N();      
 103   1          Delay_0_5us(8);
 104   1          
 105   1          ClearPinSS_N();      
 106   1          Delay_0_5us(8);
 107   1          WritePinSS_N();      
 108   1          Delay_0_5us(8);
 109   1      
 110   1              if((mode!=0&&mode!=3)||rate>400||rate*1000>=CPUHZ)
 111   1                      return ;
 112   1              ClkMode=mode;
 113   1              DelayCnt=1000/0.5/2/rate;
C51 COMPILER V7.02b   SPI                                                                  08/28/2006 18:59:06 PAGE 3   

 114   1      
 115   1              if(ClkMode==0)
 116   1                   ClearPinSCL();                        
 117   1              else
 118   1                   WritePinSCL();       
 119   1              
 120   1              return ;
 121   1      }
 122          
 123          
 124          
 125          /*******************************************************
 126          function: Initialize SS_N SCL,SDA,SDOUT pin. 
 127          if in mode 3  add half clk low   
 128          *******************************************************/
 129          static void  StartSPI()
 130          {
 131   1               WritePinSS_N();
 132   1           WritePinSDA();     
 133   1      //     WritePinSDOUT(1);        
 134   1               Delay_0_5us(DelayCnt);
 135   1              
 136   1      //     WritePinSDOUT(0);
 137   1           ClearPinSS_N();
 138   1               Delay_0_5us(DelayCnt);               //tL  
 139   1           if(ClkMode==3)
 140   1               {
 141   2                      Delay_0_5us(DelayCnt);
 142   2                  ClearPinSCL();         
 143   2               }
 144   1               
 145   1      }
 146          /*******************************************************
 147          function: configure SS_N ,SCL,SDA,SDOUT to default value 
 148          *******************************************************/
 149          static void  EndSPI()
 150          {
 151   1              Delay_0_5us(DelayCnt);                           
 152   1              if(ClkMode==0)
 153   1                 ClearPinSCL();
 154   1              else
 155   1                 WritePinSCL();
 156   1              WritePinSDA();
 157   1      //      WritePinSDOUT(1);
 158   1      
 159   1              Delay_0_5us(DelayCnt);                           
 160   1              WritePinSS_N();
 161   1              Delay_0_5us(DelayCnt);                           //tI  idel time
 162   1      }
 163          /*******************************************************
 164          function: send one byte data with 8 bits. when Clock is 
 165          rise range, the flash will receive. So MCU must send data 
 166          with rise range clock. dat -- data sent by MCU
 167          *******************************************************/
 168          static void  Spi_SendByte(BYTE dat)
 169          {
 170   1        BYTE BitCnt;
 171   1        if(ClkMode==0)
 172   1        {
 173   2          for(BitCnt = 0;BitCnt < 8;BitCnt++)  
 174   2          {
 175   3             if((dat << BitCnt) & 0x80)   /*判断发送位*/
C51 COMPILER V7.02b   SPI                                                                  08/28/2006 18:59:06 PAGE 4   

 176   3                    WritePinSDA();
 177   3             else      
 178   3                    ClearPinSDA();
 179   3             Delay_0_5us(DelayCnt); 
 180   3             WritePinSCL();               
 181   3             Delay_0_5us(DelayCnt); 
 182   3                 ClearPinSCL();  
 183   3             }        
 184   2              
 185   2               }
 186   1        else
 187   1        {
 188   2          ClearPinSCL();
 189   2          for(BitCnt = 7;BitCnt !=0;BitCnt--,dat=dat<<1)  
 190   2          {
 191   3             if(dat& 0x80)   /*判断发送位*/
 192   3                   WritePinSDA();
 193   3             else      
 194   3                   ClearPinSDA();
 195   3             Delay_0_5us(DelayCnt); 
 196   3             WritePinSCL();               
 197   3             Delay_0_5us(DelayCnt); 
 198   3                 ClearPinSCL();  
 199   3             }
 200   2             if(dat&  0x80)  /*判断发送位*/
 201   2                   WritePinSDA();
 202   2             else       
 203   2                   ClearPinSDA();
 204   2             Delay_0_5us(DelayCnt); 
 205   2             WritePinSCL();                   
 206   2        }
 207   1      }
 208          /*******************************************************
 209          function: receive one byte data with 8 bits. when Clock is 
 210          fall range, the flash will send data. So MCU must receive
 211          data, and then  command flash to send next data with fall 
 212          range clock. retc -- data received from T8
 213          *******************************************************/
 214          static BYTE  Spi_RecByte()
 215          {
 216   1          BYTE retc;
 217   1          BYTE BitCnt;
 218   1          BOOL t;
 219   1                      
 220   1          retc = 0; 
 221   1      //    SDOUT = 1;             
 222   1       
 223   1        if(ClkMode==0)
 224   1        {
 225   2          for(BitCnt = 0;BitCnt < 8;BitCnt++)
 226   2          {             
 227   3                  Delay_0_5us(DelayCnt);   
 228   3              WritePinSCL();
 229   3                  Delay_0_5us((DelayCnt>>1));   
 230   3              retc = retc << 1;
 231   3                  t=ReadPinSDOUT();   
 232   3              if(t== 1)
 233   3                          retc = retc + 1;
 234   3                  Delay_0_5us((DelayCnt>>1));                 
 235   3                  ClearPinSCL();
 236   3            }
 237   2          return(retc);
C51 COMPILER V7.02b   SPI                                                                  08/28/2006 18:59:06 PAGE 5   

 238   2        }
 239   1        else
 240   1        {
 241   2                  for(BitCnt = 7;BitCnt !=0;BitCnt--)
 242   2                  {             
 243   3                          ClearPinSCL();
 244   3                          Delay_0_5us(DelayCnt);   
 245   3                      retc = retc << 1;
 246   3                      t=ReadPinSDOUT();       
 247   3                  if(t== 1)
 248   3                                  retc = retc + 1;
 249   3                      WritePinSCL();
 250   3                          Delay_0_5us(DelayCnt);   
 251   3                          ClearPinSCL();
 252   3                    }
 253   2                  Delay_0_5us(DelayCnt);   
 254   2              retc = retc << 1;
 255   2                  t=ReadPinSDOUT();   
 256   2              if(t== 1)
 257   2                          retc = retc + 1;
 258   2              WritePinSCL();
 259   2                return(retc); 
 260   2              }
 261   1      }
 262          /*******************************************************
 263          function: read data from T8, buffer -- data address
 264          Len -- read data length. 
 265          *******************************************************/
 266          void  Spi_ReadData( BYTE *buffer, BYTE Len)
 267          {
 268   1               BYTE i;
 269   1        #if COMENINT==1        
                 vDisableIsrs();//add   
                 #endif 
 272   1               StartSPI();
 273   1               for(i = 0;i < Len ;i++)
 274   1               {
 275   2                       *buffer = Spi_RecByte();
 276   2                       buffer++;
 277   2               }
 278   1               EndSPI();
 279   1         #if COMENINT==1       
                 vEnableIsrs();//add
                 #endif 
 282   1      }
 283          
 284          /*******************************************************
 285          function: write data to T8
 286          *******************************************************/
 287          void  Spi_WriteData(BYTE *buffer, BYTE Len)
 288          {
 289   1              BYTE i;
 290   1        #if COMENINT==1        
                 vDisableIsrs();//add   
                 #endif 
 293   1              StartSPI();
 294   1      
 295   1              for(i = 0;i < Len ;i++)
 296   1              {
 297   2                  Spi_SendByte(*buffer);
 298   2                  buffer++;
 299   2              }
C51 COMPILER V7.02b   SPI                                                                  08/28/2006 18:59:06 PAGE 6   

 300   1             EndSPI();
 301   1         #if COMENINT==1       
                 vEnableIsrs();//add
                 #endif 
 304   1      }
 305          #elif(!SPI_USED) //both SW_SPI and HW_SPI are not need,define dummy functions
              void  Spi_Init(BYTE mode,WORD rate){}
              
              void  Spi_WriteData( BYTE *buffer, BYTE Len){}
              void  Spi_ReadData( BYTE *buffer, BYTE Len){}
              #endif //SW_SPI


MODULE INFORMATION:   STATIC OVERLAYABLE
   CODE SIZE        =    737    ----
   CONSTANT SIZE    =   ----    ----
   XDATA SIZE       =      3      18
   PDATA SIZE       =   ----    ----
   DATA SIZE        =   ----    ----
   IDATA SIZE       =   ----    ----
   BIT SIZE         =   ----    ----
END OF MODULE INFORMATION.


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

⌨️ 快捷键说明

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