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

📄 main.lst

📁 该程序利用单片机完成数据在串口与SMBus总线(即I2C)之间的转发
💻 LST
📖 第 1 页 / 共 2 页
字号:
C51 COMPILER V7.06   MAIN                                                                  12/04/2006 10:14:46 PAGE 1   


C51 COMPILER V7.06, COMPILATION OF MODULE MAIN
OBJECT MODULE PLACED IN Main.OBJ
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE Main.c BROWSE DEBUG OBJECTEXTEND

stmt level    source

   1           //程序说明:
   2          //该程序利用单片机完成数据在串口与SMBus总线(即I2C)之间的转发,即将串口接收
   3          //到的数据通过I2C转发出去,将I2C接收到的数据通过串口转发出去
   4          //
   5          #include "c8051f310.h"
   6          #define  SYSCLK         24500000             // System clock frequency
   7          #define  SMB_FREQUENCY  50000                // Target SMBus frequency
   8          
   9          
  10          #define  WRITE          0x00                 // SMBus WRITE command
  11          #define  READ           0x01                 // SMBus READ command
  12          
  13          // Device addresses (7 bits, lsb is a don't care)
  14          #define  F310_SLAVE     0xA0                 // Device address for slave target
  15          
  16          // Status vector - top 4 bits only
  17          #define  SMB_MTSTA      0xE0                 // (MT) start transmitted
  18          #define  SMB_MTDB       0xC0                 // (MT) data byte transmitted
  19          #define  SMB_MRDB       0x80                 // (MR) data byte received
  20          // End status vector definition
  21          
  22          //-----------------------------------------------------------------------------------
  23          // Global VARIABLES
  24          //-----------------------------------------------------------------------------------
  25          unsigned char SMB_DATA_IN;                   // Global holder for SMBus data
  26                                                       // All receive data is written here
  27          
  28          unsigned char SMB_DATA_OUT;                  // Global holder for SMBus data.
  29                                                       // All transmit data is read from here
  30          
  31          unsigned char TARGET;                        // Target SMBus slave address
  32          
  33          bit SMB_BUSY;                                // Software flag to indicate when the
  34                                                       // SMB_Read() or SMB_Write() functions
  35                                                       // have claimed the SMBus
  36          
  37          bit SMB_RW;                                  // Software flag to indicate the
  38                                                    // direction of the current transfer
  39          
  40          /*SMBus地址*/
  41          #define         ADDR_SLAVE                      0x10
  42          #define         ADDR_SLAVE_W            0x20
  43          #define         ADDR_SLAVE_R            0x21
  44          #define         ADDR_SELF                               0x01
  45          #define         ADDR_SELF_W                     0x02
  46          #define         ADDR_SELF_R                     0x03
  47          
  48          
  49          #define         MAXBUFSIZE      50                                                                      //缓冲区大小
  50          /*全局变量*/
  51          unsigned char   SMBus_Txchar;                                                                   //存储SMBus口待发送的字节
  52          unsigned char   SMBus_TxCount = 0;                                              //SMBus发送字节的计数
  53          
  54          unsigned char xdata UARTRxBuf[MAXBUFSIZE];              //定义一个UART串口接收缓冲区
  55          unsigned char UARTRxBuf_Front = -1;                                             //UART串口缓冲区的头位置
C51 COMPILER V7.06   MAIN                                                                  12/04/2006 10:14:46 PAGE 2   

  56          unsigned char UARTRxBuf_Rear = -1;                                              //UART串口缓冲区的尾位置
  57          bit UART_Tx_Complete = 0;                                                                                       //指示UART发送一个字节完毕
  58          
  59          unsigned char xdata SMBusRxBuf[MAXBUFSIZE];             //定义一个SMBus接收缓冲区
  60          unsigned char SMBusRxBuf_Front = -1;                                    //SMBus接收缓冲区头位置
  61          unsigned char SMBusRxBuf_Rear = -1;                                             //SMBus接收缓冲区尾位置
  62          bit SMBus_Tx_Complete = 0;                                                                              //指示SMBus发送一个字节完毕
  63          
  64          /*函数原型声明*/
  65          void SMBus_Tx(unsigned char ch);
  66          void UART_Tx(unsigned char ch);
  67          unsigned char ReadBuffer(unsigned char* Buffer,unsigned char* pFront,unsigned char* pRear);
  68          
  69          unsigned char UartFrame_Complete=0;
  70          unsigned int pRomAddr=0;
  71          unsigned char xdata UartFrame[300];//,UartTxFrame[32];
  72          int i=0;
  73          int UartFrame_Num=0;
  74          unsigned char  temp_last='P';
  75          int num=0;
  76          sbit LED = P3^3;
  77          unsigned char temp,t=0,IIC_Busy=0,RWByte=0,RxFlag=0,RxData=0x00,StopRequire=0;
  78          int I2CRxNum=0;
  79          // Peripheral specific initialization functions,
  80          // Called from the Init_Device() function
  81          void PCA_Init()
  82          {
  83   1          PCA0MD    &= ~0x40;
  84   1          PCA0MD    = 0x00;
  85   1      }
  86          
  87          void Port_IO_Init()
  88          {
  89   1      
  90   1          XBR0      = 0x05;
  91   1          XBR1      = 0x40;
  92   1      
  93   1      
  94   1      
  95   1      /*   P0MDIN = 0xFF;                            // All P0 pins digital input
  96   1      
  97   1         P0MDOUT = 0x00;                           // All P0 pins open-drain output
  98   1         P0 = 0xFF;   */                             //
  99   1      
 100   1      }
 101          /*
 102          void Timer_Init()
 103          {
 104              
 105              TMOD      = 0x22;//Mode2:8 bit Auto Reload
 106              CKCON     = 0x0C;//Timer0,1 use SYSCLK
 107              TH0       = 0xF6;
 108              TH1       = 0x2B;//Timer1 as UART Baud Triger, 0X2B~57600
 109                  TL1       = 0x2B;
 110                  TCON      = 0x50;//Timer1,0 permission
 111          }
 112          */
 113          void Timer0_Init (void)
 114          {
 115   1         CKCON &= 0xF8;                           // Timer0 clock source = SYSCLK / 12
 116   1         TMOD  |= 0x02;                              // Timer0 in 8-bit auto-reload mode
 117   1      
C51 COMPILER V7.06   MAIN                                                                  12/04/2006 10:14:46 PAGE 3   

 118   1         TH0 = -(SYSCLK/SMB_FREQUENCY/12/3) ;       // Timer0 configured to overflow at 1/3
 119   1                                                   // the rate defined by SMB_FREQUENCY
 120   1      
 121   1         TL0 = -(SYSCLK/SMB_FREQUENCY/12/3);       // Timer0 preloaded to overflow at the
 122   1                                                   // rate defined by SMB_FREQUENCY
 123   1      
 124   1         TR0 = 1;                                  // Timer0 enabled
 125   1      /*
 126   1             TCON      = 0x50;
 127   1          TMOD      = 0x22;
 128   1          CKCON     = 0x0C;
 129   1          TH0       = 0xF6;
 130   1          TH1       = 0x61;
 131   1              TL1       = 0x61;*/
 132   1      }
 133          
 134          void UART_Init()//use timer1
 135          {
 136   1          TMOD     |= 0x20;//Mode2:8 bit Auto Reload
 137   1          CKCON    |= 0x08;//Timer1 use SYSCLK
 138   1          //TH0       = 0xF6;
 139   1          TH1       = 0x2B;//Timer1 as UART Baud Triger, 0X2B~57600
 140   1              TL1       = 0x2B;
 141   1              TCON      |= 0x40;//Timer1 permission
 142   1      
 143   1      
 144   1              SCON0     = 0x10;
 145   1      
 146   1      
 147   1      }
 148          
 149          void SMBus_Init()//use timer0
 150          {
 151   1        //  SMB0CF    = 0x80;
 152   1      //      SMB0CF = 0x5C;                            // Use Timer0 overflows as SMBus clock 
 153   1                                                   // source
 154   1                                                   // Disable slave mode
 155   1                                                   // Enable setup & hold time extensions
 156   1                                                   // Enable SMBus Free timeout detect
 157   1                                                   // Enable SCL low timeout detect
 158   1              SMB0CF |= 0x80;                           // Enable SMBus;
 159   1      }
 160          
 161          void Interrupts_Init()
 162          {
 163   1          EIE1      = 0x01;//SMBUS
 164   1          IE        = 0x10;//UART
 165   1      }
 166          
 167          // Initialization function for device,
 168          // Call Init_Device() from your main program
 169          void Init_Device(void)
 170          {
 171   1      
 172   1              PCA_Init();
 173   1              OSCICN  |=0x03;//SYSCLK取内部振荡器,不分频
 174   1          Port_IO_Init();
 175   1          Timer0_Init();
 176   1          UART_Init();
 177   1          SMBus_Init();
 178   1          Interrupts_Init();
 179   1      }
C51 COMPILER V7.06   MAIN                                                                  12/04/2006 10:14:46 PAGE 4   

 180          
 181          
 182          
 183          /*main()主函数*/
 184          void main()
 185          {
 186   1              Init_Device();                  //单片机初始化
 187   1              IE = 0x90;                                      //UART中断使能
 188   1      
 189   1      //LED=0;
 190   1      //LED=1;
 191   1              EA=1;
 192   1      
 193   1      //      SBUF0=0X33;
 194   1      //      while(UART_Tx_Complete == 0);
 195   1      //      UART_Tx_Complete = 0;
 196   1              //TI0 = 0;
 197   1      //      pRomAddr=UartFrame[2];//获取当前Rom地址
 198   1      //      while(SMB0CF & 0x20 != 0);//查询SMBus总线是否BUSY
 199   1      //      STA = 1;        //发起一个I2C START
 200   1      
 201   1              while(1)
 202   1              {
 203   2                      if(UartFrame_Complete==1)
 204   2                      {
 205   3      
 206   3              /*              for(i=0;i<UartFrame_Num;i++)
 207   3                              {
 208   3                                      
 209   3                                      SBUF0=UartFrame[i];
 210   3                                      while(UART_Tx_Complete == 0);
 211   3                                      UART_Tx_Complete = 0;                   
 212   3                              }*/
 213   3                              SBUF0='O';
 214   3                              while(UART_Tx_Complete == 0);
 215   3                              UART_Tx_Complete = 0;
 216   3                              SBUF0='K';
 217   3                              while(UART_Tx_Complete == 0);
 218   3                              UART_Tx_Complete = 0;
 219   3      
 220   3                              UartFrame_Complete=0;
 221   3                      //      UartFrame_Num=0;
 222   3                              num=0;
 223   3                              //先写
 224   3                              RWByte=0xA0;//write
 225   3                              pRomAddr=UartFrame[2];//获取当前Rom地址
 226   3                              while(SMB0CF & 0x20 != 0);//查询SMBus总线是否BUSY
 227   3                              STA = 1;        //发起一个I2C START
 228   3                              IIC_Busy=1;
 229   3                              while(IIC_Busy==1);
 230   3                              if(UartFrame[1]==0xA1)//写后的再读
 231   3                              {
 232   4                                      RWByte=0xA1;//read
 233   4                                      I2CRxNum=UartFrame[3]+1;        //需要读的长度,0表示读1个
 234   4                                      pRomAddr=UartFrame[2];//获取当前Rom地址
 235   4                                      //pRomAddrEnd=pRomAddr+UartFrame[2]-1;
 236   4                                      while(SMB0CF & 0x20 != 0);//查询SMBus总线是否BUSY
 237   4                                      STA = 1;        //发起一个I2C START

⌨️ 快捷键说明

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