Positron Compiler Documentation

Hbusin2

Positron16 · Revision 1.1.3.5 · PDF pages 165–167

Assignment Variable = Hbusin2 Control, { Address }

or

Assignment Variable = Hbusin2

or

Hbusin2 Control, { Address }, [ Variable {, Variable…} ]

or

Hbusin2 Variable

Receives a value from the I2C bus using the MSSP2 module, and places it into variable/s. If syntax structures Two or Four (see above) are used, then No Acknowledge, or Stop is sent after the data. Syntax structures One and Three first send the control and optional address out of the clock pin (SCL), and data pin (SDA).

Variable is a user defined variable or constant. Control may be a constant value or a Byte sized variable expression. Address may be a constant value or a variable expression. Assignment Variable is a user defined variable.

The four variations of the Hbusin2 command may be used in the same BASIC program. The Second and Fourth syntax types are useful for simply receiving a single byte from the bus, and must be used in conjunction with one of the low level commands. i.e. HbStart, HbRestart, HbusAck, or HbStop. The First, and Third syntax types may be used to receive several values and designate each to a separate variable, or variable type.

The Hbusin2 command operates as an I2C master, using the microcontroller's MSSP1 module, and may be used to interface with any device that complies with the 2-wire I2C protocol.

The most significant 7-bits of control byte contain the control code and the slave address of the device being interfaced with. Bit-0 is the flag that indicates whether a read or write command is being implemented.

For example, if we were interfacing to an external EEPROM such as the 24LC32, the control code would be %10100001 or $A1. The most significant 4-bits (1010) are the EEPROM's unique slave address. Bits 2 to 3 reflect the three address pins of the EEPROM. And bit-0 is set to signify that we wish to read from the EEPROM. Note that this bit is automatically set by the Hbusin2 command, regardless of its initial setting.

View original PDF page 165Open page in PDFOriginal PDF page 165
' Receive a byte from the I2C bus and place it into variable Var1.

  Dim MyByte as Byte                  ' We'll only read 8-bits
  Dim Address as Word                  ' 16-bit address required
  Symbol Control = %10100001           ' Target an EEPROM
  Address = 20                        ' Read the value at address 20
  MyByte = Hbusin2 Control, Address    ' Read the byte from the EEPROM

or

Hbusin2 Control, Address, [MyByte]  ' Read the byte from the EEPROM

Address, is an optional parameter that may be an 8-bit or 16-bit value. If a variable is used in this position, the size of address is dictated by the size of the variable used (Byte or Word). In the case of the previous EEPROM interfacing, the 24LC32 EEPROM requires a 16-bit address. While the smaller types require an 8-bit address. Make sure you assign the right size address for the device interfaced with, or you may not achieve the results you intended.

The value received from the bus depends on the size of the variables used, except for variation three, which only receives a Byte (8-bits). For example: -

Dim MyWord as Word        ' Create a Word size variable
MyWord = Hbusin2 Control, Address

Will receive a 16-bit value from the bus. While: -

Dim MyByte as Byte        ' Create a Byte size variable
MyByte = Hbusin2 Control, Address

Will receive an 8-bit value from the bus.

Using the Third variation of the Hbusin2 command allows differing variable assignments. For example: -

Dim MyByte as Byte
Dim MyWord as Word
Hbusin2 Control, Address, [MyByte, MyWord]

Will receive two values from the bus, the first being an 8-bit value dictated by the size of variable MyByte which has been declared as a byte. And a 16-bit value, this time dictated by the size of the variable MyWord which has been declared as a word. Of course, Bit type variables may also be used, but in most cases these are not of any practical use as they still take up a byte within the EEPROM.

The Second and Fourth syntax variations allow all the subtleties of the I2C protocol to be exploited, as each operation may be broken down into its constituent parts. It is advisable to refer to the datasheet of the device being interfaced to fully understand its requirements. See sections on HbStart2, HbRestart2, HbusAck2, or HbStop2, for example code.

View original PDF page 166Open page in PDFOriginal PDF page 166

Hbusin2 Declares

Declare HSDA2_Pin Port.Pin Declares the port and pin used for the MSSP2 module’s data line (SDA). If the declare is not used in the program, it does not have a default to the pin and will give an error message, so make sure they are in the BASIC listing.

Declare HSCL2_Pin Port.Pin Declares the port and pin used for the MSSP2 module’s clock line (SCL). If the declare is not used in the program, it does not have a default to the pin and will give an error message, so make sure they are in the BASIC listing.

Declare Hbus2_Bitrate Constant 100, 400, 1000 etc. The standard speed for the I2C bus is 100KHz. Some devices use a higher bus speed of 400KHz. The above Declare allows the I2C bus speed to be increased or decreased. Use this Declare with caution, as too high a bit rate may exceed the device's specs, which will result in intermittent transactions, or in some cases, no transactions at all. The datasheet for the device used will inform you of its bus speed. The default bit rate is the standard 100KHz.

Because the I2C protocol calls for an open-collector interface, pull-up resistors are required on both the SDA and SCL lines. Values of 1KΩ to 4.7KΩ will suffice.

Str modifier with Hbusin2

Using the Str modifier allows variations Three and Four of the Hbusin2 command to transfer the bytes received from the I2C bus directly into a byte array. If the amount of received characters is not enough to fill the entire array, then a formatter may be placed after the array's name, which will only receive characters until the specified length is reached. An example of each is shown below: -

  Dim MyArray[10] as Byte      ' Create an array of 10 bytes
  Dim Address as Byte           ' Create a word sized variable

  Hbusin2 %10100000, Address, [Str MyArray] ' Load data into all the array
'
' Load data into only the first 5 elements of the array
'
  Hbusin2 %10100000, Address, [Str MyArray\5]
  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
  HbRestart2                  ' Send a Restart condition
  Hbusout2 %10100001           ' Target an EEPROM, and send a Read command
  Hbusin2 Str MyArray           ' Load all the array with bytes received
  HbStop2                       ' Send a Stop condition

An alternative ending to the above example is: -

Hbusin2 Str MyArray\5        ' Load data into only the first 5 elements of array
HbStop2                       ' Send a Stop condition

See also : HbusAck2, HbRestart2, HbStop2, HbStart2, Hbusout2, HbusAck, HbRestart, HbStop, HbStart, Hbusout, Hbusin.

View original PDF page 167Open page in PDFOriginal PDF page 167