• PicŪ Basic


  • PIC18F Busin/Busout 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 Busin, Busout, BStart, BReStart, BStop, BusAck and BusNak, software 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.

    For example, the bit-bashed I2C routines do not implement clock stretching. With the code presented in BASIC, you can add this feature yourself.

    The code below shows the use of the Busin/Busout 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 "Busin.inc" include file at the top of the program.

    Code:
    '
    ' Demonstration for using the Busin/Busout code replacement routines
    '
      Include "Amicus18.inc"          ' Configure the compiler for a PIC18F25K20 at 64MHz. i.e. An Amicus18 board
      Include "Busin.inc"             ' Load the replacement code for the Busin, Busout 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
    
      BStart                          ' Send a Start condition
      BusOut 100000                   ' Target an eeprom with bus address 0, and send a Write command
      BusOut 0                        ' Send the High Byte of the address
      BusOut 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
        BusOut bLoop                  ' Write the value of bLoop to the eeprom
      Next                            ' Close the loop
      BStop                           ' 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
    
      BStart                          ' Send a Start condition
      BusOut 100000                   ' Target an eeprom with bus address 0, and send a Write command 
      BusOut 0                        ' Send the High Byte of the address
      BusOut 0                        ' Send the Low Byte of the address
      BReStart                        ' Send a Restart condition
      BusOut 100001                ' Target an eeprom with bus address 0, and send a Read command
      For bLoop = 0 To 9              ' Create a loop
        sMyString[bLoop] = BusIn      ' Load the String with bytes received
        If bLoop = 9 Then             ' Are we at the last byte?
          BStop                       ' Yes. So send a Stop condition
        Else                          ' Otherwise...
          BusAck                      ' 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 "Busin.inc" file.

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

    In order for any program to be able to use the "Busin.inc" file, copy it into to the compiler's "sources" folder, default location "C:\Program Files\AmicusIDE\Includes\Sources"

    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
  • Recent Activity

    xldaedalus-432

    Mysterious PORTB problem

    Thread Starter: xldaedalus

    I'm using Proton+ to develop firmware for a product with switches. The MCU is an 18F26K22. Most of the switches reside on PORTB. I am NOT using a...

    xldaedalus Yesterday, 18:21 Go to last post
    Les-15

    Pic16f18877 oread

    Thread Starter: evoortman

    Hi, On a PIC16F18877 the OREAD command doesn't seem to work. The code is working on a PIC16F1939. Both controllers use 32MHz int osc. If i...

    Les Yesterday, 13:05 Go to last post
    amod-29593

    Multiple functions by a single switch

    Thread Starter: amod

    Hi, How can I use multiple function by a single switch.I want to use 4 functions by a single switch.

    amod Yesterday, 15:31 Go to last post
    Oldhack-15978

    FUSES for PIC18F47K20

    Thread Starter: gtv_pic

    000 Good morning I am using the PIC18F47K20 and in the help of the proton I did not locate the FUSES for said micro. Where can I locate...

    Oldhack Yesterday, 16:34 Go to last post
    Les-15

    SHIN generates ASM ERROR with the 16F18877

    Thread Starter: evoortman

    Hi, On a PIC16F18877 the SHIN command generates an ASM ERROR: Could this also be related to renamed or moved SFRs in the newer controllers? ...

    Les Yesterday, 19:18 Go to last post