This repository contains an MPLAB® X project that demonstrates the basic feature of LED Toggling and Switch Press on the PIC18F26Q71 microcontoller (MCU) using the MCU Sample Kit 1. The LED toggles at regular intervals, and the toggling rate is displayed on the serial terminal through the USB-to-UART bridge.
This example is also available for the tinyAVR® microcontroller included in the MCU Sample Kit 1 at the following link:
More details and code examples on the PIC18F26Q71 can be found at the following links:
- MPLAB X IDE v6.25 or newer
- MPLAB Code Configurator (MCC) Plugin v5.6.2 or newer
- MPLAB XC8 Compiler v3.10 or newer
- PIC18F-Q_DFP 1.28.451 or newer
-
MCU Sample Kit 1 - PIC18F_28P Breakout Board
-
MPLAB PICkit™ 4 In-Circuit Debugger or any Programmers/Debuggers
The system toggles the LED based on a time-out mechanism and displays the toggling frequency to the serial terminal through the USB-to-UART interface. The periodicity of the time-out will change from 500 ms to 1s, 2s, 4s, and back to 500 ms every time the user presses the switch on breadboard.
The LED toggling period is controlled by Timer1 (TMR1). With every switch press, the period of TMR1 is updated to correspond to the selected time-out interval. To ensure reliable detection of switch presses, a debouncing mechanism is implemented to eliminate the unwanted inputs that can cause false triggers.
An Interrupt-on-Change (IOC) is generated on the negative edge of the switch input. After the interrupt occurs, a 20 ms delay is generated using the Timer0 (TMR0). Once the TMR0 intterupt is triggered, the state of the switch is verified to determine if the pressed state is maintained.
The following peripheral configurations are set up using MPLAB® Code Configurator (MCC) Melody for the PIC18F26Q71:
-
TMR0:
- Clock Source: HFINTOSC
- Prescaler: 1:8192
- Requested Period: 20 ms
- TMR Interrupt Enable: Enable
-
TMR1:
- Timer Enable: Enable
- Clock Source: LFINTOSC
- Prescaler: 1/2
- Period: 0.5s
- TMR Interrupt Enable: Enable
-
UART:
- Baud Rate: 115200
- Redirect STDIO to UART: Enable
- Enable Receive: Enable
- Enable Transmit: Enable
- Enable UART: Enable
-
Interrupt Manager:
- Global Interrupt Enable: Enable
-
Prin Grid View:
-
Pins:
- Place the MCU Sample Kit 1 (PIC18F26Q71 MCU) on the breadboard
- MPLAB® PICkit™ 4 allows the In-Circuit Debugger/Programmer to be used for debugging and programming the application
Note: The MCU Sample Kit 1 (PIC18F26Q71 MCU) does not have a built-in debugger, so the MPLAB® PICkit™ 4 is used for programming the application.
-
Connect the MPLAB® PICkit™ 4 to the programming header (J22) on the MCU Sample Kit 1 (PIC18F26Q71 MCU)
-
Connect the MPLAB® PICkit™ to the host PC
-
Connect the user LED to pin RC7 and the switch to pin RC2 on the breadboard
-
Establish the connection between the USB-to-UART Bridge and the MCU Sample Kit 1 (PIC18F26Q71 MCU) on the breadboard using the following connections:
The figure below shows the assembly used for this project.
- Open the MPLAB Data Visualizer terminal
- Identify the USB-to-UART Bridge COM port and set the baud rate to 115200
- The LED toggles continuously on every time-out
- The default periodicity of the time-out is 500 ms
- Press the switch to change the default periodicity of the time-out to one second. Also, the LED toggling rate is displayed on the serial terminal
- Every subsequent pressing of the switch changes the periodicity of the time-out to 1s, 2s, 4s, and back to 500 ms in cyclic order
This section demonstrates how to use the MPLAB X IDE to program a PIC® device with an Example_Project.X
. This is applicable to other projects, too.
-
Connect the board to the PC.
-
Open the
Example_Project.X
project in MPLAB X IDE. -
Set the
Example_Project.X
project as main project.
Right click the project in the Projects tab and click Set as Main Project. -
Clean and build the
Example_Project.X
project.
Right click theExample_Project.X
project and select Clean and Build. -
Select PICkit 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 PICkit (click SN), click Apply and then click OK.
Note: Other PICkit versions can be used as well.
Note: Make sure to enable power from PICkit. -
Program the project to the board.
Right click the project and click Make and Program Device.