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

          新聞中心

          EEPW首頁 > 嵌入式系統(tǒng) > 設計應用 > 單片機模擬I2C總線控制EEPROM讀寫程序

          單片機模擬I2C總線控制EEPROM讀寫程序

          作者: 時間:2016-11-11 來源:網(wǎng)絡 收藏
          之前寫的EEPROM程序雖然能夠軟仿成功,但燒到單片機里的時候卻不能用,無疑是時序的問題,今天修正了時序,總算硬仿成功了。對照上次的程序可以發(fā)現(xiàn)就是添加了頭函數(shù):,這樣就可以通過“_nop_()”指令較為準確的控制時序。

          上次那個問題依然沒有解決:就是接收緩沖區(qū)的數(shù)據(jù)是從readbuf[6]開始的,以這個程序為例:

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

          readbuf[6]中存放0x96

          readbuf[7]中存放0x84

          readbuf[8]中存放0xd5

          readbuf[9]中存放0x63

          readbuf[10]中存放0x7c

          readbuf[11]中存放0x8c

          其實我是想把收到的數(shù)據(jù)存放在readbuf[0]~[5]中的,我也不知道為什么結果會這樣,有知道的麻煩指教下。

          不多說了,上程序:

          #include
          #include
          #define unit unsigned int
          #define uchar unsigned char

          uchar num=6;
          uchar idata sendbuf[6]={0x96,0x84,0xd5,0x63,0x7c,0x8c};
          uchar idata readbuf[6];

          sbit scl=P2^0;
          sbit sda=P2^1;

          start(void) //start
          {
          sda=1;
          _nop_();
          _nop_();
          scl=1;
          _nop_();
          _nop_();
          _nop_();
          _nop_();
          _nop_();
          sda=0;
          _nop_();
          _nop_();
          _nop_();
          _nop_();
          _nop_();
          scl=0;
          }

          stop(void) //stop
          {
          sda=0;
          _nop_();
          _nop_();
          scl=1;
          _nop_();
          _nop_();
          _nop_();
          _nop_();
          _nop_();
          sda=1;
          _nop_();
          _nop_();
          _nop_();
          _nop_();
          _nop_();
          scl=0;
          }

          answer(void) //answer
          {
          sda=0;
          _nop_();
          _nop_();
          _nop_();
          scl=1;
          _nop_();
          _nop_();
          _nop_();
          _nop_();
          _nop_();
          scl=0;
          }

          noanswer(void)//no answer
          {
          sda=1;
          _nop_();
          _nop_();
          _nop_();
          scl=1;
          _nop_();
          _nop_();
          _nop_();
          _nop_();
          _nop_();
          scl=0;
          }

          checkanswer(void) //check answer
          {
          F0=0;
          sda=1;
          _nop_();
          _nop_();
          scl=1;
          _nop_();
          _nop_();
          _nop_();
          _nop_();
          _nop_();
          if(sda==1) F0=1;
          scl=0;
          }

          sendabyte(uchar idata *saddress) //send a byte
          {
          uchar n=8,temp=*saddress;
          while(n--)
          {
          if((temp&0x80)==0x80) sda=1;
          else sda=0;
          _nop_();
          scl=1;
          _nop_();
          _nop_();
          _nop_();
          _nop_();
          _nop_();
          scl=0;
          temp=temp<<1;
          }
          checkanswer();
          if(F0==1) return;
          }

          sendnbyte(uchar n) //send n byte
          {
          uchar idata *ps;
          ps=&sendbuf[0];

          while(n--)
          {
          sendabyte(ps);
          ps++;
          }
          stop();
          }

          readabyte(uchar idata *raddress) //read a byte
          {
          uchar n=8,temp=0;
          while(n--)
          {
          _nop_();
          _nop_();
          scl=1;
          _nop_();
          _nop_();
          temp=temp<<1;
          if(sda==1)
          temp=temp|0x01;
          else
          temp=temp&0xfe;
          _nop_();
          _nop_();
          _nop_();
          scl=0;
          _nop_();
          _nop_();
          _nop_();
          }
          *raddress=temp;
          }

          readnbyte(uchar n) //read n byte
          {
          uchar idata *pr;
          pr=&readbuf[0];
          while(n--)
          {
          readabyte(pr);
          answer();
          pr++;
          }
          noanswer();
          stop();
          }

          main(void) //MAIN
          {
          start();

          sendabyte(0xa0);

          sendabyte(0x00);

          sendnbyte(num);

          /*-----------------------*/
          _nop_();
          _nop_();
          _nop_();
          _nop_();
          _nop_();

          start();

          sendabyte(0xa0);

          sendabyte(0x00);

          sendabyte(0xa1);

          readnbyte(num);

          P1=readbuf[11];

          while(1);
          }



          評論


          技術專區(qū)

          關閉
          看屁屁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); })();