<meter id="pryje"><nav id="pryje"><delect id="pryje"></delect></nav></meter>
          <label id="pryje"></label>

          新聞中心

          EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計應(yīng)用 > AT89S52單片機模擬I2C總線協(xié)議讀寫AT24C04

          AT89S52單片機模擬I2C總線協(xié)議讀寫AT24C04

          作者: 時間:2016-11-18 來源:網(wǎng)絡(luò) 收藏
          I2C總線是2條線總線.數(shù)據(jù)線SDA,時鐘線SCL.結(jié)構(gòu)簡單.

          AT24C04是具有I2C總線接口的EEPROM.大小為512*8bit.單片機AT89S52本身不具有I2C總線結(jié)口,所以可編寫程序用并行端口模擬I2C總線協(xié)議讀寫AT24C04.

          本文引用地址:http://www.ex-cimer.com/article/201611/315772.htm

          多個設(shè)備通信的重點(1.電平的區(qū)別,如串口通信中PC與單片機通信,PC機串口電平值為+12V~-12V,單片機為TTL電平0V~+5V.,所以要用電平轉(zhuǎn)換芯片轉(zhuǎn)電平.2,通信協(xié)議.(串口通信協(xié)議))

          具體的協(xié)議內(nèi)容與數(shù)據(jù)格式可查資料.

          代碼如下:

          #include

          #define WriteDeviceAddress 0xa0
          #define ReadDeviceAddress 0xa1

          sbit SCL = P3^4;
          sbit SDA = P3^5;
          sbit DOG = P0^0;
          sbit PP = P0^1;
          sbit DOG1 = P0^7;

          void DelayMs(unsigned int number)
          {
          unsigned char tmp;
          for(;number!=0;number--,DOG1=!DOG1)
          {
          for(tmp=112;tmp!=0;tmp--)
          {
          }
          }
          }

          void Start()
          {
          SDA = 1;
          DelayMs(1);
          SCL = 1;
          DelayMs(1);
          SDA = 0;
          DelayMs(1);
          SCL = 0;
          DelayMs(1);
          }

          bit Write8bit(unsigned char input)
          {
          unsigned char tmp;
          for(tmp =8;tmp!=0;tmp--)
          {
          SDA = (bit)(input&0x80);
          DelayMs(1);
          SCL = 1;
          DelayMs(1);
          SCL = 0;
          DelayMs(1);
          input = input << 1;
          }
          return 1;
          }

          bit TestAck()
          {
          bit ErrorBit;
          SDA = 1;
          DelayMs(1);
          SCL = 1;
          DelayMs(1);
          ErrorBit = SDA;
          DelayMs(1);
          SCL = 0;
          DelayMs(1);
          return(ErrorBit);
          }

          void Stop()
          {
          SCL = 0;
          DelayMs(1);
          SDA = 0;
          DelayMs(1);
          SCL = 1;
          DelayMs(1);
          SDA = 1;
          DelayMs(1);
          }

          void WriteI2C(unsigned char *Wdata, unsigned char RomAddress, unsigned char number)
          {
          Start();
          Write8bit(WriteDeviceAddress);
          TestAck();
          Write8bit(RomAddress);
          TestAck();
          for(;number!=0;number--)
          {
          Write8bit(*Wdata);
          TestAck();
          Wdata++;
          }
          Stop();
          DelayMs(1);
          }

          unsigned char Read8Bit()
          {
          unsigned char tmp,rbyte = 0;
          for(tmp=8;tmp!=0;tmp--)
          {
          SCL = 1;
          DelayMs(1);
          rbyte = rbyte << 1;
          DelayMs(1);
          rbyte = rbyte|((unsigned char)(SDA));
          SCL = 0;
          DelayMs(1);
          }
          return(rbyte);
          }

          void Ack()
          {
          SDA = 0;
          DelayMs(1);
          SCL = 1;
          DelayMs(1);
          SCL = 0 ;
          DelayMs(1);
          SDA = 1;
          DelayMs(1);
          }

          void NoAck()
          {
          SDA = 1;
          DelayMs(1);
          SCL = 1;
          DelayMs(1);
          SCL = 0 ;
          DelayMs(1);
          }
          void ReadI2C(unsigned char* RamAddress,unsigned char RomAddress,unsigned char bytes)
          {
          Start();
          Write8bit(WriteDeviceAddress);
          TestAck();
          Write8bit(RomAddress);
          TestAck();
          Start();
          Write8bit(ReadDeviceAddress);
          TestAck();
          while(bytes != 1)
          {
          *RamAddress = Read8Bit();
          Ack();
          RamAddress++;
          bytes--;
          }
          *RamAddress = Read8Bit();
          NoAck();
          Stop();
          }

          void main()
          {
          unsigned char writeByte[8] = {0xC0,0X34,0X12,0X22,0X11,0X01,0X00,0X00};

          unsigned char readByte[8];

          unsigned char *addw;
          unsigned char *addr;

          unsigned char i;
          unsigned char ok = 0;

          bit write = 1;
          DOG = 1;


          while(1)
          {
          if(write == 1)
          {
          addw = writeByte;
          addr = readByte;
          WriteI2C(addw,0x00,8);
          ReadI2C(addr,0x00,8);
          for(i=0;i<8;i++)
          {
          if(writeByte[i] == readByte[i])
          {
          ok++;
          }
          }
          if(ok == 8)
          {
          DOG = 0; //一樣P0.0亮

          }
          else
          {
          PP = 0; //不一樣P0.1亮
          }
          write = 0;
          }
          }
          }



          評論


          技術(shù)專區(qū)

          關(guān)閉
          看屁屁www成人影院,亚洲人妻成人图片,亚洲精品成人午夜在线,日韩在线 欧美成人 (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })();