PIC18F To PIC24F Migration and Performance Enhancement Guide DS00002991A
PIC18F To PIC24F Migration and Performance Enhancement Guide DS00002991A
Migration Considerations the PIC18F and the PIC24F devices are handled auto-
matically by the compiler and do not affect programs
The primary consideration when migrating from written in C.
PIC18F to PIC24F is that the PIC24F core uses FOSC/2
as its instruction clock, as opposed to the FOSC/4
Differences Between PIC18F Devices
instruction clock of the PIC18F device. This effectively
doubles the instruction rate at the same input clock without DMA and PIC18F Devices with DMA
speed, but also changes the base clock used by many There are few differences in the core between older
peripherals that utilize the instruction clock as their and newer PIC18F devices that fundamentally change
basis. This frequency change needs to be accounted the migration path between them or to PIC24F devices.
for when migrating to ensure that peripherals function
as expected. Most other changes in the CPU between
PROGRAM STACK
As discussed in the “CPU Core” section on page 1,
PIC18F devices use a hardware stack for program flow
management. The stack is not memory-mapped and
has a fixed size of 32 levels, but the Top-of-Stack (TOS)
is mapped through the TOSU/H/L and STKPTR SFRs.
PIC24F architecture uses a stack implemented entirely
in mapped data space. The stack begins at 0800h in
Near Data Space, just outside of the SFR area, and
grows towards higher memory addresses using the
W15 register as a dedicated pointer. The size of the
stack is entirely user-defined with the SFR register,
SPLIM, which sets the address for stack overflow traps.
PIC24F devices maintain the same general nomenclature Select the user-assigned priority level for the interrupt
for interrupt bit names as PIC18F devices, with two source by writing the control bits in the appropriate
important differences. Both families maintain interrupt IPCx register. The priority level will depend on the
enable, flag and priority bits that are generically named specific application and type of interrupt source. If
xxxIE, xxxIF and xxxIP (where ‘xxx’ is the mnemonic for multiple priority levels are not desired, the IPCx register
the interrupt source). The first major difference is the control bit for all enabled interrupt sources may be
presence of three interrupt priority bits for each source, programmed to the same non-zero value.
instead of the one used for PIC18F devices. These bits, Note: Upon Reset, all interrupts are assigned a
generically named xxxIP2 through xxxIP0, allow the default priority level of 4.
interrupt to be assigned to one of eight relative priority
levels. The other difference is the number of interrupt
sources. While many interrupts have the same (or very
Disabling User Interrupts
similar) name as PIC18F devices, others are new. Other To disable interrupts on PIC18F devices, it is only
interrupts have similar names but have a different mean- necessary to clear the GIE bit (GIEH or GIEL if priority
ing from their PIC18F counterparts. Users should refer to levels are used).
the appropriate PIC24F device data sheet for a complete
To disable user interrupts on PIC24F devices, these
list of interrupts and their meanings.
steps are required:
Setup and Enabling Interrupts 1. Push the current STATUS Register (SR) value
onto the software stack using the PUSH instruction.
The following are the required steps to set up and 2. Force the CPU to Priority Level 7 by inclusive
enable interrupts on PIC18F devices: ORing the value, 0Eh, with the low byte of the
1. Clear the interrupt flag status bit associated with STATUS Register. To enable user interrupts, the
the peripheral in the associated PIRx or POP instruction may be used to restore the
INTCONx register. previous STATUS Register value.
2. Enable the interrupt source by setting the interrupt Note: The DISI instruction allows interrupts of
enable control bit associated with the source in Priority Levels 1-6 to be disabled for a
the appropriate PIEx or INTCONx register. fixed period of time.
Note: The interrupt flag still needs to be cleared
prior to exiting an ISR. Migration Considerations
Optional steps: PIC18F architecture only has the ability to assign either
high or low-priority interrupts to individual sources.
1. Select the user-assigned priority level for the
PIC24F architecture allows the assignment of multiple
interrupt source by writing to the control bits in
priority levels for interrupts (Priorities 0 through 7 are
the RCON register. Select high priority or low
user-defined and Priorities 8 through 15 are hardware-
priority using the priority bit in the corresponding
defined). At the very least, interrupts in native PIC18F
IPRx register. The interrupt priority feature is
applications will need to be reassessed and their
enabled by setting the IPEN bit (RCON[7]).
priority levels redefined in PIC24F terms.
2. (On newer PIC18F devices) Enable or disable the
Interrupt Vector Table using the MVECEN Config- For both PIC18F and PIC24F devices, the RETFIE
uration bit and configure the IVTBASE register for instruction exits an Interrupt Service Routine (ISR), but
the location of the Interrupt Vector Table. this instruction does behave slightly different depend-
ing on the microcontroller. For PIC18F, this instruction
The following are the required steps to set up and will set the GIE bit to re-enable global interrupts. Since
enable interrupts on PIC24F devices: the GIE bit does not exist for PIC24F, this instruction
1. Set the NSTDIS bit (INTCON1[15]) if nested will restore the previous priority level.
interrupts are not desired.
2. Clear the interrupt flag status bit associated with
the peripheral in the associated IFSx register.
3. Enable the interrupt source by setting the inter-
rupt enable control bit associated with the
source in the appropriate IECx register.
Note: The Interrupt/trap flag still needs to be
cleared prior to exiting an ISR.
Note: The PIC24F UART does not support synchronous communications. If synchronous serial communication
is required, use the SPI module instead.
Migration Considerations PIC18F devices with DMA to PIC24F devices has even
fewer considerations, as most features are shared
The biggest consideration of migration is that timer between the two.
functionality may not be on the same timers between
PIC18F and PIC24F devices. Period registers are on all The newer PIC18F devices also have some new timers
PIC24F timers, rather than only some of the PIC18F for more specific applications. The Timer2/4/6 module on
timers. Prescalers are present on both architectures, but PIC18F devices with DMA is an 8-bit timer with a period
PIC18F prescalers differ by each timer, while PIC24F register, which can be started, run, frozen or reset by
prescalers are the same for each timer. Finally, PIC24F external signals (either from other peripherals or an exter-
timers do not have postscalers, a feature that is common nal pin). The module also has One-Shot and Monostable
on many PIC18F timers. Either a software solution or modes. These timers can be used with the PWM to
different prescaler/period option may be needed in order perform more sophisticated waveform control, such as
to achieve the same timer period on a PIC24F device. pulse density modulation. There are no equivalent timers
on the PIC24F devices.
Differences Between PIC18F Devices PIC18F devices with DMA also have the SMT, a 24-bit
without DMA and PIC18F Devices with DMA timer intended for measuring a variety of digital signal
parameters (such as pulse width, frequency and duty
The PIC18F devices with DMA have many more timer cycle), as well as use as a synchronous timer or asyn-
features than the legacy variants, which make them chronous counter. It also has gated timer features and
match up better with PIC24F devices and make migra- a period match register. There is no equivalent timer on
tion easier. Both Asynchronous and Synchronous the PIC24F devices, although the 32-bit timers on the
modes are now available on all timers, along with 32 kHz PIC24F can perform most of the non-signal measure-
crystal support. More timers have period registers, and ment functions of the SMT (for example, if the SMT is
Timer1, 3 and 5 offer gated timer functionality. These only being used as a large timer).
features mean that migrating generic timer code from
The PIC24F input capture and output compare modules input clock features for these devices. Please refer to
can use either Timer2 or Timer3, where the PIC18F the device data sheet to check the availability of the
modules can use either Timer1 or Timer3. Capture events MCCP or SCCP modules.
can be generated on every rising, falling, 4th rising and
16th rising edge of the ICx pin. Migration Considerations
With Single Compare Match mode selected, both archi-
The PIC24F output compare peripheral clock source is
tectures can select the initial state of the OCx pin. Upon
based on FOSC/2 which differs from the PIC18F FOSC/4
the match, the pin can either transition or toggle. For
clock source. Ensure the equations available in the
each mode and in both architectures, the output
product data sheet are used for the various clock
compare interrupt flag is set.
source calculations.
All of the PIC18F PWM modes are supported by the
PIC24F family. The significant difference is that each Differences Between PIC18F Devices
output compare peripheral can generate only one
without DMA and PIC18F Devices with DMA
output. Therefore, half-bridge support requires two
peripherals and full-bridge support requires four. The primary difference in the CCP module between
PIC24F PWM mode is an extension of the output legacy PIC18F variants and PIC18F devices with DMA
compare peripheral. This mode is similar to the Single is not directly related to the CCP, but instead, to the
Output Compare mode and with the addition of Fault timers. Both the Timer1 and Timer2 have differences in
protection pins, OCFA and OCFB, can stop the pulse them from legacy variants that may affect how the CCP
train; similar to the PIC18F PWM mode. operates, which should be taken into consideration.
PIC18F devices with DMA also have the Compli-
Some of the PIC24F devices have dedicated modules, mentary Waveform Generator (CWG) module which
called MCCP and SCCP, which stands for the Multiple supports PWM with dead band.
or Single Capture/Compare peripheral. The devices
that include these modules have multiple modes of
operation, where they can work as a timer, capture,
compare, as well as a PWM option. There are multiple
Unsupported PIC18F Features • Most of the input buffers for PIC24F devices are
Schmitt Triggers (ST). Verify that the output levels
The PIC24F port architecture does not permit the port of associated components meet the ST input
output to drive the peripheral input. The user must voltage thresholds.
either configure the pin as a peripheral input or port
• Making the pins analog or digital can vary from
output, but not both.
device to device. Some of the PIC18F devices
have this control in the ADC registers. Some of
Migration Considerations the PIC24F devices have a register, called
• Enabling a digital or analog input or output onto a ADxPCFG, to implement the same feature. Some
pin with a configurable open-drain option will not of the PIC18F and PIC24F devices have
cause the pull-up to be automatically disabled. dedicated registers for each pad to make the pins
The pull-up is not disabled if the pin has a configu- analog or digital (ANSELx register).
rable open-drain option. It is important to disable
the pull-up in software when it is not needed. Differences Between PIC18F Devices
• Pins without an analog function can tolerate input without DMA and PIC18F Devices with DMA
voltages up to 5.5V. This can minimize hardware
There are few to no differences between PIC18F
changes when migrating from a PIC18F device. A
devices with DMA and PIC18F devices without DMA in
higher voltage output can be created by adding an
respect to I/O port controls.
external pull-up resistor on the pin and writing a
zero to the data latch. Setting the TRISx bit will
pull the output up to the supply voltage and
clearing the TRISx bit will output a digital zero.
• Drive strength, slew rate and input voltage thresh-
olds can change automatically when a peripheral
is enabled. It is important to review the specific
data sheets for differences between devices.
Migration Considerations often necessary to read all of the Reset flags to deter-
mine the cause of the Reset, while on the PIC24F
All legacy PIC18F Resets are also supported by devices, often only one flag needs to be read).
PIC24F devices (in addition to a few additional Reset
sources). The primary migration concern is in how the
Differences Between PIC18F Devices
PIC24F handles stack overflow/underflow cases. While
these cases trigger a Reset on PIC18F devices, they without DMA and PIC18F Devices with DMA
are instead implemented as traps in the PIC24F PIC18F devices with DMA have two additional Reset
architecture. In theory, this leads to more flexibility in sources not on legacy PIC18F devices without DMA.
handling stack overflow/underflow instances, but appli- The first is a Watchdog Timer Window Violation Reset,
cations that expect a Reset on stack overflow will need which is covered in the “Watchdog Timer” section of
to take this change into account. A secondary migration this document. The second is the Memory Execution
concern is that PIC24F devices do not have a configu- Violation Reset, which occurs if the core attempts to
rable start-up timer, so the timer will need to be execute code from either an address outside
accounted for instead of programmed. The third major implemented program memory or from the specially
consideration is that the polling of Resets is different designated storage area Flash. PIC24F devices do not
between PIC18F devices and PIC24F devices, with have equivalents to either of these Resets. Other than
PIC24F Reset flags being active-high instead of these two additional sources, Resets on PIC18F
PIC18F active-low bits, and the PIC24F Reset bits devices with DMA behave the same as PIC18F devices
being more narrowly defined (on PIC18F devices, it is without DMA.
The PIC24F and PIC18F A/D Converter modules have • When configuring the A/D to use the conversion
similar features. Both have a 10-bit, Successive clock, several factors will affect the PIC24F
Approximation Register (SAR) A/D, capable of using a divider selection. These include the reduced TAD
combination of reference pins (VREF+ and VREF-) and and the instruction rate. The PIC24F conversion
analog power pins (AVDD and AVSS) for the reference clock is based on the instruction clock, TCY/2,
voltages. Both product lines feature an A/D conversion where the PIC18F is based on FOSC/2. Due to
status bit, selectable A/D clock divider, dedicated A/D different instruction rates for a given system clock
RC, auto-sampling with configurable sample time, frequency, the smallest period for the PIC24F A/D
analog/digital input selection and run-time selectable clock divider is one FOSC period and two FOSC
A/D input. Conversions can be initiated by software, an periods for PIC18F.
external interrupt or an output compare event.
Differences Between PIC18F Devices
Migration Considerations without DMA and PIC18F Devices with DMA
• The source impedance for the PIC24F module is PIC18F devices with DMA add a few additional features
2.5 kOhm. Many PIC18F devices are 10 kOhm, to the PIC18F ADC module. First, there are hardware
although 2.5 kOhm is recommended. CVD controls, which allow for better use of the ADC
• For the PIC24F module, the module’s internal module for capacitive touch applications. Second,
sampling capacitor is 4.4 pF, typical; for the there is a hardware calculation engine (ADC features)
PIC18F module, it is 25 pF, typical. The reduced that allows for core-independent hardware averaging
capacitance increases the affect of the external and low-pass filtering of ADC results.
capacitance on the analog input.
• Microchip believes that its family of products is one of the most secure families of its kind on the market today, when used in the
intended manner and under normal conditions.
• There are dishonest and possibly illegal methods used to breach the code protection feature. All of these methods, to our
knowledge, require using the Microchip products in a manner outside the operating specifications contained in Microchip’s Data
Sheets. Most likely, the person doing so is engaged in theft of intellectual property.
• Microchip is willing to work with the customer who is concerned about the integrity of their code.
• Neither Microchip nor any other semiconductor manufacturer can guarantee the security of their code. Code protection does not
mean that we are guaranteeing the product as “unbreakable.”
Code protection is constantly evolving. We at Microchip are committed to continuously improving the code protection features of our
products. Attempts to break Microchip’s code protection feature may be a violation of the Digital Millennium Copyright Act. If such acts
allow unauthorized access to your software or other copyrighted work, you may have a right to sue for relief under that Act.
== ISO/TS 16949 ==