This page contains examples of four program tasks using an enhanced mid-range PIC® microcontroller (MCU). Each example page shows the C language code needed to complete the task.
These examples were created with MPLAB® X IDE. Information on the IDE, project, and configuration bit settings for each of these examples is given below.
Resources Used in Example Programs
The example programs were designed to be executed on Microchip's F1 Evaluation Kit (part number DV164132).
This kit includes:
- The F1 Evaluation Kit (part number DM164130-1).
- The PICkit™ 3 programmer/debugger (part number PG164130).
The partial block diagram of the F1 Evaluation Kit illustrates the connections used by the example programs:
|PORTD Bit1 (RD1)||Digital||Output||drives LED1|
|PORTE Bit2 (RE2)||Digital||Output||drives LED2|
|PORTE Bit1 (RE1)||Digital||Output||drives LED3|
|PORTE Bit0 (RE0)||Digital||Output||drives LED4|
|PORTD Bit2 (RD2)||Digital||Input||switch 1 (SW1)|
VDD is supplied through the PICkit 3 connector. In order to execute the example code, you must configure the PICkit 3 to supply the power to the target.
The MPLAB®X IDE projects used in the sample programs share a similar structure.
The sample projects are simple applications that demonstrate one feature of the MCU architecture.
The C language version of the examples has two files:
- config.h containing the configuration bit settings
- main.c containing all the code
Examples in MPASM™ only have one source file (main.asm) included in the project.
Project Properties Settings
The projects used in the example programs all use:
- PIC16LF1937 as the processor
- PICkit™ 3 as the hardware tool
Projects in C use the MPLAB XC8 Compiler (v1.21 or higher).
MPASM projects use the latest version of MPASM as the software tool.
A primary concern when setting the configuration bits for any application is the clock source. The example programs were written for a development board that does not have an external clock source for the MCU. As a result, the configuration bits direct the PIC16LF1937 to use the internal RC oscillator combined with the 4x PLL as the clock source.
No other configuration-enabled features are needed by the example programs.
For the example programs written in assembly language the configuration bits are coded in-line using the __CONFIG directive:
For example, in programs written in C the configuration bit settings are provided in a file called configs.h.
The file configs.h is located in the project directory and loaded into the project with an #include directive from main.c
More information on configuration bit settings can be found on the Enhanced Mid-Range Tutorial.
Processing an interrupt
This examples uses a Timer2 interrupt to toggle a pattern on two LEDs. To accomplish this task the application does the following:
- Initialize I/O ports
- Initialize Timer2 to generate a periodic interrupt
- Write an interrupt service routine to toggle the LEDs
- Enable Timer2 interrupts
Processing Multiple Interrupts
This example uses a Timer2 interrupt to blink the LEDs. The time interval used by Timer2 is determined by the value of the analog potentimeter connected to AN8. The conversion routine collecting the value on AN8 is triggered by an interrupt. This sample program does the following:
- Initializes the I/O ports
- Internalizes Timer2 and the ADC modules
- Implements Timer2 and ADC interrupt service routines
- Enables interrupts
main() and Interrupt Service Routine
QUESTION: Rather than have the Timer2 ISR start the next analog-to-digital conversion, wouldn't it make more sense to have the ADC ISR start a conversion?
ANSWER: In this example, the Timer2 period is such that several thousand A-D conversions could occur between timer interrupts. Since only the last ADC result is placed in PR2, all other ADC values captured in a period would not be used. Having the Timer ISR restart the ADC reduces the number of interrupts.
The files for the labs can be downloaded below:
| Windows|| Linux|| Mac OSX|
Project and Source Files
To run the demonstration programs you will need to have MPLAB X IDE and MPLAB XC8 C Compiler installed on your computer. The projects can be run on the F1 Evaluation board with the PICkit 3 or by using MPLAB X IDE's software simulator.
To install the lab files, unzip the downloaded file into any directory on your computer.
The "Getting Started" document in the downloaded project files shows how to install the compiler and MPLAB X IDE.
Configuring the Demonstration Programs
When installed, the lab files contain MPLAB X projects for each demonstration.
The demo projects have all been configured to use MPLAB X IDE's internal simulator as the hardware tool. You have the option of selecting the F1 Evaluation Kit as the simulation tool. Changing the default settings can be done from the Project Properties window for each of the projects.
Running the Demonstration Programs
You can run the demonstration project using MPLAB X IDE. You will need to open the project and run them in Debug mode. If you are using the F1 Evaluation kit with a PICkit 3 you have the option of pushing the Program Target Project button to see the project run on the target hardware.