Getting started with C programming on PIC18s - Page 1
Forum section: electronics / general

author: Delta
author_rank: Super Contributor
timestamp: September 29, 2015, 12:11:14 pm
content: Hi, sorry if this is a long post, but I thought I'd give as much info on my programming background (or lack thereof, to be more precise) in order that people know where I'm coming from.No academic training in computer science or programming. (left school at 16 and went in to an Electronics Tech apprenticeship)Started programming BASIC as a kid on the Commodore 64.Got into doing a bit of Machine Language on the C64 (never used an assembler, just used the MLM on the Action Replay cartridge). Never got too advanced, but managed to make some pretty raster bars, multiplex sprites etc)I've done fair bit of work with Siemens Step7 PLCs - a bit of programming and a lot of debugging / using the code to fault find on hardware problems. (My 6502 experience actually came in really handy when I got to PLC routines written in STL - Statement List)About 4 years ago, I bought some PIC18s, a PICKit3 and started playing. I did all my programming in ASM. Never quite understood "relocatable code", so all my projects were in one big .asm file... I was actually really pleased with what I achieved - "all on my own": Using interrupts and timers, 16bit integer maths (the only routine I copy/pasted from the net as a division one), driving Hitachi LCDs in 4bit mode, Manchester Encoded UART comms with cheapo ASK RF modules....Never touched Microcontrollers since.... :-(About 3 years ago, I wanted to learn a bit of C, so got a copy of the H&K book and started playing with gcc on Linux. Wrote some really basic programs, simple substitution ciphers (I'd just read a book on the history of cryptography!) but nothing really useful. Never touched C since... :-(So now I want to do some more MCU stuff, and was tempted to stick with ASM as I can get going right away, but I am aware that all the advice is to use C, so really want to go that route. I've got MPLAB X and the XC8 compiler (PRO Version ;-) )So finally my question: Are there any good guides or tutorials for XC8? I've done a lot of goooooooogling, but most things I've found seem to assume that the reader is good with C but new to micros, but I'm kind of the other way round. Also, when you have no knowledge to start with, it's difficult to pick out the good info from the crap...)Please direct me, don't make me become the "assembler only geek" that Dave hates!

author: Maxlor
author_rank: Frequent Contributor
timestamp: September 29, 2015, 01:01:36 pm
content: Well, seems to be you already know everything you need? C on microcontrollers is often pretty close to ASM anyway, what with all the register manipulation you need to do.I'd suggest simply reading some example code, and asking here if you have any specific questions. Or if you just need some help with getting started:http://servforu.blogspot.ch/2012/09/blinking-led-using-mplab-x-pic16f877a.html

author: plastygrove
author_rank: Contributor
timestamp: October 01, 2015, 10:09:35 pm
content: In C the compilers may change but the basics rarely do. If you're new to C maybe useful to readk&r? I did it cover to cover and it was pretty useful.For xc8 nuances, you can refer to theirmanual (PDF)which was useful to me when trying to figure out the syntax for interrupts.I've done a lot of C on the xc8 (free) for the pic18f4550.(EDIT) PS: Inspite of what Dave says, I think there's a certain coolness at being able to 'assembly' your way in everything. I've done some assembly on PICs and AVRs, but nothing really large scale. Mostly for fun. Big projects are only in C because I'm more comfortable with it.

author: commie
author_rank: Frequent Contributor
timestamp: October 04, 2015, 01:05:15 pm
content: Are you aware of Amicus18?http://www.myamicus.co.uk/content.phpThis is a IDE Basic compiler for the PIC 18 series of chips which is free to download and has unlimited functionality. Basically, it is the PIC version of AVR ARDUINO.Don't be put off byBASICcompilers as they are just as powerful as C compilers and are very easy to use.

author: Howardlong
author_rank: Super Contributor
timestamp: October 04, 2015, 01:47:20 pm
content: It sounds like you have plenty of the right experience, you just want to glue it all together.The biggest benefit of doing it in C over assmebly language is that you end up with much more readable and maintainable code, even if it's only ever going to be looked at by you.I'd start off with the basics, getting a blinky going and move up from there, say to polled UART then to interrupt driven. I had no XC8 training, my first PIC C compilers were the B Knudsen ones, CC5X and CC8E, maybe 15 years or so ago, but things have moved on a lot since those days: you used to have to do the RAM bank switching manually for example. XC8 manages all that for you.I too was brought up on assembly, but these days I write 99.9% in C in emdedded stuff. I do drop into disassembly often, but actually writing it is fairly rare nowadays, just at occasional pinchpoints.

author: Ian.M
author_rank: Super Contributor
timestamp: October 04, 2015, 02:45:41 pm
content: +1 for getting a copy of K&R 2nd edition. Brush up your C writing console programs on a hosted environment. Also this online edition of 'The C book' may be helpful:http://publications.gbdirect.co.uk/c_book/N.B. XC8 and many other embedded cross-compilers are far closer to the ANSI C89/ISO C90 standard than they are to anything more modern + you only have a subset of the normal libraries you get on a hosted system, so many newer reference books and C tutorials lead you in directions that are not appropriate for embedded work.The best introduction to XC8 flavour C on PICs that I know of:http://www.gooligum.com.au/PIC-TutorialsRegister for free download of approx half of the tutorials. Pay most attention to the PIC12/16F1xxx enhanced midrange ones as the I/O and peripherals are mostly similar to the PIC18 family. The differences in the processing core are almost entirely hidden when using C.

author: zapta
author_rank: Super Contributor
timestamp: October 04, 2015, 03:08:08 pm
content: I suggest to first focus on C and then apply it to microcontrollers. Get an easy to use C environment for your OS of choice (what is it?), write a few program in C, learn the basic concepts (basic data types, strings, structs, loops, functions, macros, include files, printf, arrays, pointers, etc). It will be much easier that way and are useful for programming microcontrollers.Then you can move to your PIC environment and learn the embedded specific parts such as working with i/o, interrupts, etc.BTW, switching from assembler to C is an excellent move IMO.

author: Delta
author_rank: Super Contributor
timestamp: October 05, 2015, 10:53:04 am
content: Thanks for all the replies. It kind of confirms what I had been thinking, that I need to concnentrate on C in general rather than "C on MCUs".I do have a copy of the K&R book, not sure why I wrote H&K, I think they make guitar amps!But is it really advisable to start with C on the PC (I use Ubuntu btw) rather than practising on micros from the off? I ask because as I understand it, C on a micro involve much more low level bit tweaking than on a PC?If I was to play with C on my Ubuntu PC, is there a simple IDE you could recommend? When I dipped my toes in last time, I was just using gEdit text editor and gcc from the command line...Thanks again!

author: Flenser
author_rank: Regular Contributor
timestamp: October 05, 2015, 01:38:32 pm
content: Quote+1If you are happy to buy a couple of PIC12F509 and PIC12F506's to work with to learn C then his tutorials for the Baseline PICs are available for free.

author: true
author_rank: Frequent Contributor
timestamp: October 09, 2015, 03:49:03 am
content: Quote from: Delta on October 05, 2015, 10:53:04 amhttp://hk-usa.com/Just a little bit different from C.

author: Delta
author_rank: Super Contributor
timestamp: October 09, 2015, 11:55:22 am
content: Quote from: true on October 09, 2015, 03:49:03 amNah, make music not war!http://hughes-and-kettner.com/

author: MrAl
author_rank: Super Contributor
timestamp: October 09, 2015, 01:24:46 pm
content: Hi,For learning C and C++ there is the famous Watcom compiler with IDE.The most important part is probably understanding the different data types such as integer, long, unsigned, etc., so you know what you can do with each type.In pure ASM everything is an 8 bit byte and you have to make up your own 16 bit and 32 bit integers or whatever.Another consideration is there are a lot of C libraries available for various microcontrollers so you can download and use them in your project too.

author: jnz
author_rank: Frequent Contributor
timestamp: October 10, 2015, 04:14:59 pm
content: I would be remiss if I didn't mention that...I have 12 years or so with production scale products using PICs. My view is that if I was starting with PIC today, I'd entirely be wasting my time.Don't do it, course correct while you can!There is nothing specifically "wrong" with the chip itself if it was 10 years ago. Microchip has really questionable compiler issues going on (pricing, features, stability, etc) and their part prices are horrendous compared to ARM chips. I think PIC is fine to learn on, but would much sooner suggest dumping it and going right to an ARM board like an ST32-Discovery kit, you're going to be much happier overall.

author: Kalvin
author_rank: Super Contributor
timestamp: October 10, 2015, 06:50:06 pm
content: If you don't have any compelling reason to choose an 8-bit device, I would go for at least 16-bit device like PIC24F. The C compiler can generate much better code for the 16-bit architecture compared to the 8-bit architecture. Quite often you will have to deal with 16-bit data types or 32-bit data types where the 8-bit device will struggle. You may also ask yourself how much you want to spend your time struggling with poor architecture and other limitations imposed by PIC16/PIC18 devices if you need to push the device to its limits (like fast interrupt response, tight control loops etc.). The free version of the XC16-compiler can produce quite good code even when the all optimization is not available. I haven't been using the free XC8-compilers, so I cannot say how good code they produce.The 32-bit ARM devices are forth looking at, as they as powerful and very inexpensive. For my last project I selected PIC24F over STM32F-device because I was in a hurry and the PIC24F-device had just suitable evaluation board available so I could evaluate the device parameters and compiler quality within tight schedule. Currently I have a few STM32F1- and STM32F4-boards on my desk waiting some attention. The free GCC compiler tool-chain produces decent code for the ARM-architecture.That said, the 8-bit devices are just as good as the 16-bit or 32-bit devices if you are just wanting to learn embedded C programming, and your applications are not too demanding in speed. There are also quite a lot of C code examples for the PIC16/PIC18 devices which can be quite an advantage when starting out. Typically the on-chip peripherals of the 8-bit processors are less complicated to configure and easy to work with, where as the more advanced processors have quite often also more advanced peripherals which may require a bit more head scratching to get configured properly.

author: Stuart Coyle
author_rank: Contributor
timestamp: October 13, 2015, 02:27:36 am
content: Another vote here for reading K&R, there is no substitute for learning C.

author: briselec
author_rank: Regular Contributor
timestamp: October 19, 2015, 01:00:55 am
content: Totally agree with the advice to dump the PIC and go for something ARM like the STM32 series.I had to write code for a PIC many years ago when they first came out. From that experience I've stayed clear of them ever since. Going by what they said on an Embedded.fm podcast I listened to yesterday I made the right decision. The PIC18 was mentioned.

author: briselec
author_rank: Regular Contributor
timestamp: October 19, 2015, 01:20:03 am
content: Quote from: Delta on October 05, 2015, 10:53:04 amEclipse is very popular. I tried it for a morning and deleted it. Don't like it at all. I use CodeBlocks but mainly just for its editor features. There's nothing wrong with using gEdit and the command line, creating your makefiles by hand. You will learn more than you will using an IDE. For embedded system development I use the command line a lot.

author: Galenbo
author_rank: Super Contributor
timestamp: October 23, 2015, 09:17:17 am
content: Quote from: Delta on September 29, 2015, 12:11:14 pmWhat's the reason you started, and stopped?I bought an Easypic and a MikroC license, thought it would be a littlebit more expensive, but also a better jumpstart.Although I could make remarks and complaints, I never regretted that investment that allowed me to get through that dark steep beginners learning curve.