This repository provides an MPLAB® X project for the PIC16F13276 family of microcontrollers, demonstrating a fully software-based dice (eDice) application. The project utilizes the Configurable Logic Block (CLB) peripheral, which is implemented as an array of Look-Up Tables (LUTs) similar to a small FPGA, to reduce software complexity in electronic logic implementation. The application drives seven LEDs as outputs, which continuously change to simulate a rolling dice and stop when the user presses a button.
Important: This example contains blinking lights. If you are sensitive to flashing/blinking lights, use caution when viewing or recreating.
More details and code examples on the PIC16F13276 can be found at the following links:
- PIC16F13276 Product Page
- PIC16F13276 Code Examples on Discover
- PIC16F13276 Code Examples on GitHub
- 7-Segment Display Decoder Example
- MPLAB® X IDE v6.30 or newer or MPLAB® Tools for VS Code®
- MPLAB® XC8 v3.10 or newer
- PIC16F1xxxx_DFP v1.29.444 or newer
- The PIC16F13276 Curiosity Nano Development board is used as a test platform:

To program the Curiosity Nano board with this MPLAB X project, follow the steps provided in the How to Program the Curiosity Nano Board chapter.
The CLB significantly reduces the software overhead associated with implementing this example. In software, the CPU would need a periodic interrupt, increment a counter, perform counter logic, map the counter output to the I/O pins and then return to its previous task. With the CLB, all the functions associated with this example are handled by the hardware, which leaves the CPU free to do other tasks without interruption.
The CLB has a series of LUTs, similar to an FPGA. In the PIC16F13276 family of MCUs, there are 32 LUTs available. To configure the logic, use the CLB Synthesizer tool inside MCC (or the equivalent standalone online tool). The logic diagrams comprise screenshots of the tool. All the configuration files (.clb and .v) are included in the example folders.
There are three main function blocks in this program - an input handler, a free-running counter for number generation, and a series of LUTs for the display outputs. The CLB clock source is from TMR2.
The input is composed of two sections - an input debouncer and a one-shot generator. When SW0 is pressed, a series of small "bounces" occur before the value stabilizes. To avoid instability due to this behavior, the CLB logic implements a small input debouncer out of two D-Flip Flops, an inverter, and an AND gate, as shown in the image below.
Then, a one-shot pulse generator is implemented to enable/disable the output Flip-Flops when the user presses a button. If the one-shot was removed, the dice would roll when the user held the button.
The one-shot emits a pulse when the debouncer output in the previous clock cycle is not equal to the current output of the debouncer, and the current signal is 1 (when pressed). Then, the one-shot generates a pulse that causes the JK Flip-Flop to toggle on the next clock cycle. At this point, the one-shot output will become 0 since the previous values match the current.
The state of the outputs is shown on the LED0 of the Curiosity Nano. If the LED is ON, the output Flip-Flops maintain their state. When the LED is OFF, the output Flip-Flops can change the value.
The counter module is responsible for the "rolling" of the dice; it counts up until it reaches five, on every edge of the CLB clock. When the value of five is reached, the next clock cycle will reset it to 0. This behavior is implemented in the Verilog module below.
Note: The counter changes quickly, creating the illusion of randomness.
module Counter(CLK, out0, out1, out2);
reg[2:0] out;
input CLK;
output out0, out1, out2;
assign out0 = out[0];
assign out1 = out[1];
assign out2 = out[2];
always @(posedge CLK)
begin
if (out >= 5)
out <= 0;
else
out <= out + 1;
end
endmodule
A LUT is used on each output in order to generate the outputs for the pips (LEDs). To determine the values in the LUT, go through the list of possible input values and decide if each pip should be ON (1) or OFF (0). Since the timer starts at 0, all outputs are offset by 1, which means that an input of 0 is a dice roll of 1.
A copy of the LUTs is shown below.
| Input Value | PIP1 | PIP2 | PIP3 | PIP4 | PIP5 | PIP6 | PIP7 |
|---|---|---|---|---|---|---|---|
| 000 | 0 | 0 | 0 | 1 | 0 | 0 | 0 |
| 001 | 0 | 1 | 0 | 0 | 0 | 1 | 0 |
| 010 | 1 | 0 | 0 | 1 | 0 | 0 | 1 |
| 011 | 1 | 0 | 1 | 0 | 1 | 0 | 1 |
| 100 | 1 | 0 | 1 | 1 | 1 | 0 | 1 |
| 101 | 1 | 1 | 1 | 0 | 1 | 1 | 1 |
| X | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
Note: X in the table above can have any other value.
Then, a D Flip-Flop with Enable is used to hold the value when the user is not rolling the dice. The enable signal comes from the output of the JK Flip-Flop in the Input block.
The following peripheral and clock configurations are set up using the MPLAB Code Configurator (MCC) Melody for the PIC16F13276:
-
Configuration Bits:
-
CLB Synthesizer Library:
-
CRC:
- Auto-configured by CLB
-
Timer2:
-
Pin Grid View:
To set up the hardware for this project, follow the steps below:
- Build seven copies of the following circuit on a breadboard. For ease of wiring, the circuits can be placed in parallel, rather than building it like the dice dot (also known as a pip) pattern shown in step 2.
- Connect each copy of the circuit with the appropriate pip, as shown in the figure and table below.
- Plug in the PIC16F13276 Curiosity Nano board.
- Program the Curiosity Nano board with this MPLAB X project; the steps are provided in the How to Program the Curiosity Nano Board chapter
Note: This table contains all I/Os used in the project, including I/Os that are already connected on the Curiosity Nano.
| I/O Pin | Function |
|---|---|
| RD0 | Pip 1 |
| RD1 | Pip 2 |
| RD2 | Pip 3 |
| RD3 | Pip 4 |
| RD5 | Pip 5 |
| RB2 | Pip 6 |
| RB5 | Pip 7 |
| RC2 | LED0 (STOP/nRUN Status) |
| RE1 | SW0 (START/STOP) |
| RE3 | MCLR |
| GND | GND |
To start "rolling" the dice, press and hold SW0 until the LEDs start blinking. This allows the LED outputs (Pips) to change state. In this mode, the LEDs will blink quickly as the timer value is constantly increasing.
To stop "rolling" the dice, press and hold SW0 until the LEDs stop changing. This locks the outputs to the last value of the counter.
Important: The randomness of the dice roll comes from the user's response time. This is not a random number generator.
To change the rate the dice rolls at, adjust the period of TMR2 in MCC, press the Generate button on the top left window, and reprogram. The default period is 20 ms.
Faster periods will be harder to predict, unlike slower periods. The response time for button presses will improve on faster periods and reduce on the slower ones.
This example has shown how the CLB can operate independently of the CPU. After the initialization of the peripherals, the CPU is fully Idle and able to do any other task in the MCU.
This chapter demonstrates how to use the MPLAB X IDE to program a PIC® device with an Example_Project.X. This is applicable to other projects.
-
Connect the board to the PC.
-
Open the
Example_Project.Xproject in MPLAB X IDE. -
Set the
Example_Project.Xproject as main project.
Right click the project in the Projects tab and click Set as Main Project.
-
Clean and build the
Example_Project.Xproject.
Right click theExample_Project.Xproject and select Clean and Build.
-
Select PICxxxxx Curiosity Nano in the Connected Hardware Tool section of the project settings:
Right click the project and click Properties.
Click the arrow under the Connected Hardware Tool.
Select PICxxxxx Curiosity Nano (click the SN), click Apply and then click OK:
-
Program the project to the board.
Right click the project and click Make and Program Device.








