This MPLAB X Melody code example shows how to make an LED blink using the Configurable Custom Logic (CCL) found in the AVR® DB.
The CCL is configured as a toggling J-K flip-flop, also known as a T flip-flop. While a regular J-K flip-flop has two inputs and a clock signal that toggles the output on rising edges, the T flip-flop only uses a clock signal to toggle and has no other inputs. To achieve this, the J and K input needs to be set to constant logical '1'.
An internal clock signal can be used for the toggling, but it will toggle the output so fast that the blinking of an LED will appear as an LED that is always on. To get a more reasonable time between blinks, an alternative is to use the AVR event system. The event system needs a generator to generate the event, and a peripheral to make use of the event. In this example, a timer will trigger an event each time the timer overflows. The event will then be used as the clock input for the T flip-flop. This way makes it easier to set a specific time between each blink.
- MPLAB Code Configurator
- AVR DB Family Product Page
- AVR128DB48 Hardware User Guide
- AVR128DB48 Datasheet
- MPLAB® X IDE 5.45 or newer
- MPLAB® XC8 2.31 or a newer compiler
- MPLAB® Code Configurator (MCC) 4.1.0 or newer
- MPLAB® Melody Library 1.37.26 or newer
- Microchip AVR-Dx Series Device Support 1.7.98 or newer
MCC with the Melody library was used to implement this example, once downloaded and opened in MPLAB X, it only requires the project to be built and flashed to an AVR DB Curiosity nano device.
To change configurations in MCC, simply push the MCC icon in MPLAB X, and wait for MCC to start.
The configurations in MCC for this example, are demonstrated and explained below. In this example, the three peripherals CCL, TCA timer, and the Event System are added and configured.
-
Add CCL from “Drivers” in the Device Resources list by pressing the corresponding green cross.
-
Enable the CCL.
-
Go into the CCL GUI and choose the LUTs to use. Each LUT can be configured by pressing the related cogwheel.
-
In this example, we want to use a JK flip-flop as a T flip-flop.
-
We need two LUTs for the J and K input.
-
The two inputs need to be constant 1 so we use a custom logic set to 0xFF.
-
We want the T flip-flop to be triggered by an event instead of a clock.
-
To get an event as an input for the clock, one of the logic inputs needs to be set as the new clock signal.
-
We here set it to be IN2.
-
In the logic gate, we choose IN2 to be one of the event channels, EVENTA.
-
Enable LUT and LUT output in the two chosen LUTs.
-
We want the toggling to happen to a specific pin. This pin will be used to toggle an LED. This will affect the LUTs you use. In our case, the PB3 pin is preferable because it has an LED connected to it for the CNANO. We therefore use LUT4 and LUT5.
-
Connect the output of the JK flip flop to the output pin. To connect an output in MCC, press the output and drag it to the selected output pin.
-
Add the TCA0 from the Timer list, found under “Drivers” in the Device Resources list.
-
Open the TCA tab.
-
Enable TCA.
-
Choose a clock selection. Bigger divisions of the System clock give a slower clock which allows for longer delays. We have chosen "System Clock / 16".
-
Choose count direction. We have chosen UP count.
-
Choose the Waveform generation mode. We have chosen the default, "Normal Mode".
-
Choose the desired clock time between each overflow. We have chosen 0.25 seconds, this is the time between overflows, meaning an event will trigger every 0.25s. The T flip flop will therefore toggle the LED every 0.25s.
-
Open the Resource management tab.
-
Add the EVSYS initializer from “Peripherals” in the Device Resources list.
-
Open the EVSYS initializer tab.
-
Set the generator. We use the TCA to generate an event on each overflow. We choose this in MCC by setting one of the channels, CHANNEL0 to TCA0_OVF_LUNF .
-
Set the user. When configuring the CCL, event channel A (EVENTA) was used as an input for LUT4. To set LUT4 as the user of the event, select USERCCLLUT4A, and set it to the same channel as the generator, CHANNEL0.
Once all configurations are complete the code needs to be generated. Go to the Resource Management [MCC] tab, and click on the Generate button to generate the code.
Build the code and flash your device.
If build and flashing were successful the LED on the board should start blinking with 0.25s intervals.
After going through this example you should be able to make an LED blink, and be more familiar with how to:
- Configure the Timer to generate an overflow event
- Configure the Event System to route a Timer overflow event to CCL
- Configure the CCL JK flip-flop with constant inputs, and an overflow event as the clock