Page 1 of 1

I2C from PBP3 to Swordfish (no EEprom)

Posted: Mon Apr 24, 2017 11:59 am
by maisoft
For many years (2001) I used PicBasicPro and until yesterday I used PBP3 which was perfect for my projects. Now I've been to Swordfish and I'm converting PBP3 programs into Swordfish
Swordfish is more powerful and more suitable for my new exegesis, in mariculture math is very interesting. Unfortunately I have encountered some obstacles to the use of I2C libraries for now.

Read data in to device from the Microchip ADC MCP3421, by I3C bus:
a) Old PBP3 instruction code that works perfectly
I2Cread sda, scl, $D5, [B3,B2,B1,B0]

b) New code education in Swordfish:
Dim B0, B1, B2, B3 As Byte
I2C.Start
I2C.WriteByte($D5)
B3 = I2C.ReadByte
I2C.Acknowledge(I2C_NOT_ACKNOWLEDGE)
B2 = I2C.ReadByte
I2C.Acknowledge(I2C_NOT_ACKNOWLEDGE)
B1 = I2C.ReadByte
I2C.Acknowledge(I2C_NOT_ACKNOWLEDGE)
B0 = I2C.ReadByte
I2C.Acknowledge(I2C_NOT_ACKNOWLEDGE)
I2C.Stop

It's correct?
Thanks
Umberto

Re: I2C from PBP3 to Swordfish (no EEprom)

Posted: Mon Apr 24, 2017 2:49 pm
by Jerry Messina
When you read you want the master to ACK every byte but the last, so it should look more like

Code: Select all

Dim B0, B1, B2, B3 As Byte
 I2C.Start
 I2C.WriteByte($D5)
 B3 = I2C.ReadByte
 I2C.Acknowledge(I2C_ACKNOWLEDGE)
 B2 = I2C.ReadByte
 I2C.Acknowledge(I2C_ACKNOWLEDGE)
 B1 = I2C.ReadByte
 I2C.Acknowledge(I2C_ACKNOWLEDGE)
 B0 = I2C.ReadByte
 I2C.Acknowledge(I2C_NOT_ACKNOWLEDGE)
 I2C.Stop
There's also a form of ReadByte that will take care of this for you

Code: Select all

Dim B0, B1, B2, B3 As Byte
 I2C.Start
 I2C.WriteByte($D5)
 B3 = I2C.ReadByte(I2C_ACKNOWLEDGE)
 B2 = I2C.ReadByte(I2C_ACKNOWLEDGE)
 B1 = I2C.ReadByte(I2C_ACKNOWLEDGE)
 B0 = I2C.ReadByte(I2C_NOT_ACKNOWLEDGE)
 I2C.Stop

Re: I2C from PBP3 to Swordfish (no EEprom)

Posted: Mon Apr 24, 2017 9:21 pm
by maisoft
:D Thank you very much