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.
_PLLDIV_1, _FOSC_HS, _PWRT_ON, _WDT_OFF, _MCLRE_ON, _PBADEN_OFF, _LVP_OFF, _XINST_OFF,
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.
PIC-Based Battery Monitor - My friends, as usual, drove me to craziness again and led me to design, fabricate and test a smart battery monitor... The usual blinking lights and so on.....
9 years ago