Textbook examples¶
This page gives example PIC24 code accompanying the second edition of textbook titled “Microcontrollers: From Assembly to C with the PIC24 Family”, by B. Jones, R. Reese, and J.W. Bruce published by Cengage Learning in August 2014.
The next section discussing using the example code, while the example code directory lists all available examples.
Using the example code¶
First, make sure you’ve read the getting started guide, insuring that your hardware and software are correctly configured.
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 dsPIC33EP128GP502 system. 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 PICKit 3 programmer then delete the linker file from the MPLAB X 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 in 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 baud rate of 230,400 baud. To change these assignments, edit the function called configUART1()
in common/pic24_uart.c. To change the default baudrate, edit the #define DEFAULT_BAUDRATE
macro in include/pic24_libconfig.h (this file 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. By default, the library signals an EOL using a line feed 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 include/pic24_libconfig.h.
The best project to start with is reset.c - this only assumes basic serial port functionality.
Example code directory¶
Chapter 3: translation from C arithmetic to assembly¶
Chapter 4: Unsigned 8/16-bit arithmetic¶
Chapter 6: Pointers and subroutines¶
- countOnes.c - Counts the number of 1 bits in u16_v
- countOnes_assem.s - Counts the number of 1 bits in a word
- fib.c - A recursive subroutine example to compute a Fibonnachi number
- fib_register.s - A recursive subroutine example to compute a Fibonnachi number
- fib_stack.s - A recursive subroutine example using the stack to compute a Fibonnachi number
- psv_intarray.s - Initialize array from program memory (PIC24H/dsPIC33F)
- psv_intarray-24E33E.s - Initialize array from program memory (PIC24E/dsPIC33E)
- psv_upcase.s - Initialize string from program memory (PIC24H/dsPIC33F)
- psv_upcase_24E33E.s - Initialize string from program memory (PIC24E/dsPIC33E)
- swapU32.s - Swap two U32 values referenced by a pointer
- upcase.s - Change a string to uppercase
- upcase_cversion.c - Change a string to uppercase
Chapter 7: Advanced assembly language - higher math¶
- aschex2byte.c
- aschex2byte_asm.s
- bcd_test.s
- bytes2aschex.c
- bytes2aschex_asm.s
- div_asm_tests.s
- float_bench.c - A simple program that benchmarks float vs u32 operations
- float_encoding.s
- mult_32x32_uns - Demonstrate unsigned 32 bit multiplication
- mult_asm_tests - test some multiply combinations
- sat_add.c - Demonstrate saturating addition
Chapter 8: initial startup, parallel port examples¶
- ledflash_nomacros.c - Flashes an LED, does not use I/O macros.
- ledflash.c - Flashes an LED, uses I/O macros.
- echo.c - Inputs chararacters from UART RX1, echos back chars+1.
- asm_echo.s – Demonstrates calling C from assembly
- reset.c - Demonstrates software reset, idle, sleep, watchdog timer operation.
- ledtoggle.c - Uses an FSM approach to toggle an LED whenever a pushbutton input is pressed and released.
- ledsw1.c - Demonstrates using a FSM approach for a LED/switch IO problem.
- lcd4bit.c - Character LCD interface example
- lcd4bit_4lines.c - Demonstrates a 4-line 20x4 Character LCD
Chapter 9: interrupts, simple timer usage¶
- trap_test.c - Code which causes a math error (divide by zero) trap
- trap_test_handled.c - contains an ISR which handles the math error (divide by zero)
- change_wakeup.c - wake from sleep using a change notification interrupt
- change_latency.c - measures ISR latency using a change notification interrupt
- int1_wakeup.c - Uses INT1 to wake from sleep mode.
- squarewave.c - Generates a square wave using timer 2 and an ISR.
- filt_test.c - test an extern low-pass RC filter effectiveness by generating a pulse train that is read by another port
- softfilt_test.c - Implements a software filter for input pulses less than a specified duration.
- ledsw1_sampling.c - Example of implementing a FSM using intput sampling
- ledsw1_cn.c - Example of implementing a FSM using a timer and change notification interrupt.
- ledsw1_cn_revised.c - Example of implementing a FSM using a timer and change notification interrupt.
- rot_enc.c - A 2-bit incremental Gray code rotary encoder example
- keypad.c - Implements a 4x3 key scanned keypad interface.
- rot_enc_trace.c - Demonstrates use of an ISR trace buffer for capturing the states of a rotary encoder.
Extras¶
These programs demonstrate related concepts, but are not directly discussed in the textbook.
- change_bounce.c - uses a change notification interrupt to detect switch bounce
- change_test.c - demos a change notification interrupt using an input pushbutton
- int0_bounce.c - use the INT0 interrupt to detect switch bounce.
- int0_wakeup.c - uses INT0 to wake from sleep mode.
- int1_bounce.c - use the INT1 interrupt to detect switch bounce.
- ledflash_timer.c - uses a periodic timer interrupt to flash an LED.
- ledtoggle_timer.c - toggle an LED using a periodic interrupt to poll the switch input
Chapter 10 (UART, SPI, I2C)¶
- soft_uart.c - Demonstrates software-driven UART TX/RX using PIO pins
- reverse_string.c - Inputs a string, outputs the reverse
- mcp41xxx_spi_pot.c - SPI example: PIC24 uC in Master mode to Microchip MCP41xxx Digital Potentiometer
- ds1722_spi_tempsense.c - SPI example: PIC24 uC in Master mode to Maxim DS1722 Digital Thermometer
- spi_master_revstring.c - SPI master uses slave to reverse strings
- spi_slave_revstring.c - SPI slave reverse a string provided by a master
- ds1621_i2c.c - PIC24 uC in Master mode to Maxim DS1621 Digital Thermometer using I2C
- ds1631_i2c.c - PIC24 uC in Master mode to Maxim DS1631 Digital Thermometer using I2C
- mcp24lc515_i2c_eeprom.c - PIC24 uC in Master mode to Microchip 24LC515 EEPROM using I2C
Extras¶
These programs demonstrate related concepts, but are not directly discussed in the textbook.
Chapter 11: Data Conversion - ADC, DAC¶
- adc2pots1.c - Demonstrates 2-channel manual sampling with the ADC
- adc7scan1.c - Samples 7 channels sequentially with automatic channel scanning (only for PIC24 CPUs without DMA)
- adc7scan2.c - Above plus uses double buffering (only for PIC24 CPUs without DMA)
- adc4simul.c - Simultaneous sampling of 4 channels (only for PIC24 CPUs without DMA)
- 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)
- adc_test.c - Demonstrates reading the internal ADC in 10-bit mode and converting it to a voltage
- adc_test_12bit.c - Demonstrates reading the internal ADC in 12-bit mode and converting it to a voltage
- adc7scan1_dma_scatter_gather_1.c - Samples 7 channels sequentially with automatic channel scanning in scatter/gather mode
- adc7scan1_dma_conv_order.c - Samples 7 channels sequentially with automatic channel scanning in ordered mode
- adc4simul_dma.c - Simultaneous sampling of 4 channels (only for PIC24 CPUs with DMA)
- dac_r2r.c - Create a sine wave using a DAC
Chapter 12: Timer coverage - PWM, input capture, output compare, time keeping¶
- incap_freqmeasure.c - Uses IC1 to measure period, uses averaging mode.
- incap_switch_pulse_measure.c - Uses 16-bit input capture to measure pulse width
- ir_biphase_decode.c - Decodes IR bi-phase bitstream from IR remote control
- manual_switch_pulse_measure.c - Uses 16-bit timer to measure pulse width
- timer32bit_switch_pulse_measure.c - Uses 32-bit timer to measure pulse width
- outcompare_contpulse.c - Generate an asymmetric square wave using output compare (OC1), continuous pulse mode
- outcompare_squarewave.c - generates a square wave using output compare (PIC24F/H/E families)
- outcompare_squarewave_24e.c - generates a square wave using output compare (dsPIC33E/PIC24E only)
- outputcompare_oneservo.c - Demonstrates servo control using output compare for PWM. Pulse width is set by the ADC input.
- outcompare_multiservo.c - Demonstrates pulse width modulation using four digital outputs and the OC1 module to create four PWM outputs for hobby servos.
- 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.
- pwm_dac.c - Demonstrates a PWM DAC
- rtcc.c - Demonstrates use of the Real Time Clock Calendar Module with an external 32.768 kHZ crystal for timekeeping.
Extras¶
These programs demonstrate related concepts, but are not directly discussed in the textbook.
- incap_32bit_switch_pulse_measure.c - Uses IC1,IC2 in cascade mode to measure pulse width
- 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.
- pwm_dac.c - Demonstrates a PWM DAC
- time1_sosc.c - Demonstrates use of Timer1 with a secondary oscillator.
Chapter 13: Advanced topics - DMA, ECAN, I2C slave, flash programming, comparator¶
- comparator_example.c - Illustrates a simple use of the comparator module
- dma_example.c - Uses DMA in ping-pong mode to capture data from the UART, write it to the 24LC515 EEPROM
- ecan_example_nofifo.c - Illustrate CAN transmit, receive
- ecan_example_fifo.c - Illustrate CAN transmit, receive
- ecan_example_fifo_eid.c - Illustrate CAN transmit, receive
- flash_example.c - Illustrates run time self programming of the flash memory
- i2c_master_reverse_string.c - Master in pair of files that shows a PIC24 I2C master talking to a PIC24 I2C slave
- i2c_slave_reverse_string.c - Slave in pair of files that shows a PIC24 I2C master talking to a PIC24 I2C slave
- i2c_multmaster_rstring.c - Two PIC24 uCs communicate with each other over I2C, acting both as master and slave devices
Extras¶
These programs demonstrate related concepts, but are not directly discussed in the textbook.
Chapter 14: ESOS examples¶
- esos_skel.c - basic ESOS template
- app_flashled.c - flash an LED
- app_echo1.c - ESOS version of the echo.c program
- app_reverse.c - demonstrates a user task that reverses a string
- app_timerLEDecho.c - flash an LED using a software timer
- app_semaphore1.c - demonstrates use of semaphores for signaling synchronization
- app_semaphore2.c - demonstrates rendezvous synchronization with semaphores
- app_childtask.c - demonstrates child tasks
- app_irqs.c - demostrates IRQ handling in ESOS
- app_ds1631.c - I2C example using the DS1631
- app_ds1722.c - SPI example using the DS1722
Examples converted to be compatible with the Explorer-16, 100-pin demo board¶
- reset.c - Demonstrates software reset, idle, sleep, watchdog timer operation
- lcd4bit.c - Character LCD interface example
- adc_test.c - Demonstrates reading the internal ADC in 10-bit mode and converting it to a voltage
- rtcc.c - Demonstrates using the secondary oscillator with a 32768 Hz oscillator
- rtcc.c - Demonstrates use of the Real Time Clock Calendar Module with an external 32.768 kHZ crystal for timekeeping.
- mcp25lc256_spi_eeprom.c - Demonstrate SPI usage
Examples using the data transfer protocol¶
The following programs use the data transfer protocol, providing working examples of its usage.