Getting a Digital I/O pin to control an Light Emitting Diode (LED) can be a rewarding project in itself. It confirms that you have completed the software build, the hardware build, and were able to program the microcontroller successfully. For this reason this first project will step through a simple example of lighting an LED from a microcontroller pin.
Step by Step Instructions
Using the MPLAB® Code Configurator (MCC) simplifies the setup for the various registers within the device to create a digital output. To demonstrate how to get started, this project controls an LED from a digital output: an LED connected to one of the output pins of the microcontroller will light up.
The project uses:
- PIC16F1769
- PICDEM™ Lab II Development Board
- Power from a 9 V Power Adapter
- PICkit™ 3 Programmer/Debugger
- MPLAB® X IDE
- MPLAB Code Configurator (MCC) plug-in
- MPLAB XC8 Compiler
PICDEM™ Lab II Connections:
- Connect 5 V and Gnd to the power bus of the protoboard.
- Connect 5 V to the Vdd pin of the PIC16F1769
- Connect Gnd to the Vss pin of the PIC16F1769
- Connect a wire from the RA2 pin (pin 17) to the breadboard Row 23
- Connect a wire from the ground bus to breadboard Row 27
- Connect the LED Anode to breadboard Row 23
- Connect the LED Cathode to breadboard Row 24
- Connect a 330 ohm resistor between Row 24 and Row 27
To follow along with these steps, MPLAB X IDE should be open and the Programmer/Debugger connected to both the computer and the development board. The setup was described in the Setup and Installation section of this training module. You should see a screen similar to the one below to move on to Step 1.
1
Create a new "Stand-Alone" project in MPLAB X IDE for a PIC16F1769. Instructions are below if this is your first project.
2
3
Select the peripherals for your project.
In this first project these 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 and then click on the PORTA pin 2 (RA2) Output blue lock symbol. It should turn green. This adds this RA2 I/O pin to the project. Also make sure the Reset pin has the blue lock shown. If it's green then click on the green lock to make it blue. This will make the Reset internal. It should look like the picture here when completed:
5
Close the Pin Manager and then click on the Pin Module selection in the Project Resources section.
The center screen should show RA2 listed on the I/O chart. Click on the output box to make the pin an output (if not checked) and make sure Analog and WPU are not checked (click on them to uncheck them).
6
The System needs to be setup next. Click on the System name in the Project Resources list.
The System section will appear. In this section the oscillator settings and the configuration settings are selected.
Oscillator
- Select the INTOSC from the drop down menu for the System Clock Select.
- Select the 1MHz_HF selection from the Internal Clock drop down menu.
This will enable the internal 1 MHz internal oscillator as the system clock.
Configuration
Each configuration setting can be changed under the Register tab of the System window. Match the selections shown here.
7
Click on the Generate Code button to have the MCC create the software libraries for this project.
8
The project will now include both generated header and source Files. It should also have a generated main.c file.
Note: MCC may offer to generate a main.c file. Click YES to allow it to complete that task.
Double click on main.c to open it up in the editor window.
9
Add the following code to the end of the main.c file:
while (1)
{
IO_RA2_SetHigh(); // Add your application code
}
Note: The IO_RA2_SetHigh() function is a macro that was generated by the MCC and its definition is located in the pin_manager.h file.
#define IO_RA2_SetHigh() do { LATA2 = 1; } while(0)
This is a simple example of how the MCC generates a library of useful macros to make creating project code much easier and quicker.
11
Make sure your project has the programming tool selected and power is supplied to your development board.
The PICkit 3 has limited power capability so we recommend powering the board separately.
The ICD 3 can power a development board, but for the sake of simplicity, we recommend powering the board separately.
The Real ICE cannot power the development board so powering the board separately is required.
Click on the Make and Program Device icon. 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 and Verify Successful message and the LED connected to RA2 should be lit on the PICDEM Lab II Development Board.
Connecting to MPLAB PICkit 3...
Firmware Suite Version.....01.34.11
Firmware type..............Enhanced Midrange
Target detected
Device ID Revision = 6
The following memory area(s) will be programmed:
program memory: start address = 0x0, end address = 0x7ff
configuration memory
Device Erased...
Programming...
Programming/Verify complete
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
12
The project can be closed in MPLAB X IDE. The project is saved automatically when it is built, but any changes to files or configuration may ask to be saved before the project is closed.
The project can be closed under the File Menu > Close Project
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 1 Files
|
| | | |