Skip to content

Class_I2C

Ko-ichiro Sugiyama edited this page May 15, 2022 · 7 revisions

I2C

I2C シリアルインターフェースを扱います。

コンストラクタ

I2C.new( scl, sda, port, freq )

  • scl: SCL PIN
  • sda: SDA PIN
  • port: port (default 0)
  • freq: frequency (default 40000)

i2c = I2C.new(22,21)

出力

I2C.write( i2c_adrs_7, [data1, data2,...] ), I2C.writeto( i2c_adrs_7, [data1, data2,...] )

  • i2c_adrs_7: I2C address
  • [data1, data2, ...]: data array ​ 指定されたバイトを順次出力します。

i2c.write( i2c_ADRS_7, [0x02, 0x16, 0x00] )

write( i2c_adrs_7, "string" )

指定された文字列を出力します。

i2c.write( ADRS, "string" )

入力

read( i2c_adrs_7, read_bytes, *params ) -> String

指定されたバイト数のデータを読み込みます。指定されたバイト数のデータが到着していない場合、ブロックします。
paramsを指定することで、read前にparamsを出力します。すなわち、以下のシーケンスとなります。

	(S) - ADRS7 (W)(A) - [params ...] - (Sr) - ADRS7 (R)(A) - data_1 (A)... data_n (A|N) - (P)
	S : Start condition	P : Stop condition
	Sr: Repeated start condition
	A : Ack	N : Nack

s = i2c.read( ADRS, 2, 0xfe )		# 0xfeレジスタから2バイト取得

I2C.readfrom( i2c_adrs_7, read_bytes ) -> integer array

  • i2c_adrs_7: I2C address
  • read_bytes: read_bytes (integer)

array = i2c.readfrom( ADRS, 2) #指定されたバイト数のデータを読み込む.
Clone this wiki locally