PIC/Arduino, programming, compiling basics? - Page 1
Forum section: electronics / beginners

author: LektroiD
author_rank: Frequent Contributor
timestamp: August 01, 2015, 03:58:22 am
content: Over the past few years I've rekindled my childhood hobby of electronics. A lot has changed and progressed since the early '80s, and seems more and more projects are relying on Arduino based microcontrollers. This is something I'd really like to get into but have no clue about coding or compiling (using 4000 series CMOS chips was about as far as I went in the '80s).I've seen many lists to follow; do this, do that, copy/paste this command, etc. but none explain what's actually going on in each step, and without those fundamentals, it means I have to follow these lists blindly each time, and not gaining any real understanding. Anyone know of a good video tutorial which explains microcontroller programming/compiling basics?

author: RogerRowland
author_rank: Regular Contributor
timestamp: August 01, 2015, 07:57:37 am
content: For PIC rather than Arduino, and PDF rather than video, but these are the best PIC (C and/or assembler) introductory tutorials I've found:http://www.gooligum.com.au/PIC-Tutorials

author: picandmix
author_rank: Frequent Contributor
timestamp: August 01, 2015, 09:46:41 am
content: Though there are masses of different micros and developemt systems out there, think is fair to say there are basically two or three different ways for a beginner to get going.The easiest and cheapest way is with an Arduino Uno board ( as little as £5 for a clone board on Ebay , UK supplier)That is ready to connect direct to your PC, has inbuilt bootloader ( no separate programmer needed at this stage) and comes with masses of ready made software modules to get even quiet advanced projects started.It uses C++, a quiet 'high' level language, again many web based free turorials.The other is the Microchip Pic chips which can be bought with a small dev board, but must have a programmer like a Pickit3 or the earlier Pickit2.These can be progammed in C, but are also very well covered for Assembly code which really is as low down as you can get.It really gets you into the bit and bytes of the system and by comparison to the Arduino can seems very slow and limiting, but you do get a better understanding of the base fucnctions.Expect to pay £50 for a programmer and small dev board or a lot more if you want a fancier board with lots of ready fitted devices like lcd.( the Ardunio can also be programmed in Assemby, but not so well covered on the web, unlike Pic Assembly)That Gooligum was a great free tutorial but think you will find most of it now costs; there are many other free ones though.Another option is the Raspberry PI, though that seems cheap at about £30 you have to buy another £30 of parts to get it connected , but unlike the previous which run direct from your PC, this is a stand alone system that you connect direct to a monitor or tv screen.Should just add that you do not have to buy any hardware to learn / try out the varoius programming methods.You can download the free compliers and create code and use the inbuilt simulators to test you code long before you need any hardware.EditTry installing the Ardunio IDE and looking at its Examples Library.https://www.arduino.cc/en/Main/SoftwareSame with the Pics, download the (technically, obselete, but still good) MPlab 8.92 IDE and try out some Assembly coding.http://www.microchip.com/pagehandler/en-us/devtools/dev-tools-parts.html

author: Ian.M
author_rank: Super Contributor
timestamp: August 01, 2015, 05:52:55 pm
content: The above all assume you can actually write a computer program. If you aren't at least reasonably familiar with writing computer programs, preferably in C, then learning to program a PIC, AVR or other MCU or small embedded systems like Arduino will be much much harder.If you need to learn to program as well, I would recommend getting a copy of "The C Programming Language" 2nd Edition by Brian W. Kernighan & Dennis M. Ritchie,http://www.amazon.com/The-Programming-Language-Brian-Kernighan/dp/0131103628and a free C compiler for your computer, and learning to write console (text mode) programs. The debugging facilities on your computer are likely to be far far better than on most embedded systems or MCUs and there are fewer obstacles in the way of running your first 'hello word' program successfully.Its then a far less daunting task to write your first C program for a MCU or other embedded system target as you only have to concentrate on the new stuff like how to configure the chip to run the program at the clock speed desired, how to use a port pin or a whole port, and how to transfer the compiled program to the chip.About half of the Gooligum tutorials for various 8 bit Microchip PICs are still free, but you have to register with their eShop to be able to download the free samples.

author: alsetalokin4017
author_rank: Super Contributor
timestamp: August 01, 2015, 06:44:17 pm
content: TLDR:1. Buy an Arduino UNO starter kit, which will include the Uno and some basic components, USB cable, jumpers, etc.2. Install the Arduino IDE on your computer.3. Start with the Examples that are included in the IDE.4. Play around with the code for the simplest Example Sketches, like Blink.5. Change things in the code a little at a time and see what happens.6. Work your way through more and more complicated Sketches to build understanding.7. When you have some specific project, find an existing Sketch that is similar to what you want, and change it so it does just what you want.8. Sure, buy the K&R text, but not before you have played around a little bit first.(If you are using Linux you probably already have gcc, and other compilers, built in and available at the command prompt.)

author: zapta
author_rank: Super Contributor
timestamp: August 01, 2015, 09:54:59 pm
content: Get this very popular book and an Arduino UNO board.It will teach you everything you need to know about Arduino.http://amzn.com/1118549368

author: void_error
author_rank: Frequent Contributor
timestamp: August 02, 2015, 04:28:24 pm
content: Quote from: RogerRowland on August 01, 2015, 07:57:37 amThat's the way I've learned. Although coding in assembly is not used much nowadays for complex projects, it still might be useful to write one or two short programs so you familiarize yourself with how a PIC works. Also, the datasheet examples are in assembly. Translating that into C is easier if you know a bit of assembly.

author: RogerRowland
author_rank: Regular Contributor
timestamp: August 02, 2015, 05:34:52 pm
content: Quote from: void_error on August 02, 2015, 04:28:24 pmYes I agree - I'm a software engineer (38 years in the job now) and doing my first project in assembly was a great way to get closer to the metal than I'd been used to. Transferring to C from there was no problem, you just need to learn to navigate the datasheets and get to know the various peripherals. That's why I prefer PIC to Arduino - I think Arduino "protects" you too much ....

author: zapta
author_rank: Super Contributor
timestamp: August 02, 2015, 06:16:25 pm
content: Quote from: RogerRowland on August 02, 2015, 05:34:52 pmArduino is merely a standard avr-gcc C++ compiler with additional libraries. Its' up to you to decide what portions of the libraries you want to use and what I/O registers you want to access directly.Example here:https://github.com/zapta/power-monitors/blob/master/pmon_3v8/arduino/io_pins.h

author: RogerRowland
author_rank: Regular Contributor
timestamp: August 02, 2015, 06:38:01 pm
content: Quote from: zapta on August 02, 2015, 06:16:25 pmYes, I know that and you know that, but there's a whole bunch of people out there thatdon'tknow that because the tutorials, libraries, example code etc. doesn't tell you much (if anything) about what's happening behind the scenes. That's not a good way to learn about microcontrollers in general, in my opinion. It would be better if there were a migration path from the Arduino IDE and various pre-compiler wrappings to gradually get to using GCC and maybe the Atmel IDE and to progress the learning down to the underlying hardware rather than making it a system of collecting and connecting pre-built modules.How many Arduino questions do you see asking for clarification of the Atmel datasheets? Not a lot. More are about, "where can I download a library to make this sensor work?".Sorry, ranting a bit, and not at you, just frustrated that there's learning opportunities being "wasted" a bit and maybe it's a personal preference too, so I'll calm down a bit

author: Hideki
author_rank: Frequent Contributor
timestamp: August 02, 2015, 08:22:44 pm
content: It's not about learning. It's about getting things done without having to understand much at all. Mission accomplished

author: farsi
author_rank: Regular Contributor
timestamp: August 02, 2015, 08:33:19 pm
content: In one of our Arduino meetups, we had Elliot Williams (hackaday contributor and author of AVR Programming) giving a talk about this:http://munich-arduino.de/2015/05/12/Learning-about-the-Arduino-toolchain/- it was really interesting to see what the different Arduino software tools do behind the scenes.

author: apis
author_rank: Super Contributor
timestamp: August 02, 2015, 08:36:35 pm
content: Quote from: Hideki on August 02, 2015, 08:22:44 pmBut if you don't understand what is going on you'll inevitably mess up and things will not work the way you think they do. But I suppose ignorance is bliss.

author: RogerRowland
author_rank: Regular Contributor
timestamp: August 03, 2015, 06:12:47 am
content: Quote from: Hideki on August 02, 2015, 08:22:44 pmThat's what I meant when I said it might be my personal preference. I'm more interested in learning by doing rather than just doing. However, if you just want to get things done, you can probably buy something that switches the light on when it gets dark, opens your garage door, monitors your houseplants, tracks your cat, etc. etc. .... no need to bother with an Arduino at all if there no desire to "understand much at all". In my opinion, a bit of a sad state of affairs though.

author: picandmix
author_rank: Frequent Contributor
timestamp: August 03, 2015, 09:34:48 am
content: Would be interesting to know what LektroiD thinks after two days of chatter ..... ?

author: LektroiD
author_rank: Frequent Contributor
timestamp: August 05, 2015, 07:28:48 pm
content: Hello, and thank you for all your replies. Sincerely appreciated...I prefer to learn the oldskool way; I'm certainly more interested in understanding what is happening under the hood, rather than having it all done for you, even if I do end up using the odd library here or there once I have a better understanding of what's what.it looks to me like the PIC is my best introduction to the whole coding thing. I'll probably go for an Arduino at a later stage...So, would it be recommended to buy something like a PicKit3 and a few empty chips to start with?Also, if you could recommend one concise book to get a beginner like myself off the ground, which would it be?

author: RogerRowland
author_rank: Regular Contributor
timestamp: August 05, 2015, 07:40:16 pm
content: Well, I can only tell you what worked best for me.Yes, definitely a genuine PICKit3 - stay away from the clones. Then download MPLAB X and order a few PIC16Fxxxx whatever takes your fancy. I would (and did) pay for the Gooligum tutorials because, although I know C inside out, I don't know the PIC and its vagaries and those tutorials were the most understandable to get things started. Having both C and ASM versions helps too. Many of them are free, so give it a look before buying if you prefer.I didn't find one single book that was useful for PICs as most books tended to choose one particular device and a small range of applications that did little good unless they happened to coincide with what I wanted. However, I did find a lot of accessible information in the book "Practical Electronics For Inventors" - just enough to get me going until the 3rd Edition of The Art Of Electronics was published.Regarding Arduino, I've found it useful to use it with prebuilt projects to test new sensors or devices before manually reimplementing support on a PIC of my choosing and tailoring the device according to the mix of peripherals I need. It takes away the "is it the code or is it the circuit?" type of doubts and also gives a useful insight into how the different micros work internally.For the PIC especially, you need to eat sleep and breathe the datasheets until they become second nature. Baffling at first (and second and third) but once it all clicks, you can migrate up and down the range of devices with hardly a stutter and everything feels familiar.Hope this all helps. To get an idea of the level I've been learning at, I've blogged a few of my projects here:http://rogerrowland.blogspot.co.uk/

author: ez24
author_rank: Super Contributor
timestamp: August 05, 2015, 10:54:44 pm
content: Quote from: RogerRowland on August 05, 2015, 07:40:16 pmSome images are missing such ashttp://4.bp.blogspot.com/-2T0_CfhryAo/VEy-4HkcSKI/AAAAAAAAAOc/3x7avZNto1k/s1600/board_02.JPG

author: RogerRowland
author_rank: Regular Contributor
timestamp: August 06, 2015, 04:29:13 am
content: Quote from: ez24 on August 05, 2015, 10:54:44 pmHmmm ... looks ok for me on Chrome and Firefox, although IE is a bit crap (unsurprisingly) - what browser are you using?

author: ez24
author_rank: Super Contributor
timestamp: August 06, 2015, 07:01:19 am
content: QuoteFirefoxThe missing images start right after Circuit Training on this page:http://rogerrowland.blogspot.co.uk/The next missing one is the second one past "Slight deviation"And a few more down the page (ie most pics show up)

author: RogerRowland
author_rank: Regular Contributor
timestamp: August 06, 2015, 07:24:28 am
content: Not sure what's happening there - it seems to be the larger schematics that you can't see. That part of the blog in Firefox on my PC looks like this:Do you see anything if you click in the blank space?

author: picandmix
author_rank: Frequent Contributor
timestamp: August 06, 2015, 08:36:45 am
content: All pictures viewable via my FF, Ez24 using some add ons that are blocking certain pics ?

author: Docholiday
author_rank: Regular Contributor
timestamp: August 06, 2015, 12:39:45 pm
content: Quote from: Hideki on August 02, 2015, 08:22:44 pmDon't tell me how it works just give me the answer!You will never learn anything thst way...

author: retrolefty
author_rank: Super Contributor
timestamp: August 06, 2015, 02:36:28 pm
content: Quote from: Docholiday on August 06, 2015, 12:39:45 pmI think there is room in the world for both types of people, those that wish to learn how everything works and for those that just want the results as easy as possible to get their 'project' to work. EE and electronic hobbyists types usually belong to the former type and the rest of the world the ladder type.The vast popularity of the 'Arduino world' proved that there is a market and interest in micros for the non techie artist types. They don't have to be interested in all the underlining complexity to get some benefit. We don't all grow our own food.

author: ez24
author_rank: Super Contributor
timestamp: August 06, 2015, 05:24:46 pm
content: QuoteA different view of 12 pictures comes up and the one that is highlighted is blackHere is what it looks like before I click on itand the lower right corner when I click on itStrangeWAIT !!! I just took another look and everything is thereReally strange.Keep up the good work, I like what you are doing - thanks

author: LektroiD
author_rank: Frequent Contributor
timestamp: August 11, 2015, 12:05:53 pm
content: There's an interesting article on PIC programming in this months Elektor Electronics magazine, seems indepth enough to get a good grasp. However they recommend a PicKit2, the PicKit3 isn't mentioned... Any advantages/disadvantages with either?

author: RogerRowland
author_rank: Regular Contributor
timestamp: August 11, 2015, 12:28:14 pm
content: Quote from: LektroiD on August 11, 2015, 12:05:53 pmWell, see what Microchip says:http://www.microchip.com/DevelopmentTools/ProductDetails.aspx?PartNO=pg164120"Not recommended for new designs and does not support new devices. To be discontinued. Please see the PICkit 3: PG164130. "PICkit 3 is the way to go .....

author: rs20
author_rank: Super Contributor
timestamp: August 11, 2015, 12:30:43 pm
content: Quote from: LektroiD on August 11, 2015, 12:05:53 pm

author: LektroiD
author_rank: Frequent Contributor
timestamp: August 11, 2015, 12:47:07 pm
content: Hmm, conflicting advice between the two above posts.Has nothing changed since Dave made that video shunning the PicKit 3 in 2009?And why would this months article in Elektor recommend the PicKit 2? Is the 3 compatible with the smaller PICs which are used in the article (PIC10F200 / PIC12F675)?I want to make sure I buy the right thing from the start, I don't want to buy twice.

author: rs20
author_rank: Super Contributor
timestamp: August 11, 2015, 01:03:24 pm
content: I can't really recall, but check the followup videos -- I think some of his concerns were addressed.Also, while I'm providing conflicting advice, what's the deal with the Microchip software stack? IIRC, the Microchip compiler is bizarrely closed off and you have to pay to get optimization? I may be way out of date on this. The Atmel compiler is the open-source avr-gcc, so it has optimization built in for free. Plus you can use the compiler flags to see the assembler output, object output and final linked hex output, which may be useful for the OP's goal of really understanding the steps in the process -- but not sure if OP is actually interested in the nuts and bolts of the compilation process.Also Arduino-vs-PIC is a bizarre comparison, like "diesel engines" vs "Toyota Corolla". A reasonable comparison is AVR-vs-PIC (vs others). Another reasonable comparison is Arduino-vs-other PIC dev boards.If you want a full-on MCU experience, and want to avoid the sheltered world of Arduino (a very wise goal I applaud), you can still buy the Arduino hardware (which is a prefectly acceptable ATMega328P breakout board), buy a Atmel ICE Basic (the Atmel equivalent of a PicKit basically), and program it using close-to-metal code using Atmel Studio via the 2x3 programming headers on the Arduino. As dumb as some of the hardware design decisions in the Arduino are, you can't look past the huge number of cheap shields available that allow you to easily experiment with other chips, whether they be motor driver chips, ADCs, DACs, SD cards, etc. Just my 2c.

author: RogerRowland
author_rank: Regular Contributor
timestamp: August 11, 2015, 01:24:32 pm
content: Microchip actually took it very well and most, if not all concerns were addressed - see here:http://www.eevblog.com/2009/10/30/microchips-video-response-to-my-pickit-3-review-in-eevblog-39/Personally, I've used PICkit3 with MPLAB X on mid-range PICs with very little trouble (Window 8.1 and Windows 10 too). The original question was "PICkit2 or PICkit3", and I think even if the latter is less than perfect, it's a better way forward than PICkit 2.Certainly, PICkit 3 supports all current devices, which is not true for PICkit 2.