Maker Pro
Maker Pro

Help Getting Started with PIC Microcontrollers

That is nonsense. Various projects have various requirements for RAM, program ROM and CPU speed. If you are making a commercial product and plan to buy many thousands of PICs, it is useful to make it run on the smallest, cheapest PIC that meets the needs. But for one-off projects, you can almost always just use a bigger / better PIC for $1-2 more and get away with unoptimized C code. Also, if you know the code patterns that are generated, you can do almost any optimization that the compiler is going to do by hand.

Yes I am talking about commercial products.

For one-off project why do you want to use PIC in the first place? :D Just spend 5$ and get a 80486 motherboard and use its ISA cards, or spend 10$ more and but a Pentium board, the conventional PCI bus gives you 266 MB/s of I/O speed at least! Who cares! just spend 50$ more and get something faster and the list goes on ... It is always just some few dollars more and a little bit more space ! Can you see the point ? for one single project one can use almost anything ! and always you can say the price is not important because it is only one single prototype.

Those people who wrote those C compilers are not stupid to provide it to the public for free. They know that if somebody going to be serious and do commercial production that free edition C compiler will be of no use and he has to buy the compiler. That's why they put those limits that you call NONSENSE !
 

KrisBlueNZ

Sadly passed away in 2015
For one-off project why do you want to use PIC in the first place?
To learn.
Those people who wrote those C compilers are not stupid to provide it to the public for free. They know that if somebody going to be serious and do commercial production that free edition C compiler will be of no use and he has to buy the compiler.
For serious commercial production, yes, you would buy the full version of the compiler. What is your point?
 
The one thing you have to watch out for is the package the PIC comes in. Some of the high end PICs are not available in DIP packages and cannot be installed on a breadboard. While learning about PICs I would suggest buying a PicKit3 with a development board as these normally have higher end PICs on them and allows you to play with the various peripherals and they have a little space where you can build your own circuits. Because they are PICs with more pins, they allow interactive debugging that can help understanding how a PIC works. Later you can also buy boards to extend peripherals to play with(PicTails I think they are called). These boards also come with sample code in assembler and/or C that will help you learn C as well as how a PIC works. You can study the source code in conjunction with the data sheet for the PIC and see how you set up a peripheral and use it. That is how I started with PICs.

The last thing you need while learning to program a PIC is a hardware wiring error and not being sure if it is the program or the hardware failing.

As far as using C on mid-range PICs is concerned: For most hobbyist projects it works fine. I found the 12F1840 recently that has most peripherals but only 8 pins. So it is easy to build on a breadboard and you can play with all the different peripherals available on PICs. It also has a on-chip clock, so needs no external resonators or crystals.

Ehsan does not know what he is talking about. Not all projects are huge, to the contrary most PIC projects will be simple as many others have mentioned. This is especially true for hobbyists like myself.

The question that needs to be asked is why do you want to use PICs for robotics? From what I have seen the most popular hardware used in robotics is the Arduino boards that have headers on them for attaching you own hardware, but the basics are taken care of on the main board. Arduino also interface to 'shields' which are specific peripherals you to use in your robot, like accelerometers etc. Have a look at the Arduino ATMega256. These boards break out most of the processor pins to headers. The Arduino also has its own IDE for code development and programming the board.
 
Last edited:

Take delay functions in C as an example. To use them in a PIC project you only need to set the clock frequency of your PIC and then call one of those delay functions with the amount of delay as an argument.

What does a beginner LEARN by doing that ? almost nothing !

Instead, if you forward him to the PIC datasheet and insist him to LEARN the architecture first, then by reading it he will know that each instruction takes 4 cycles to finish, so a single NOP (NO Operation) instruction then takes 4 cycles to finish, and if he put it in a loop he can generate desired delay. You see how much more he might learn if he does not use C? Meanwhile he will know timers, interrupts and other stuffs ...

Another point is that if you forward them to datasheet (which contains everything needed and PIC's Assembly instructions only of course), they can start learning by themselves ( Self-Study ) rather than being lost in choosing which language to pick and following myriad of tutorials in different languages.

Anyway it would be nice to hear the opinion of the OP after few months of learning. I will wait here for that.
 
@Eshan:
You are so hellbent on performance - it is sad that you don't understand that timing is best done with interrupts and not loops.

Using C rather than assembler does not mean you don't have to study the data sheets - to the contrary. We get that you don't like C, so you keep on coding in assembler, but don't try to force everybody, with invalid arguments, to follow suit. Some things are best coded in assembler, but most hobbyist, and newbie, projects can be done equally well in C. You can in any event code critical blocks of code in assembler inside your C source or link to assembler libraries. So stop trying to justify not using C as it is great for getting something done quickly. Well designed C is better than badly designed assembler and at least even badly written C is maintainable, which cannot be said of assembler.

Many commercial products are now available that are coded in C - a simple example is the flight controllers used in Multicopters(drones). For companies it becomes a tradeoff between time to develop and maintain vs cost of higher spec hardware. It is invariably easier to find higher spec hardware, but finding competent programmers can be a problem and again, with C, they have a chance that a new programmer will pick up the code more easily and can be less specialized. As with all things it is 'horses for courses' and the right hardware and language must be selected for each project.

Following your logic, why do you use an assembler compiler at all, surely you would learn more if you coded the machine code directly into a hex file as then you would have to know the exact format of each machine instruction. Extrapolating that further; why use a PIC at all, instead build the discrete hardware then you also learn about the underlying electronics. In the end it is a compromise between complexity and simplicity.
 
Last edited:
@Eshan:
You are so hellbent on performance - it is sad that you don't understand that timing is best done with interrupts and not loops.

lol That is why I have just joined this forum ! to learn more ! I will contemplate on your tip later on, when I back to my PIC projects. Thank you.

Following your logic, why do you use an assembler compiler at all, surely you would learn more if you coded the machine code directly into a hex file as then you would have to know the exact format of each machine instruction. Extrapolating that further; why use a PIC at all, instead build the discrete hardware then you also learn about the underlying electronics. In the end it is a compromise between complexity and simplicity.

Ummm I think there must be a balance between things. OP did not ask that he wants to LEARN things deep down to the gate level design. In that case he can follow your advise and go deep down to the MOSFET transistors.

In my opinion the balance will be met at the level of Assembly language. Going lower into details is as bad as hiding the details by using one of those high level language: BASIC for PICs ! lol.

It seems lots of people here didn't get my point. I am using C myself right now ! but I gained my knowledge in programming the PIC in plain Assembly language. After I got comfortable with the architecture I moved to C. Now I am able to switch back and forth and do whatever imaginable. Anyway soon or later the OP will come to this conclusion that he has to have the Assembly language of device and the internal architecture as background to get things done properly.[/QUOTE]
 

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
I agree 100% with Eshan.

People who just go out and buy a microcontroller and program it in C can never really say they understand it completely. Sure they might even program in assembler, but a real man will encode the instructions into binary by hand. There's so much to gain from the experience.

As soon as I have managed to grow a really pure silicon crystal I'm going to attempt to make my own microcontroller. Because you don't *really* learn anything unless you develop it from scratch from naked silicon.

Actually, I have toyed with the idea of developing by own form of semiconductor. Oh to hell with that, I'm going to start from "I think therefore I am" and develop an entire system of science from fundamental axioms.
 
lol at Steve.

I smell a bit of sarcasm in your saying, but actually if you open any proper electronic book, its first chapter is on Semiconductors and Silicon crystals, and how they manufacture devices by doping them and etc...

Are they crazy to start explaining things from scratch ? or maybe they understand this: "to do things properly one must start learning from the fundamental axioms."
 

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Let's give some simple, useful answers that don't involve navel gazing:

1) It looks like you have to know C to program PICs. So what would be the best way to learn it? I learned BASIC by a manual that PICAXE made on BASIC commands. Is there something like that for PICs?

There are several things you need to learn:
  1. The development environment.
  2. The syntax for whatever language you're using.
  3. The methods of accessing various "peripherals" available to you.
  4. Software libraries.
  5. Hardware interfaces.
2) What software do I need? is there some standard software for PICs?

3) What programing hardware do I need?

I recommend you make things as easy as possible for yourself.

What is the most common beginner environment? You've already been told about Microchip's IDE -- use that. The environment also includes the programmer. Find one that's widely supported. PICkit 2 and 3 have been mentioned. They're cheap.

Pick the most common language. It's going t be either C or Assembler -- personally I recommend C. You'll get more done more quickly to begin with. Also it's common (in some respects) between many target environments.

The combination of the above will dictate a range of chips (unless you've forked out for an expensive compiler). So choose something reasonably priced in a DIP package so you can breadboard it.

Software libraries will come later. By choosing a common development environment these will be easier to find.

Hardware interfaces you should already have a head start with. Read the datasheet (or crib from the web) to find out how to connect up power and the programmer and maybe a single LED on an output pin.

Your first program should be something really simple. Do nothing more than flash the LED. Don't even try to write it yourself, just take a demo, compile and run it (make sure your LED is on the right pin).

This step is somewhat deceptive. You may find yourself left trying to figure out what "fuses" or configuration words are needed. This is where keeping as close as possible to the laid out example will stand you in good stead.

I use a simple program like that *every time* I use a microcontroller. It allows me to check that I've got all the basics working.

Now start modifying the program to add functionality you're interested in implementing.

If at some point you come up against limitations, then *maybe* you might need to resort to assembler.
 
shumifan50 said:
The question that needs to be asked is why do you want to use PICs for robotics?

There small and cheep. ;)

Ehsan said:
Going lower into details is as bad as hiding the details by using one of those high level language: BASIC for PICs ! lol.

I don't think those BASIC PIC things are that bad. I would be really good for a beginner in the world of Microcontrollers(kind of like me). And I'm not saying it has no limitations, all I'm saying is it would be good way to get started with a simple syntax. But I all ready know BASIC and I would like to learn C.

but actually if you open any proper electronic book, its first chapter is on Semiconductors and Silicon crystals, and how they manufacture devices by doping them and etc...

I beg to differ. The first chapter should be on passive components. Then it should work it's way up to semiconductors.

(*steve*) said:
Your first program should be something really simple. Do nothing more than flash the LED. Don't even try to write it yourself, just take a demo, compile and run it (make sure your LED is on the right pin).

What I was thinking was ask here for someone to write a program for the PIC I have at the time, that makes a LED blink. Then ask them any questions I have about the program.

If at some point you come up against limitations, then *maybe* you might need to resort to assembler.

I think thats the key.

Thanks Everyone!! :D

Dan
 
Last edited:
As you wish to learn about PICs and C, I would suggest the following:

1. Buy a PIC 12F1840. It has a UART, PWM, ADC and DAC and inbuilt clock,so wiring is kept to a miimum and several periphereals can be played wit.
2. Construct it on breadboard connecting 10K pullup resistors to RA0, RA1 and RA2, Also connect a push button switch to each of these to ground. The resistors are not strictly needed as you can use the internal pullups.
3. Connect a LED to RA5 - remember a current limiting resistor but the PIC can drive it directly so no driver transistors are required.
4. Start writing some programs:
a. A program that turns the LED on when a button is pressed
b. Then a program that flashes the LED depending on which switch is pressed: flash once when RA0 is pressed then a pause. Flash 2 times when RA1 is pressed then pause and so on. Use __ms_delay(milliseconds) to insert the timing.
c. Now change the program to use a timer and interrupts to do the timing.
for (b).
d. Buy a cheap servo and program the PIC to use PWM to set the servo full left, full right or middle depending on which switch is pressed. This will teach you about PWM. It might be useful if you had a scope for this exercise.
e. Now get a 10K pot and replace the 10K resistor with the pot with the PIC leg on the wiper and the other 2 legs of the pot to 5V and ground. Now flash the LED faster and slower depending on the position of the pot using ADC to read where the pot is positioned. This will teach about ADC.
f. The last exercise is to use the UART. That requires either a serial display or a second 12F1840. Connect the TX to the RX of the other device and the RX to the TX of the device. Send the ADC value or which key is pressed to the other device. If it is another PIC, flash the LED as per (b), but based on the value received through the serial transmission. If it is a display send the value to the display in ASCII. These serial displays are around £15 here in the UK.

That will have given you some idea of most of the peripherals generally available on PICs and would be useful for robotics. A further exercise that might have some value is i2c communication.

You could also use the EEProm to 'remember' the last switch pressed and flash the LED accordingly after power cycling the PIC.

This should all be possible for less than £5 excluding the breadboard.
 
Last edited:
Learn C. If you are unfamiliar to programming in general C is a good place to start to start thinking programmatically, and of structure. It is widely used, and is just good to have in mind when other projects arise. Many programming languages are ‘C like’ so having knowledge of C as a foundation will make other programming languages start to look similar, and easier to grasp. Assembly is a bit more tedious than C to understand, and takes a bit more time to grasp from device to device. In my opinion I would rather use C, and see results in one day’s work instead of pounding my head against the wall to come up with a similar assembly program.
 
OK, I'm thinking of buying some PIC stuff soon. So I'm going to get a 12F1840, but it only has 8 pins and I will need some PICs with more then that. So I looked at some other PICs and I'm thinking of getting these:

PIC12F1840 8 pin
PIC16F1455 14 pin
PIC16F1509 20pin
PIC16F1788 28 pin

PIC16LF1907 40 pin

How does my selection look? Can I program the 40 pin PIC with the PICKIT 2? Does any one know of a sported PIC list for the PICKIT 2? Would it be best to buy all this directly from microchip?

Thanks :)

Dan
 
I love the 12F1840 as it is enough for most small projects, but sometimes you do need more pins.
PIC16LF1907 40pin does not have PWM which could be important for robotics. I would not be too concerned about having a LCD interface as a display can easily be connected using the USART, which requires only 1 PIC pin (if used for output only). I would not use this one for robotics. On top of that the 40 pin DIP packages have a huge footprint.

PIC16F1788 - This looks like a nice one AND it supports in-circuit debugging (if you can afford to lose the 2 pins).

I would not know about the PicKit2, but definitely with the PicKit3 as it supports all the PICS. As a matter of interest, I have had no issues with my PicKit3; maybe I am not advanced enough.

You buy them at the cheapest source, which is normally not Microchip. However, you can get free samples from Microchip if you are prepared to wait for delivery. I don't know where OKC is, so cannot make recommendation.
NOTE: Make sure you get the correct package (DIP).
 
I'm starting to have doubts about getting the PICKIT 2. I think what I will do is get the PICKIT 3 and if I don't like it then I can sell it on ebay and use the money to get a PICKIT 2. How does that sound? OKC = Oklahoma City, Oklahoma USA.

Thanks shumifan50

Dan
 
PICKIT 2 Supported Devices list

There is no definitive list for the PICKIT 3 for some reason. The only way I have found to see if the device works with the PICKIT 3 is to go into MPLAB and go to Configure->Select Device, and then click on the Device drop-down. There are status indicators to show if it is compatible with the PICKIT 3. Kind of a hassle if you ask me.

Anything up to the mid-ranges should by fine to start. Don't let that keep you from getting others however. I would say take a look at digikey to get a feel for price from device to device, and when you find a reasonable one to check out the data sheet to see if it includes features you will use. Most of the devices in a family are very similar.
 
Last edited:
Top