Wnting to learn C or C++ for pic micro programming

Thread Starter

Dodgydave

Joined Jun 22, 2012
8,591
I want to start learning C for pics, i can already programme in Asm no problems using Mplab X ide.

Or is it better to learn C++ for pics.??

But most of the programming on here is in C, which books would be the best to start learning, i was looking to get this..Unless you have better recommended ones...

Also will i need a C compiler or can i use MPLAB X ide.?

Screenshot_20190612-111149.png
 
Last edited:

Ian Rogers

Joined Dec 12, 2012
682
We will help out as much as you need... That book uses Hitech C compiler ( now XC8 ) if you download XC8 you can just write the code "as is" from the book.. HTC format is still valid and will compile with the new version.... I have tons of tutorials over at "the other place" that use HTC and Nigel's tutorial boards..
 

JohnInTX

Joined Jun 26, 2012
3,915
Try one of the ‘Curiosity’ boards. They come with a built in debugger (so you don’t need a PICkit) and are dirt cheap. They also accept Mikro’s snap in boards for all manner of peripherals.
Download MPLABX, XC8 and maybe MCC and you’re ready to go. Avoid clones.

Right now, use the code MCHPFREE at Microchip Direct for free shipping.

Have fun!
 

nsaspook

Joined Aug 27, 2009
6,610
The complete C++ machine abstraction is not IMO a good fit for smaller PIC controllers. The latest XC8 uses the clang C compiler front end (Clang version 4.0.1 based upon LLVM 4.0.1) for C99 compliance so finding C99 code examples would be nice for learning a modern C programming style. You can still use C90 if needed.
http://www.simplyembedded.org/archives/battle-of-the-standards-round1/

http://ww1.microchip.com/downloads/en/DeviceDoc/xc8-2.05-full-install-release-notes-PIC.pdf
https://microchipdeveloper.com/xc8:moving-to-xc8-2-0
 

Thread Starter

Dodgydave

Joined Jun 22, 2012
8,591
Try one of the ‘Curiosity’ boards. They come with a built in debugger (so you don’t need a PICkit) and are dirt cheap. They also accept Mikro’s snap in boards for all manner of peripherals.
Download MPLABX, XC8 and maybe MCC and you’re ready to go. Avoid clones.

Right now, use the code MCHPFREE at Microchip Direct for free shipping.

Have fun!
I already have Mplab X ide installed, don't know what version, had over 6yrs now,used for Asm pic programming using a pickit 3 clone, works great.
 

JohnInTX

Joined Jun 26, 2012
3,915
PK2 is OK but won’t support a lot of newer chips. Those newer chips have much better debugging capabilities. Good enough to start, though.

You can get a lot of C learned by running the code in the simulator, too.
 

AlbertHall

Joined Jun 4, 2014
8,814
The free XC8 version has crippled optimisation but that is only going to matter for time or space critical applications which is unlikely to bother you at this stage.
Presumably you already have a PICKIT for programming and debugging. Note that not all PICs have the debugging capability built in. Microchip have a large table of PIC types and which programmers or debuggers can be used. But I can't find it :(
 

nsaspook

Joined Aug 27, 2009
6,610
The free XC8 version has crippled optimisation but that is only going to matter for time or space critical applications which is unlikely to bother you at this stage.
Presumably you already have a PICKIT for programming and debugging. Note that not all PICs have the debugging capability built in. Microchip have a large table of PIC types and which programmers or debuggers can be used. But I can't find it :(
XC8 V2.05 has almost all C language optimizations enabled on the 'free' version.

https://www.microchip.com/forums/m1081917.aspx
3.1. Version 2.05
More bits for your buck The OS X version of this compiler and license manager is now a 64-bit application. This will ensure that the compiler will install and run without warnings on recent versions of Mac OS X.
Less code for no bucks Unlicensed (Free) versions of this compiler now allow optimizations up to and including level 2. This will permit a similar, although not identical, output to what was previously possible using a Standard license. Virtually all code generation optimizations are now enabled regardless of the license type, but most assembler optimizations still require a PRO license for them to be enabled. The --mode option to the legacy driver, xc8, no longer has any effect.
Expanded long long support Support for 64-bit long long types has been expanded to include Enhanced Mid-range devices. These devices, as well as PIC18 devices, can use these types in expressions, but note that their use will greatly increase the amount of code and data memory required by the project.
Wider C99 support You can now use the C99 library with Enhanced Mid-range devices that use the reentrant stack model. Previously with these devices, you were limited to using C99 with the compiled (non-reentrant) stack model.
Larger stack allocation Functions that use the reentrant stack model in Enhanced Mid-range projects were limited to a total of 31 bytes of stack for local objects. This limitation has been lifted and there is now no theoretical limit to how much data a function can define on the stack. Note, however, that exceeding 31 bytes of stack usage will increase the size of generated code for each access of these stack objects by few instructions.
int24_t types added to C99 The int24_t and uint24_t types (along with the existing __int24 and __uint24 types) are now available when using the C99 library and when CCI is not active.
Welcome MPASM The MPASM assembler for 8-bit devices is now included in the XC8 compiler installer, rather than being distributed with the MPLAB X IDE. This assembler is not used by the XC8 compiler, but is available for projects based on hand-written assembly source.
 

BobaMosfet

Joined Jul 1, 2009
892
Title: Understanding Basic Electronics, 1st Ed.
Publisher: The American Radio Relay League
ISBN: 0-87259-398-3

Title: Standard C [Quick Ref]
Author(s): P.J.Plauger, Jim Brodie
ISBN: 1-55615-158-6

Title: Algorithms in C, 3rd Ed. [Parts 1-4, Fundamentals, Data Structures, Sorting, Searching]
Author: Robert Sedgewick
ISBN: 0-201-31452-5
 

dl324

Joined Mar 30, 2015
9,332
Or is it better to learn C++ for pics.??
Most IDE's that support C will also support C++. But, if you don't use or need C++ features, C will generally use less memory.

C isn't a difficult language to learn. I learned the bulk of it in a one week course and learned more about pointers as I used the language.
 

nsaspook

Joined Aug 27, 2009
6,610
Most IDE's that support C will also support C++. But, if you don't use or need C++ features, C will generally use less memory.

C isn't a difficult language to learn. I learned the bulk of it in a one week course and learned more about pointers as I used the language.
Friends don't let friends do C++ on 8-bit controllers. This is especially true on limited instruction set controllers with limited memory. While X86 PC hardware compilers have the entire CISC (complex instruction set computer ) instruction set of the latest PC processor to target for optimization, most 8-bit controllers have a limited branching and indexing RISC (like) instruction set that makes it hard to optimize C++ constructs efficiently to 8-bit hardware instructions on much slower hardware without using more memory as a construct helper.
https://cs.stanford.edu/people/eroberts/courses/soco/projects/risc/risccisc/
 

ApacheKid

Joined Jan 12, 2015
124
Dave,

Without a doubt I highly recommend this book by Augie Hansen. I've read many books on C and written a great deal of code in C too, trust me this is a genuinely helpful book that answers a lot of question that other books gloss over, this probably isn't the only book you'll need for C but it will be the best.

Now if you use Windows then I also recommend getting Visual Studio Community Edition (free) and then getting VisualGDB (which support the community edition).

VisualGDB isn't free but it also isn't hugely expensive either - take a look here.

I was amazed when I first tried this product, I was almost not going to bother because I said "Hmm a Visual Studio extension surely can't do all this or do it well...".

I was very impressed, within a short time I was debugging C code on the STM32 DISCO board with the Visual Studio debugger (one of the most sophisticated debuggers there is by the way).

There's a bit of setup but it went well and was less of a pain than I'd feared, I just connect the board to the PC with a USB cable and can build and deploy code from Visual Studio right to the board, start it and set breakpoints etc.

I'm a very experienced software developer and originally trained in electronics (in Liverpool in fact!) but must tell you I have done very little with embedded so others here may well have different but very good advice too, I use Windows and Visual Studio a lot so it makes sense for me to use Visual GDB.
 
Last edited:

ApacheKid

Joined Jan 12, 2015
124
I'd also say Dave that I think you should focus purely on C, at least for the time being, there's no huge advantage with C++ for most embedded stuff, certainly for the hobbyist anyway, I'll be avoiding C++ myself despite the fact that language offers OO it is very idiosyncratic and far too complex, it will just get in the way as you learn.
 
Top