tterev3's PIC quickstart guide

71 posts / 0 new
Last post

Pages

tterev3
Offline
Last seen: 1 month 3 weeks ago
Joined: 03/28/2014 - 11:22
Posts: 253
Location: NC, USA
tterev3's PIC quickstart guide

As requested by ImA4Wheelr:

ImA4Wheelr wrote:
It would be awesome if you could create a thread giving a high level how to on PIC programming.

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:
1. Download the IDE, and check the box to get the XC8 compiler too (if you don’t see this, download the compiler).
2. Buy a PICkit 3 and write the pinout it with a marker.
3. Get a 1×5 header and solder flying leads to it so it’s easy to connect your PICkit to a breadboard.
4. Buy some bare DIP-package PIC parts, or get one of my drivers to play with.
5. Start with existing code and play with it—it will be a long and frustrating process to start from scratch.
6. Run the project wizard and select your PIC
7. 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
8. Press build and hope it works
9. Choose PICkit3 under programmer, and change the settings so it outputs 5V to your target
10. Connect to your PIC and click program
11. 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)

Edited by: tterev3 on 04/17/2014 - 19:17
nickelflipper
nickelflipper's picture
Offline
Last seen: 6 days 20 hours ago
Joined: 10/20/2012 - 09:31
Posts: 491
Location: Oregon

Yay, another PIC person comes out of the closet Smile . I’ll be using one in my BLF contest entry.

Kent

ImA4Wheelr
Offline
Last seen: 5 days 20 hours ago
Joined: 02/03/2013 - 14:51
Posts: 7915
Location: SC

Thank you very much tterev3.  Great write up.  Just what I needed to start digging into the subject of PIC flashing/programming.  When I tried to research it in the past, I just couldn't seem to find any clear info.  Started to wonder if PIC's are only programmed in China or something.  I'm sure I just didn't know enough to conduct effective searches.  Time to start studying.

Also gotta decide if I want to invest $50.  I have so many drivers and devices with PIC MCU's that I think it may be worth it.

EDIT: Just finished reading your write up and checking out some of the links.  Your source code for you RGBWUV driver is fricken crazy. So elaborate and complex to my untrained eye.  I see your source is in Assembly language.  It appears to share some of the same commands and structure as C.  So I have a opinion question. 

I have limited programming knowledge/experience (Basic, Cobol, and JCL in college and very light C just playing around on my own).  So essentially, I'm still a blank page.  I don't see myself designing anything beyond single emitter (one color) FW.  Since it may be just as easy (or should I hard) to learn Assembly as C, which would you recommend I try to learn?

tterev3
Offline
Last seen: 1 month 3 weeks ago
Joined: 03/28/2014 - 11:22
Posts: 253
Location: NC, USA
ImA4Wheelr wrote:
Since it may be just as easy (or should I hard) to learn Assembly as C, which would you recommend I try to learn?

Definitely go with C. Writing assembly is a hard-earned skill that is valuable for squeezing the most performance out of a tiny, cheap processor, but it has tons of disadvantages compared to high level languages like C. In the case of flashlight (or other) hobbies, it’s no big deal to buy a $2 microcontroller instead of a 50¢ one, since we’re only building a handful. I learned assembly designing high-volume products where we had to squeeze every penny, so I’m very comfortable with it now, and that’s the only reason I use it so much in my hobbies.

WarHawk-AVG
WarHawk-AVG's picture
Offline
Last seen: 6 days 10 hours ago
Joined: 01/04/2014 - 06:47
Posts: 5071
Location: H-Town

ImA4Wheelr wrote:

Thank you very much tterev3.  Great write up.  Just what I needed to start digging into the subject of PIC flashing/programming.  When I tried to research it in the past, I just couldn’t seem to find any clear info.  Started to wonder if PIC’s are only programmed in China or something.  I’m sure I just didn’t know enough to conduct effective searches.  Time to start studying.

Also gotta decide if I want to invest $50.  I have so many drivers and devices with PIC MCU’s that I think it may be worth it.

EDIT: Just finished reading your write up and checking out some of the links.  Your source code for you RGBWUV driver is fricken crazy. So elaborate and complex to my untrained eye.  I see your source is in Assembly language.  It appears to share some of the same commands and structure as C.  So I have a opinion question. 

I have limited programming knowledge/experience (Basic, Cobol, and JCL in college and very light C just playing around on my own).  So essentially, I’m still a blank page.  I don’t see myself designing anything beyond single emitter (one color) FW.  Since it may be just as easy (or should I hard) to learn Assembly as C, which would you recommend I try to learn?


better yet, get some PIC guys to get the stuff from China…see if they can get the firmware and either reproduce it or make custom firmware with better modes and better PWM frequencies
Take what they have and make em better
ImA4Wheelr
Offline
Last seen: 5 days 20 hours ago
Joined: 02/03/2013 - 14:51
Posts: 7915
Location: SC

Thank you tterev3.  C it is then.

That's kind of the plan WarHawk.  I have so many "discarded" PIC drivers.  Some of them l like except for the mode spacing and next mode memory.

ImA4Wheelr
Offline
Last seen: 5 days 20 hours ago
Joined: 02/03/2013 - 14:51
Posts: 7915
Location: SC

Still way early in the learning stages.  I was looking at your Ultimate Flashlight Code post that you made in your blog back in October 2012.  That the PIC12F1822 data sheet is interesting.  Compared the Attiny13A data sheet, it looks more attractive to my untrained eyes.  Has twice the RAM and 4 times the EPROM memory.  Standby power usage is super low (20nA vs 24uA).

Would you still use the PIC12F1822 MCU if you were to update your D10 or EX11?


Sirius9
Sirius9's picture
Offline
Last seen: 8 hours 3 min ago
Joined: 03/04/2012 - 19:23
Posts: 4581
Location: South Europe

You can get PICkit3 from BG for US$20.30 (even cheaper with BLF coupon code)… but I still like Atmel more Silly programmer is like 5 bucks or less…

 

blfdemigod
blfdemigod's picture
Offline
Last seen: 1 year 10 months ago
Joined: 10/03/2013 - 17:55
Posts: 520
Location: San Antonio, TX

tterev3 wrote:
Skyray King with RGBWUV and custom UI: (assembly language)

Wow man, I would love to have an SRK like this! Amazing work! Need to get some chinese manufacturer to make this, and call it the SRK - RGBWUV Edition!

And would be cool with a 7 XM-L2 SRK, make 3 LED's white, then the other 4 LED's red, green, blue, and UV!

Edit: Is it possible to sell me one of these drivers already made for the SRK?

My collection 2 Small

Disclosure:

Sirius9
Sirius9's picture
Offline
Last seen: 8 hours 3 min ago
Joined: 03/04/2012 - 19:23
Posts: 4581
Location: South Europe
blfdemigod wrote:

Need to get some chinese manufacturer to make this, and call it the SRKRGBWUV Edition!

And would be cool with a 7 XM-L2 SRK, make 3 LED’s white, then the other 4 LED’s red, green, blue, and UV!

But the question is: would you pay 60-70$ for something like that?

 

jacktheclipper
jacktheclipper's picture
Offline
Last seen: 50 min 9 sec ago
Joined: 10/31/2010 - 21:18
Posts: 4858
Location: Florida , U.S.A.

Welcome to the BLF , and thanks for sharing your knowledge , tteverv3 .

Very impressive mod , sir .

ImA4Wheelr
Offline
Last seen: 5 days 20 hours ago
Joined: 02/03/2013 - 14:51
Posts: 7915
Location: SC

Sirius9 wrote:

You can get PICkit3 from BG for US$20.30 (even cheaper with BLF coupon code)…

Nice find.  Which code works for this item?

Sirius9
Sirius9's picture
Offline
Last seen: 8 hours 3 min ago
Joined: 03/04/2012 - 19:23
Posts: 4581
Location: South Europe

ImA4Wheelr wrote:

Sirius9 wrote:

You can get PICkit3 from BG for US$20.30 (even cheaper with BLF coupon code)…

Nice find.  Which code works for this item?


Good question, I forgot that blf only works for flashlight and batteries

 

ImA4Wheelr
Offline
Last seen: 5 days 20 hours ago
Joined: 02/03/2013 - 14:51
Posts: 7915
Location: SC

Still a very good find.  At $20, I'm in.  Thank you Sirius9.  Smile

blfdemigod
blfdemigod's picture
Offline
Last seen: 1 year 10 months ago
Joined: 10/03/2013 - 17:55
Posts: 520
Location: San Antonio, TX

Sirius9 wrote:
blfdemigod wrote:

Need to get some chinese manufacturer to make this, and call it the SRK - RGBWUV Edition!

And would be cool with a 7 XM-L2 SRK, make 3 LED's white, then the other 4 LED's red, green, blue, and UV!

But the question is: would you pay 60-70$ for something like that?

Hmmm, actually I probaby would pay $60 for a flashlight with this kind of mod. Would be an amazing flashlight with dedicated XP-E's in red, blue, green, and UV, then have 3 XM-L2 U2 1D, that would be amazing! Ya, for $60 I would definitely get it!

My collection 2 Small

Disclosure:

WarHawk-AVG
WarHawk-AVG's picture
Offline
Last seen: 6 days 10 hours ago
Joined: 01/04/2014 - 06:47
Posts: 5071
Location: H-Town

blfdemigod wrote:

Sirius9 wrote:
blfdemigod wrote:

Need to get some chinese manufacturer to make this, and call it the SRKRGBWUV Edition!

And would be cool with a 7 XM-L2 SRK, make 3 LED’s white, then the other 4 LED’s red, green, blue, and UV!

But the question is: would you pay 60-70$ for something like that?

Hmmm, actually I probaby would pay $60 for a flashlight with this kind of mod. Would be an amazing flashlight with dedicated XP-E’s in red, blue, green, and UV, then have 3 XM-L2 U2 1D, that would be amazing! Ya, for $60 I would definitely get it!

You and me both!

The BLF SRKRGBWUVKLMNOXYZ Edition or “The dial a light” Big Smile

tterev3
Offline
Last seen: 1 month 3 weeks ago
Joined: 03/28/2014 - 11:22
Posts: 253
Location: NC, USA
ImA4Wheelr wrote:
Would you still use the PIC12F1822 MCU if you were to update your D10 or EX11?

Yes, the 12F1822 is the perfect choice for writing in C on a tiny 8-pin part. I haven’t used them much in my mods simply because my work throws away lower-level parts (12F617) by the boatload, so I use those since I hate to see them go to landfill. If you pursue a project with the 12F1822 we would be able to port pieces of my MELD code right over to it (if that’s the kind of functionality you’re looking for)

ImA4Wheelr
Offline
Last seen: 5 days 20 hours ago
Joined: 02/03/2013 - 14:51
Posts: 7915
Location: SC

Thank you again tterev3.  I purchased the flasher you recommended above.  So I will be flashing and that MCU you discussed is very affordable.  I think I will first attempt to adapt my favorite Attiny13a FW to the 12F1822 first.  I have to study to the 12F1822 datasheet more so that I can figure out what adjustments I would need to make to the code.

MELD looks like an incredible program.  It just has so much more functionality that I feel I should tackle at this time.  I really like your concept of having all those colors and UV in one light.  I have separate, green, red and UV lights.  It would be really cool to have all that available in one light.  It's just too much for me at the moment.  I need baby steps.

Thanks again for your very gracious sharing of your knowledge and efforts. Smile

tterev3
Offline
Last seen: 1 month 3 weeks ago
Joined: 03/28/2014 - 11:22
Posts: 253
Location: NC, USA

Here's an example program I put together for the 1822. It's a very simple flashlight with 3 brightnesses and a randomized strobe that changes modes on power cycle. This should help you get started with the 1822: 

//v0 4/21/2014 by Everett
	//initial version
	//simple flashlight controller. mode change on power cycle
	

#include <htc.h>
#include <pic12f1822.h>

#define _XTAL_FREQ 8000000

__CONFIG(FOSC_INTOSC & WDTE_SWDTEN & PWRTE_ON & MCLRE_ON & CP_OFF & CPD_OFF & BOREN_ON & CLKOUTEN_OFF & IESO_OFF & FCMEN_OFF);
__CONFIG(WRT_BOOT & PLLEN_OFF & STVREN_ON & BORV_LO & LVP_OFF);


persistent unsigned char mode;	//must be declared persistent for ram retention trick to work
enum mode{
	max=0,
	med=1,
	low=2,
	strobe=3,
};
#define max_mode 3
#define default_mode 0

#define on_time 15	//milliseconds

unsigned int strobe_timer;
unsigned char strobe_position;
bit on_off;
persistent unsigned int key;

void delayms(int milliseconds);
void configure(void);
unsigned char stun_rate_lookup(unsigned char input);

void interrupt isr(void)
{
	if(TMR1IF){
		TMR1IF=0;
		}

	if(TMR0IF){	//fires at 1kHz
		TMR0IF=0;
		if(strobe_timer){strobe_timer--;}	//count down milliseconds
	}
}




void main(void)
{
	configure();	//set up hardware peripherals
	
	delayms(15);	//short delay to avoid power glitches incrementing mode
	
	if(key==12345){		//RAM retention trick to detect quick power cycles
		mode++;			//go to next mode
		if(mode>max_mode){mode=0;}
	}	
	else{			//long power loss. default to first mode
		mode=default_mode;
		key=12345;
	}		


	switch(mode){	//initialize current mode
		default:
		case max:
			CCPR1L=255;
			break;
		case med:
			CCPR1L=25;
			break;
		case low:
			CCPR1L=1;
			break;
		case strobe:
			strobe_timer=0;
			on_off=0;
			CCPR1L=0;
			break;
	}		

	
	GIE=1;	//turn on interrupts

	while(1){
		
		if(mode==strobe){	//no other modes need active tasks while running
			if(strobe_timer==0){	//timer ran out
				on_off=~on_off;	//flip it
				if(on_off){
					CCPR1L=255;
					strobe_timer=on_time;	//set on time
				}
				else{
					CCPR1L=0;
					strobe_position++;
					strobe_timer=stun_rate_lookup(strobe_position);	//set off time
				}	//set output
			}	
		}
		
		
		
	}	
}


unsigned char stun_rate_lookup(unsigned char input)
{
	static const char table[72]=28,30,33,31,12,24,28,33,29,23,19,33,33,23,14,26,9,18,15,29,15,10,19,31,9,27,33,10,15,26,16,28,30,24,14,23,10,24,30,10,23,31,25,33,9,15,19,17,19,18,32,23,33,17,31,13,31,18,20,8,24,33,17,21,20,14,9,20,26,16,22,16;
	if(input>71){input-=71;}
	if(input>71){input-=71;}
	if(input>71){input-=71;}
	return table[input];	
}	



void delayms(int milliseconds)
{
	while(milliseconds!=0){ __delay_ms(1); milliseconds--;}
}



void configure(void)
{
	INTCON=0b01100000;
	PIR1=0;
	PIR2=0;
	T1CON=0b00110001;
	T2CON=0b00000101; 
	PR2=255; 
	TMR2=0;
		
	LATA=0;
	TRISA=0b11111011;
	ANSELA=0b00000000;	//
	WPUA=0b11111011;
	APFCON=0;

	OSCCON=0b01110011;	//8MHz
	PIE1=0b00000000;
	PIE2=0;
	OPTION_REG=0b00000010;	//8 prescale for 1ms interrupts

//	FVRCON=0b11000001;	//1.024v to adc
//	ADCON0=0b00001101;	//
//	ADCON1=0b00010000;	//1/8, left justify, vref from vdd

	CCPR1L=0;
	CCP1CON=0b00001100;
}


__EEPROM_DATA(0,0,0,0,0,0,0,0); 

__EEPROM_DATA(0, 0, 0, 0, 0, 0, 0, 0);
__EEPROM_DATA(0, 0, 0, 0, 0, 0, 0, 0);

__EEPROM_DATA('1','8','2','2',' ','f','l','a');
__EEPROM_DATA('s','h','l','i','g','h','t',' ');
__EEPROM_DATA('b','y',' ','E','v','e','r','e');
__EEPROM_DATA('t','t',' ','e','v','e','r','e');
__EEPROM_DATA('t','t','.','b','r','a','d','f');
__EEPROM_DATA('o','r','d','@','g','m','a','i');
__EEPROM_DATA('l','.','c','o','m',' ',' ',' ');
sinner
sinner's picture
Offline
Last seen: 22 hours 8 min ago
Joined: 05/14/2013 - 18:33
Posts: 328
Location: UAE

Simply amazing , great work tterev3 these mode settings are off the charts on the epic scale.

ImA4Wheelr
Offline
Last seen: 5 days 20 hours ago
Joined: 02/03/2013 - 14:51
Posts: 7915
Location: SC

Thank you.  Very good idea to start with code that works since someone new to PIC programing/flashing (like me) will probably be dealing with other first time issues.  One less potential complication.  Looks like the code is for a normal clickie type switch (on/off, half click to interrupt power).  Is that correct?

tterev3
Offline
Last seen: 1 month 3 weeks ago
Joined: 03/28/2014 - 11:22
Posts: 253
Location: NC, USA
ImA4Wheelr wrote:
Looks like the code is for a normal clickie type switch (on/off, half click to interrupt power).  Is that correct?

That’s right, it will work with any light that interrupts power to cycle modes. The pwm output is on pin 5

ImA4Wheelr
Offline
Last seen: 5 days 20 hours ago
Joined: 02/03/2013 - 14:51
Posts: 7915
Location: SC

Received my programmer today.  Excited to start going down this path, but I will have to be patient as I have quite a few demands for my time right now, plus the Scratch Build Contest.

Mattaus
Mattaus's picture
Offline
Last seen: 1 year 8 months ago
Joined: 10/20/2012 - 23:51
Posts: 833
Location: Brisbane, Australia

Thanks for the write up. In my brief experimentation with PIC, I found MPLAB-8 much clearer than MPLAB-X, and that was coming into it with ZERO experience. I might have to start using my PICKit 3 again...

baterija
baterija's picture
Offline
Last seen: 4 years 1 month ago
Joined: 03/25/2010 - 03:06
Posts: 223

Great info …on your third day. I’m going to go in to the corner and shudder remembering the one time I had to not just have a basic understanding of assembler on the mainframe but actually got stuck having to get in the code.

Mattaus
Mattaus's picture
Offline
Last seen: 1 year 8 months ago
Joined: 10/20/2012 - 23:51
Posts: 833
Location: Brisbane, Australia

I'm doing a bit of reading and I'm really interested in using that little 6-pin PIC10F322. I've been looking at making a really small 3 mode flashlight driver for years, but don't want to use QFN parts. The PIC10F322 is actually small enough for my project! Obviously it can do PWM out, and I'm assuming it can accept a voltage divider input for battery monitoring (on it's single input-only pin). However, can I by any chance use the internal voltage reference to estimate the supply voltage (less parts required)?

nickelflipper
nickelflipper's picture
Offline
Last seen: 6 days 20 hours ago
Joined: 10/20/2012 - 09:31
Posts: 491
Location: Oregon

Yes, an a-d conversion, with no resistor divider solution, per this thread http://budgetlightforum.com/node/20407

Kent

Mattaus
Mattaus's picture
Offline
Last seen: 1 year 8 months ago
Joined: 10/20/2012 - 23:51
Posts: 833
Location: Brisbane, Australia

Oh, sweet. Thanks.

Another question. I note in the OP that ttreve3 lists assembly beside the PIC10F322. Does this mean it will only work with assembly code, or does it simply mean there probably won't be enough room to use C on it? I was going to take the code supplied in post #18, remove the strobe and see if I could get that working on the PIC10F322.

nickelflipper
nickelflipper's picture
Offline
Last seen: 6 days 20 hours ago
Joined: 10/20/2012 - 09:31
Posts: 491
Location: Oregon

No eeprom on the 10f322, although it can self write for a pseudo eeprom. A no nonsense driver shouldn’t be a problem using C, or even Basic.

Kent

Mattaus
Mattaus's picture
Offline
Last seen: 1 year 8 months ago
Joined: 10/20/2012 - 23:51
Posts: 833
Location: Brisbane, Australia

nickelflipper wrote:
No eeprom on the 10f322, although it can self write for a pseudo eeprom. A no nonsense driver shouldn't be a problem using C, or even Basic.

This may give you an idea about who you're dealing with here, but I don't know what that means in regards to my application lol. So is this a waste of time? It's ttreve3's code with the strobe removed (I think...I would be grateful if someone would check it over). Will I be able to compile that in MPLAB 8 and flash it using my PICkit 3?

Edit: note I have not taken the time as yet to check things like registers line up etc. And seen as there is no eeprom the last section is likely a waste of time.

nickelflipper
nickelflipper's picture
Offline
Last seen: 6 days 20 hours ago
Joined: 10/20/2012 - 09:31
Posts: 491
Location: Oregon

Correct on the _eeprom_data code, it is irrelevant, so just get rid of it. I don’t play with C, so can’t say what compiler problems you may encounter, if any, with MPLAB 8. The htc.h include file will give an indication on what compiler is being used, and compatibility.

Kent

Pages