I2C from PBP3 to Swordfish (no EEprom)

Coding and general discussion relating to the compiler

Moderators: David Barker, Jerry Messina

Post Reply
maisoft
Posts: 8
Joined: Mon Apr 17, 2017 6:16 pm

I2C from PBP3 to Swordfish (no EEprom)

Post by maisoft » Mon Apr 24, 2017 11:59 am

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

Jerry Messina
Swordfish Developer
Posts: 1469
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Re: I2C from PBP3 to Swordfish (no EEprom)

Post by Jerry Messina » Mon Apr 24, 2017 2:49 pm

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

maisoft
Posts: 8
Joined: Mon Apr 17, 2017 6:16 pm

Re: I2C from PBP3 to Swordfish (no EEprom)

Post by maisoft » Mon Apr 24, 2017 9:21 pm

:D Thank you very much

Post Reply