My first experiments with PIC Microcontrollers was on the PIC32MX170F256B. But after discovering that there was no USB module on that controller, I then purchased a PIC32MX270F256B which did have one.
This article serves as a reminder for the minimum setup for this microcontroller.
First, take a look at the Datasheet for the PIC32MX270F256B.
Here is the overall layout on the breadboard:
Note the following pins on the Microcontroller:
Next, a couple of LEDs for later testing:
On both the Power and Ground rails on the breadboard, add a 0.1uf capacitor each.
Also between Pin 20 (CAP) and Pin 19 (VSS) add a 0.01uf capacitor.
Add a 10k pull up resistor from Pin 1 (MCLR) to Power. MCLR is Master Clear or Reset.
That should be the minimum required to test the microcontroller.
You can also get a diagram of the recommended setup on page 28 of the PIC32MX270F256B Datasheet, Figure 2-1.
Attach a USB cable between the PICkit3 and the computer's USB port.
Read
to see if PICkit3 can find the microcontroller.The Output should result in:
Programmer to target power is enabled - VDD = 3.300000 volts. Target device PIC32MX270F256B found.
A simple first test is to light an LED from RA1
which is pin 3 on the microcontroller.
In the MPLABX IDE, create a new C project by doing the following:
New Project > Microchip Embedded > Standalone Project Next > Family: 32-bit MCUs (PIC32) Device: PIC32MX270F256B Select Tool: PICkit3 Next > Select Compiler: XC32 Compiler Project Name: MyTestProject (or anything really) (keep all defaults) Finish Under ''Source Files'': New > C Main File Finish
Click the Build Spanner icon to test the project build. Should build successfully.
Change the whole c file to contain:
#ifdef __XC32 #include <xc.h> #endif #include <stdio.h> #include <stdlib.h> int main(int argc, char** argv) { TRISA = 0b00000000; //all PortA pins are output PORTA = 0b00000010; //set pin 3 (RA1) to on. (supply 3.3 volts) return (EXIT_SUCCESS); }
The #include <xc.h>
line includes defined variables like PORTA
and TRISA
.
A nice simple test. TRISA
means all the A pins (RA0, RA1, RA2 etc). Setting each bit to 0 in this way means that every pin will be set as an output pin. This means that voltage can be output from the pin to power something.
PORTA
is the way of turning on or off that voltage. So in the above code, only pin 3 (RA1) will be supplied with 3.3 volts. All the other pins will be 0 volts.
As we did earlier on the breadboard, you have a wire from RA1
, to a 100Ohm resistor, to an LED and then to ground.
Compile the code. A hex file will be created at:
project\dist\<my project name>\production\myfile.hex
Switch back to the MPLABX IPE and browse for your hex file. Program it to the chip by clicking the Program
button.
The LED should light up. Good stuff. Your development flow is complete.
To blink back and forth between the two leds, change the code to:
#ifdef __XC32 #include <xc.h> #endif #include <stdio.h> #include <stdlib.h> #include <plib.h> int main(int argc, char** argv) { TRISA = 0b00000000; //all PortA pins are output PORTA = 0b00000001; //set pin 3 (RA1) to on. (supply 3.3 volts) int count = 0; while(1) { if (count >= 500000){ mPORTAToggleBits(BIT_0); mPORTAToggleBits(BIT_1); count = 0; } count++; } return (EXIT_SUCCESS); }
Notice plib.h
is included. This supports defines like mPORTAToggleBits
. All this changes in later versions. This document will be updated at some point.
And that gives a basic project that will alternate between flashing LEDs.
The following shows what happens, or what could happen if part of the minimum set up is not complete. This is only what I have noticed, the effects could be greater for others.
Component | During Programming | For Execution |
---|---|---|
Not using a pull up on MCLR to Positive | No effects | Could cause random resets on the PIC if the pin is floating. |
Pull up on MCLR to Negative | Cannot program | - |
No 0.1uf bypass capacitors | Unstable effects | No noticeable effect on basic test |
No External Oscillator | Not required | Internal can be used |
No AVDD on Pin 28 | PIC can't be found | ? |
No AVSS on Pin 27 | PIC can't be found | Executes normally |
No Cap between VCAP on 20 to 21 to GND | ? | Executes normally |
No VDD on Pin 13 | ? | Executes normally |
No VSS on Pin 8 | ? | Executes normally |
No PGEC1 connected | Cannot connect to target device | ? |
No PGED1 connected | Cannot connect to target device | ? |