首先讲一下24c01的i2c接口,
I2C是2线的.
I2C的原则是:
在SCL=1(高电平)时,SDA不能动
否则,SDA下跳则判为"起始信号",SDA上跳则判为"停止信号".
在SCL=0(低电平)时,SDA随便动
每个字节后应该由对方回送一个应答信号ACK做为对方在线的标志.
非应答信号一般在所有字节的最后一个字节后.一般要由双方协议签定.
SCL必须由主机发送,
首字节是"片选信号",即7位从机地址加1位读写控制.
从机收到自己的地址必须发送应答信号表示自己在线.
读写是站在主机的立场上定义的.
"读"是主机接收从机数据,"写"是主机发送数据给从机.
7位I2C总线可以挂接127个不同地址的I2C设备,0号"设备"作为群呼地址.
下面介绍如何用lssp控制24c01。
此芯片具有16个gpio口,每个口都可以进行配置成输入或输出。
现在我将0号io口接到sda,1号接到scl。
地址3FEF是io使能寄存器地址,3FF0是gpio输出寄存器地址。
程序实现的是先将1写入1010000设备的0地址,在把此数据读出。结果在r4中。
汇编程序如下:
MOV R0,#0X0
MOV R1,#0X3FEF ;GPIO[7:0]使能寄存器地址放入r1
MOVO @R1,#0X3 ;设置GPIO[1:0]输出enable
MOV R2,#0X3FF0 ;GPIO[7:0]数据寄存器地址放入r2
MOVO @R2,#0X3 ;设置GPIO[1:0]输出high电平
;9 cycle reset sda must be high
MOVO @R2,#0X1 ;falling edge
MOVO @R2,#0X3 ;rising edge
MOVO @R2,#0X1 ;falling edge
MOVO @R2,#0X3 ;rising edge
MOVO @R2,#0X1 ;falling edge
MOVO @R2,#0X3 ;rising edge
MOVO @R2,#0X1 ;falling edge
MOVO @R2,#0X3 ;rising edge
MOVO @R2,#0X1 ;falling edge
MOVO @R2,#0X3 ;rising edge
MOVO @R2,#0X1 ;falling edge
MOVO @R2,#0X3 ;rising edge
MOVO @R2,#0X1 ;falling edge
MOVO @R2,#0X3 ;rising edge
MOVO @R2,#0X1 ;falling edge
MOVO @R2,#0X3 ;rising edge
MOVO @R2,#0X1 ;falling edge
MOVO @R2,#0X3 ;rising edge
;WRITE
MOVO @R2,#0X2 ;start
MOVO @R2,#0X0 ;falling edge
;device address is 1010000
MOVO @R2,#0X1 ;data 1
MOVO @R2,#0X3 ;rising edge
MOVO @R2,#0X1 ;falling edge
MOVO @R2,#0X0 ;data 0
MOVO @R2,#0X2 ;rising edge
MOVO @R2,#0X0 ;falling edge
MOVO @R2,#0X1 ;data 1
MOVO @R2,#0X3 ;rising edge
MOVO @R2,#0X1 ;falling edge
MOVO @R2,#0X0 ;data 0
MOVO @R2,#0X2 ;rising edge
MOVO @R2,#0X0 ;falling edge
MOVO @R2,#0X0 ;data 0
MOVO @R2,#0X2 ;rising edge
MOVO @R2,#0X0 ;falling edge
MOVO @R2,#0X0 ;data 0
MOVO @R2,#0X2 ;rising edge
MOVO @R2,#0X0 ;falling edge
MOVO @R2,#0X0 ;data 0
MOVO @R2,#0X2 ;rising edge
MOVO @R2,#0X0 ;falling edge
MOVO @R2,#0X0 ;write
MOVO @R2,#0X2 ;rising edge
MOVO @R2,#0X0 ;falling edge
MOVO @R1,#0X2 ;input
MOVI R4,@R2
AND R4,R4,#0X1
ACK1: JCNE R4,R0,ACK1 ;if r4 not equal r0, loop back and wait, until sda is low.
MOVO @R2,#0X2 ;rising edge
MOVO @R2,#0X0 ;falling edge
MOVO @R1,#0X3 ;output
MOVO @R2,#0X2 ;rising edge
MOVO @R2,#0X0 ;falling edge
MOVO @R2,#0X2 ;rising edge
MOVO @R2,#0X0 ;falling edge
MOVO @R2,#0X2 ;rising edge
MOVO @R2,#0X0 ;falling edge
MOVO @R2,#0X2 ;rising edge
MOVO @R2,#0X0 ;falling edge
MOVO @R2,#0X2 ;rising edge
MOVO @R2,#0X0 ;falling edge
MOVO @R2,#0X2 ;rising edge
MOVO @R2,#0X0 ;falling edge
MOVO @R2,#0X2 ;rising edge
MOVO @R2,#0X0 ;falling edge
MOVO @R2,#0X2 ;rising edge
MOVO @R2,#0X0 ;falling edge
MOVO @R1,#0X2 ;input
MOVI R4,@R2
AND R4,R4,#0X1
ACK2: JCNE R4,R0,ACK2
MOVO @R2,#0X2 ;rising edge
MOVO @R2,#0X0 ;falling edge
MOVO @R1,#0X3 ;output
MOVO @R2,#0X2 ;rising edge
MOVO @R2,#0X0 ;falling edge
MOVO @R2,#0X2 ;rising edge
MOVO @R2,#0X0 ;falling edge
MOVO @R2,#0X2 ;rising edge
MOVO @R2,#0X0 ;falling edge
MOVO @R2,#0X2 ;rising edge
MOVO @R2,#0X0 ;falling edge
MOVO @R2,#0X2 ;rising edge
MOVO @R2,#0X0 ;falling edge
MOVO @R2,#0X2 ;rising edge
MOVO @R2,#0X0 ;falling edge
MOVO @R2,#0X2 ;rising edge
MOVO @R2,#0X0 ;falling edge
MOVO @R2,#0X1 ;data 1
MOVO @R2,#0X3 ;rising edge
MOVO @R2,#0X1 ;falling edge
MOVO @R1,#0X2 ;input
MOVI R4,@R2
AND R4,R4,#0X1
ACK3: JCNE R4,R0,ACK3
MOVO @R2,#0X2 ;rising edge
MOVO @R2,#0X0 ;falling edge
MOVO @R1,#0X3 ;output
MOVO @R2,#0X0 ;
MOVO @R2,#0X2 ;rising edge
MOVO @R2,#0X3 ;STOP
MOVO @R2,#0X2 ;rising edge
MOVO @R2,#0X0 ;falling edge
;READ
MOVO @R2,#0X1 ;data 1
MOVO @R2,#0X3 ;rising edge
MOVO @R2,#0X2 ;start
MOVO @R2,#0X0 ;falling edge
MOVO @R2,#0X1 ;data 1
MOVO @R2,#0X3 ;rising edge
MOVO @R2,#0X1 ;falling edge
MOVO @R2,#0X0 ;data 0
MOVO @R2,#0X2 ;rising edge
MOVO @R2,#0X0 ;falling edge
MOVO @R2,#0X1 ;data 1
MOVO @R2,#0X3 ;rising edge
MOVO @R2,#0X1 ;falling edge
MOVO @R2,#0X0 ;data 0
MOVO @R2,#0X2 ;rising edge
MOVO @R2,#0X0 ;falling edge
MOVO @R2,#0X0 ;data 0
MOVO @R2,#0X2 ;rising edge
MOVO @R2,#0X0 ;falling edge
MOVO @R2,#0X0 ;data 0
MOVO @R2,#0X2 ;rising edge
MOVO @R2,#0X0 ;falling edge
MOVO @R2,#0X0 ;data 0
MOVO @R2,#0X2 ;rising edge
MOVO @R2,#0X0 ;falling edge
MOVO @R2,#0X0 ;write
MOVO @R2,#0X2 ;rising edge
MOVO @R2,#0X0 ;falling edge
MOVO @R1,#0X2 ;input
MOVI R4,@R2
AND R4,R4,#0X1
ACK4: JCNE R4,R0,ACK4
MOVO @R2,#0X2 ;rising edge
MOVO @R2,#0X0 ;falling edge
MOVO @R1,#0X3 ;output
MOVO @R2,#0X2 ;rising edge
MOVO @R2,#0X0 ;falling edge
MOVO @R2,#0X2 ;rising edge
MOVO @R2,#0X0 ;falling edge
MOVO @R2,#0X2 ;rising edge
MOVO @R2,#0X0 ;falling edge
MOVO @R2,#0X2 ;rising edge
MOVO @R2,#0X0 ;falling edge
MOVO @R2,#0X2 ;rising edge
MOVO @R2,#0X0 ;falling edge
MOVO @R2,#0X2 ;rising edge
MOVO @R2,#0X0 ;falling edge
MOVO @R2,#0X2 ;rising edge
MOVO @R2,#0X0 ;falling edge
MOVO @R2,#0X2 ;rising edge
MOVO @R2,#0X0 ;falling edge
MOVO @R1,#0X2 ;input
MOVI R4,@R2
AND R4,R4,#0X1
ACK5: JCNE R4,R0,ACK5
MOVO @R2,#0X2 ;rising edge
MOVO @R2,#0X0 ;falling edge
MOVO @R1,#0X3 ;output
MOVO @R2,#0X1 ;data 1
MOVO @R2,#0X3 ;rising edge
MOVO @R2,#0X2 ;start
MOVO @R2,#0X0 ;falling edge
MOVO @R2,#0X1 ;data 1
MOVO @R2,#0X3 ;rising edge
MOVO @R2,#0X1 ;falling edge
MOVO @R2,#0X0 ;data 0
MOVO @R2,#0X2 ;rising edge
MOVO @R2,#0X0 ;falling edge
MOVO @R2,#0X1 ;data 1
MOVO @R2,#0X3 ;rising edge
MOVO @R2,#0X1 ;falling edge
MOVO @R2,#0X0 ;data 0
MOVO @R2,#0X2 ;rising edge
MOVO @R2,#0X0 ;falling edge
MOVO @R2,#0X0 ;data 0
MOVO @R2,#0X2 ;rising edge
MOVO @R2,#0X0 ;falling edge
MOVO @R2,#0X0 ;data 0
MOVO @R2,#0X2 ;rising edge
MOVO @R2,#0X0 ;falling edge
MOVO @R2,#0X0 ;data 0
MOVO @R2,#0X2 ;rising edge
MOVO @R2,#0X0 ;falling edge
MOVO @R2,#0X1 ;READ
MOVO @R2,#0X3 ;rising edge
MOVO @R2,#0X2 ;falling edge
MOVO @R1,#0X2 ;input
MOVI R4,@R2
AND R4,R4,#0X1
ACK6: JCNE R4,R0,ACK6
MOVO @R2,#0X2 ;rising edge
MOVO @R2,#0X0 ;falling edge
MOVO @R2,#0X2 ;rising edge
MOVO @R2,#0X0 ;falling edge
MOVO @R2,#0X2 ;rising edge
MOVO @R2,#0X0 ;falling edge
MOVO @R2,#0X2 ;rising edge
MOVO @R2,#0X0 ;falling edge
MOVO @R2,#0X2 ;rising edge
MOVO @R2,#0X0 ;falling edge
MOVO @R2,#0X2 ;rising edge
MOVO @R2,#0X0 ;falling edge
MOVO @R2,#0X2 ;rising edge
MOVO @R2,#0X0 ;falling edge
MOVO @R2,#0X2 ;rising edge
MOVO @R2,#0X0 ;falling edge
MOVO @R2,#0X2 ;rising edge
MOVI R4,@R2 ;READ DATA
MOVO @R2,#0X0 ;falling edge
MOVO @R1,#0X3 ;output
MOVO @R2,#0X1 ;NO ACK
MOVO @R2,#0X3 ;rising edge
MOVO @R2,#0X1 ;falling edge
MOVO @R2,#0X0
MOVO @R2,#0X2 ;rising edge
MOVO @R2,#0X3 ;STOP
MOVO @R2,#0X1 ;falling edge
ENDS