This will be the first of my PIC series. MPLAB X is Microchip’s new (relatively) IDE and XC8 is the new (remember HI-TECH C compiler) C compiler for the 8 bit controllers. Unlike previous Microchip C Compiler (known as MCC18 – for 8 bit) which allowed only PIC18 series, now they cover from PIC10 up to PIC18. Then you move on to XC16 for 16 bit and XC32 for 32bit controllers.
I wanted to start this article by starting a new project and go on, but Microchip has already done a nice job. Given below are the links that you should visit before further reading.
Link for IDE and Compiler : http://www.microchip.com/pagehandler/en_us/devtools/mplabxc/
Link for Installation instructions : http://microchip.wikidot.com/
Problems, “Not working” a.k.a FAQ type questions : http://www.microchip.com/forums/
Yeah, the last link is for some problems you might be facing. I have taken a long time in drafting this whole series and I want to put my time writing it down. So use this as a guide/tutorial.
Before we start, some
1) No bread board – bread board for microcontrollers is a fiasco. If you were having success till now, you are at the mercy of your luck.
2) Read Datasheet – If you did not read the data sheet, then don’t call yourself someone who has used microcontrollers.
1) You know basic electronics (ohm’s law, transistors, etc..)
2) You know logic gates and digital circuits.
3) You know some C, the programming language (yes, we will be doing a lot of C and of course pointers)
Also please download this pdf titled “Microchip XC 8 Compiler User’s Guide” (http://ww1.microchip.com/downloads/en/DeviceDoc/52053B.pdf). As the title say’s, that’s your compiler guide.
|Please leave feedback in the comments section, so that I can improve the quality
I am using PIC18F4550 and PIC18F46K22, 40 Pin DIP/PDIP package. Codes should be a lot similar. If there is any difference, I’ll let you know.
The hardware/prototyping board I’m using is from EZModule.com (http://www.ezmodule.com/). The site has many more goodies for an electronics hobbyist. Here is the picture of the board I’m using (makes your life a lot easier).
Assuming you have installed the latest version of MPLABX and XC8 compiler, you can start a new project by following the screenshots. Here I am making one for PIC18F4550. It’s the same for all controllers
Now we have successfully created a project and this is how the project tab will look like. Its plain and no files inside. Which means we will be adding files to make it work.
Next step is to create configuration bits. If you don’t setup the configuration bits or if it set wrong, then the microcontroller will not execute anything. Setting the configuration bits wrong or forgetting to set the bits is the first and foremost mistake everyone makes. To view the configuration bits follow the screenshots (Pictures make my life a lot easier)
At the bottom of the screen you will see the configuration bits page.
Setting Configuration Bits
Set Clock (Internal Oscillator)
Clock is the heartbeat of the microcontroller. I am setting up the Internal Oscillator as the main clock which means I won’t be connecting any external crystal oscillator or resonators to the controller. The PIC will use its internal components to create its own clock. The internal Oscillator can go upto 8MHz (by default the internal oscillator is 1MHz – read the datasheet). Here is a small screenshot of what clock options you have according to the datasheet
To set the bits you have to flip through the options like the screenshot given below
Now you know how to set a configuration bit, we shall look into the other bits for basic setup.
|INTIO – Internal Osc
HS/XT – External Osc
|usually HS, but read the datasheet before you set it
|Turn off the watchdog timmer. I’ll cover a chapter on it in the later part of the series.
|Brown out voltage – read datasheet
|Will set all the PortB pints to Digital I/O
|Disable low voltage programming – read datasheet
After setting the Configuration bits, you need to save the file. Follow the screenshots
Select all and the save it as “config.h”
We need 2 files for basic operation
1) main.c – our source file
2) config.h – we just saved one
To add a new main.c do the following
To add config.h (we have already created it – the configuration bits)
Build it now. Build should be successful.
The next tutorial which is the IOPorts