You are on page 1of 17

Microchip Minutes: blink Application Lab

Contents
References: ..................................................................................................................... 1
Prerequisites ................................................................................................................... 2
Overview ......................................................................................................................... 2
Equipment Needed.......................................................................................................... 2
Software ....................................................................................................................... 2
Hardware ..................................................................................................................... 2
Procedure........................................................................................................................ 4
Create a new project: ................................................................................................... 4
Setup MPLAB® Code Configurator (MCC) to Generate Code...................................... 8
Adding Code to main.c ............................................................................................... 13
Download Project to Curiosity Board ......................................................................... 16
Result ......................................................................................................................... 17

References:
 PIC16F1619 Datasheet:
http://www.microchip.com/wwwproducts/Devices.aspx?product=PIC16F1619
 MPLAB® X IDE User’s Guide: www.microchip.com/mplabx see Documentation
section at bottom of the page.
o User’s Guide direct download:
http://www.microchip.com/mymicrochip/filehandler.aspx?ddocname=en55
6757
 Microchip Minutes: helloWorld Application Lab:
o http://www.microchip.com/stellent/groups/SiteComm_sg/documents/Devic
eDoc/en574949.pdf

1
Prerequisites
This lab assumes that the reader is familiar with introductory concepts covered in
Microchip Minutes: helloWorld Application Lab document and video including:

 Setting up a project in MPLAB X IDE


 Installed MPLAB Code Configurator

Overview
The purpose of this lab is to introduce basic concepts of how to set up a project in
MPLAB® X IDE while using the MPLAB® Code configurator to blink an LED.

Equipment Needed
This lab was developed using the following tools:

Software
 MPLAB X Integrated Development Environment: www.microchip.com/mplabx
 MPLAB XC8 C Compiler: www.microchip.com/xc8
 MPLAB Code Configurator: www.microchip.com/mcc

Hardware
This lab was completed using the Curiosity Development Board
(www.microchip.com/curiosity) and its 4 user LEDs connected as follows:

 D7 to pin 5 (RC5)
 D6 to pin 17 (RA2)
 D5 to pin 18 (RA1/PGEC)
 D4 to pin 2 (RA5)

2
All LEDs are connected with anodes (positive connector) through a current limiting 1kΩ
resistor to the MCU pin and cathodes (negative connector) to VSS. Therefore, to light an
LED, the pin connected must be driven HIGH (Logic 1). To turn off an LED, the
connected pin must be driven LOW (Logic 0).

In order to “blink” the LED, the connected pin is driven LOW-to-HIGH (Toggled HIGH)
and then HIGH-to-LOW (Toggled LOW) repeatedly. A delay is used between toggles to
slow the process so that it is visible to the human eye.

3
The application can be defined by the following flowchart:

Procedure
Create a new project:
1. Open MPLAB X IDE
2. Create a new project by clicking the New Project icon or by selecting
File>New Project…

3. In the New Project window select Microchip Embedded in the Categories:


frame and Standalone Project in the Projects: frame and click Next.

4
4. Select the Mid-Range 8-bit MCUs (PIC10/12/16/MCP) in the Family dropdown
in the Select Device window and then select PIC16F1619 in the Device
dropdown. Click Next>.

5. Select the tool that will be used to download code to the PIC16F1619 device. In
this case, select the Curiosity board in the Select Tool window and click Next>.

5
6. Next, select the XC8 compiler in the Select Compiler window and click Next>.

7. Select a project location and name the project to something meaningful such as
blink. Be sure that the check box next to Set as main project is selected and
click Finish.

Note: it is best to keep the path as short as possible when creating project
locations.

6
8. The MPLAB X IDE workspace should look similar to below. If not, common
windows such as Projects can be opened through Windows>Projects etc…

7
Setup MPLAB Code Configurator (MCC) to Generate Code
1. Open MPLAB Code Configurator (MCC) from Tools>Embedded>MPLAB Code
Configurator.

2. Opening the MCC for the first time will display an overlay describing key sections of
the workspace including Project Resources, Device Resources and Composer Area.
The reader is encouraged to take a moment to familiarize themselves with these
sections. The overlay can be closed by hitting the ESC key and reopened anytime
by selecting the button.

Other buttons in the MCC include:

Accesses the MCC Homepage and Forums


Accesses Options to customize how the MCC behaves. These labs will use the
default configuration

3. In the Project Resources area, select the System resource to open the user
interface in the composer area.

The system user interface is where core device operational parameters are setup
such as oscillator selection and enabling or disabling features such as brown-out
reset, windowed-watchdog use and so on.

8
Expand the System Clock Select drop-down in the System user interface and
select INTOSC to configure the system to use an internal oscillator on the
PIC16F1619. The oscillator frequency can be selected by expanding the Interval
Clock drop-down in the Frequency Select section of the user interface. This lab will
use the default 500 KHz internal oscillator setting.

4. The Curiosity Demo Board has an on-board programmer/debugger that programs


the populated MCU using what’s called “Low-Voltage Programming Mode”. The
PIC® MCU must be configured to use this mode by expanding the CONFIG2 then
Low-Voltage Programming Enable [LVP OFF] drop-down in the System user
interface and selecting the Low-voltage programming enabled radio button.

9
5. A General Purpose I/O pin will be driven HIGH and then LOW repeatedly to blink
the LED connected to it. In the Device Resource Area, locate and expand the GPIO
drop-down and double-click on the GPIO::GPIO selection to add to the Project
Resources Area.

6. Select GPIO::GPIO in the Project Resources Area to open the GPIO peripheral
user interface in the Composer Area.

Note that an additional area appears called the MPLAB Code Configurator Pin
Manager. This area provides a graphical representation of the selected device along
with available pins for the selected peripheral in blue. Pins are assigned by selecting
the desired pin in the lower section of the area.

10
7. Ensure that the appropriate package is selected in the Pin Manager using the
Package drop-down menu. This lab utilizes the Curiosity Development board
populated with a PIC16F1619 in 20-pin PDIP package.

8. Select one of the pins connected to an LED on the Curiosity Demo Board by clicking
on the appropriate pin in the lower Pin Manager window. Here RC5 (pin 5)
connected to LED D7 is selected. Note the changes in the Pin Manager with the pin
selected now appearing green with a lock icon .

11
9. Pin names can be changed by the user so that they are easier to identify. In the
Composer Area, configure the I/O pin as an output by checking the appropriate box
and change the name to something meaningful such as LED7.

10. Next, click on the Generate Code button in the Composer Area to generate the
necessary C code that corresponds to the configuration setup in the MCC.

Note that the MCC will ask if the user would like to generate a main.c file if one
doesn’t already exist. In this instance, click Yes.

12
The Output – MPLAB Code Configurator window at the bottom of MPLAB X IDE
should indicate that code has been generated successfully. If the Output window is
not visible it can be opened by selecting Window>Output>Output.

Adding Code to main.c


1. Expand the blink project tree in the Projects pane on the left side of the MPLAB® X
IDE workspace. Expand the Header and Source folders to reveal the MCC
Generated Files corresponding to the configuration done in the previous steps.

13
2. In the Header Files folder, locate the pin_manager.h file and double-click to open.
Scroll through the file and note the various macros, defines and function prototypes
that can be used. Also note the names of the macros and defines using the LED7
unique pin name configured in Step 8.

Copy the LED7_Toggle() a macro that will be used to toggle the pin LED7 from
HIGH/LOW or LOW/HIGH each time it is called.

14
3. Under the Source Files folder, double-click the main.c file to open. Scroll down
through the opened file and locate the while(1) loop. In the section marked //Add
your application code paste the LED7_Toggle(); copied in the previous step
making sure to add the semi-colon.

4. In order to actually see the LED transition ON/OFF a delay will be needed between
LED7_Toggle() executions. Here a convenient library function included with the
Microchip MPLAB XC8 compiler will be used. Open MPLAB XC8 help file by
selecting Help>Help Contents. This will open the main help window. In the Contents
pane, locate the list of available libraries by navigating to Language Tools>XC8
Toolchain>MPLAB XC8 C Compiler>Library Functions to find the __delay_ms(x)
function.

15
This function can be used to generate a delay for a user defined number of
milliseconds.
Returning to the main.c file, add a delay of 500 mS as shown below

Download Project to Curiosity Board


1. Connect the J2 USB-mini connector using a USB cable to an available USB port on
the computer.

2. MPLAB X IDE will need to be set-up so that Low-Voltage Programming Mode is


used.

To configure the IDE, select File>Project Properties(blink) to open the Project


Properties window.

3. Select Curiosity Demo Board in the Categories pane. In the Option categories
drop-down menu, select Program Options.

16
Check the box next to Enable Low Voltage Programming in the dialogue and then
OK to apply and close the dialogue.

4. To compile and download to the PIC16F1619, click the Make and Program Device
button in MPLAB X IDE.

Result
If configured correctly, the LED D7 on the Curiosity Demo Board should now flash
ON/OFF at 500 mS intervals.

17

You might also like