This project demonstrates how to read a digital input pin on a microcontroller and control a Light Emitting Diode (LED) based on the state of a momentary switch connected to the digital input.
Step by Step Instructions
Using the MPLAB® Code Configurator (MCC) simplifies the setup for the various registers within the device to create a digital input. To demonstrate how to get started, this project monitors a momentary switch connected to pin RB4 that is enabled as a digital input using an internal pull-up. The code will light an LED connected to RA4, enabled as a digital output when the switch is pressed.
The project uses:
- PIC16F18875
- HPC Curiosity Board
- MPLAB X IDE
- MPLAB Code Configurator (MCC) plug-in
- MPLAB XC8 Compiler
To follow along with these steps, MPLAB X IDE should be open and the HPC Curiosity Board connected to the computer through a USB cable.
1
Create a new standalone project in MPLAB X for a PIC16F18875.
If this is your first time creating an MPLAB X project, please visit the "Create a Standalone Project" page to follow a step-by-step instruction on how to do this.
3
Select the peripherals for your project. For this project, the following peripherals need to be selected:
- System Module
- Interrupt Module
- Pin Module
The System Module, Interrupt Module, and the Pin Module will all be automatically included when you launch the MCC. The result should look like the picture below:
4
Open the Pin Manager: Grid View. Next, click on the PORTA - 4 blue lock symbol in the Output row. This will change it to locked and turn it green in color. Also, click on the PORTB - 4 blue lock symbol in the Input row to change it to green and locked. Check that the Reset is blue and unlocked. Click on it if it is set to green and locked. This adds the RA4 and RB4 I/O pins to the project. It should look like the picture below when completed:
5
Close the Pin Manager: Grid View. Then, click on the Pin Module in the Project Resources area.
The center section should show RA4 and RB4 listed on the I/O chart.
- RB4 will be the input that will read the switch. The Weak Pull-Up (WPU) box is checked.
- RA4 will be an output and control the LED. Click on the Output box to make the pin an output.
Change the names of the pins to LED for RA4 and Switch for RB4.
6
Next, the system needs to be set up. Click on System Module in the Project Resources list. It should be highlighted in blue when selected.
In this section, the oscillator settings and the configuration settings are selected.
Oscillator
- Select the HFINTOSC from the Oscillator Select drop-down menu.
- Select the 4MHz selection from the HF Internal Clock drop-down menu.
- Select Clock Divider value of 4.
This will enable the internal 1 MHz internal oscillator as the system clock.
Also, make sure the Low-voltage Programming Enable mode is selected at the bottom of the System Module screen.
9
Add the following code to the end of the main.c file:
void main(void)
{
// initialize the device
SYSTEM_Initialize();
while (1)
{
// Add your application code
if (Switch_GetValue() == 0)
{
LED_SetHigh();
}
else
{
LED_SetLow();
}
}
}
The Switch_GetValue(), LED_SetHigh() and, LED_SetLow() are #define's and macros that were generated by the MCC. Their definitions are located in the pin_manager.h file.
#define Switch_GetValue() PORTBbits.RB4
#define LED_SetHigh() do { LATAbits.LATA4 = 1; } while(0)
#define LED_SetLow() do { LATAbits.LATA4 = 0; } while(0)
This is a simple example of how the MCC generates a library of useful functions to make creating project code much easier and quicker.
11
Make sure your project has the Curiosity Board selected and the USB cable is connected to the board.
Click on Make and Program Device. This will build the project again and launch the programmer. In the Output window, you should see a series of messages and, if successful, it will end with a "Programming/Verify complete" message and the D2 LED connected to RA4 will be lit when the S1 button on the center on the HPC Curiosity Board is pressed.
Output Window:
Connecting to Starter Kit on Board...
Currently loaded firmware on Starter Kit on Board
Firmware Suite Version.....01.54.00
Firmware type..............Enhanced Midrange
Target voltage detected
Target device PIC16F18875 found.
Device Revision ID = 2002
Device Erased...
Programming...
The following memory area(s) will be programmed:
program memory: start address = 0x0, end address = 0x7ff
configuration memory
Programming/Verify complete
When the project is loaded you will be able to press the switch and see the LED light up.
If it's the first time the programmer is connected to the board, the programming tool may need to download the proper operating firmware for the exact device. You may see a series of processes if this occurs. This should only happen once:
Downloading Firmware…
Downloading bootloader
Bootloader download complete
Programming download…
Downloading RS…
RS download complete
Programming download…
Downloading AP…
AP download complete
Programming download…
Firmware Suite Version…..01.34.11
Firmware type…………..Enhanced Midrange
Download
If you have any problems with your project, the completed MPLAB X project file can be downloaded from the link below:
File | Download |
Installation
Instructions |
||
---|---|---|---|---|
Windows | Linux | Mac OSX | ||
Project 2 Files
|
| | | |