Thursday, April 10, 2008

Lesson 2 Part 2

If you have wired the LCD as per connection schematic and successfully compiled and programmed the PIC, the LCD will show "I'm alive... :-)" on the upper row.

The bottom row of LCD will show an incrementing number (row 2, column 1) and decrementing number (row 2,column 9)

Pressing the switches will sound the buzzer. Either at 1000Hz or 1100Hz depending on which switch. If the switch connected to PORTB 7 is pressed, the buzzer will sound at 1000Hz.
Switch connected to PORTB 6 will sound buzzer at 1100Hz.

The LED will blink alternately at 100ms interval. (as per line 18 & line 20)

P/S. You can also make the buzzer sound 1000Hz at 100ms interval by changing line 21 to "if (PORTB.F2==1){

Wednesday, April 9, 2008

Lesson 2: Let's C some more... LCD now active

Hello Gangs,

As I will be on assignment for next few days, I may not be updating this blog as fast (so everyone can catch up and digest and read and test and loose some hairs)... As mentioned earlier, we'll do the LCD next. It will be much easier to learn C and the intricacies of PIC when you can see the result in more meaningful ways, more than just blinking LEDs and beeping sounds...

Compile and burn the following codes to test your LCD. Make sure you adjust the contrast setting on the development board. Study the codes by referring to MikroC online help or the user manual... I'll dissect the codes in the next posting as well as learn more C matters in next lesson...

Download Lesson 2 Project File

Monday, April 7, 2008

Preparation for next lesson... wire up the LCD...

I've be covering the LCD next. I'll be using the 16x2 characters LCD. As such, please prepare the necessary cable for the LCD unless you want to use jumpers.

Having the LCD will make learning easier and faster. As you develop you own codes, you are bound to have logical errors (which are not detected by MikroC). Using strategically embedded LCD updating codes, you can diagnose and troubleshoot you codes faster.

The connection diagram (copied from the development datasheet) is shown below. Don't forget the power connections to the LCD (not shown in the schematic below).

Let's C... Dissecting the codes

As a continuation to this blog entry, in this section, will start dissecting the C program to understand the what and why of the codes. The codes shown below for reference...

Okay, lets start.

Comments
As mentioned earlier, comments can use the // or /*...*/ format. Thus, line 1-3 are just comments line. It's good to start your program with comments on what the program is all about. After developing million lines of codes, you'll appreciate how short your memory span is...

the main() function
All complete C program MUST have what's call a main() function. When CPU is ready for execution (main CPU clock is stable), it will execute the first line in the main() function. The format of the main() function is as follows:

void main(){
//your codes here
}

The void means the main() function doesn't return any value when it exit (finish running). In normal PC programming, some software needs to inform the operating system something such as error code when it exit. This doesn't apply to microcontroller.

The curly braces are to signify the block of codes belonging to main().
Do take note that you MUST NOT put a semicolon after the closing brace.

the Setup needed for PIC
Line 6: TRISB = 0b1100000; //Set PortB to all output
As pointed by 9W2GU, the comment is wrong... it should say pin 6 & 7 as inputs, remaining are output.

TRISB is a special function register of PIC used to control the direction of PORTB. If the bit is set (1) then the corresponding pin of PORTB is configured as input otherwise it will be configured as output. By default, all ports pins are configured for input on power up.

As you can see, using binary format make coding easier as you can see which pin is configured for what. In C, binary numbers are preceded by 0b, hexadecimal numbers by 0x, octal numbers by 0. Other numbers (starting with 1 to 9) are considered as decimal. Thus 0b10101010 = 0252 = 170 = 0xAA in binary, octal, decimal and hexadecimal.

Also take note that the line ends with a semicolon (;). This is the syntax of C language. All statement MUST ends with semicolon. Since the compiler ignore white spaces (as well as carriage returns and line feed characters), the semicolon serve to tell the compiler where a statement ends.

the Setup needed for MikroC
Line 7: Sound_Init(&PortB, 0);
This line is needed for us to use the Sound_Play() function later in the program. Open MikroC help and search for Sound in the index. The functions and how to use it are explained there. In this example, we specify that we want sound to be generated on PORTB, pin 0.

This is an example of a library function. The capabilities is provided by MikroC, not PIC directly. MikroC will create the necessary PIC codes to perform the functions thus making our lives a lot easier.

the while(expr) section, line 8 to 15
the while statement is used to execute a block of codes bounded by the opening and closing braces. The codes will execute and loop for as long as the expr expression evaluate to true. In C, anything that is not zero (0) is true. Thus, in this example, since we used while(1), the expr will always be true, thus the codes within the braces will execute indefinitely... infinite loop.

Do take note that the expr is evaluated when the while statement is first encountered and re-evaluated after the last statement in the block is executed. Thus, the while statement can be used to execute a block of code while certain condition is true.

The while statement can also be a one liner such as while(PORTA.F0==1);
In the above example, the while statement will continue until PORTA pin 0 is pulled to the ground. It's normally used to wait for a key to be pressed.

The above example also bring up another very important C syntax...
the = sign is used for assignment. Thus when we write TRISB = 0b11000000; the compiler will assign TRISB the value of 0b11000000
the == sign is used for comparison. Thus when we write PORTA==0xFF we are asking the compiler to check if the value of PORTA is equal to 0xFF or not. If equal then the expression is true otherwise will be set as false.
A very common error is using = instead of == in conditional testing.
Using = alone will result in the expression always evaluating to true...

the Actual codes... finally...
Line 9: PORTB = 0b00000010;
Here we are turning on pin 1 of PORTB. It effectively switching off the LED connected to the pin. Check the development board schematic to confirm this.

Line 10: delay_ms(400);
Another MikroC library function. It causes the PIC to wait for 400 milliseconds.

Line 11: PORTB = 0b00000100;
Let's switch off pin 1 and switch on pin 2 to get the alternating blinking LEDs on pin 1 & 2.

Line 12: delay_ms(400);
Wait for 400 milliseconds again. Without this delays, the LEDs will appear to lit continuously, because the on and off cycle to tooooo fast for the eyes to detect.

the Conditional test
Line 13: if (PORTB.F7==0) Sound_Play(1000,200);
Line 14: if (PORTB.F6==0) Sound_Play(1100,200);
Okay, remember the Sound_Init() line above? If you notice, there's no port or pin information in Sound_Play() function. It uses the information provided via the Sound_Init().

In these two lines, we do what's call conditional testing. We use the if statement... normal format as follows:
if(expr){
//execute if expr evaluate to true
}else{
//execute if expr evaluate to false
};

Just as the while() statement, if statement can also be one liner. Format is
if(expr) statement;

In the example above, we are checking if PORTB pin 6 or 7 are pressed. If pressed (0), then PIC execute the Sound_Play() function otherwise, PIC continue with the next statement.

After execution of line 14, PIC will re-evaluate the while() statement. Since the statement is true, PIC will loop back to line 9 and the process continue until you pull the plug...

How's the first lesson... ready to give up yet? Hang in there... there's more torture in the subsequent lessons... With the few statements already explained above, you're ready to embark on your initial exploration... Look into what library functions provided by MikroC... commonly used functions are there...

Okay, forgot to mention earlier... MikroC already predefines the PIC registers based on the CPU selected when you create new project. The register names are identical to the one used in the datasheet... eg, PORTA, PORTB, TRISA, ADCON1, ANSEL, etc.

Also, to refer to a single pin of any port, you can use the .Fn format. Thus PORTA.F0 refer to PORTA, pin 0 whereas PORTE.F1 refer to PORTE pin 1.

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.

_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.

Sunday, April 6, 2008

New casing for the development board

I was looking for suitable casing for the development board... I noticed the casing for the digital caliper was lying on the shelf gathering dust so... in the spirit of recycling, the casing for the development board was born... the 16x2 LCD was added to the box... also fabricated the female-female jumpers needed for the lessons...

Tuesday, April 1, 2008

Introduction to PIC...

While waiting for board distribution, in between fabrication of the jumpers, maybe the gang can browse through this webBook for some introduction to PIC microcontroller. Although the webBook refer to the PIC16F84, the concepts applies to the rest of PIC family...

PIC microcontrollers, for beginners too