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.
