Bus Pirate v3 PIC 24FJ blinking LED programming tutorial

From DP

Revision as of 12:58, 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;

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;
      LATAbits.LATA1 = ~PORTAbits.RA1;   
      while (i)
      {
         Nop();
         i--;
      }
       }
}

C30 Source Code