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

          新聞中心

          EEPW首頁 > 嵌入式系統(tǒng) > 設計應用 > PIC單片機CCS之C語言(#USE I2C)

          PIC單片機CCS之C語言(#USE I2C)

          作者: 時間:2016-11-17 來源:網絡 收藏
          #USE I2C

          語法:#use i2c(options)

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

          options被逗號隔開,可能是:

          MASTER //設置成主機方式

          SLAVE //設置成從機方式

          SCL=pin //指定SCL引腳(pin是一個位地址)

          SDA=pin //指定SDA引腳

          ADDRESS=nn //指定從機方式地址

          FAST //使用fast I2C規(guī)范

          SLOW //使用slow I2C規(guī)范

          RESTART_WDT //在I2C_READ等待的時候,重新啟動WDT

          FORCE_HW //使用硬件I2C函數

          NOFLOAT_HIGH //不允許信號漂浮至高,從低到高驅動信號

          SMBUS //總線不使用I2C,但很相似,即模擬I2C

          目的:I2C的零件庫包含了一個實現(xiàn)I2C總線的函數, #USE I2C使得I2C_START, I2C_STOP, I2C_READ, I2C_WRITE和I2C_POLL函數保持有效,直到下一個#USE I2C的出現(xiàn)為止.除非指定了FORCE_HW,否則會產生模擬I2C的軟件函數.SLAVE方式只能同內置的SSP一起被使用.

          例子:#use I2C(master, sda=PIN_B0, scl=PIN_B1)

          例子:#use i2c(master,sda=EEPROM_SDA, scl=EEPROM_SCL)

          // init_ext_eeprom(); Call before the other functions are used //
          // write_ext_eeprom(a, d); Write the byte d to the address a //
          // d = read_ext_eeprom(a); Read the byte d from the address a //

          #define EEPROM_ADDRESS long int
          #define EEPROM_SIZE 32768

          void init_ext_eeprom()
          {
          output_float(EEPROM_SCL);
          output_float(EEPROM_SDA);

          }
          void write_ext_eeprom(long int address, BYTE data)
          {
          short int status;
          i2c_start();
          i2c_write(0xa0);
          i2c_write(address>>8);
          i2c_write(address);
          i2c_write(data);
          i2c_stop();
          i2c_start();
          status=i2c_write(0xa0);
          while(status==1)
          {
          i2c_start();
          status=i2c_write(0xa0);
          }
          }
          BYTE read_ext_eeprom(long int address) {
          BYTE data;
          i2c_start();
          i2c_write(0xa0);
          i2c_write(address>>8);
          i2c_write(address);
          i2c_start();
          i2c_write(0xa1);
          data=i2c_read(0);
          i2c_stop();
          return(data);
          }
          void Long_write_ext_eeprom(long int address, Long data) {
          int A;
          for (A=0;A<4;A++)
          write_ext_eeprom(A + address, *(&data + A));
          }

          Long Long_read_ext_eeprom(long int address) {
          int A;
          Long Data;
          for (A=0;A<4;A++)
          *(&Data + A) = read_ext_eeprom( A + address);
          return(Data);
          }

          #use I2C(slave, sda=PIN_C4, scl=PIN_C3, address=0xa0, FORCE_HW)例子文件:ex_extee.c同2464.c一起使用. 在前面已介紹過了.



          關鍵詞: PIC單片機CCSUSEI2

          評論


          技術專區(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); })();