This page contains examples of four program tasks using an enhanced mid-range PIC® MCU. Each example page shows both the assembly and 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 board (part number DM164130-1).
- The PICKit3™ programmer/debugger (part number PG164130).
The partial block diagram of the F1 Evaluation board 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 Bit1 (RE0)||Digital||Output||drives LED3|
|PORTD Bit2 (RD2)||Digital||Input||switch 1 (SW1)|
Vdd is supplied through the PICKit3 connector. In order to execute the example code you must configure the PICKit3 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.
C language version of the examples have two files:
- config.h containing the configuration bit settings
- main.c containing all the code
Examples in MPASM have only 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 which 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 programs written in C, the configuration bit settings are provided in a file called configs.h.
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.
This example program turns on LED1 by outputting a logical '1' to RD1 (i.e. PORT D, bit 1).
This example program takes the input from RD2 (PORT D, bit2) and sends it on the output pin RE2 (PORT E, bit2)
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
- Enable 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"
Included page "_download:labs" does not exist (create it now)
To run the demonstration programs you will need to have the MPLAB®X IDE and the MPLAB®XC8 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. Windows users have the option of using the Proteus VSM simulator to simulate the program.
To install the lab files simply 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, MPLAB X IDE and Proteus.
Configuring the Demonstration Programs
When installed the labs 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 board as the simulation tool. If running Windows you also have the option of selecting LabCenter's Proteus VSM as the simulator. Changing the default settings can be done from the Project Properties Window for each of the projects.
Running the Demonstration Programs
The demonstration project can be run using MPLAB X IDE. You will need to open the project and run them in debug mode. If using the F1 Evaluation kit/with a PICKit3 you have the option of pushing the "Program Target Project" button to see the project run on the target hardware.