Minimum Viable Program for PIC12LF1572

Kevin Chubb (currently a final-year project student here in DIT) is designing a tiny robot using Microchip’s compact PIC12LF1572 microcontroller. It’s an interesting chip and Kevin’s doing great things with it. I decided to strip back one of the example programs he sent me just to find out exactly what’s required in the code to get a minimal program (flashing LED) working. In this example, I’m just blinking an LED twice a second on pin RA5 (pin 2). Thanks for the example code Kevin!

//
// MVP (minimum viable program) for PIC12LF1572
// Written by Ted Burke based on an example by Kevin Chubb
// Last updated 13-4-2017
//

#include <xc.h>

#pragma config FOSC = INTOSC    // Select INTOSC (internal) oscillator: I/O function on CLKIN pin
#pragma config WDTE = OFF       // Disable Watchdog Timer
#pragma config MCLRE = OFF      // Disable MCLR Pin (MCLR/VPP pin function is digital input)
#pragma config LVP = OFF        // Disable Low-Voltage Programming (High-voltage on MCLR/VPP must be used for programming)

int main(void)
{
    OSCCON = 0b01111010;  // 16 Mhz oscillator (optional)
    TRISAbits.TRISA5 = 0; // Make RA5 an output
    
    while(1)
    {
        LATAbits.LATA5 = 1; // LED on
        _delay(1000000);    // 250ms @ Fosc=16MHz
        LATAbits.LATA5 = 0; // LED off
        _delay(1000000);    // 250ms @ Fosc=16MHz
    }
}

This is the circuit:

I’m using a PICkit 2 programmer in Linux, so I used the pk2cmd command line application to transfer the program to the microcontroller. Unfortunately, the standard device file (PK2DeviceFile.dat) does not include the relatively recent PIC12LF1572, so I needed to download an edited version of the file from here:

http://github.com/GBert/misc/blob/master/pickit2/pk2cmd/pk2cmd/PK2DeviceFile.dat

Once I placed the updated PK2DeviceFile.dat file into my program’s folder, pk2cmd worked perfectly. These were the commands I used to build the code and program the microcontroller:

xc8 --chip=12LF1572 main.c
sudo pk2cmd -PPIC12LF1572 -M -F main.hex -T

If the location of pk2cmd is not included in the path, you’ll probably need to specify its location in the command.

Thai Phan pointed out in one of his comments below that you don’t need to run pk2cmd as root (as I did above, using “sudo”). You can configure udev so that all users have permission to access the PICkit 2. As Thai explained it:

Create a new file /etc/udev/rules.d/99-pickit2.rules containing the following:

SUBSYSTEM==”usb”, ENV{DEVTYPE}==”usb_device”, SYSFS{idVendor}==”04d8″, SYSFS{idProduct}==”0033″, MODE=”0666″

Then restart udev, as follows:

/etc/init.d/udev restart

Unplug your PICkit 2 and then plug it back in. It should now be possible to use pk2cmd without sudo. Thanks for the useful tip Thai Phan!

Finally, Kevin had quite a few additional configuration options at the beginning of his original example program. I’ll list them here because, although they weren’t required to get my example running, some of them presumably would be required in other situations. Here they are:

// Other configuration settings from Kevin's example that I didn't use:
#pragma config BOREN = OFF      // Brown-out Reset Enable (Brown-out Reset disabled)
#pragma config CLKOUTEN = OFF   // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)
#pragma config PWRTE = OFF      // Power-up Timer disable
#pragma config CP = OFF         // Flash Program Memory Code Protection (Program memory code protection is disabled)
#pragma config WRT = OFF        // Flash Memory Self-Write Protection (Write protection off)
#pragma config PLLEN = OFF      // PLL Enable (4x PLL disabled)
#pragma config STVREN = OFF     // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will not cause a Reset)
#pragma config BORV = LO        // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)
#pragma config LPBOREN = OFF    // Low Power Brown-out Reset enable bit (LPBOR is disabled)

Thanks again to Kevin Chubb for the example code.

This entry was posted in Uncategorized and tagged , , , , , , , , , , , , , , . Bookmark the permalink.

8 Responses to Minimum Viable Program for PIC12LF1572

  1. Thái says:

    Oops, there is a mistake in the comment on line 12 of the example. Common mistake I also have because who care about the comment anyway 😀
    Can’t help but notice you use ‘sudo’ for pk2cmd. I guess it is for permission to use USB port. This can be fixed by adding a udev rules for pickit2.

    • Thái says:

      Create a new file /etc/udev/rules.d/99-pickit2.rules containg the following:
      SUBSYSTEM==”usb”, ENV{DEVTYPE}==”usb_device”, SYSFS{idVendor}==”04d8″, SYSFS{idProduct}==”0033″, MODE=”0666″

      Then restart udev
      # /etc/init.d/udev restart

      unplug then plug back in your pickit2.

      I love how pickit2 works.

    • batchloaf says:

      Thanks Thai!

      The comment on line 12 actually came directly from Kevin’s original example code. I guess he just included the labels to explain what each configuration setting was controlling and then switched them on and off to figure out what worked. Anyway, I’ve adjusted the comments on lines 9-12 so that hopefully they’re all consistent now.

      Regarding the udev thing, I’ve never bothered setting it up before because I’m just not that worried about running pk2cmd as root. Of course, your way is better practice. Thanks very much for the clear instructions. I’ve added them to the article above – I hope that’s ok with you?

      I too love the PICkit 2. I think I’ve been using it for about 10 years now, both in my own development work and in classes I’ve been teaching, and it has been an amazing workhorse. I’d love to be able to do some things in Linux that I can do with the PICkit 2 software in Windows (e.g. Logic Analyzer, UART Tool). As a matter of interest, have you found any way to do that?

      Ted

      • Thái says:

        Regarding udev, yes, please use whatever I wrote because I just googled it from someone else on the Internet.

        About pickit2 other function in Linux, I did find something about pickit2 emulating a uart-usb converter but I forgot the link. If I found it again I will definitely post it here.

        I’ve been following your blog since maybe 2 years ago. Great blog Ted!

  2. Thái says:

    Ah, I found these:
    1. pk2-la: pickit2 as logic analyzer.
    Need python-usb package. So just install in from the repo.
    https://sourceforge.net/projects/pk2-la/?source=typ_redirect

    2. pk2serial
    http://zwizwa.be/darcs/staapl/tools/
    To build it: ‘make all’
    But I still haven’t figured out how to use it yet.
    Author comments: ‘Connect to PK2 and start a serial console on stdio. Use this in
    conjunction with socat for pty/socket/… emulation.’

    Hope that’ll help.

    Thai

  3. jaime arevalo says:

    what is the Resistor value to activate the LED, how many miliamperes current use the LED load in RA5 output?

    • batchloaf says:

      You probably want a current of about 10mA through the LED. I suggest a resistor value between about 220 ohms and 470 ohms. You can safely use a higher resistance – it won’t damage anything, but the LED might be too dimly lit.

Leave a comment