Programming PIC 24Vwith MPLAB X
Programming PIC 24Vwith MPLAB X
a PIC24 in
MPLAB X
I
Table of Contents
Making a Project and Navigating the IDE 1
How Programming a PIC24 Differs from Writing Code in C++ 4
Basics of a PIC24 Program 6
General Good Practices 8
How to Setup Basic PIC24 Connections 10
Configuring and Use the Debugging Tool 12
Configuring Pins as Digital I/O Pins without Change Interrupts 14
Code Example 16
Configuring Pins as Digital I/O Pins with Change Interrupts 17
Code Example 20
About the Analog to Digital Converter 22
Configuring the Analog to Digital Converter without Threshold Detect 24
Code Example 28
Configuring the Analog to Digital Converter with Threshold Detect 30
Code Example 38
Configuring a Timer and Timer Interrupt 41
Code Example 44
Configuring a PWM Signal and Counter Interrupt without Fault Inputs 46
Code Example 48
Configuring a PWM Signal and Counter Interrupt with Fault Inputs 50
Code Example 55
Configuring Input Capture and Input Capture Interrupts 57
Code Example 62
Code Example – HC-SR04 Ultrasonic Range Finder 64
II
1
Choosing an Oscillator
Frequency
An important thing about programming
a PIC24 is the frequency of your oscillator. The
Currently we have no files within our project. frequency of your oscillator divided by two is
To remedy this, we need to make a main.c file. called your cycle frequency. Each iteration of
If we right click on the Source Files folder we the cycle frequency runs one command of
get the following options: code. Each line of code can translate to
different amounts of commands but often it is
about six commands per line. When
programming timers or PWM signals this is
much more important since you are limited in
the PIC24’s memory to how many cycles you
can count and thus how long a timer or PWM
period can be. The options you have are as
follows:
FRC – This is the internal 8 MHz
oscillator
FRCDIV – This refers to the internal 8
MHz oscillator with a postscaler
divider. A postscaler divider means the
frequency of the oscillator will be
reduced.
Selecting the mainXC16.c option we can
LPRC – This is the 31.25 kHz Low-
rename the file to just main.c and hit finish.
Power internal oscillator
From there we should have a file open that
LPFRC – This is the 500 kHz Low Power
looks like the following image:
internal oscillator with a postscaler
divider.
7
To set this up in the code simply place the Writing in the Main Loop
following line before your main loop:
The main loop will loop repeatedly after
#pragma config FNOSC = /**/;
it finishes all commands. While at times this
In place of the /**/ simply place one of the 4
may be advantageous often we want it to run
options from above. If you chose any of the
once and loop only certain parts. To prevent
clock options with a postscaler divider you will
this from happening we just insert the
need an additional line of code. While the clock
following code into our main loop at the end
can only be defined once out of the main loop.
before the return
This line defines the divider and can be
while(1);
changed whenever in the code. This is useful if
you need to change clock signals to This is an infinite loop that has our main loop
temporarily be able to make a PWM signal idle while we may have things going on with
work or get a timer to wait for a certain period interrupts or other code. Now any code we
of time. For the divider use the following line: insert before this command will run once and
then the PIC24 will stay in that infinite loop
_RCDIV = 0bXXX;
until it times out after a few minutes on
Where the XXX is you must choose one of the
inaction or until we reset it.
following values when you are using the
FRCDIV 8MHz oscillator:
111 = 31.25 kHz (divide-by-256)
110 = 125 kHz (divide-by-64)
101 = 250 kHz (divide-by-32)
100 = 500 kHz (divide-by-16)
011 = 1 MHz (divide-by-8)
010 = 2 MHz (divide-by-4)
001 = 4 MHz (divide-by-2) (default)
000 = 8 MHz (divide-by-1)
When you are using the LPFRC 500 kHz
oscillator
111 = 1.95 kHz (divide-by-256)
110 = 7.81 kHz (divide-by-64)
101 = 15.62 kHz (divide-by-32)
100 = 31.25 kHz (divide-by-16)
011 = 62.5 kHz (divide-by-8)
010 = 125 kHz (divide-by-4)
001 = 250 kHz (divide-by-2) (default)
000 = 500 kHz (divide-by-1)
After setting the oscillator for your PIC24 your
code is now ready to be written.
8
This icon programs your PIC24 and opens the This tells you how many breakpoints you have
Debugging tool. Upon programming your available for use with your debugging tool.
PIC24 you should see the following window The PICKit 3 supports three breakpoints.
open up at the bottom of your screen: However, it is important to note that using all
three will result in the inability to reset the
PIC24 programmatically as well as other
features. If you run your debugged code and
reach your breakpoint you should see the two
The output tab is your standard project
things occur. First the screen should show a
warnings. The Breakpoints tab refers to points
green line like this:
that you would like the code to pause at. We
13
Threshold Detect
The analog to digital converter has a
built-in feature for detecting when a pin rises
above a certain value, falls below a certain
value, is within a certain window of values, or
is outside a certain window of values. This
feature can throw an interrupt if desired so
that you can address these changes or can
make it so that you don’t need to store the
value the sensor is at or run any comparison
logic on your pins and rather just look at what
pins tripped.
24
Interrupt }
Where the $ in the handler name is replaced
A counter interrupt occurs after every
with the number of the module you are
pulse from a PWM signal. It can be used to
configuring. The handler must be written with
count the pulses for things like counting steps
that name in order for the PIC24 to properly
on a stepper motor, counting time, or reacting
function. The interrupt flag line must also
to sensors while a PWM signal is being driven.
always be at the top of the interrupt to clear the
To configure it use the following lines of code:
flag.
54
Optional: Using the Counter
Interrupt without a PWM
Output
The output compare module can be
used in PWM mode like a timer interrupt to
run code at consistent intervals and leave the
pin open for other uses. This is done by using
the following line of code:
OCXCON2bits.OCTRIS = 1;
Where X is the number of the module which
you are configuring. This sets the output of the
compare module to be tri-stated, meaning a
high impedance state that effectively removes
it from the other circuits on the PIC24. This
means we can still use the pin as an analog
input or digital I/O pin while using the output
compare module.