Positron Compiler Documentation

HbStart

Positron16 · Revision 1.1.3.5 · PDF page 155

HbStart

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

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

' Interface to a 24LC32 serial 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

  Dim bLoop as Byte
  Dim Array[10] as Byte

  RPOR7 = 3                         ' Make PPS Pin RP14 U1TX
'
' Transmit bytes to the I2C bus
'
  HbStart                    ' Send a Start condition
  Hbusout %10100000        ' Target an EEPROM, and send a Write command
  Hbusout 0                  ' Send the HighByte of the address
  Hbusout 0                  ' Send the LowByte of the address
  For bLoop = 48 to 57      ' Create a loop containing ASCII 0 to 9
     Hbusout bLoop           ' Send the value of bLoop to the EEPROM
  Next                       ' Close the loop
  HbStop                    ' Send a Stop condition
  DelayMs 10               ' Wait for the data to be entered into EEPROM matrix
'
' Receive bytes from the I2C bus
'
  HbStart                    ' Send a Start condition
  Hbusout %10100000        ' Target an EEPROM, and send a Write command
  Hbusout 0                  ' Send the HighByte of the address
  Hbusout 0                  ' Send the LowByte of the address
  HbRestart                  ' Send a Restart condition
  Hbusout %10100001        ' Target an EEPROM, and send a Read command
  For bLoop = 0 to 9        ' Create a loop
     Array[bLoop] = Hbusin   ' Load an array with bytes received
     If bLoop = 9 Then HbStop : Else : HbusAck  ' Ack or Stop ?
  Next                         ' Close the loop
  HrsoutLn Str Array        ' Display the Array as a String

See also : HbusAck, HbRestart, HbStop, Hbusin, Hbusout, HbusAck2, HbRestart,2 HbStop2, Hbusin2, Hbusout2.

View original PDF page 155Open page in PDFOriginal PDF page 155