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

📄 8574.lst

📁 PCF8574 Remote 8-bit I/O expander for I2C-bus IO扩展芯片测试程序
💻 LST
字号:
C51 COMPILER V7.50   8574                                                                  12/22/2008 17:01:18 PAGE 1   


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

line level    source

   1          #include <reg52.h>
   2          #include <stdio.h>
   3          
   4          sbit SCL=P3^7;
   5          sbit SDA=P3^6;
   6          
   7          typedef unsigned char byte;
   8          
   9          byte in_patt(byte device);
  10          void out_patt(byte device, byte dirs, byte patt);
  11          void out_byte(byte o_byte);
  12          byte in_byte(void);
  13          byte nack(void);
  14          void start(void);
  15          void stop(void);
  16          
  17          void delay(int a)
  18          {
  19   1              for (;a>0;a--)
  20   1                      ;
  21   1      }
  22          
  23          void DelayMs(unsigned char t)
  24          {
  25   1              unsigned char i;
  26   1          if(t>0)
  27   1          {
  28   2                      for(i=0;i<=10;i++)
  29   2                              ;
  30   2                      t--;
  31   2              }
  32   1      }
  33          
  34          void main(void)
  35          {
  36   1              byte i_patt, device;
  37   1              byte dirs = 0x80; /* define most significant bit as an input */
  38   1      
  39   1              device = 0x00; /* determined by strapping of A2, A1, A0 */
  40   1              out_patt(device, dirs, 0xff);
  41   1              /* initialize all outputs to logic one */
  42   1              while(1)
  43   1              {
  44   2                      i_patt=in_patt(device); /* fetch from 8574 */
  45   2                      if ((i_patt && 0x80)==0) /* if switch at zero, flash LED */
  46   2                      {
  47   3                              out_patt(device, dirs, 0x00); /* LED on */
  48   3                              DelayMs(250);
  49   3                              out_patt(device, dirs, 0x01); /* LED off */
  50   3                              DelayMs(250);
  51   3                      }
  52   2                      else
  53   2                      {
  54   3                              out_patt(device, dirs, 0x01); /* turn LED off */
  55   3                      }
C51 COMPILER V7.50   8574                                                                  12/22/2008 17:01:18 PAGE 2   

  56   2              }
  57   1      }
  58          
  59          byte in_patt(byte device) 
  60          /* fetches byte from 8574 at specified addresss */
  61          {
  62   1              byte o_byte, i_patt;
  63   1              start();
  64   1              o_byte=0x40 | (device << 1) | 0x01;
  65   1              out_byte(o_byte);
  66   1              nack();
  67   1              i_patt=in_byte();
  68   1              stop();
  69   1              return(i_patt);
  70   1      }
  71          
  72          void out_patt(byte device, byte dirs, byte patt)
  73          /* outputs specified pattern on specified 8574. Note that
  74          ** inputs specified by "dirs" are maintained at a logic one */
  75          {
  76   1              byte o_byte;
  77   1              start();
  78   1              o_byte=0x40 | (device << 1);
  79   1              out_byte(o_byte);
  80   1              nack();
  81   1              o_byte = patt | dirs;
  82   1              out_byte(o_byte);
  83   1              nack();
  84   1              stop();
  85   1      }
  86          
  87          void out_byte(byte o_byte)
  88          /* shift out byte, beginning with most significant bit */
  89          {
  90   1              int n;
  91   1              for(n=7; n>=0; n--)
  92   1              { /* note SCL is low during transitions on SDA */
  93   2                      if (((o_byte >>n) & 0x01) == 0)
  94   2                      {
  95   3                              SDA=0;
  96   3                      }
  97   2                      else
  98   2                      {
  99   3                              SDA=1;
 100   3                      }
 101   2                      delay(4);
 102   2                      SCL=1;
 103   2                      delay(4);
 104   2                      SCL=0;
 105   2                      delay(4);
 106   2              }
 107   1      }
 108          
 109          byte in_byte(void)
 110          /* fetch byte, most significant byte first */
 111          {
 112   1              byte i_byte=0x00;
 113   1              int n;
 114   1      
 115   1              SDA=1;
 116   1              delay(4);
 117   1              for (n=0; n<8; n++)
C51 COMPILER V7.50   8574                                                                  12/22/2008 17:01:18 PAGE 3   

 118   1              {
 119   2                      SCL=1;
 120   2                      delay(4);
 121   2                      i_byte=(i_byte << 1) | SDA;
 122   2                      delay(4);
 123   2                      SCL=0;
 124   2              }
 125   1              return(i_byte);
 126   1      }
 127          
 128          byte nack(void)
 129          {
 130   1              byte ack_bit;
 131   1              SDA=1;
 132   1              delay(4);
 133   1              SCL=1;
 134   1              delay(4);
 135   1              if (SDA==0)
 136   1              {
 137   2                      ack_bit=0;
 138   2              }
 139   1              else
 140   1              {
 141   2                      ack_bit=1;
 142   2              }
 143   1              SCL=0;
 144   1              delay(4);
 145   1              return(ack_bit);
 146   1      }
 147          
 148          void start(void)
 149          /* bring SDA high to low while SCL is high */
 150          {
 151   1              SDA=1;
 152   1              delay(4);
 153   1              SCL=1;
 154   1              delay(4);
 155   1              SDA=0;
 156   1              delay(4);
 157   1              SCL=0;
 158   1              delay(4);
 159   1      }
 160          
 161          void stop(void)
 162          /* bring SDA low to high while SCL is high */
 163          {
 164   1              SDA=0;
 165   1              delay(4);
 166   1              SCL=1;
 167   1              delay(4);
 168   1              SDA=1;
 169   1              delay(4);
 170   1              SCL=0;
 171   1              delay(4);
 172   1      }
 173          
 174          
 175          


MODULE INFORMATION:   STATIC OVERLAYABLE
   CODE SIZE        =    321    ----
C51 COMPILER V7.50   8574                                                                  12/22/2008 17:01:18 PAGE 4   

   CONSTANT SIZE    =   ----    ----
   XDATA SIZE       =   ----    ----
   PDATA SIZE       =   ----    ----
   DATA SIZE        =   ----       7
   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 + -