• PicŪ Basic


  • PIC18F or Amicus18 Hbusin/Hbusout BASIC code Replacement

    For those of you who are at a more advanced programming stage, presented here is an include file that will replace the compiler's HBusin, HBusout, HBStart, HBReStart, HBStop, HBusAck and HBusNak, hardware peripheral I2C, commands with their BASIC code equivalents.

    Why would you want this?
    Changing the nature of code is what programmers do best, and being able to alter the underlying code that the compiler normally created in assembler mnemonics, has many possibilities.

    The code below shows the use of the HBusin/HBusout commands, but uses the replacement code routines. As you can see, there are no differences between this code, and the normal use of these commands, except for the addition of the "Hbus.inc" include file at the top of the program.

    Code:
    '
    ' Demonstration for using the HBusin/HBusout code replacement routines
    '
      Include "Amicus18.inc"           ' Configure the compiler to use a PIC18F25K20 at 64MHz. i.e. an Amicus18 board
      Include "Hbus.inc"               ' Load the replacement code for the HBusin, HBusout etc commands.
    '
    ' Interface to a 24LC256 serial eeprom
    ' And display on the serial terminal the results
    '
      Dim bLoop As Byte               ' Loading/retrieving loop
      Dim sMyString As String * 10    ' String to hold response
    '
    ' Write bytes to the I2C bus
    '
      HRSOut "Writing\r"              ' Transmit our intention to the serial terminal
    
      HBStart                         ' Send a Start condition
      HBusOut $A0                     ' Target an eeprom with bus address 0, and send a Write command
      HBusOut 0                       ' Send the High Byte of the address
      HBusOut 0                       ' Send the Low Byte of the address
      For bLoop = "0" To "9"          ' Create a loop containing ASCII 0 to 9
        HRSOut bLoop                  ' Display the value on the serial terminal
        HBusOut bLoop                 ' Write the value of bLoop to the eeprom
      Next                            ' Close the loop
      HBStop                          ' Send a Stop condition
      DelayMS 5                       ' Wait for the data to be entered into eeprom matrix
    '
    ' Receive bytes from the I2C bus
    '
      Clear sMyString                 ' Clear the string before we start
      HRSOut "\r\rReading\r"          ' Transmit our intention to the serial terminal
    
      HBStart                         ' Send a Start condition
      HBusOut $A0                     ' Target an eeprom with bus address 0, and send a Write command 
      HBusOut 0                       ' Send the High Byte of the address
      HBusOut 0                       ' Send the Low Byte of the address
      HBReStart                       ' Send a Restart condition
      HBusOut $A1                     ' Target an eeprom with bus address 0, and send a Read command
      For bLoop = 0 To 9              ' Create a loop
        sMyString[bLoop] = HBusIn     ' Load the String with bytes received
        If bLoop = 9 Then             ' Are we at the last byte?
          HBStop                      ' Yes. So send a Stop condition
        Else                          ' Otherwise...
          HBusAck                     ' Send an Ack condition
        EndIf
      Next                            ' Close the loop
      HRSOut sMyString, 13            ' Display the received string on the serial terminal
      Stop
    Changing the pins used for the SDA and SCL lines uses the same mechanism of Declares, and these can be found within the "Hbus.inc" file. However, because the Hbusin and Hbusout commands use the microcontroller's dedicated MSSP peripheral, their pins are fixed to PORTC.3 and PORTC.4.

    The include file, as well as the above demonstration code can be downloaded from here. HBus Replacement Code.zip

    A suitable circuit for the above program is shown below:




    The same circuit is shown below built on the Amicus18 Companion shield:




    A top down view of the same layout is shown here:





    The sketchup 7 drawing of the above layout can be downloaded from here:
    Sketchup Companion Shield with I2C eeprom.zip

    Les Johnson