Bus Pirate v3 PIC 24FJ blinking LED programming tutorial

From DP

Revision as of 12:33, 26 March 2012 by Arakis (Talk | contribs)
Jump to: navigation , search

Basic get started Demo. Use the Bus Pirate V3.x to blink the user programmable MODE LED

Contents

Overview

The Bus Pirate uses the PIC24FJ64GA002 microcontroller, and as such it can be used as a development board for the same, with some limitations. One of the pins is connected directly to the MODE LED and in this demo we will write just the basic code that is needed to blink that LED.

In the text below, the code is broken up and explained section by section. If you want you can download the full MPLAP 8 project, or copy/paste the full code for main.c that will be displayed at the bottom of the page.

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 code

Defines 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;

C30 Source Code