af. Fully parameterized software I2C bus support (PIC18F26J50 firmware v2.6.10 or later)

Sometimes, it is impossible to use hardware support for I2C bus due to unavailability of the required PIC18F26J50 pins. But I2C protocol can also be implemented on any four of free microcontroller pins. PIC18F26J50 firmware v2.6.10 or later supports parameterized I2C support on and two pins of any microcontroller port. The configuration is stored in PIC18F26J50 EEPROM and may be altered with PC USB Projects HEX Editor v3.0 or later. One also needs an appropriate FWI (firmware information) file: PIC18F26J50 v4.fwi or later. See USB Projects HEX Editor user guide for more information.

The I2C settings are stored in the following EEPROM table:

The following new functions are added to support I2C operation:

Function SWI2C_Init() as Integer

Function SWI2C_WriteReg(adr as Byte, reg as Byte, dta as Byte) as Integer

Function SWI2C_ReadReg(adr as Byte, reg as Byte) as Integer

Function SWI2C_WriteRegWord(adr as Byte, reg as Byte, dta as Uint16) as Integer

Function SWI2C_ReadRegWord(adr as Byte, reg as Byte) as __Int32

Init function initializes all the register values og the desired port for I2C operation. High logical state is achieved by setting the SDA output to tristate and letting pull-up resistors to set high values, while low logical state is represented by voltage close to 0 V by setting SDA output to logical value 0.

Though the external devices my acknowledge any data without the need for the program to care about the right timing.

There are two kinds of register write and read functions: SWI2C_WriteReg writes 1 byte of data to the external device, while SWI2C_WriteRegWord writes two bytes to two consecutive device registers. The other pair of functions: SWI2C_ReadReg and SWI2C_ReadRegWord reads one and two consecutive bytes from an external I2C device.

Full example is available in Weather Station v2.1 x64.zip file from Downloads section.

EXAMPLE 1: Reading air pressure from HP03MA sensor

Function ReadAP_D1() As Int32 ' Air Pressure

Dim w As Int32

If PIC.SWI2C_WriteReg(&H77, &HFF, &HF0) = 1 Then

Delay(40) ' Wait for 40 ms

w = PIC.SWI2C_ReadRegWord(&H77, &HFD)

If w < 0 Then

Return -1

Exit Function

End If

Else

Return -1

End If

Return w

End Function

I2C also depends on common register values presets that are stored in the following table: