Login - Register -

Getting Started with PIC and MPLAB

Components

This project requires the PIC chip, two LEDS (plus limiting resistors), one button.

Wiring the Circuit

To be able to program the chip you need to connect the serial board as below.

  • Pin 1 Vdd - Connect to 5v power
  • Pin 14 Vss - Connect to Gnd
  • Pin 6 RC4/TX - Connect to Rx
  • Pin 5 RC5/RX - Connect to Tx
  • Pin 4 MCLR - Connect to Gnd via a Button (optional)

This will allow us to use the Bootloader software on the PC.

Now connect the items we will use for this project:

  • Pin 10 RC0 - To a Green LED - to a 220R resistor - to ground.
  • Pin 9 RC1 - To a Red LED - to a 220R resistor - to ground.
  • Pin 2 RA5 - To a Button - to ground.

Creating the Project

  1. First step is to make sure you have installed MPLAB-X and XC8, both available from the Microchip website (See References page)
  2. Now launch MPLabX IDE and select Create New Project, Categories: Microchip Embedded, Projects: Standalone Project
  3. Select Device: Family: Mid-Range 8-bit MCUs, Device: PIC16F1455
  4. Debug Header: None, Select Tool: Simulator, Select Compiler: XC8
  5. Now you can give your project a name. e.g. cointoss

Creating the source file

  1. Right click on Source Files on the Projects tab, New, C Main File.
  2. You need to add the following include files: xc.h, pic16f1455.h, stdint.h

Configure the CPU

We need to select the clock speed that the CPU will run at, and tell the compiler what we have chosen so that it can correctly compute delay loops later.

This is where you start to need the Datasheet for the cpu (see references page), Section 5.0 Oscillator Mode is the part we need, looking at 5.9 Register definitions on Page 75 we can see the values we might need to set in the OSCCON register.

At the top of the main() function add code to select a 4MHz clock.

This can either be done by setting all of the bits at once, e.g. OSCCON = 0b00110100;

or we can set only the part of the register we care about, OSCCONbits.IRCF = 0b1101;

Tell the compiler that speed

#define _XTAL_FREQ 4000000

Selecting IO Pins

Chapter 12.0 I/O Ports - We need to choose some pins to connect our LEDs and button. Let us use pins RC0, RC1 (pin 10 & 9) for the LEDs, RA5 (pin 2) for the button.

Enable weak pull-ups WPUEN bit of OPTION_REG (Chapter 19.2 page 185)

For the input pin, set to input mode (TRISA), turn the Weak Pull-up on (WPUA), page 132 on.

For the output pins, turn analog off (ANSELC), set to output mode (TRISC), page 140 on.

The main routine

Now loop waiting for the button to be pressed (PORTAbits.RA5 == 0) when it does, fetch the next random number, if the lowest bit is 0 turn on RC0 and off RC1. of bit is 1 set the opposite. repeat.

Generating random numbers

The following routine returns a 16bit pseudo-random number.

 
uint16_t dorand(void)
{
    static uint16_t lfsr = 0xACE1u;
    lfsr = (lfsr >> 1) ^ (-(lfsr & 1u) & 0xB400u);

    return lfsr;
}

Testing the result

Now you need to write your program into your chip, when you Build Project in MPLAB one of the many things it will output in its dist/ directory is a .hex file. this is the finished code, and what the bootloader requires.

With your board all wired up and plugged in, run: boot.exe -v myproject.hex it should prompt you to hit reset on your dev board, and if all is correct it will identify the chip and write your program. A few seconds after writing the chip will reset and start running your program.

Extra Credit

To make button presses more reliable, check that it has reached a stable state before and after being pressed, like this...

 
#define CHECK_MSEC 5        // sample key every 5 mS
#define PRESS_MSEC 10       // stable before presses
#define RELEASE_MSEC 100    // stable before released

void KeyPress(void)
{
    uint8_t count = PRESS_MSEC / CHECK_MSEC;

    while (count > 0) {
        if (PORTAbits.RA5 == 0)
            count--;
        else
            count = PRESS_MSEC / CHECK_MSEC;
        __delay_ms(CHECK_MSEC);
    }
    count = RELEASE_MSEC / CHECK_MSEC;
    while (count > 0) {
        if (PORTAbits.RA5 == 1)
            count--;
        else
            count = RELEASE_MSEC / CHECK_MSEC;
        __delay_ms(CHECK_MSEC);
    }
}
- Last change October 18, 2013, at 04:15 PM
- Registered in England and Wales 08777436