This code example demonstrates the usage of PIC16F15276 microcontroller as an Input/Output (I/O) expander in embedded applications, where the microcontroller provides additional I/O pins through a serial communication interface like I2C, Serial Peripheral Interface (SPI) or Universal Asynchronous Receiver Transmitter (UART). This particular example shows the usage of the Enhanced Universal Synchronous Asynchronous Receiver Transmitter (ESUART) peripheral and General Purpose Input/Output (GPIO) to relize as UART I/O expander.
The PIC16F152xx family of microcontrollers are available in packages for various embedded applications. The PIC16F15276 simplified feature set includes Peripheral Pin Select (PPS), digital communication peripherals, timers and EUSART, which can be configured as a full-duplex asynchronous system or as a half-duplex synchronous system. Full-Duplex mode is useful for communications with peripheral systems, such as CRT terminals and personal computers. Half-Duplex Synchronous mode is intended for communications with peripheral devices, such as Analog-to-Digital (A/D) or Digital-to-Analog (D/A) integrated circuits, serial EEPROMs or other microcontrollers. Several embedded applications use I/O expander to add the additional I/O pins required for the main microcontroller while interfacing standalone IC modules, display modules, for various features implementation.
For more details about the host device implementation see the UART I/O Expander: Host Implementation using PIC16F15276 code example.
- PIC16F15276 Product Page
- PIC16F152xx MCU Family Product Brief
- How to use PIC16F15244 Family as an I/O Expander
- SPI I/O Expander using PIC16F15276 micrcocontroller
- PIC16F15244 Code Examples on GitHub
- PIC16F152xx MCU Family Video
Microchip’s free IDE, compiler and graphical code generators are used throughout the application firmware development. The tools used for this demo are:
- MPLAB® X IDE 6.15.0 or newer
- MPLAB® XC8 Compiler 2.45.0 or newer
- MPLAB® Code Configurator (MCC) 5.3.7 or newer
- Microchip PIC16F1xxxx_DFP Device Support 1.21.368 or newer pack
- ESUART MCC Melody driver 7.1.5
Note: For running the demo, the installed tool versions should be the same or later. This example is not tested with the previous versions.
- PIC16F15276 Curiosity Nano Board
- Curiosity Nano Base for Click BoardsTM
- PROTO Xplained Pro Extension Kit
- Seven Segment Display
In this example, the host microcontroller expects to display the numbers on Seven Segment Display (SSD). To interface the SSD with the host device, seven I/O pins are required. To minimize the pins on the host, a secondary microcontroller is used as client. The device drives the SSD while the UART serial communication interface is used to establish the communicate between the two devices.
For ease of demonstration, this code example uses two PIC16F15276 microcontrollers, one as a host and the other as a client and a PIC16F15276 Curiosity Nano Development board.
On the host device, the on-board switch of the PIC16F15276 Curiosity Nano board is used to send a command to the client device through the UART interface. The on-board LED is used to notify the user about successful switch press event detection and subsequent UART command transmission by the host. After receiving the command from the host, the client device drives the respective I/O pins connected to the SSD in a pre-defined sequence to display numbers from zero to nine in an ascending order.
- In this example, PIC16F15276 CNANO boards, PROTO Xplained Pro extension kit with SSD circuitry populated over it and few connecting jumper cables are used for demonstrating UART I/O Expander
- The on-board switch of host device is used to initiate the communication between the host and client devices over UART interface. The HLT mode of Timer2 module is used for switch debounce implementation
- The client receives the command from the host device over UART interface
- The client microcontroller verifies the commands upon reception, if it matches, then it initiates the number display from zero to nine on the SSD. It is followed up with SSD value resetting to zero.
Each of the seven segment LEDs is called a segment because, when illuminated, the segment forms a part of a numerical to be displayed.
An additional eighth LED is sometimes used within the same package, thus allowing the indication of a decimal point (DP), which is used when two or more 7-segment displays are connected to show a number greater than ten.
Each one of the seven LEDs in the display is given a positional segment with one of its connection pins being brought straight out of the rectangular plastic package. These individual LED pins are labelled from "a" to "g" represent each individual LED. The other LED pins are connected and wired to form a common pin.
Figure 2: SSD Segment Naming Conventions
Seven Segment Hex values:
Number | HGFEDCBA | Hexadecimal |
---|---|---|
0 | 11000000 | 0xC0 |
1 | 11111001 | 0xF9 |
2 | 10100100 | 0xA4 |
3 | 10110000 | 0xB0 |
4 | 10011001 | 0x99 |
5 | 10010010 | 0x92 |
6 | 10000010 | 0x82 |
7 | 11111000 | 0xF8 |
8 | 10000000 | 0x80 |
9 | 10010000 | 0x90 |
Resistor Value Calculations
If forward current (If) is 20 mA If forward voltage (Vf) is 2.2V Input voltage (Vin) is 5V
Resistor ( R ) = (Vin – Vf ) / ( If ) = 140 ohms
Note: 330 or 470 ohms resistor will be used.
Figure 3: SSD LED Resistor Driver Circuit
This example is supplemented with two standalone firmware named host and client.
When a valid switch press event is detected, the host sends a command to the client over the UART interface, which also blinks the on-board LED each time a command is successfully sent to the client.
The client firmware is comprised of two sections. The first section is verifying the command received from the host through the UART interface. The second section consists of driving SSD, so the SSD starts displaying digits from zero to nine in an incrementing order each time a valid command is received from the host. Additionally, the client firmware toggles on-board LED each time after a digit is displayed on the SSD and resets display value to zero when the cycle is completed.
The following figure consists of populated PROTO Xplained Pro extension kit, Curiosity Nano Adapter Board and PIC16F15276 Curiosity Nano Evaluation Kit (host and client devices). The figure shows the detailed information about the hardware setup. The populated PROTO Xplained Pro extension kit is interfaced with PIC16F15276 microcontroller using an extension header.
Note: In order to use on-board mechanical switch on host device, a timer is used to avoid debouncing. Thereby, timer input pin RC3 and pin RB5 (on-board switch), need to be shorted using a jumper. The usage of jumper is required as RB5 pin cannot be selected as timer input through the PPS feature of the PIC16F15276 microcontroller.
- Make the hardware connections as shown in the Hardware Setup section. Power up the Curiosity Nano board using a micro-USB cable.
- Download the firmware available from the GitHub code example page.
- Build the project using latest version of tools as mentioned in the Software Tools section and flash the generated file on the PIC16F15276 microcontroller.
- On the host, the on-board switch press event is performed to transmit the command to the client through the UART interface.
- On the host, the on-board LED is used as an indicator for the switch press event and indicates a command is sent to the client.
- On the client, after receiving a command from the host, the client device verifies the command and drives the 7-segment LED display to show the numbers from zero to nine. The on-board LED of the CNANO board toggles for every number display.
In many real-world embedded applications, the microcontroller requires to interface with multi-pin standalone IC modules and display units to perform various functionalities. So, the developers have a challenge in effectively using microcontrollers I/O pins for interfacing the external modules without opting an expensive, higher pin and memory variant. This code example demonstrates how cost effective and entry level PIC16F15726 microcontroller can be used as I/O expander using UART interface.
MPLAB® Code Configurator is a graphical programming environment that generates seamless, easy to understand C code to give a head start to the project, saving the designer’s time to initialize and configure all the modules, and to go through the data sheets. Using an instructive interface, it enables and configures all peripherals and functions specific to the application requirements.
Start by creating a new Project and open MCC
- Go to File and click New Project
- Select Microchip Embedded and click Standalone Project
- Enter the device name, in this case, PIC16F15276
- Name the project
- Launch MCC tool by navigating to Tools>Embedded>MPLAB Code Configurator v4: Open/Close . Alternatively, click the MCC icon to launch the MCC tool.
-
Configure Clock
Open Clock Control setup present under "System" dropdown menu in Project Resources tab. Host and the client device will be configured with same configuration as given below.
- Set Clock Source as HFINTOSC
- Set HF Internal Clock as 8_MHz
The Configurations Bits (Project Resources>System) window in MCC is used for MCU oscillator, Watchdog timer and low voltage programming configuration. The Watchdog timer is disabled in the application.
The following figure shows the clock configuration setting in MCC tool.
-
Timer 2 Configuration
Configure Timer2 in HLT mode for switch debouncing functionality with the following configuration.
-
Enable Timer checkbox
-
Control Mode – Monostable
-
Ext Reset – T2INPPS
-
Start/Reset Option – Start on rising edge on TMR2_ers
-
Clock Source – MFINTOSC 31.25 kHz
-
Polarity – Rising Edge
-
Prescaler – 1:16, Postscaler – 1:1
-
Time Period – 100 ms
-
Enable Timer Interrupt checkbox
Figure 6: Timer 2 Configuration
- EUSART Configuration
Figure 7: EUSART Configuration
The following images informs about the pin usage in the project.
- Host Device
Figure 8: Pin Configuration (Host)
- Client Device