Maker Pro
Maker Pro

Help Getting Started with PIC Microcontrollers

I have posted a new thread with the schematics and source code to drive a HD44780 based LCD display. It uses timer0 to generate a software PWM signal to control the LED backlight. It uses I/O pins to gate the data to the LCD and it uses timer1 to generate a 0.2 second resolution heartbeat. The whole lot is based on a PIC 16F886 but should easily port to another PIC with enough pins.
 
Yeah, I already saw that thread, and downloaded the zip files you attached to it. When I learn C I will have a closer look at it. :)

Regards

Dan
 
Well C for dummies got here yesterday, after no short time. :rolleyes: I have some questions about a C IDE. Should I post them here? Or maybe start a new tread in the off topic section?

Thanks
Dan
 
OK, from what I understand in a IDE you can:
  • Write you code.
  • Compile and link it
  • Run your program.
Is that right?

In C for dummies they recommend using Code::Blocks for a IDE, so I downloaded it. When I open the program I get a weird error message, but it still lets me mess around with the program. It looks really complex (more then I need). So does anyone know of a simpler C IDE? One that works good?

Thanks:)
Dan
 

KrisBlueNZ

Sadly passed away in 2015
For PIC programming, use MPLAB. The new version is MPLAB X. Downloadable from http://www.microchip.com.

For general purpose C programming for Windows I recommend a package called Pelles C. It includes an IDE and can generate executables for various types of Windows environments.
 
Well after downloading Pelles C, my antivirus program said that there were risks on my computer. I deleted the risks, uninstalled Pelles C, and I am now running a full antivirus computer scan. I think that I will stick with Code::Blocks.

Dan
 
This is why Linux is wonderful. It comes with free C, C++ and every other language you can think of and there are several free IDE as well.
I am using MPLABX, but I used MPLAB before and it is a much better IDE, had less bugs and a more intuitive interface.
The one thing not in your list is interactive debugging; it allows you to set breakpoints, view variables, set variables in a debug session. Some also allow emulation.
 

KrisBlueNZ

Sadly passed away in 2015
Other antivirus programs were also mentioned in those threads I linked to. Detecting viruses isn't an exact science; false positives do happen, and every antivirus suite has slightly different performance.

I think the chance of Pelles C having a genuine virus is pretty low. It's an original package, compiled with itself, presumably, and represents a huge amount of work; it's not a copy of some hacked software that someone could have easily thrown a virus into.
 
Other antivirus programs were also mentioned in those threads I linked to. Detecting viruses isn't an exact science; false positives do happen, and every antivirus suite has slightly different performance.

I think the chance of Pelles C having a genuine virus is pretty low. It's an original package, compiled with itself, presumably, and represents a huge amount of work; it's not a copy of some hacked software that someone could have easily thrown a virus into.

Yeah, I see your point. But all the same, that computer cost me big bucks, and I have put a lot of work into it. I would hate to lose it to some hacker who thinks messing up other peoples computers is funny. :confused:

Regards
Dan
 
Make a partition on one of your hard disks and install Ubuntu Linux on it. You can then boot into Windows or Linux and get the free software that comes with Linux. In the case of Ubuntu, packages are installed from Ubuntu and you know you run no risk(well virtually no risk) of contaminated software and you get automatic updates as with Windows. It also does not need a lot of disk space; around 80GB should do the job for what you need. Install C, C++ and Eclipse for compilers and an IDE.
You can even download Ubuntu and run it off the CD without installing it to see if you like it. That of course limits the ability of installing software.
 
Make a partition on one of your hard disks and install Ubuntu Linux on it. You can then boot into Windows or Linux and get the free software that comes with Linux. In the case of Ubuntu, packages are installed from Ubuntu and you know you run no risk(well virtually no risk) of contaminated software and you get automatic updates as with Windows. It also does not need a lot of disk space; around 80GB should do the job for what you need. Install C, C++ and Eclipse for compilers and an IDE.
You can even download Ubuntu and run it off the CD without installing it to see if you like it. That of course limits the ability of installing software.

I don't think I want to turn my computer in to a linux just so I can program in C. Also I only have about 38GB of hard drive space left.. So I would need to buy another one, which I could not afford. Thanks for the idea though. :)

On a related note: I got Code::Blocks working, so I'm now having tons of fun making really simple C programs. :D

Thanks for all the help you guys have offered! I wouldn't be this far in learning C if it wasn't for you! :)

Dan
 
Last edited:
Hey Guys

Is there a document with all most common C commands/keywords? Or how about a good place to see a bunch of sample C PIC programs? Both of those would come in very handy. :)

Thanks again!
Dan
 
I don't think I want to turn my computer in to a linux just so I can program in C. Also I only have about 38GB of hard drive space left.. So I would need to buy another one, which I could not afford. Thanks for the idea though. :)

On a related note: I got Code::Blocks working, so I'm now having tons of fun making really simple C programs. :D

Thanks for all the help you guys have offered! I wouldn't be this far in learning C if it wasn't for you! :)

Dan
How did you get code blocks running for you? I had a bunch of directory issues and shelved it.
 
Hey Chopnhack!

Well what I did is I installed the version of Code::Blocks that came with the MinGW complier. When I opened it for the first time, I got a message saying something like this "Can't find the compiler executable in the configured search path for Microsoft Visual C++ toolkit 2003". So I thought that I might have installed it wrong. So what I did is, I went under 'Settings'->'Compiler' -> 'Toolchain Executables', and made sure under the 'Compiler installation directory' I had the right path to the installed version of MinGW.

The way I make a new project now, is I go under 'File'->'New' -> 'Project'. Select 'Console application', Click 'Go' I then go through the Console Wizard Selecting the C language and entering a project title. Then When I get to where it says "Please select the compiler to use and which configurations you wan't enabled in your project." I select the 'GNU GCC compiler' and leave everything else how it was. Select finish, and wa-la I got a project that works just peachy. :)

I still get that message when I start it up, but I'm just going to ignore it, unless it starts causing problems.:D

I don't know if that's what you where after or not. But I would be happy to try to answer any more questions. :)

Just wondering, but are you trying to get started with PICs too?
Dan
 
Last edited:
Hey Chopnhack!

Well what I did is I installed the version of Code::Blocks that came with the MinGW complier. When I opened it for the first time, I got a message saying something like this "Can't find the compiler executable in the configured search path for Microsoft Visual C++ toolkit 2003". So I thought that I might have installed it wrong. So what I did is, I went under 'Settings'->'Compiler' -> 'Toolchain Executables', and made sure under the 'Compiler installation directory' I had the right path to the installed version of MinGW.

The way I make a new project now, is I go under 'File'->'New' -> 'Project'. Select 'Console application', Click 'Go' I then go through the Console Wizard Selecting the C language and entering a project title. Then When I get to where it says "Please select the compiler to use and which configurations you wan't enabled in your project." I select the 'GNU GCC compiler' and leave everything else how it was. Select finish, and wa-la I got a project that works just peachy. :)

I still get that message when I start it up, but I'm just going to ignore it, unless it starts causing problems.:D

I don't know if that's what you where after or not. But I would be happy to try to answer any more questions. :)

Just wondering, but are you trying to get started with PICs too?
Dan

Thanks Dan!!
Yes, that was the problem I was having when I stopped looking at it. I will have to look into that tonight and see if I can get it to play nice.
Thanks in advance for the offer, I will be taking you up on that! :cool:

Yes, Kris B. has graciously offered his design and assistance for me to create something (its my other really looooong post) for the shop. I had a block diagram of what I needed and was thinking of using relays (clunky but within my grasp of thinking). He mentioned the PIC and I was amazed. When I was learning programming (meddling with it more) I saw the potential in the 80's of being able to control so many things via code and a computer. At the time there weren't any interfaces readily available that I was aware of, but I was certainly charmed at the time thinking how the future would look if I was able to interface with the many common items and automate processes - mundane stuff, but think of how much more productive life would be... Automatic lighting, automatic pet feeder/water dispenser, automatic plant waterer, pool chemistry meter, automatic pool filler, the list goes on. So the PIC and μcontrollers in general are a realization of my dreams from yesteryear. I am hoping that as I learn more about them, my peaked interest will bolster my lack of skills!
 
OK, so I'm diving head first into this again. :) I have finished the "C for dummies book", and part of the "C for dummies all-in-one desk reference". So I think I know enough C code for now. I reread this whole tread again to refresh my memory, so I have some questions.
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
I never really understood what Stack levels are... Do they have something to do with how the MCU is made? "Number of instructions" does that mean how many assembler instructions that certain PIC has? What do they mean by "instructions"? On page 375-387 of the PIC16F1789 they outline them for that PIC, but it looked like just a bunch of different ways to do stuff with binary.

Also, since there is not standard place to find all the C commands for PICs, how does this book look for learning about some of them? Are there any other books that outline the C commands for PICs that you guys would recommend?

Thanks all you Guys for spending your time helping me out!! I hope I'm not asking too many questions. :):D:p
Dan
 

KrisBlueNZ

Sadly passed away in 2015
The stack is a block of memory that is structured as a last-in first-out buffer, like a stack of cards, which the CPU uses to keep track of return addresses for calls to functions or subroutines.
Code:
#include <stdio.h>
static int n;

void function1(void)
{
    printf("One\n");
    n = 1;
}

void function2(void)
{
    printf("Two\n");
    n = 2;
}
I've defined two functions here that both call printf(). When the CPU executes either of these functions, it needs to transfer control to the printf() function. This is done using a call. When the CPU finishes executing the printf() function code, it needs to return to the statement that immediately follows the place it was called from. If printf() was called from inside function1(), it needs to return to the statement n = 1. If printf() was called from inside function2(), it needs to return to the statement n = 2. This is done using call and return instructions.

In function1(), printf() is invoked using a call instruction. As part of executing the call instruction, the CPU "pushes" the address of the following code (the code that implements n = 1) onto the top of the stack, then jumps to the printf() function code. When the printf() function code is finished, it executes a return or ret instruction. The CPU "pops" the address that's at the top of the stack and puts that into the program counter, so execution continues from that point.

In function2(), printf() is invoked in the same way, but the return address is the address of the code that implements n = 2, and that's where execution continues when printf() executes its return instruction.

Pushing and popping are two complementary operations that access the stack structure through a register called the stack pointer (SP), which is part of the CPU/MCU core. The stack pointer typically points to the data item at the top of the stack. A push increments SP then stores the return address at the location now pointed to by SP; a pop reads the location pointed to by SP and copies it into the program counter then decrements SP.

There are architecture-specific variations on the stack implementation. On some devices, SP points just past the top item, so a push does a store then increment, and a pop does a decrement then read. Also, stacks on most architectures actually build downwards in memory, not upwards. Also, in a few architectures, the value pushed onto the stack is not the exact return address; it may be one or two less than the return address. Those are the main variations.

This system allows any function or subroutine to be called from anywhere. When the function or subroutine ends with a return instruction, execution continues from the place immediately following the call, regardless of where the function was called from.

Because functions can call other functions, which can call other functions, and so on, the stack is a list of nested return addresses.

In general C implementations, the stack is also used to pass parameters to functions, and for local variables used by functions, since it's a convenient place for temporary storage. The stack is normally placed in an area of main RAM that has been reserved for this purpose, and return addresses, parameters, and temporary variables can all be accessed in memory using one or more kinds of stack-relative addressing.

But the 8-bit PIC family doesn't have a proper stack in main RAM. The stack is actually internal to the core, and can't be accessed directly. Ditto the stack pointer. In fact the only way to access it is through the call instruction and various versions of the return instruction. (It is also used when an interrupt is accepted.) This means that function parameters and temporary variables cannot be placed on the stack and must be allocated in main RAM by the compiler, and it means that recursion is impossible without special tricks.

The worst thing about the 8-bit PICs' stack architecture is the size of the stack. The very smallest ones only have two levels! That means that function A can call function B, then function B can call function C, but function C can't call any functions! In other words, the device only supports two levels of function nesting. This is acceptable for very simple programs and programs that use tricks to work around this limitation, but it's a big nasty limitation. Even with the larger 8-bit PIC variants you have to be aware of the limited stack size.

If you're working from a C background, you probably haven't even considered that there might be a limitation on the nesting depth of functions. One function can call another function, and that function can call other functions, and those functions can call more functions, and so on; the nesting depth can be pretty large. Not so when you're working with a PIC.


The number of instructions is the number of different assembly language instructions that the device supports. It's not very important really; manufacturers with competing microcontroller architectures like to boast about how many instructions their devices support, or how few instructions "you'll have to learn", but it's all hot air really. Other factors are much more important.


That book would be good, I'm sure. But you don't need to buy a book to learn about the library functions available. They are documented with the compiler (since they are actually part of the supporting resources for the compiler).

Different compilers use different approaches to library support. The standard, free (for the non-optimising version) 8-bit PIC compiler, XC8, which used to be Hi-Tech C for PIC, has relatively few library functions; if you want to initialise a peripheral, or modify an I/O pin, you have to access the registers directly, i.e. you have to "show your work".

Another PIC compiler provides dozens of special functions for these things; this can make your program look tidy and compact, but it hides the detail, can introduce invisible bugs, and makes for a lot of work if you want to port the code to a different compiler.

That book uses the MPLAB X IDE (and presumably the XC8 compiler), which is Microchip's official development environment, and that's what I would recommend.
 
Last edited:
Top