As requested by ImA4Wheelr:
PIC quickstart guide
This guide will assume some basic microcontroller knowledge, as a primer on micros in general would be a much bigger topic, so this is limited to getting started on the PIC architecture specifically. If you have worked with another manufacturer’s µC and would like to try PIC, it is very easy to get started.
Toolchain:
Microchip provides the IDE for free, as well as a free of the C compiler (which doesn’t generate code that’s as efficient as the paid version, but generally is good enough). For simplicity, I (and many engineers I’ve talked to) prefer the old version, MPLAB 8, available here. The current IDE is MPLAB X, available here. I’ve used MPLAB X and don’t like it, but you can still accomplish what you need to do with it. Don’t install both versions on one computer as they use different drivers for the programming tools, which will cause you problems.
For hobbyists, the programmer of choice is the PICkit 3, which costs about $50. You can get the previous version for cheaper, but you may not be able to work with the newer PIC parts, so it’s worth going straight to 3. Other programmers exist but are more expensive.
Programming interface:
PIC parts use a proprietary protocol for ICSP (in circuit serial programming), and it’s the same protocol for any PIC part. You need 5 connections: power, ground, mclr, data, clock. If your part is powered in the system, you can get away with just 4 of those. If you want to be able to program in-system, don’t put high loads or capacitance on the data and clock pins, and generally don’t put anything on mclr. During programming, 12V is applied to mclr and it can mess up other circuitry if you are using that pin for something else. Mclr is always input-only on PICs.
Microcontrollers:
Microchip.com has a good compare tool to choose a part. If you’re coding in C, I suggest choosing from the enhanced mid-range (PIC16Fxxxx where there are 4 digits after the F instead of 3). These are their newer parts which work better with C, have better peripherals, and are usually cheaper than the older equivalents. The 10F line is 6-pin parts, which are great for space-constrained applications but usually must be programmed in assembly. The 12F line is 8-pin parts, choose one with 4 digits after the F to get a newer part that will handle C code better.
For flashlights, the important peripheral to look for is called CCP or ECCP ([enhanced] capture compare PWM) which you use to generate PWM signals. Some parts have more than 1 of these modules to generate multiple independent PWMs.
Other peripherals applicable to flashlight drivers are EEPROM memory and ADC to read analog signals.
My go-to parts are:
• PIC10F322 (6-pin) for tiny stuff (assembly)
• PIC12F617 (8-pin) for intermediate (assembly)
• PIC16F1824 and 1825 (14-pin) for heavy-duty (c code)
• PIC16F1829 (20-pin) for high pin count stuff (c code)
Attributes specific to PIC:
• There is only one accumulator (if you are working in c, it doesn’t matter)
• All RAM is directly accessible (if you are working in c, it doesn’t matter)
• Device is set up with something called a configuration word (equivalent to fuses on AVR), sets up options like code protect, oscillator, watchdog timer
• Instructions are 4x pipelined. Even if you don’t know what pipelining is, there’s a very important takeaway: instructions run at ¼ the speed of the quoted CPU frequency. If your PIC is running at 4MHz, it is executing instructions at 1MHz (unlike AVR, which is 1 to 1)
• There is only one interrupt vector (until you get up to bigger 18F parts), so you have to check the flags to see which ISR to handle
Getting started:
- Download the IDE, and check the box to get the XC8 compiler too (if you don’t see this, download the compiler).
- Buy a PICkit 3 and write the pinout it with a marker.
- Get a 1x5 header and solder flying leads to it so it’s easy to connect your PICkit to a breadboard.
- Buy some bare DIP-package PIC parts, or get one of my drivers to play with.
- Start with existing code and play with it—it will be a long and frustrating process to start from scratch.
- Run the project wizard and select your PIC
- Add the source file, the htc.h file (in program files>microchip>xc8>ver xx>include) and the part-specific .h file (example p16f1824.h, in same directory) to the project
- Press build and hope it works
- Choose PICkit3 under programmer, and change the settings so it outputs 5V to your target
- Connect to your PIC and click program
- Ask questions here
If you are starting a project, let me know what your PIC part is and I can show you an applicable source file to use as a starting point. I have tons of c code examples but I think they would clutter this post if I put them right in it.
As an assembly example, here’s the code for my Romisen RGBWUV lights
Hope this helps
EDIT: I guess I should show some motivation for learning all of this boring stuff! Here are a few lights I’ve programmed:
Nitecore D10 custom UI (assembly language)
Novatac 120P with MELDv2 driver: (C language)
Skyray King with RGBWUV and custom UI: (assembly language)