@@ -6511,6 +6511,198 @@ if __name__ == '__main__':
65116511
65126512```
65136513
6514+ # #### I2C_simulation
6515+
6516+ 类功能:用于gpio模拟标准i2c协议。
6517+
6518+ 除了创建对象外,其它的操作(读写)均与I2C 一致
6519+
6520+ # #####
6521+
6522+ # ##### 创建I2C_simulation对象
6523+
6524+ > ** from machine import I2C_simulation **
6525+ >
6526+ > ** i2c_obj = I2C_simulation(GPIO_clk , GPIO_sda , CLK )**
6527+
6528+ * 参数说明
6529+
6530+ | 参数 | 类型 | 说明 |
6531+ | -------- | ---- | ---------------------------------------------------- - |
6532+ | GPIO_clk | int | i2c的CLK 引脚(需要控制的GPIO 引脚号,参照Pin模块的定义) |
6533+ | GPIO_sda | int | i2c的SDA 引脚(需要控制的GPIO 引脚号,参照Pin模块的定义) |
6534+ | CLK | int | i2c的频率 (0 ,1000000Hz ] |
6535+
6536+ - 示例
6537+
6538+ ```python
6539+ from machine import I2C_simulation
6540+
6541+ i2c_obj = I2C_simulation(I2C_simulation .GPIO10 , I2C_simulation .GPIO11 , 300 ) # 返回i2c对象
6542+ ```
6543+
6544+
6545+
6546+ # ##### 读取数据
6547+
6548+ > ** I2C_simulation .read(slaveaddress, addr,addr_len, r_data, datalen, delay)**
6549+
6550+ 从 I2C 总线中读取数据。
6551+
6552+ ** 参数说明**
6553+
6554+ | 参数 | 类型 | 说明 |
6555+ | ------------ | -------- - | -------------------------------- |
6556+ | slaveaddress | int | i2c 设备地址 |
6557+ | addr | bytearray | i2c 寄存器地址 |
6558+ | addr_len | int | 寄存器地址长度 |
6559+ | r_data | bytearray | 接收数据的字节数组 |
6560+ | datalen | int | 字节数组的长度 |
6561+ | delay | int | 延时,数据转换缓冲时间(单位ms) |
6562+
6563+ * 返回值
6564+
6565+ 成功返回整型值0 ,失败返回整型值- 1 。
6566+
6567+
6568+
6569+ # ##### 写入数据
6570+
6571+ > ** I2C_simulation .write(slaveaddress, addr, addr_len, data, datalen)**
6572+
6573+ 从 I2C 总线中写入数据。
6574+
6575+ * 参数说明
6576+
6577+ | 参数 | 类型 | 说明 |
6578+ | ------------ | -------- - | -------------- |
6579+ | slaveaddress | int | i2c 设备地址 |
6580+ | addr | bytearray | i2c 寄存器地址 |
6581+ | addr_len | int | 寄存器地址长度 |
6582+ | data | bytearray | 写入的数据 |
6583+ | datalen | int | 写入数据的长度 |
6584+
6585+ * 返回值
6586+
6587+ 成功返回整型值0 ,失败返回整型值- 1 。
6588+
6589+
6590+
6591+ # ##### 使用示例
6592+
6593+ 该示例是驱动AHT10 获取温湿度。
6594+
6595+ ```python
6596+ import log
6597+ # from machine import I2C
6598+ from machine import I2C_simulation
6599+ import utime as time
6600+ """
6601+ 1. calibration
6602+ 2. Trigger measurement
6603+ 3. read data
6604+ """
6605+
6606+ # API 手册 http://qpy.quectel.com/wiki/#/zh-cn/api/?id=i2c
6607+ # AHT10 说明书
6608+ # https://server4.eca.ir/eshop/AHT10/Aosong_AHT10_en_draft_0c.pdf
6609+
6610+
6611+ class aht10class():
6612+ i2c_log = None
6613+ i2c_dev = None
6614+ i2c_addre = None
6615+
6616+ # Initialization command
6617+ AHT10_CALIBRATION_CMD = 0x E1
6618+ # Trigger measurement
6619+ AHT10_START_MEASURMENT_CMD = 0x AC
6620+ # reset
6621+ AHT10_RESET_CMD = 0x BA
6622+
6623+ def write_data(self , data):
6624+ self .i2c_dev.write(self .i2c_addre,
6625+ bytearray (0x 00 ), 0 ,
6626+ bytearray (data), len (data))
6627+ pass
6628+
6629+ def read_data(self , length):
6630+ print (" read_data start" )
6631+ r_data = [0x 00 for i in range (length)]
6632+ r_data = bytearray (r_data)
6633+ print (" read_data start1" )
6634+ ret = self .i2c_dev.read(self .i2c_addre,
6635+ bytearray (0x 00 ), 0 ,
6636+ r_data, length,
6637+ 0 )
6638+ print (" read_data start2" )
6639+ print (' ret' ,ret)
6640+ print (' r_data:' ,r_data)
6641+ return list (r_data)
6642+
6643+ def aht10_init(self , addre = 0x 38 , Alise = " Ath10" ):
6644+ self .i2c_log = log.getLogger(Alise)
6645+ self .i2c_dev = I2C_simulation(I2C_simulation .GPIO10 , I2C_simulation .GPIO11 , 300 )
6646+ self .i2c_addre = addre
6647+ self .sensor_init()
6648+ pass
6649+
6650+ def aht10_transformation_temperature(self , data):
6651+ r_data = data
6652+ # 根据数据手册的描述来转化温度
6653+ humidity = (r_data[0 ] << 12 ) | (
6654+ r_data[1 ] << 4 ) | ((r_data[2 ] & 0x F0 ) >> 4 )
6655+ humidity = (humidity/ (1 << 20 )) * 100.0
6656+ print (" current humidity is {0} %" .format(humidity))
6657+ temperature = ((r_data[2 ] & 0x f ) << 16 ) | (
6658+ r_data[3 ] << 8 ) | r_data[4 ]
6659+ temperature = (temperature * 200.0 / (1 << 20 )) - 50
6660+ print (" current temperature is {0} °C" .format(temperature))
6661+
6662+
6663+ def sensor_init(self ):
6664+ # calibration
6665+ self .write_data([self .AHT10_CALIBRATION_CMD , 0x 08 , 0x 00 ])
6666+ time.sleep_ms(300 ) # at last 300ms
6667+ pass
6668+
6669+
6670+ def ath10_reset(self ):
6671+ self .write_data([self .AHT10_RESET_CMD ])
6672+ time.sleep_ms(20 ) # at last 20ms
6673+
6674+ def Trigger_measurement(self ):
6675+ # Trigger data conversion
6676+ self .write_data([self .AHT10_START_MEASURMENT_CMD , 0x 33 , 0x 00 ])
6677+ time.sleep_ms(200 ) # at last delay 75ms
6678+ # check has success
6679+ r_data = self .read_data(6 )
6680+ # check bit7
6681+ if (r_data[0 ] >> 7 ) != 0x 0 :
6682+ print (" Conversion has error" )
6683+ else :
6684+ self .aht10_transformation_temperature(r_data[1 :6 ])
6685+
6686+ ath_dev = None
6687+
6688+ def i2c_aht10_test():
6689+ global ath_dev
6690+ ath_dev = aht10class()
6691+ ath_dev.aht10_init()
6692+
6693+ # 测试十次
6694+ for i in range (5 ):
6695+ ath_dev.Trigger_measurement()
6696+ time.sleep(1 )
6697+
6698+
6699+ if __name__ == " __main__" :
6700+ print (' start' )
6701+ i2c_aht10_test()
6702+
6703+
6704+ ```
6705+
65146706
65156707
65166708# #### SPI
0 commit comments