Monday, April 7, 2008

Let's C... Your first C Program using MikroC

Orait gang,

Here is your first program... You just download the file, expand it into a directory, start MikroC, click on Project>Open and select the L001 project file. We'll use it to kick off things...

Download First Project File

You should jumper the wires as shown below, connections are:
RB0 (PortB Pin 0) to the Speaker
RB1 (PortB Pin 1) to one LED
RB2 (PortB Pin 2) to another LED
RB6 (PortB Pin 6) to one switch
RB7 (PortB Pin 7) to another switch
Note: In PIC and MikroC, pin are numbered from 0 to 7... not 1 to 8...

Before I explain the programming aspect of things, please burn the hex file to the board with your programmer (PICKIT2, ICD2, WinPic, ICProg, etc). The program is designed to do the followings:
1. The LEDs will blink alternately upon power up.
2. Depending on which switch is pressed, different beep sound will be generated by the speaker.
3. If both switches are pressed, both beep sound will be heard.

We'll use the simple program to explain a bunch of things needed, covering PIC and C Programming.

Creating new project in MikroC
Start MikroC then click on Project>New Project...
Complete the top fields as shown (project name, directory, description, CPU type, CPU speed)... then click (checkmark will appear) on the following Device Flags and then click OK:

The following flags will be used for most of our initial programming. Other flags will be addressed in later module. As an exercise for you, you may want to refer the datasheet as to why I've selected these flags to be turned on... the information is in the Special Features of the CPU section.


Update (080410) You need to enable _CPUDIV_OCS1_PLL2 flag as well.

After clicking OK, you'll be presented with a blank screen... so much work just to get a blank screen :-)

On the blank screen, please type EXACTLY as shown below. It's your first C program... Mind the semicolons, braces... C compiler is worse than lawyers... missing one semicolon can make you loose your remaining hair...

As with any C compiler, white spaces are ignored thus the indentation is there to help us see the program structure... the compiler will ignore it...

In C, inline comments are preceded with //
In C, block (multiple lines) comments are enclosed with /* ..... */ as shown below
/* This is a sample of multi lines comments
note that this line doesn't require any preceding identifier
and this is the last line. */

After typing the codes, click on Project>Build
IF you have done exactly as I outline above, the program should compile correctly... The indication is at the bottom of the next screen shot. You can then load the resulting hex file into your programming software to program the hardware with it...
Make sure you slide the RUN/PROG switch on the development board to PROG for programming and RUN for running the program.

Please use the chat box on the left or comments link for questions, clarifications. So far, you've learn the process of creating a project in MikroC, entering (more like copying) C codes into MikroC, compiling the code and taking the resulting hex code to program the device.


9w2gu said...
This comment has been removed by the author.
Unknown said...

Yes, you're right... I saw that after posting the blog... too lazy to change... as always, comments are last thing I review... hi hi..

9w2gu said...

void main(){
TRISB = 0b11000000; //Set PortB to all output

PortB pin 6 and 7 is input and rest are output.

0b tells the compiler the number that follows is binary.
11000000 is bit 7 to 0 read from left to right.
when a bit value is 1 is input
when bit value is 0 is output
also can be set as:-
trisb = 192 which is same as 0b11000000

it is easier to use binary.

Unknown said...

Ayoyo... so fast huh... I'm still typing the explanation on the codes lar... will be on the next blog entry...

Anonymous said...

just a question - does this board need any external devices in order to flash the program into it?
looking at futurlec site i see: "Programs can be downloaded or updated directly to the microcontroller using the USB in-circuit programming interface" - so it looks like the board has a built-in in-circuit programmer (like the development boards sold by mikroe)