Background and Theory
The original purpose of this board is to send strings of commands to a servo controller board and a high precision motor controller board, but the code can be modified to connect additional boards such as power modules, and other sensor boards.
This board doesn't create the commands it just relays them to the appropriate board. This board receives string of commands through the UART1 port, the RX logic level can be both 3.3V or 5V, and the TX line is 3.3V but can be changed to 5V with a jumper.
The current code uses an interrupt on UART1 RX buffer. An interrupt service routine checks if a new package has arrived, store the package, if data is invalid, and if the package has ended.
The current code is done knowing what kind of data will arrive and in what order. This code normalizes/parses the data that has arrived into meaningful numerical values that will be required by the client boards. It will only send relevant packages to each of the clients, this lifts weight from the clients. In the case of the servo controller board, for example, it allows to create a higher resolution on the PWM signal it creates since it doesn't spend computing cycles on interpreting the packages, this is all done by this project, the Serial Port Expander.
The I/O connectors of this board are as follows:
- Input power terminal: 7805 pin compatible regulator is used for voltage regulation
- UART1 3-pin header: This is usually connected to the client sending commands such as a BeagleBone Black, Raspberry Pi, or other main MCU.
- UART2 3-pin header: In this project is used to send to a client that uses UART standard communication, some examples are: servo controllers, GPS module, Wi-Fi Modules, etc.
- 4 SPI connectors with JST 5-pin connector: This board is configured as an SPI Master controller as well. All 4 SPI connectors are connected to the MSSP1 peripheral of the PIC MCU. Every connector has GND, SDO, SDI, SCLK pins connected together. The SS pin on the connectors are connected to different MCU GPIO pins in order to control 4 different clients as per SPI standard.
- High Power LED terminal: This board contains a latching relay that switches 5V-regulated to two power-resistors, which are then connected to two block terminals. The main idea of these terminals is to power two high-power LEDs. In case that the LED(s) that need to be powered already contain built-in resistors (such as 5V LED strips), the onboard power-resistors can be bypassed by a wire (22 AWG solid wire is a reasonable jumper). Note: the 5V comes from the 7805 regulator, make sure to stay within its electrical and thermal ratings.
- 3 indicator LEDs: These onboard LEDs can be used to indicate different states of the board. I used to assist in the debugging process, but they can be used for other purposes.
- Pickit Connector: This is a 5-pin connector that aligns with the Pickit programmers from Microchip.
Both UART1 and UART2 are setup to communicate with a baud rate of 115200 baud. This can be modified by changing the value in the registers: SPBRG1:SPBRGH1 (for UART1) and SPBRG2:SPBRGH2 (for UART2). These registers are set on the file: "uart.c" and inside the "uart_init()" function.
The SPI perfipheral is set run at a clock of 500 KHz, this can be modified by changing the value in the SSP1ADD register. The SSP1ADD register is set in the file "spi_master.c" inside the spi_master_init(void) function. The formula to calculate the SCLK is as follows:
SCLK = (Fosc)/((SSP1ADD + 1)(4))
The Fosc is the system's frequency, in this project, this board uses a 16 MHz crystal, and then the MCU uses a 4x phase lock loop to increase the frequency to 64 MHz, therefore: Fosc = 64 MHz. And the current value for SSP1ADD is 31.
If we plug in the values we get: (64x10^6) / ((31 +1) (4)) =.5x10^6 or 0.5 MHz. In the equation, you can solve for the SSP1ADD variable and change the SCLK for the desired frequency.Getting Ready
This circuit can be built on a breadboard for prototyping purposes. For a permanent solution, the PCB drill and gerber files have been included, they are ready for JLCPCB rules. In case that the project needs to be implemented as soon as possible or at a very low price, this project can be built on a prefboard.
1 / 3 • Figure 1-1: This is the bare PCB sent by JLCPCB
The schematic that is included at the bottom of the page, the bill of materials that is also attached and Figure 1-3 can be used to assemble the PCB. Please follow those files and references to build this circuit.
I have uploaded the code for this project in GitHub. Click here to download it in order to compile it and program the microcontroller.
The steps for compiling and programming are listed bellow. I have also posted a video showing all of those steps and giving a quick demo on how to connected to client boards. Compiling the Serial Port Expander
To compile the code for the MCU, follow the next steps:
- Download the repository as mentioned above
- Extract the contents of the ZIP file somewhere where the path name doesn't contain too many character, e.g. root of the C: drive
- Open MPLABX (install it if you have to, make sure to also install the XC8 compiler)
- On the toolbar, click on File-> Open Project
- Navigate to the location where the contents of the zip file extracted
- Open the folder, then click on the project and click on "Open Project"
- Once the project is open, you may navigate the source and modify it to your needs.
- On the toolbar go to Production->Clean and Build Project
The compiler should have generated a ".hex" file. which can be used to program the microcontroller.Programming the microcontroller
To program the microcontroller, make sure that the microcontroller is powered on with 5V and the Pickit 3 Programmer is connected to the "Programmer" header pins.
- In the toolbar go to Production->Make and Program Device
- An MPLABX warning window will popup to remind you to make sure that the chip connected is the same as selected in the project and for the voltage to be the one recommended. click OK.
These are the steps to program the Serial Expander (AKA: Signal Distribution Board). All the steps to build and program the code into the board are included in the YouTube video. Please watch it as it also gives an explanation on how to use it.