Microchip PICs - Getting Started

MicrochipLogo.gif

What's one of those?

A Microchip PIC is an example of a microcontroller. Basically, a tiny but very complete computer in a single chip. The PICs have their own program memory, CPU and RAM. Most of them now have their own on-board clock, serial interfaces and ADCs as well. All in one little package. Faster processors (although less RAM) than my first home computer (a Sinclair ZX Spectrum).

Oh, I must address one unfortunate misconception: the PIC does not REQUIRE a bootloader. Many folk use a "PicAxe" which is a PIC programmed by the PicAxe people with a bootloader which will accept specially compiled programs using a BASIC compiler. We will not require a bootloader, but I can show you later why one might be useful. (I'm no PicAxe expert - please correct me if I'm the one under the misconception.)

Which one should I get?

The 16F84 is the first PIC I used. It's extremely well documented, with tons of example code all over the web and the ICSP (in circuit serial programming) works the same as on any other PIC. HOWEVER, it does require an external crystal oscillator and a pair of capacitors as the timing circuit and an external resistor to keep it out of programming mode. Also, it has no analog inputs or any of the fancy gubbins.

So.... I would recommend the 16F690. You can run it with NO (count them: zero) external components. It has a built in 8MHz clock (convenient because it means 1 command = 500ns execution time (except for jumps and conditions which can take two cycles). It has I2C (slave only), a built in asynchronous (ie RS-232 style) serial port and up to 12 analog inputs. If you want it to be an I2C master, there are loads of freely downloadable examples of "bit-banging" in code.

Engineering Smaples

Never, never, never buy PICs. Don't do it untill you're into mass production. Microchip are happy to give them to you in the hope that one day you'll be buying them by the truck-load. They will send you a dozen at a time and they'll even pay the postage. (Microchip Direct link below.)

Microchip will not send you samples if you use a generic email address such as google or hotmail. I have also heard that comcast addresses don't work. Your ISP account may work. Failing that order through your work email. If that doesn't work, lease a custom domain name and set up aliasing. If you're really stuck, someone here might order them for you.

Which TYPE should I get?

I've been asked which PIC16F690 to sample. There are [four] to choose from on the Microchip website:

http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=1335&dDocName=en023112&treeid=1

-I/ML, -I/P, I/SO, I/SS.

These suffixes refer to packaging information. I suggest the /P. This is the 0.3in dil package electronics hobbiests are probably most familiar with. I believe /SO (SOIC) and /SS (SSOP) are surface mount versions and /ML is a QFN type (wee square one and the sockets are sodding expensive). If you look at back of the data sheet, you can see the different shapes of the chips.

What programming hardware do I need?

As for the programmer, can you use a soldering iron? If so, I HIGHLY recommend the JDM programmer. (Link below.) I built a cut-down ICSP version for less than UK£4. (About US$8.) I have been using it for about 10 years and it has NEVER failed. I made the mistake of building in a ZIF (Zero Insertion-Force) socket. This works well, but the socket cost twice what the rest of the hardware cost and once I discovered ICSP, I never used it.

If you're not too handy with a soldering iron, I have to question wether robots are the true path to your happiness. There are simpler programmers. The ones which run off the serial port of a PC are usually "LUDIPIPO" programmers. (If anyone can tell me if this is an acronym, or its historical significance, please do.) Essentially, that means that rather that using the Tx line for data as you might expect, it's used to generate the programming voltage while the RTS is the clock line and DTR/CTS are used in combination for data in/out. Don't worry about all that, though: the programmer will handle it.

Beware

Some USB/RS-232 convertors do not drive LUDIPIPO porgrammers properly. I recommend you use an actual serial port if your computer is old enough to still have one. The PC has direct access to the UART which is what is required for this application.

Microchip makes the distinction that programmers not manufactured by Microchip should be considered "development programmers". Proprietary Microchip programmers are the reserve of commercial users, educational establishments and rich DIY robot fans. There's nothing wrong with home-made or OEM programmers, it's just that Microchip make no guarantees if a commercial application fails due to not using the official kit.

What software drives the programmer?

Sadly, this programmer does not work in conjunction with the Microchip IDE. (Only the Microchip programmers and a few others can program PICs directly from the IDE.) So, the software I recommend to program the PIC is the freeware WinPic. (Link, as ever, below.)

I strongly recommend this piece of software as it uses the data in the Microchip MPLAB IDE installation to set up the PIC correctly. Many other software packages have this data coded into them which means they require a modification in order to work with new PICs. WinPic should, in theory, program any PIC without modification.

What's this ICSP?

Yes, I mentioned it several times. It is simply that the PIC can be programmed while it's in a circuit. We attach a clock line, a data line and a programming voltage to the PIC and it can be programmed in situ. The programmer sends the compiled program to the PIC. It's no more complicated than that.

Serial Port Issues (ie "I don't have a serial port")

Some folk have tried LUDIPIPO (eg JDM) programmers using USB/serial interfaces. Some of them have worked. Some of then have not worked.

The problem with the serial port programmers is that the Tx line is used to switch the device into programming mode and the handshaking lines are toggled to clock the data into and out of the PIC.

I have two guesses as to why the USB converter things don't always work:

1) Since the bits need to be clocked (synchronous) into the pic, I imagine that the ons and offs need to be under direct CPU control for the synchronicity to happen properly.

2) It's possible that teh convertors just plain don't have enough "oomph" (that's a technical term which generically translates to "power").

What a pic-based USB programmer will do differently is that there's no RS-232 involved at any stage. They contain a pic which has an inbuilt native USB interface. The USB comms are sent to this pic and the pic is responsible for clocking the signal out to the pic which is being programmed.

All in all, I think the best bet might be http://www.instructables.com/id/S8PH18HFAQCISGU/. I'm tempted to have a stab at building it myself.

I'm also looking into the PicSquirt. http://www.p10link.net/plugwash/picsquirt/

It should work and you should be able to build it for less than $20.

UNFORTUNATELY, the firmware for the PicSquirt MCU only claims to support a few of the 18F series chips, and I haven't established what mods (probably software only) would be required to make it work. Nor can I figure out what windows software drives it.

I'm afraid the USB/serial converter I have is NOT one of the ones which works. I haven't tried others because I have serial ports!

What language will we be using?

In later installments, I'll look at Microchip's own native RISC assember. I'll try to knock up examples in increasing levels of complexity. I'll start with how to flash an LED, moving on to taking input from a switch, then look at creating a PWM motor control. If anyone is really dribbling at this point, I'm going to talk about "bit-twisted PWM" (oooooooh!) later, too. I might even get around to servo motor control and a little on analog inputs. I'll look at serial port I/O and introduce a line level driver IC which does all the hard work for us. Of course it wouldn't be complete without an I2C tutorial (but I have to teach myself that first!)

What other info do I need?

Whatever PIC you get, download also its data sheet. Read this next beit very carefuly. What follow is my VERY BEST tip: read the data sheet FROM COVER TO COVER. You won't understand it, but don't worry. You will have absorbed answers to questions which you haven't even asked yet. It will fall into place as soon as you try to use it.

Don't ask me questions unless you're at least skim-read it from cover to cover. I will know by your question if you've read it. You'll be embarassed if you ask me a question and my answer is: "see page 47".

One section of the data sheet you should print and keep is the instruction set. There are only 32 instructions for the PICs we'll be using. (Wait a minute. Isn't that the same number of instructions as there are in the C language once you strip away al the libraries? Think about it.) If your data sheet contains only a summary of commands, there's a Microchip document number DS31029 which contains the full explaination of all the commands.

Your first assignment is write a program and build a circuit to flash an LED at 1Hz. Shall I leave you to get on with it? Of course, I'll provide the answer eventually...

...in the mean time, ask me questions.


Useful links:

Microchip website (free Integrated Developpment Environment)

Microchip website (free PIC "samples")

WinPic (PIC programmer software)

JDM (PIC programmer hardware)

Microchip website (8-bit PIC selection table)

 

Update 2008-07-07

The guys at Gooligum Electronics have done a PERFECT walkthrough.

 

Great walk through! - You’ve

Great walk through! - You’ve convinced me to try it out (that and the link to free samples) . Prepare yourself for a bunch of noobish question - i’ve only programmed pc’s before except for a small stint with asm on a mc68000+emulator (that dates me).

noobish question #1
I’ve never seen a picaxe - but you mention that its just a pic with a picaxe bootloader? Can the same hardware setup you mentioned (JDM programmer) be used to program it as well?

noobish question #2
It seems alot of the comments are about usb to serial conversion - are all pics capable of being programmed through the serial port (no USB exclusively) ?

noobish question #3
Is the JDM programmer capable of programming any PIC you know of?

Oooo … I’m all tingley with excitement

You’ll love it. Only 31

You’ll love it. Only 31 instructions to learn!! You already know them all if you’ve done 68K. Bonus!

#1 I know nothing about picaxe. Sorry. The comment about a bootloader is speculation. The answer is “probably not.” If the picaxe is, indeed, programmed by a bootloader, the protocol is likely to be different. jip’s comment here appears to evidence this.

#2 Yes is the short answer. LUDIPIPO programmers (like JDM) will program all ICSP pics if they are attached to a PC serial port. Some USB/serial adapters DO work properly. My only experience is that mine DOESN’T and from what I’ve gleaned here, only a very few do. That said, some OLD PC (argualbly non-compliant) serial ports need and additional diode added to the JDM. That’s all documented on Jens’ site, though. (Hmmm… I wonder if the addition of this component would cause it to work with the USB adapter. Oh, well. If it ain’t broke, don’t fix it.)

#3 I don’t see why not. If it has ICSP, requiring a high programming voltage on !MCLR, it should work. It won’t do low voltage programming, but I’ve never seen a PIC which has only LVP. Jens lists PICs that he has tested it with. I’ve not found a pic I coldn’t program with it. My personal 8-bit stock consists of: 16F628A, 16F677, 16F688, 16F690, 16F72, 16F73, 16F74, 16F77, 16F818, 16F84A, 16F872, 16F88, 16F882 and 16F883. I’m pretty sure I’ll never need to introduce any others and there are some I’ll never use, but they all work.

Update 2008-07-05

I can confirm that WinPIC / JDM works for most, if not all 18F devices. You may need a programming adaptor for DSPIC30 stuff.

 

#1 All the PICAXE chips are

#1 All the PICAXE chips are just regular PIC chips pre-programmed with the PICAXE firmware (or bootloader or whatever that thing is called). I have the PICAXE 40X1 which is in fact a PIC 16F887 chip.

I’m pretty sure you can’t program a PICAXE using a regular PIC programmer but the hardware needed to program a PICAXE is minimal - and cheap: you need a mini stereo jack socket and two resistors. Of course you can use the PICAXE as a regular PIC and just use a regular PIC programmer to overwrite the PICAXE firmware - but this is generally a bad idea since there is no way to re-program the chip with the PICAXE firmware - better to get free PIC samples from Microchip.

PicHatchet

Do you know if there’s a 100% picaxe compatible firmware available as a freeware or opensource project?

Whoever thought of picaxe is sitting on an absolute goldmine. Money for nothing.

Wasn’t there a song?"Money

Wasn’t there a song?

“Money for nothin, and your Chips for free”

A) Where is that firmware?B)

A) Where is that firmware?

B) Price difference from the Microchip and the Picaxe version (the goldmine) is about USD 1,- And "Nothing" is then all the documentation, development of boards, driving the continued development…

C) Dire straits, Brothers in arms.

yeah i know, i was making a

yeah i know, i was making a joke Fritsy - next I’ll raise my hand :slight_smile:

Hmmm

1) That’s my question.

(II) Where can you get picaxe for $1? (Microchip PICs are free.)

C - see II - chips for free.

 

On (II) I often wonder why someone would write a language in another language. Then, just to compound it, why would someone want to learn the “macro” language, when learning the language it was written in is no more complicated?

Also, if you can’t connect stuff together to make your own boards, why would you want to go an buy in a load of other stuff to learn how to connect them together to make a robot? (I’m playing devil’s advocate, here.) You’ll only end up reading the documentation and kicking yourself thinking "I could have done that myself."


That’s what I did when I bought my first PIC programmer. Then, once I figured out how ICSP works, I wondered why ANYONE would EVER buy a PIC programmer!!

The joke was on you, I was

The joke was on you, I was responding the main thread :smiley:

(Next time I will add a PS :smiley: )

I like Picaxe.(and I am too

I like Picaxe.

(and I am too far away for you to slap me!)

yeah right

yeah right

Wow dueling PIC

Wow dueling PIC programmers… yikes…

now i could weigh in and say … why fight over micro-processors when your local tax office is throwing away mother-boards? You guys are going to run out of memory and cpu horse power in short order (this should get me electronically slapped)… bill gates is constantly making good hardware obsolete with his bloated os and apps, which is a great thing for hardware scavangers like myself…

and talking about resources of free code? There’s alot more for pc’s than there is for micro-controllers :stuck_out_tongue:

FLAME ON!

I still like Picaxe better.
I still like Picaxe better.

I agree
Yes, grogg. Totally. Picaxe IS somewhat bloated compared to assembler.

I saw the yellow drummer video and…

then i came to this very cool site and i’ve caught the robotics bug again. So ive ordered up some samples of the pic16f690 dip and have been perusing the chip manual. I also have a Stamp kit from my last flirtation with robots. i think i will dust that off as well. My last bit of electronics fun was modding an Xbox, but it never could navigate itself around the room. :stuck_out_tongue:

 

Size matters when it comes

Size matters when it comes to PICs :smiley:

GroG

Slash Pee

3 rules:

1) Read the manual cover-to-cover.

2) Get the /P variant!!

3) Read the manual cover-to-cover.

Even if you don’t understand the manual, you’ll know where to find the answer to your first question.

It’s a matter of bigness
You totally have a size obsession!

Well yeah, a bit… I mean -

Well yeah, a bit… I mean - why not go with a micro-pc, you’ll always want your bot to do “more things” … but i’m starting to get an appreciation for discreet microcontrollers which are dedicated to a single or a few simple tasks - you’ve convinced me, I’ll try it … I was just wondering about the best approach…

I’m excited about the possiblities of connecting multiple microcontrollers to a single micro-atx-pc puter…

but i get exited about things like that…