目录

1.2.3.4.4 I2C 对象接口说明

优质
小牛编辑
122浏览
2023-12-01

更新时间:2018-09-12 20:32:45

API

API说明
I2C.open(id)功能:打开 i2c 端口 参数: id:和板级配置文件中的 id 保持一致 返回值:ok=资源 handle,fail=-1;
I2C.close(handle)功能:关闭 i2c 端口 参数: handle:资源 handle,为I2C.open的返回值; 返回值:0=ok other=fail
I2C.write(handle,buff)功能:向某 i2c 设备写入数据 参数: handle:资源 handle,为 I2C.open 的返回值; buff:需要写入数据,类型是 array; 返回值:0=ok other=fail
I2C.read(handle,size)功能:读取寄存器值; 参数: handle:资源 handle,为 I2C.open 的返回值; size:需要读取的数据长度; 返回值:read返回值,类型是 array;

示例代码

var delay = function(val){
 var i = 0;
 var tmp = 0;
 for(i=0;i<val;i++){
   tmp = 0;
 }
};

var tmp102 = function(id) {
 this.handle = I2C.open(id);
 this.openFlag = 0;
 this.regRead = function(reg){
   var data = [0x00];
   data[0] = reg;
   I2C.write(this.handle,data);
   var val = I2C.read(this.handle,1);
   return val[0];

 };

 this.init = function(){
   var data = [0x00,0x00];
   data[0] = 0x70;
   data[1] = 0xa0;
   I2C.write(this.handle,data);
 };

 this.getTemperature = function(){

   if(0 == this.openFlag){
     this.init();
     this.openFlag = 1;
   }

   var data = [0x00];
   I2C.write(this.handle,data);
   var val = I2C.read(this.handle,2);
   var temp = ((val[0]<<8) | (val[1])) >> 4;
   temp = temp * 0.0625;
   return temp;
 };
};