The blink led is a great start (or re-start in my case) with microcontrollers. In this project, I will describe how to set up the MPLBX and run a simple blink (hello word) C program.
The great advantage of starting (or re-starting) programming microcontrollers with a simple code, such as Blink, is that you can focus on installing and configuring the compiler and potentially many other bugs while installing or setting up your microcontroller environment.
I love programming and for me, life is kind of boring if I am not "thinkering" or working with embedded systems. I believe that 8-bit platforms, but if you can start with 32-bits that works too. The most important is to learn how to configure your microcontroller, how to read a datasheet, what are the limitations, and most importantly what is your main goal.
Today, and in the next series of posts, I will be using the PIC 18F45k22 and the development board PIC-EK. The PIC-EK is manufactured by
LogiFind and I bought it on Ebay.
The PIC-EK is a versatile development board and supports an infinity of microcontrollers including families with 8, 10, 14, 18, 20, 28, and 40 pins. Moreover, the PIC-EK has multiple peripherals including LEDs, switches, 7 segment display, joystick, memory, buzzer, and others.
Development board - PIC - EK
I am using the PIC18F45k22 with a 4MHz crystal.
PIC18F45k22 with a 4MHz crystal.
To program the PIC, I am using the PICKit3.
Once the code is written and compiled, you connect the PICKit3 in the PIC-EK and download the binary file.
To program and write code for my embedded systems application, I could use the Mikro C pro or the MPLABx. The major advantage of MikroC is the number of libraries available. On the other side, the pro (full version) costs 250 dollars. A free version, the MPLABx requires more work, but it is free and it gives you full access to the writing memory. I will be using the MPLABx and I will teach you (or at least document) how to install and configure your enviroment for your first program.
Download MPLAB® X IDE v5.25 (
Link to windows) and install the drivers.
If you have had experience with creating a new project then you can go to the bit setting part. If not, I suggest you follow these steps:
1) Start a new project:
2) Choose the microcontroller family and your pic

3) Select the compiler. If this is your first time using MPLABx, you have to click the Download XC8 option. (I had already done this and just selected the XC8)
4) Choose the name and location of your project (I forgot to take a print screen, but this part is very straight forward)
Okay, your project has been created.
5) Now you have to click on the helper above the Sources folder and select New >> main.c
6) Okay, now we have a project and a main. Before you start programming, we have to tweak the configuration bits (Warning: if you have no programming experience, this part may seem quite confusing, but it is nothing like a seven-headed array. The microcontroller is a very flexible device, and you have to "adjust" your preferences so that everything works just fine (unfortunately or fortunately) you will need to read the microcontroller datasheet.
7) setting bit: Click on the tab.
8) In this project, I modified only 3 bits: oscillator (HSMP), watchdog (OFF) and, PORTB AD (OFF)
9) Click on Generate Source Code ..
10) Copy and paste the code into the program.
Step 11) Ready! Now we can start programming!
#include <xc.h>
void DELAY_ms(unsigned int ms_Count);
int main()
{
TRISB = 0; //RB0 as Output PIN
while(1)
{
PORTB = 0xFF; // LED ON
DELAY_ms(100); // 100 ms Delay
PORTB = 0x0; // LED OFF
DELAY_ms(100); // 100 ms Delay
}
return 0;
}
void DELAY_ms(unsigned int ms_Count)
{
unsigned int i,j;
for(i=0;i<ms_Count;i++)
{
for(j=0;j<100;j++);
}
}
Then just compile (on the hammer), connect PICKit3 to PIC-EK and click on the icon with the green arrow pointing down (Make and program)
A window will pop up for you to select your programmer (PICKit3)
Some microcontrollers work with 3.3V and others with5V. The PIC18F45k22 is at 5V. This option, I just clicked OK. (I am not responsible if you burn your PIC because of the wrong voltage. Read everything carefully and always be cautious!)
Here is the result:
I hope you have been able to follow the step by step and get rid of the PIC microcontroller bits ai. Please post comments with questions, program suggestions, and improvements.
Next steps:
Put the code in github (public)
Create an .h file for configuration functions.
Explore pushbutton LED routines and timer and hardware interrupt routines.
Cheers!