This page gives example PIC24 code accompanying the textbook titled
"Microcontrollers: From Assembly to C with the PIC24 Family" by R. Reese, B. Jones and J.W. Bruce to be published by Cengage Learning in December 2008.
The next section discussing using the example code, while the example directory lists all available examples.
First, make sure you've read the
getting started guide, insuring that your hardware and software are correctly configured.
You can place this directory anywhere that you wish; the project files use relative path names.
Documentation starts at docs/index.html. These examples have minimal reliance on the libraries shipped with the PIC24 compiler, and instead use libraries that have been developed by the authors. The new libraries are meant to be more friendly to programmers who are new to the PIC24 Family, as this material and code examples are used in an introductory microprocessors class at Mississippi State University.
Most of the examples are meant for a reference PIC24HJ32GP202 system (some examples in Chapter 13 use the PIC24HJ64GP502, which is pin compatible with the PIC24HJ32GP202 but has additional on-chip peripherals). The projects all have a custom linker file intended for use with a serial bootloader - if you want generated hex files to be compatible with the PICKIT2 programmer then delete the linker file from the MPLAB project. The MPLAB project files do not have workspace files associated with them - this means that double-clicking on a MPLAB project file opens a blank workspace, and you do not see the project files. Instead of double-clicking on a project file, start MPLAB manually, and use 'Project->Open' to open one of the example projects. This creates a new workspace for that project, which you can save when you close the project.
If you have a different PIC24 Family member, simply change the target device in the MPLAB to that device. When you compile the files, you will get warnings that the internal oscillator with PLL is being used, and that the default configuration bit settings are being used.
To change clock options, see the documentation on common/pic24_clockfreq.c and include/pic24_clockfreq.h. To change config bits, see the documentation on common/pic24_configbits.c
All of the examples assume a serial port using UART1; our reference system uses pins RP10 (RX) and RP11 (TX) with a default baudRate of 57600. To change these assignments, edit the function called configUART1() in common/pic24_uart.c. To change the default baudrate, edit the #define DEFAULT_BAUDRATE
in include/pic24_libconfig.h (The file include/pic24_libconfig.h includes all of the macros for configuring user-defineable behavior for library functions).
If the end-of-line (EOL) output behavior is not correct for the serial terminal program that you are using (i.e, printed new lines do not return to the left edge of the screen), then you can change this by selecting an appropriate value for the SERIAL_EOL_DEFAULT macro contained in include/pic24_libconfig.h. The three choices are SERIAL_EOL_CR_LF
, SERIAL_EOL_CR
, or SERIAL_EOL_LF
. By default, the library uses SERIAL_EOL_LF
(an EOL is a LF only).
Our examples also assume an LED tied to port RB15 - this is used as a 'heartbeat' and is blinked by our examples when waiting for input. You can reassign this to another port (see HB_LED) or remove it entirely (see USE_HEARTBEAT) by editing pic24_libconfig.h.
The best project to start with is chap8/reset.mcp - this just assumes serial port functionality. Chapter 8 examples are parallel port I/O, while Chapter 9 gives some interrupt examples. See the following listing for descriptions of individual code examples.
Example code includes:
- Chapter 8 (initial startup, parallel port examples)
- Chapter 9 (interrupts, simple timer usage)
- Chapter 10 (UART, SPI, I2C)
- Chapter 11: Data Conversion (ADC, DAC) subdirectory
- chap11/adc2pots1.c - Demonstrates 2-channel manual sampling with the ADC
- chap11/adc7scan1.c - Samples 7 channels sequentially with automatic channel scanning (only for PIC24 CPUs without DMA).
- chap11/adc7scan2.c - Above plus uses double buffering (only for PIC24 CPUs without DMA).
- chap11/adc4simul.c - Simultaneous sampling of 4 channels (only for PIC24 CPUs without DMA).
- chap11/adc_spidac_test.c - Demonstrates reading the internal ADC in 12-bit mode and then sending the upper 8-bits to an external 8-bit SPI DAC (MAXIM 548A)
- Chapter 12 (Timer coverage - PWM, input capture, output compare, time keeping)
- chap12/incap_freqmeasure.c - Measures the square wave frequency using input capture and Timer2
- chap12/incap_switch_pulse_measure.c - Measures the pulse width of pushbutton switching using input capture and Timer2
- chap12/ir_biphase_decode.c - Decodes bi-phase bitstream from IR remote control as output by an IR receiver. The protocol is Phillips VCR control, 13 bit command (start bit, toggle bit, 5-bit address, 6-bit data). The Timer2 divider must be set such that one bit time does not exceed the timer period.
- chap12/manual_switch_pulse_measure.c - Measures the pulse width of a pushbutton switch using Timer2 without input capture.
- chap12/timer32bit_switch_pulse_measure.c - Measures the pulse width of pushbutton switch using Timer2/3 in 32-bit mode with INT1 for edge detection.
- chap12/outcompare_contpulse.c - Generate an asymmetric square wave using output compare (OC1), continuous pulse mode.
- chap12/outcompare_squarewave.c - Generate a squarewave using output compare (OC1).
- chap12/outputcompare_oneservo.c - Demonstrates pulse width modulation using the OC1 output to control a hobby servo. The ADC input value on AN0 is used to vary the pulse width between its min and maximum values.
- chap12/outputcompare_multiservo.c - Demonstrates pulse width modulation using four digital outputs and the OC1 module to create four PWM outputs for hobby servos. A table is used to control the pulse widths of the four servos.
- chap12/ledpwm.c - Demonstrates pulse width modulation by controlling the intensity of an LED. The ADC input value on AN0 is used to vary the PWM period.
- chap12/pwm_dac.c - Demonstrates a PWM DAC - connect an RC filter on the OC1 output and vary the pulse width of the PWM signal, and monitor the DC value on the capacitor. The RC time constant should be at least 10x greater than the PWM period. Examples values used for testing were R=6.8k, C = 1.0u, PWM period= 500 us
- Chapter 13 Advanced topics (DMA, ECAN, I2C slave, flash programming, comparator)
- Chapter 14 ESOS examples (also see the ESOS section)
- Chapter 15 Capstone examples
Examples converted to be compatible with the Explorer-16, 100-pin demo board
Other examples not tied to textbook Figures:
- chap11/adc_test.c - Demonstrates reading the internal ADC in 10-bit mode and converting it to a voltage.
- chap11/adc_test_12bit.c - Demonstrates reading the internal ADC in 12-bit mode and converting it to a voltage.
- chap11/adc7scan1_dma.c - Samples 7 channels sequentially with automatic channel scanning in scatter/gather mode; uses DMA (only for PIC24 CPUs with DMA).
- chap11/adc7scan1_dma2.c - Samples 7 channels sequentially with automatic channel scanning in ordered mode; uses DMA (only for PIC24 CPUs with DMA).