Connect with us

Help Getting Started with PIC Microcontrollers

Discussion in 'Microcontrollers, Programming and IoT' started by Supercap2F, Jun 16, 2014.

Scroll to continue with content
  1. Supercap2F

    Supercap2F

    550
    150
    Mar 22, 2014
    Hey Everyone!

    I have been doing lots of research on PICs but I still have lots of questions. And after these questions have been answered then I will most likely have more. ;)

    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?
    2) What software do I need? is there some standard software for PICs?
    3) What programing hardware do I need?

    Thanks so much Everyone! :D

    Dan
     
  2. BobK

    BobK

    7,682
    1,688
    Jan 5, 2010
    If you have no experience with C, I would recommend finding a tutorial and a free compiler and learn it on your PC first. There are plenty of resources on line.

    As far a PIC programming, I would recommend that you get a PICKIT 2 OR 3, and use the free C compilers from Microchip. I would recommend that you use the old MPLAB IDE vs MPLAB X. It is currently at version 8.92. MPLAB X is still quite buggy and frustrating according to users. I have never updated to it myself. PICKIT 3 also has some issues, though I think it is probably usable at this point. It supports newer PICs that PICKIT 2 does not support for hardware debugging.

    I would also recommend that you learn the assembly language for the PIC series you are going to use. This is supported by MPLAB.

    Bob
     
  3. KrisBlueNZ

    KrisBlueNZ Sadly passed away in 2015

    8,393
    1,271
    Nov 28, 2011
    There is at least one BASIC compiler for the PIC: PICBASIC (see http://melabs.com/) I haven't tried it or looked into it, but it might be usable.

    PICs are mostly programmed in C or assembly language. There are lots of C tutorials on the web, and books, but you need to focus on embedded C because of the restrictions imposed by the PIC architecture, which are pretty severe, especially on the baseline and mid-range devices.

    The standard programming environment for the PIC is called MPLAB and is provided by Microchip. It's an IDE (integrated development environment). The current version is called MPLAB X (X meaning 10) and it's the first version to be based on NetBeans, and it's not that well-integrated at the moment but it works. You can install compilers and assemblers into it.

    The MPASM assembler is pretty full-featured and free. The standard compiler is called XC8. It's a rebadged Hitech C compiler. There's a free version with very limited optimisation, which is OK as long as your program isn't too big, and you can upgrade to the paid version.

    Microchip make USB programming adapters called PICkit 2 and PICkit 3. Many people say the PICkit 2 is better, but if you're using the latest devices, PICkit 3 might be a better option. These are available from Microchip's online store for around USD 50. There are also many DIY versions and knock-offs but I've seen many complaints about these not working reliably.

    Edit: Good advice Bob, especially about learning C on a PC first, just to get the syntax. The embedded environment has some pretty severe restrictions compared to the PC environment, but at least learning it on a PC will give you an understanding of the language. And learning the assembly language is as important as learning any other aspect of the device architecture IMO.
     
  4. Supercap2F

    Supercap2F

    550
    150
    Mar 22, 2014
    Wow! Tons of good info here!

    It sounds a lot better to use the MPLAB IDE, but when I searched it in microchips search engine I could not find it. Is it no longer available?

    What do you mean by "issues"?

    You mean there are different Languages for different PIC series? A series being PIC16F,PIC18F,PIC24 etc? Are some series better then others?

    Is embedded C different then C? Whats the difference between baseline and midrange devices?

    Edit: Would C for dummies be a good place to start?

    Thanks Guys :D

    Dan
     
    Last edited: Jun 16, 2014
  5. KrisBlueNZ

    KrisBlueNZ Sadly passed away in 2015

    8,393
    1,271
    Nov 28, 2011
    If Microchip have removed MPLAB 8 from their downloads, maybe they want to force everyone to use MPLAB X. You may be able to find it elsewhere though. Perhaps through an anonymous file-sharing system that I probably shouldn't mention by name, or through some independent site.

    Re PICkit 3 issues, BobK probably knows more than me. I know the PICkit 2 has four uncommitted digital inputs that can be used as a crude logic analyser; these were dropped for the PICkit 3. Also search Dave Jones's eevblog (http://www.youtube.com/user/EEVblog) - he did a video on the PICkit 3 where he was very uncomplimentary. Edit: here it is:

    The baseline, mid-range and high-end 8-bit PIC families are similar and have similar assembly languages. There are differences in assembly language and memory architecture depending on exactly how they implemented various kludges to make the devices workable over the years. What kinds of things do you intend to use the PICs for? That could help us suggest which range you should start with. The PIC10, PIC12, etc numbers don't correspond directly with the family, except that I think the PIC18 devices are all high-end. These are only the 8-bit families. There are bigger families including the dsPICs but I know very little about them.

    Embedded C is just normal C with its hands tied behind its back and a gag in its mouth. It's probably best to learn normal C then learn the differences, rather than start with embedded C.

    Baseline devices have very constrained Flash ROM (program memory) and RAM (variables and data storage). They don't support interrupts. Mid-range devices are less constrained and they do support interrupts... barely. High-end devices are much less constrained, have a slightly cleaner memory architecture and support interrupts better than mid-range devices. The XC8 compiler compiles for all of those families.

    I haven't seen C for dummies but it's probably a good starting place. What development environment do they use?
     
    Last edited: Jun 16, 2014
    chopnhack likes this.
  6. Supercap2F

    Supercap2F

    550
    150
    Mar 22, 2014
    I will be using them for robotics. So they need to be able to read ADC have a PWM option etc.

    Whats the down side of the high end devices? Price?

    I have no idea

    Thanks

    Dan
     
  7. BobK

    BobK

    7,682
    1,688
    Jan 5, 2010
    Go the the MPLAB X page, the on the left is a list of links. Click "Legacy Software". You can get any version of MPLAB ever made.
    I have not upgraded to PICKIT 3 yet, so I really don't know, I only know that many people complain about it on the Microchip forum.
    Assembly language uses the actual instruction set of the processor, thus it is different for each processor you use. All of the 8-bit ones are similar, but the 16 and 32 bit PICS have very different architectures compared to the 8 bit ones.

    Bob
     
  8. BobK

    BobK

    7,682
    1,688
    Jan 5, 2010
    Yes, prices are higher but we are talking about $1 vs 2$ here. Unless you are producing a product and selling thousands, it might be worthwhile to go to the 16-bit PICs i.e. PIC24Fxxx or PIC33Fxxx These tend to have much larger memory and a better interrupt structure. The other downside is that they are a bit more complicated, so it might be better to start with 8-bit midrange pics (I would avoid baseline as they are too limited). I think for Robotics, you will end up with the 16 bit PICS, and if you wanted to go to 32, I would recommend ARM instead.

    Edit: You can get a 16-bit PIC (PIC24F04KL100-I/P) for $1.19 unit quantity at Microchip Direct, so price is really not an issue.

    Bob
     
  9. Supercap2F

    Supercap2F

    550
    150
    Mar 22, 2014
    I have been looking at the PICkit 2 and it looks like I need another board to program the PICs. Do I? Or can I just hook it up to the PIC? If so then how? Will C work with all PICs? It looks like what I'm going to do is get C for dummies and get to know C. Then I will get the PICkit 2 and MPLAB IDE. And start out programing 8-bit PICs. What would be the best 8-bit PIC series to start out with?

    Thanks Guys

    Dan
     
  10. KrisBlueNZ

    KrisBlueNZ Sadly passed away in 2015

    8,393
    1,271
    Nov 28, 2011
    Yes, you will need another board. I think you can buy a PICkit development package which includes at least one board to hold PICs during programming, but normally, programming is done in the final application circuit board using ICSP - in-circuit serial programming. You include a 5-pin connector on your board with connections to VCC, 0V, -MCLR/VPP, and the two serial programming pins, so you can program and reprogram the PIC without removing it. Handy if you use surface-mount PICs without sockets.
    Yes.
    Bob says you'll want a powerful device for robotics projects, so you might as well start with the high-end 8-bit devices. Check out the selection guide on Microchip's web site and choose a device that has lots of timers and PWM blocks.
     
    Supercap2F likes this.
  11. Ehsan

    Ehsan

    100
    1
    Jun 12, 2014
    The BEST way to learn PIC is to use Assembly language of this micro-controller, Other high level languages will hide the details off your eyes, and you can not learn deep into the topic. After learning the structure of system and have full understanding of how things work then switch to C language.

    If you use Assembly language then the software that you need is just a free Assembler downloadable at microchip.com, but for C language you have to pay for the compiler ! although you always can use a free but limited version of those compilers.

    You only need to buy PICKIT3 as others have mentioned. Don't buy the development board. Just buy the PICKIT3 programmer and build the development board by yourself.
     
  12. KrisBlueNZ

    KrisBlueNZ Sadly passed away in 2015

    8,393
    1,271
    Nov 28, 2011
    I don't think that's necessarily true. And I'm an assembly programmer from way back. You do need to learn how the peripherals work, but most of the messy implementation details (due to the poor design of the processor core of the small PICs) will be taken care of by the compiler and it's possible to write perfectly usable firmware without even knowing any PIC instructions.

    Anyone who wants to understand the device better, and optimise their code, will want to learn the assembly language of the device, but C is a reasonable starting point, especially if you're already with the language.
    The free version of the XC8 compiler (the standard Microchip compiler) is not "limited" in the usual sense - there is no limit to the program size, number of functions, etc. Its limitation is that it doesn't include many of the optimisation features of the paid version. (By comparison, the free assembler doesn't have ANY optimisation; you wouldn't expect optimisation in an assembler, although the XC8's assembler claims to be able to do some...) and it's possible to write perfectly workable code using the free version of the compiler.
    That's probably appropriate advice if you were advising yourself, but you need to realise that there are other people with different skills and priorities than you. The best way you can advise them is to explain your opinions, instead of just saying "you should ..." without saying why. If you explain your reasoning, people can decide whether it's relevant to them. If you don't, people will tend to ignore you.

    Have you used the PICkit 3? Have you had any problems with it? Can you compare it to the PICkit 2?
     
  13. Ehsan

    Ehsan

    100
    1
    Jun 12, 2014
    Suppose you have a PIC10F200, it has only 6 pins and 16 Bytes of RAM ! Is it meaningful to program this device with C ?

    Of course if you have a general purpose PIC18f4550 and tons of RAM, and want to teach kids how to turn a LED on with it, then C is a reasonable starting point, but for serious engineering work, one MUST know how the device works internally and that means read the PICs micro-controller datasheet and use its Assembly language instructions. After that step, they can upgrade to C. It is crucial to know for example that PICs are based on Harvard architecture. You can't really grasp the difference when you just jump to PIC by using some C code. for the C compiler hides the under lying architecture. Those who come from x86 based C programming get quickly stuck in writing proper C programs for PICs. Then they have to listen to my advise and go and learn the assembly language of PIC first, and then come back to C.

    Optimization is a big limitation ! Again unless you have a general purpose PIC IC with tons of RAM and you only just want to turn a LED on! otherwise every bytes of a micro-controller counts. Can you write a workable code with free version of those C compilers for PIC16F1509 which has only half a kilo RAM ? I bet no one can unless they use the PIC for powering a LED or simple task as such.

    Yes, I own a PICKIT 3 and I can compare it to PICKIT2: PICKIT 3 is red color and PICKIT 2 is black. lol just kidding.
     
  14. KrisBlueNZ

    KrisBlueNZ Sadly passed away in 2015

    8,393
    1,271
    Nov 28, 2011
    I wouldn't, personally, but it's possible. If you just want to replace a few timers and some glue logic, then yes, you can program it in C. Have you seen the code these PIC compilers produce? The code is not very efficient, because the baseline and mid-range PIC architectures are so poorly designed for compiled languages, but it makes very good use of RAM!

    Personally I wouldn't program the baseline nor the mid-range devices in C but that's because I have lots of experience and am accustomed to working in assembly language. I prefer to write in assembler for any project that's not huge, and I hate to see the inefficiencies of compiled code on these tiny cores. But it is totally workable.
    Again I disagree. I challenge you to come up with a likely case where it's necessary to know the assembly language.

    This is why the beginners' tutorials normally start with C, even on baseline devices. Because if you're new to the whole concept of microcontrollers, knowing the assembly language intimately is definitely one thing that you can leave until later, and in some cases, never.

    If you use the CCS compiler, you can even avoid learning the SFRs! It provides a huge range of intrinsic functions to manipulate them (which then becomes a hassle if you want to move to another compiler), and it will generate your interrupt handler automatically, making it look as though each interrupt source has a separate interrupt.

    Personally I don't like that approach; I prefer to know what's going on "under the hood". But it certainly is a simple way to get started with micros.
    Again, why? Can you give an example? PIC C compilers jump through hoops to make it possible to store initialised data in ROM with little or no visible impact on the source code.
    Ah, yes, that's the point!
    No, they just need to understand the limitations of C programming on tiny architectures. Very limited RAM. No proper stack. No interrupts or a messy kludge. Indirect addressing is very inefficient. C programming on other devices, such as old 8-bit AVRs, has some of these limitations. Even C programming on some older "microprocessors" such as the 6800 has some significant limitations that the compiler has to work hard to deal with.
    In many cases, that's what PICs are used for - timing, logic, sequencing, ADC with simple comparisons, PWM generation, and non-volatility. A little 6-pin device that can replace perhaps five or ten logic ICs, often for significantly lower cost, is just perfect, and that's where the PIC's advantage lies. So to answer your question, often, yes you can.
    That's not very helpful.
     
  15. Supercap2F

    Supercap2F

    550
    150
    Mar 22, 2014
    OK post #11-14 are just discussing if C or the Assembly language is better. It looks like C uses just a little more space then the assembly language which in most cases is OK. Is that right Kris? Now back to the problem. Do all of the same pin count PICs have all of there download pins in the same place? Like is there one download board for all different 14 pin PICs? Could I just lay out a download circuit on a breadboard instade of using a pre-made download board? Whats a PWM block?

    Ehsan: did you watch the video in post #5?;)

    All the best :D

    Dan
     
  16. Ehsan

    Ehsan

    100
    1
    Jun 12, 2014

    lol , Yes I am a subscriber of Dave Jones Youtube channel.

    I watched it couple of years ago. They way he got angry at PICKIT 3 brought tears to my eyes out of laughter. loool He is a cool and knowledgeable guy, I like him.
     
  17. KrisBlueNZ

    KrisBlueNZ Sadly passed away in 2015

    8,393
    1,271
    Nov 28, 2011
    No, C will use a lot more code space than assembly language, especially on baseline and mid-range devices. I'd estimate between twice and four times as much. But that's partly because it allows you to write your code the way it should be written, rather than having to customise the design of the code to the architecture of the device. In other words, some of the code space saving you get by moving to assembly language is due to the fact that you restructure the program to make it fit with how the device works. Programming in C saves you a lot of this trouble, but you pay for it because of the inefficient code you end up with.
    I'm not familiar with enough different devices to give you a complete answer. From what I've seen, there seems to be a convention - at least, with the 8-pin devices there is, but there may be more than one convention for each package size. Download a few data sheets for devices you're interested in and compare them. The pins you're interested in are VDD, VSS, -MCLR/VPP and the two ICSP pins.
    Yes, sure.
    It's an internal circuit that generates a PWM (pulse width modulated) signal. These are often used for motor speed control, though they can also control LED brightness or any other value. See https://en.wikipedia.org/wiki/Pulse-width_modulation

    If you want to learn more about the peripherals available in PICs, go to Microchip's selection guide, find a device that has lots of peripherals, and download the data sheet for it. And look on the web for PIC programming tutorials. There are tons of them! Not all of them are very good though. Look on sites for the manufacturers of programming tools (compilers, etc).
     
  18. Supercap2F

    Supercap2F

    550
    150
    Mar 22, 2014
    OK so if you have 2 PWM blocks in a PIC then you can have two PWMs running at the same time out of different pins? I will look around at different PICs I tell you what I find.

    Edit: how do I tell if the PIC is a mid range, base range, or high end?

    Thanks

    Dan
     
  19. KrisBlueNZ

    KrisBlueNZ Sadly passed away in 2015

    8,393
    1,271
    Nov 28, 2011
    Yes.
    That's a good question. I don't know of any foolproof way, except I think all the PIC18F... devices are high-end. The selection guides on Microchip's and Digikey's web sites don't tell you either. The main distinguishing factor is the Flash ROM width, i.e. how many bits wide the instruction opcodes are. Here's a summary.
    Code:
    Family           Flash ROM width   Stack     Number of instructions
    ------           ---------------   -----     ----------------------
    Baseline             12 bits      2 levels           33~36
    Mid-range            14 bits      8 levels            35
    Enhanced mid-range   14 bits     16 levels            49
    High-end             16 bits     ~30 levels      more than 49
     
    Supercap2F likes this.
  20. BobK

    BobK

    7,682
    1,688
    Jan 5, 2010
    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.

    I have done several projects using 8-bit PICs with the free C compiler.

    Bob
     
    Last edited: Jun 17, 2014
    KrisBlueNZ likes this.
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Electronics Point Logo
Continue to site
Quote of the day

-