Bus Pirate v3 PIC 24FJ blinking LED programming tutorial
From DP
Contents |
Overview
The Bus Pirate is a handy debugging tool, but it's also a 16 bit PIC 24FJ64GA002 development board. You don't even need a programmer to try your hand at PIC programming, just upload the firmware over USB using the Bus Pirate's nearly-impossible-to-brick bootloader. Grab Microchip's free (but not open) IDE and C30 compiler, then dive in.
This demo is step one. It shows how to setup the chip to run from the internal clock, and blink the MODE LED. The code is broken up and explained section by section.
Stuff to get
Old (windows only)
- MPLAB8 IDE
- C30 compiler for 16bit PICs
New (Linux, Mac, Windows)
- MPLAB X IDE
- XC compiler
Basic Configuration
#include <p24fxxxx.h>Includes the C30 compiler's basic deffinitions for the pic24F microcontrollers
_CONFIG1( JTAGEN_OFF & //JTAG port is disabled GCP_OFF & //GSP Memory Code Protection OFF GWRP_OFF & //GCC Flash Write Protection OFF COE_OFF & // FWDTEN_OFF & //Watchdog Timer OFF ICS_PGx1) //debug over PGD1 and PGC1
The CONFIG1 registrar sets up the MCU to the following specs:
- JTAG is turned off
- General Segment Program Memory Code Protection is off
- General Segment Code Flash Write Protection is off
- (don't know)
- Watchdog Timer is off
- Debugging is handled by the PGD1 and PGC1 pins
CONFIG2( FNOSC_FRCPLL & //Internal FRC with PLL OSCIOFNC_ON & //RA3 is clk out (fosc/2) POSCMOD_NONE & //Primary oscillator disabled I2C1SEL_PRI) //Use default SCL1/SDA1 pins
The CONFIG2 registrar handles the oscillator selections, we set it up so that the MCU uses the internal FRC oscillator and PLL to drive the processor clock at ...
- Internal Fast RC oscillator is used as clock base
- Processor clock (32 MHz), divided by 2 (16 MHz),is outputted onto the RA3 pin
- Primary external oscillator is not used.
- Default pins are used for MSSP module
#pragma codeDefines that the following is the begging of the program code.
Blink the MODE LED
int main(void) { CLKDIVbits.RCDIV0=0; //clock divider to 0 AD1PCFG = 0xFFFF; // Default all pins to digital OSCCONbits.SOSCEN=0; //Disables the secondary oscilator TRISAbits.TRISA1 = 0; //sets the Mode LED pin RA1 as output LATAbits.LATA1 = 0; //turns LED off unsigned long int i;
This is the main function, it is the starting function every processor executes. First we will setup the MCU by seting and clearing a few register bits. C30 bit variables are accessed with this syntax [name of register]bits.[name od bit]. These names follow the naming found in the datasheet.
In the first line we set the clock divider to 0, by setting the RCDIV0 bit of the CLKDIV registrar to 0. Next all the pins are set to be digital, by setting all the bits in the AD1PCFG register to 1s. As a final in main configuration we disable the secondary oscillator by clearing the SOSCEN bit in the OSCCON register.
Next we set the RA1 pin which is connected to the MODE LED to be an output pin by setting the TRISA1 bit of the TRISA register. The LATA port A latch registar is used to set or clear port A pin values. By clearing the LATA1 bit in the LATA register we turn off the MODE LED.
i is a unsigned long integer variable we will use in a delay function.
while(1) { i =0xFFFFF; //sets i to 1048575 LATAbits.LATA1 = ~PORTAbits.RA1; //togles the MODE LED while (i--); //delay function } //decrements i until 0 }
The while(1) sets up a contiunous loop of the code containd in it's {} brackets. The variable i is set to count down from 1048575 (0xFFFFF). The MODE LED is toggled by reading its last value from the RA1 bit of the PORTA register. The read value is inverted and set to the A1 pin latch. The second while loop simply decrements the i variable until it hits 0, after which it exits it and goes to the beginning of the previous loop.
Using the bootloader to upload your demos
There are two ways to get code on the Bus Pirate.
- Load it over USB with the bootloader and (Pirate-Loader or ds30 Loader
- Connect a PIC programmer to the ICSP pins (we recommend ICD2, ICD3, PICkit2, PICkit3, or clones)
This demo will work with either method.
Exporting firmware for the bootloader
If you plan on using a programmer to upload your demo firmware into the Bus Pirate you can skip this part.
The Bus Pirate comes with its own bootloader, which can be used to program the demo firmware. To do this add this linker script to your demo project by:
- Downloading the p24FJ64GA002.gld file into your project folder.
- Adding this file to the Linker Script folder in your MPLAB 8 project.
Now you can proceed to build your project. After compiling is completed go to File/Export:
- Check the Program Memory checkbox
- Uncheck the Configuration Bits checkbox
- Enter the End memmory address as 0xa7f
- Hit OK
- Save the *.hex file where you can find it later
Uploading the firmware
Follow these guides to upload your firmware through the boot loader.
C30 Source Code
#include <p24fxxxx.h> _CONFIG1( JTAGEN_OFF & //JTAG port is disabled GCP_OFF & //GSP Memory Code Protection OFF GWRP_OFF & //GCC Flash Write Protection OFF COE_OFF & // FWDTEN_OFF & //Watchdog Timer OFF ICS_PGx1) //debug over PGD1 and PGC1 _CONFIG2( FNOSC_FRCPLL & //Internal FRC with PLL OSCIOFNC_ON & //RA3 is clk out (fosc/2) POSCMOD_NONE & //Primary oscillator disabled I2C1SEL_PRI) //Use default SCL1/SDA1 pins #pragma code int main(void) { CLKDIVbits.RCDIV0=0; //clock divider to 0 AD1PCFG = 0xFFFF; // Default all pins to digital OSCCONbits.SOSCEN=0; //Disables the secondary oscilator TRISAbits.TRISA1 = 0; //sets the Mode LED pin RA1 as output LATAbits.LATA1 = 0; //turns LED off unsigned long int i; ////////////////////////////////////////////////////////////////// ///FOREVER LOOP/////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////// while(1) { i =0xFFFFF; //sets i to 1048575 LATAbits.LATA1 = ~PORTAbits.RA1; //togles the MODE LED while (i--); //delay function } //decrements i until 0 }
