About this Code Example

Note: This is a direct port of Ashish Makthal's Code Example for the PIC18F46K42 over to the PIC18F26K83(See Here)

Introduction:

The PIC18FxxK42 family has a new UART peripheral which supports several protocols like DMX512, DALI and LIN. This project shows the implementation of a DMX512 receiver on a K42 family device.

The UART with protocol support has programmable address registers that determine what part of the 512 data byte stream should be received. These are the U1P2 and U1P3 registers. It is up to the user to program these registers with the start and end address of the required section of the full data stream.

The PIC18FxxK42 family of devices also have a two channel Direct Memory Access (DMA) module. The DMA module has programmable start/end addresses, counters, triggers and interrupts. This gives the user great flexibility in using the core to attend critical tasks while the DMA moves chunks of data in the background.

Description

In this example, we will be using the HPC Curiosity board with the DMX-Click board and a 4x4 RGB LED Click board. The concept is that each HPC board will receive 3 bytes of RGB data and use that to control the color and intensity of the LEDs on board. It is assumed that there is a DMX Controller transmits the required RGB data.

The DMA is configured to receive these 3 bytes of RGB data through the UART receive buffer and store it in an array. The SPI module will later use this data to control the LEDs.

As mentioned earlier, the U1P2 and U1P3 registers of the UART module determine how many bytes of the full DMX packet is of interest. Only these bytes are received into the receive buffer. In real-world situations, the addresses of these devices can change dynamically, but in this example (for now) the address is hard-coded to 0x02.

Click Boards

The DMX click board used in this project is in the initial development phase. The schematic for this board is available in the "External Links" sections below.

The RGB LED click board has 16 WS2812 LEDs that can controlled using a single line. This board is available for purchase on the MikroElektonika website.

MCC Settings

Here are the setting used for the UART (DMX mode), SPI + CLC (to operate the LEDs). These settings were done using the Microchip Code Configurator - MCC. Open MCC to look at the settings of other modules like Timers etc. The DMA module was setup manually and will be available in MCC soon.

UART Settings:

SPI Settings:

CLC Settings:

DMA Settings:

Refer to the DMA_Initialize() function in the file main.c for further details.

Operation:

Setup the Start Address on the receiver boards using the addressing board. The End Address is calculated in code by adding an offset. e.g. Start address = U1P2 = 0x06 which implies that the End address = U1P3 = 0x08

The DMX controller transmits the data packets. Refer to the DMX Controller example for more details.

The UART module which is setup as the DMX receiver, waits for a Break condition to occur on the line.

After this it accepts the first byte into the RX buffer and sets the U1RXIF flag.

The DMA is triggered off this condition and moves the byte from the U1RXBUF to the DMX_Data array.

The UART module will always accept the first byte as it is defined as the Start code in DMX universe. This determines what the next data bytes mean. In our case we are using the basic Start code = 0x00 case.

Since the Start address is 0x06, the UART will wait until the 6th byte after the 1st byte has been accepted. The UART module will suppress the RXIF interrupts until the 6th byte after the 1st byte has been received.

There isn't a need to clear the RXIF flag for the DMA to re-trigger. The event of receiving a byte in the U1RXBUF is what triggers the DMA.

The UART will accept the 6th, 7th and 8th byte after the start code byte and ignore all the other bytes the DMX controller transmits. The UART module will be active when it observes a break condition on the bus again which signals that the controller is going to start a new data stream.

The DMA will move these three bytes in to the array and reset it's counters for next operation automatically and wait for the UART trigger.

Timer0 is used to create a 1s delay and this is used to check the status of the input pins that decide the Start address. These pins are read every second and the addresses are updated. This can be seen in the GetDMXAddress() function.

Images

The DMX controller is on the left and the HPC Curiosity board on the right with Start Address = 0

The DMX controller is on the left and the HPC Curiosity board on the right with Start Address = 3