bf. Packet based wireless communications

Wireless communications are crucial for remote control applications. Transceiver modules are capable of both receiving and transmitting data.

A simple 1-bit transceiver module requires a complex communication program library, but communication modules with microcontrollers are much better option. RFM69CW is the latest HopeRF communication module that communicates with a microcontroller through an SPI interface. It has a number of control registers that enable setting different communication hardware configurations.

A crucial part of RFM69CW is also a data packet handler unit. It has a special set of control and stratus registers that define various packet formats for data transfer as well as optional CRC error detection and data encryption with AES algorithm.

RFM69CW packet handler enable fully buffered unidirectional communications. A data packet is first loaded to the module internal buffer and it is then transmitted to another RFM69CW module that operates in reception mode.

a. Packet communications

PIC18F26J50firmware v2.6.5 or later and PIC32MX250F128B firmware v2.9.1 or later and PIC32MX270F256B firmware v2.9.1 or later support packet communications with CRC error detection. An inbuilt communication protocol enables copying a byte array with a maximum length of 255 bytes to RFM69CW output buffer. The byte array is then transmitted over to another RFM69CW module and emerges in its buffer. A microcontroller on the receiving side can download and process the packet.

b. RFM69CW functions

PIC18F26J50 firmware v2.7.8 or later and PIC32MX250F128B firmware v2.9.1 or later or PIC32MX270F256B firmware v2.9.1 or later have the following inbuilt functions that are available to high level applications through LIB_PCUSBProjects v6.2.NET4(x64).dll library:

Function RFM69CW_Init_SPI1() as Integer

Function RFM69CW_SendPacket_SPI1(size as Byte) as Integer

Function RFM69CW_SetReceiveMode_SPI1(size as Byte) as Integer

Function RFM69CW_ReceivePacket_SPI1(size as Byte) as Integer

All functions return a negative value in case of an error. There are common error codes for all functions and special error codes for a particular function. Common error codes are the following:

Equal to 0 or above 0 = Function completed successfully. The data represents the function output

-2 = USB communication failed

- 4 = DLL internal error

-99 = unsupported function or receive mode set (RFM69CW_ReceivePacket_SPI1

function, only)

RFM69CW_Init_SPI1 function initializes SPI1 microcontroller interface, then enters default values to external RFM69CW module and finally sets idle operation mode to RFM69CW. The carrier frequency is set to 868 MHz.

RFM69CW_SendPacket_SPI1 function uploads data from PC output buffer packet (data array) from PIC18F26J50 or PIC32MX250F128B to RFM69CW via SP1 interface and transmits it to another RFM69CW module that operates in data receive mode. The length of the packet is determined by size parameter. PC output buffer is accessible through WriteBuffer and WriteBufferWORD commands. See Transfering data arrays between DLL and VB.NET applications in Chapter 3: PIC32MX2X0 Programming guide for more information.

RFM69CW_SetReceiveMode_SPI1 function sets RFM69CW to receive mode (RFM69CW register 1 is set to 0xC).

RFM69CW_ReceivePacket_SPI1 function first verifies RFM69CW operation mode. If it is not (0x10) receive mode, it sets receive mode and exits with error code -99. If the receive mode is set, it checks status of the RFM69CW internal buffer. It returns error code -100, if the buffer is empty, or error code -101 in case of CRC error during reception. If RFM69CW internal buffer is not empty, data is download to PC buffer that is accessible through ReadBuffer and ReadBufferWORD commands. See Transfering data arrays between DLL and VB.NET applications in Chapter 3: PIC32MX2X0 Programming guide for more information.

Size parameter determines data packet length and must have the same value as the size parameter used with RFM69CW_SendPacket_SPI1 function on the transmitting microcontroller.

RFM69CW_DEMO(x64) programming example is available from PC Projects Downloads section (https://sites.google.com/site/pcusbprojectsdownloads).

MUST READ:

- PC USB Projects Programming Guide

- How to get PIC18 SPI interface working?

- How to get PIC32 SPI interface working?

Here are two short examples:

EXAMPLE 1: INITIALIZE RFM69CW MODULE, IF NECESSARY AND SEND STRING

Dim s As String

Dim n As Integer

s = Tb_Text.Text

While s.Length < 33

s = s + " "

End While

If Not RFM69CW_initialized Then

PIC.RFM69CW_Init_SPI1()

Lb_Report.Items.Add("RFM69CW Intialization finished...")

End If

For n = 0 To 31 ' Fill buffer

PIC.WriteBuffer(n, Asc(Mid(s, n + 1, 1)))

Next

n = PIC.RFM69CW_SendPacket_SPI1(32)

Lb_Report.Items.Add("String: " + s + " sent!")

Lb_Report.Items.Add("error code = " + CStr(n))

EXAMPLE 2: INITIALIZE RFM69CW MODULE, IF NECESSARY AND RECEIVE STRING

Dim n As Integer

Dim s As String

If Not RFM69CW_initialized Then

PIC.RFM69CW_Init_SPI1()

Lb_Report.Items.Add("RFM69CW Intialization finished...")

End If

n = PIC.RFM69CW_ReceivePacket_SPI1(32)

If n = 0 Then

s = ""

For n = 0 To 31

s = s + Chr(PIC.ReadBuffer(n))

Next

Lb_Report.Items.Add("> " + s)

Else

Lb_Report.Items.Add("Receive packet error code = " + CStr(n))

End If

End Sub