Swordfish
dim RW as SSPSTAT.2
dim NAck as SSPCON2.6
dim Timer as TMR1L.AsWord
dim Value as byte
sub WaitForIdle()
repeat
until (SSPCON2 and $1F) = 0 and (RW = 0)
end sub
sub WaitForWrite(pControl as byte)
I2C.Start
I2C.WriteByte(pControl)
WaitForIdle
while NAck = 1
I2C.Restart
I2C.WriteByte(pControl)
WaitForIdle
wend
I2C.Stop
end sub
I2C.Initialize
Timer = 0
PIE1.0 = 1
T1CON.0 = 1
I2C.Start
I2C.WriteByte($A0)
I2C.WriteByte(0)
I2C.WriteByte(0)
I2C.WriteByte("S")
I2C.Stop
WaitForWrite($A0)
I2C.Start
I2C.WriteByte($A0)
I2C.WriteByte(0)
I2C.WriteByte(0)
I2C.Restart
I2C.WriteByte($A1)
Value = I2C.ReadByte
I2C.Acknowledge(I2C_NOT_ACKNOWLEDGE)
I2C.Stop
T1CON.0 = 0
end
CCS C
#use i2c(Master,sda=PIN_C4,scl=PIN_C3,FORCE_HW,slow=100000)
#byte SSPSTAT = 0x0FC7
#byte SSPCON2 = 0x0FC5
#bit RW = SSPSTAT.2
#bit NAck = SSPCON2.6
#byte PIE1 = 0x0F9D
#byte T1CON = 0x0FCD
#bit TMR1IE = PIE1.0
#bit TMR1ON = T1CON.0
void WaitForIdle() {
do {}
while ((SSPCON2 & 0x1F) != 0 && (RW != 0));
}
void WaitForWrite(int8 pControl) {
i2c_start();
i2c_write(pControl);
WaitForIdle();
while (NAck == 1) {
i2c_start();
i2c_write(pControl);
WaitForIdle();
}
i2c_stop();
}
void main() {
char Value;
set_timer1(0);
TMR1IE = 1;
TMR1ON = 1;
i2c_start();
i2c_write(0xA0);
i2c_write(0);
i2c_write(0);
i2c_write('C');
i2c_stop();
WaitForWrite(0xA0);
i2c_start();
i2c_write(0xA0);
i2c_write(0);
i2c_write(0);
i2c_start();
i2c_write(0xA1);
Value = i2c_read(0);
i2c_stop();
TMR1ON = 0;
}
MikroElectronika BASIC
dim TMR1 as word absolute 0x0FCE
dim Value as byte
sub procedure WaitForIdle
do
loop until (SSPCON2 and $1F) = 0 and (SSPSTAT.2 = 0)
end sub
sub procedure WaitForWrite(dim pControl as byte)
I2C_Start
I2C_Wr(pControl)
WaitForIdle
while SSPCON2.6 = 1
I2C_Repeated_Start
I2C_Wr(pControl)
WaitForIdle
wend
I2C_Stop
end sub
main:
I2C_Init(100000)
TMR1 = 0
SetBit(PIE1,0)
SetBit(T1CON,0)
I2C_Start
I2C_Wr($A0)
I2C_Wr(0)
I2C_Wr(0)
I2C_Wr("M")
I2C_Stop
WaitForWrite($A0)
I2C_Start
I2C_Wr($A0)
I2C_Wr(0)
I2C_Wr(0)
I2C_Repeated_Start
I2C_Wr($A1)
Value = I2C_Rd(0)
I2C_Stop
ClearBit(T1CON,0)
end.
PROTON BASIC
HBUS_BITRATE 100
Dim RW As SSPSTAT.2
Dim NAck As SSPCON2.6
Dim Value As Byte
Dim Control As Byte
Dim Timer As TMR1L.Word
Timer = 0
PIE1.0 = 1
T1CON.0 = 1
HBStart
HBusOut $A0
HBusOut 0
HBusOut 0
HBusOut "D"
HBStop
Control = $A0
GoSub WaitForWrite
HBStart
HBusOut $A0
HBusOut 0
HBusOut 0
HBREStart
HBusOut $A1
HBusIn Value
HBStop
T1CON.0 = 0
End
WaitForIdle:
Repeat
Until SSPCON2 & $1F = 0 And RW = 0
Return
WaitForWrite:
HBStart
HBusOut Control
GoSub WaitForIdle
While NAck = 1
HBREStart
HBusOut Control
GoSub WaitForIdle
Wend
HBStop
Return