Skip to content

Implement electronic dice using the CLB on the PIC16F13276 family of microcontrollers.

License

Notifications You must be signed in to change notification settings

microchip-pic-avr-examples/pic16f13276-clb-dice-mplab-mcc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Microchip Technologies Inc.

Zero-Software eDice — Use Case for CLB Using the PIC16F13276 Microcontroller With MCC Melody

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.

Related Documentation

More details and code examples on the PIC16F13276 can be found at the following links:

Software Used

Hardware Used

Operation

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.

Concept

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.

Theory of Operation

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.

CLB Diagram

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.

Input Handler

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.

Debouncer

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.

One-Shot

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.

Counter

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

Output

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.

Display Diagram

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.

Setup

The following peripheral and clock configurations are set up using the MPLAB Code Configurator (MCC) Melody for the PIC16F13276:

  1. Configuration Bits:

    • CONFIG1:
      • External Oscillator Selection bits: Oscillator not enabled
      • Reset Oscillator Selection bits: HFINTOSC (32 MHz)
    • CONFIG3:
      • WDT Operating Mode bits: WDT Disabled, SEN is ignored
  2. CLB Synthesizer Library:

    • Enable CLB: Enabled
    • Clock Selection: TMR2_PostScaler
    • Clock Divider: Divide clock source by 1
  3. CRC:

    • Auto-configured by CLB
  4. Timer2:

    • Timer Enable: Enabled
    • Control Mode: Roll over pulse
    • Start/Reset Option: Software Control
    • Clock Source: LFINTOSC
    • Prescaler: 1:32
    • Requested Period: 20 ms
    • TMR Interrupt Enable: Disabled
  5. Pin Grid View:

    • CLBPPSOUT0: RD0
    • CLBPPSOUT1: RD1
    • CLBPPSOUT2: RD2
    • CLBPPSOUT3: RD3
    • CLBPPSOUT4: RD5
    • CLBPPSOUT5: RB2
    • CLBPPSOUT6: RB5
    • CLBPPSOUT7: RC2 (on-board LED)
    • CLBIN0PPS: RE1 (SW0 - requires Weak Pull-Up (WPU) enable)

To set up the hardware for this project, follow the steps below:

  1. 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.

LED Circuit

  1. Connect each copy of the circuit with the appropriate pip, as shown in the figure and table below.

Display Diagram

  1. Plug in the PIC16F13276 Curiosity Nano board.
  2. Program the Curiosity Nano board with this MPLAB X project; the steps are provided in the How to Program the Curiosity Nano Board chapter

Full I/O Table

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

Demo

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.

Dice GIF

Important: The randomness of the dice roll comes from the user's response time. This is not a random number generator.

Changing the roll rate

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.

Timer 2 Period

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.

Summary

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.


How to Program the Curiosity Nano Board

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.

  1. Connect the board to the PC.

  2. Open the Example_Project.X project in MPLAB X IDE.

  3. Set the Example_Project.X project as main project.
    Right click the project in the Projects tab and click Set as Main Project.

  4. Clean and build the Example_Project.X project.
    Right click the Example_Project.X project and select Clean and Build.

  5. 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:

  6. Program the project to the board.
    Right click the project and click Make and Program Device.



Menu

About

Implement electronic dice using the CLB on the PIC16F13276 family of microcontrollers.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published