Positron Compiler Documentation

HbStart2

Source: Positron16 Compiler User Manual, PDF page 163

Syntax

HbStart2

Overview

Send a Start condition to the I2C bus using the microcontroller's MSSP2 module.

Notes.

Because of the subtleties involved in interfacing to some I2C devices, the compiler's standard Hbusin2 and Hbusout2 commands were found lacking. Therefore, individual pieces of the I2C protocol may be used in association with the new structure of Hbusin2, and Hbusout2. See relevant sections for more information.

Example

' Interface to a 24LC32 I2C EEPROM
  Device = 24FJ64GA002
                                     ' Select the device to compile for
  Declare Xtal = 16
  Declare Hserial_Baud = 9600      ' USART1 Baud rate
  Declare Hrsout1_Pin = PORTB.14   ' Select the pin for TX with USART1
  Declare HSDA2_Pin = PORTB.2
                                     ' Select the pin for SDA of MSSP2
  Declare HSCL2_Pin = PORTB.3
                                     ' Select the pin for SCL of MSSP2
  Dim bLoop as Byte
  Dim Array[10] as Byte
  RPOR7 = 3
                                     ' Make PPS Pin RP14 for U1TX
'
' Transmit bytes to the I2C bus
'
  HbStart 2
                                ' Send a Start condition
  Hbusout2 %10100000
                                ' Target an EEPROM, and send a Write command
  Hbusout2 0
                                ' Send the HighByte of the address
  Hbusout2 0
                                ' Send the LowByte of the address
  For bLoop = 48 to 57
                                ' Create a loop containing ASCII 0 to 9
     Hbusout2 bLoop
                                ' Send the value of bLoop to the EEPROM
  Next2
                                ' Close the loop
  HbStop2
                                ' Send a Stop condition
  DelayMs 10
                                ' Wait for the data to be entered into EEPROM matrix
'
' Receive bytes from the I2C bus
'
  HbStart2
                                ' Send a Start condition
  Hbusout2 %10100000
                                ' Target an EEPROM, and send a Write command
  Hbusout2 0
                                ' Send the HighByte of the address
  Hbusout2 0
                                ' Send the LowByte of the address
  HbRestart2
                                ' Send a Restart condition
  Hbusout2 %10100001
                                ' Target an EEPROM, and send a Read command
  For bLoop = 0 to 9
                                ' Create a loop
     Array[bLoop] = Hbusin2
                                ' Load an array with bytes received
     If bLoop = 9 Then HbStop2 : Else : HbusAck2 ' Ack or Stop ?
   Next
                                ' Close the loop
   HrsoutLn Str Array
                                ' Display the Array as a String

See also : HbusAck2, HbRestart2, HbStop2, Hbusin2, Hbusout2, HbusAck, HbRestart,2

HbStop, Hbusin, Hbusout.