I2C - Shaofa/AliOS-Things-Certification-Manual GitHub Wiki
#I2C
功能:打开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;
|
参数名
|
类型/功能/值
|
说明
|
"id"
|
功能:资源唯一性标志
类型:string
值:任意,保持数组内id值唯一;
|
该id值和js层I2C.open时的id值保持一致;
|
"port"
|
功能:端口值;
类型:number
值:和板级资源描述保持一致;
|
该port值和HAL层API对应的port保持一致;
|
"address_width"
|
功能:设备I2C地址宽度;
类型:number
值:用户自定义;
|
该值一般是7;
|
"freq"
|
功能:I2C总线速率;
类型:number
值:用户自定义;
|
该值一般在100K-400K范围;
|
"mode"
|
功能:声明I2C的主从模式;
类型:number
值:1=master,2=slave;
|
|
"dev_addr"
|
功能:声明I2C的外设的I2C地址;
类型:number
值:根据芯片实际情况定义;
|
{
"I2C":[{
"id":"shtc1",
"port":2,
"address_width":7,
"freq":100000,
"mode":1,
"dev_addr":224
}],
}
无
/*apps/js/board_config.json*/
{
"I2C":[{
"id":"shtc1",
"port":2,
"address_width":7,
"freq":100000,
"mode":1,
"dev_addr":224
}],
}
print('start i2c test.....................');
setInterval(function() {
var shtc1Handle = I2C.open('shtc1');
var Reg=[0xEF,0xC8];
I2C.write(shtc1Handle,Reg);
var retArray=I2C.read(shtc1Handle,2);
console.log('ID:'+retArray[1]);
Reg[0]=0x80;
Reg[1]=0x5D;
I2C.write(shtc1Handle,Reg);
Reg[0]=0x7C;
Reg[1]=0xA2;
I2C.write(shtc1Handle,Reg);
var TempHumm = I2C.read(shtc1Handle,6);
console.log('read len:'+TempHumm.length);
var i =0;
for(i=0;i<TempHumm.length;i++){
console.log('rawData['+i+']:'+TempHumm[i]);
}
I2C.close(shtc1Handle);
}, 2000);
print('end i2c test........................');
每隔2s,程序会去读取SHTC1的温度和湿度的裸数据,用手触摸SHTC1传感器,其裸值将会改变。
�