Structure of A C18 Program
Structure of A C18 Program
Discussion:
void main (void) main It is the main program (that is why the name main) that is always executed. It is correctly known as a function, which essentially is same as subroutine in an assembly language. It is always written (expressed) in lower case It is preceded by void, again expressed in lower case It is followed by (void), again in lower case Every C18 program will have a main function If a program has multiple function, main program (function) is always executed first main (function) can call (execute) other parts (functions) of the overall program, if these are present. { It is known as the starting brace (curly bracket) It forms the starting boundary of the main program It has to be matched by a closing brace
S Gupta mcjournal.com Page 1 of 14
Variable Declarations
char x; int num; char x; This is the first line of program, also known as a C statement It has 3 parts: o char Indicates that we want to use a variable that will be able to hold an 8-bit value o x This is the name given to this variable It is the name of the place holder, where 8-bit value will be held o ; Indicates the end of this program line (C statement). Every line of program has to be terminated by a semicolon (with some exceptions) Int x; This is the second line of the program. It again is a C statement It too has 3 parts: o Int Indicates that we want to use a variable that will be able to hold a 16-bit value o num This is the name given to this variable o ; Indicates the end of the program line (C statement). )
Variable Assignments
x = 10; num = 12345; x = 10 ; This is the third line of program (a C statement) It has 4 parts: o x The name of the variable to which we wish to assign value o = This is assignment statement (same as in algebra)
S Gupta mcjournal.com Page 2 of 14
num = 12345 ; This is the 4th C statement It too has 4 parts: o num The name of the variable to which we wish to assign value o = This is the assignment statement o 12345 This is value that is assigned to the variable o ; Indicates the end of this C statement, as is required.
Closing Remarks
All variable declarations should be done before assigning any value to these. The following two programs are incorrect: Incorrect Example 1b In the following the values are assigned first and the name of variables is declared later. void main (void) { x = 10; num = 12345; char x; int num; } (Incorrect) Example 1c In the following The first variable x is correctly declared first and then a value is assigned to it The second variable num is (incorrectly) assigned the value first are assigned first and then it is declared. void main (void) { char x; x = 10; num = 12345; int num; }
S Gupta mcjournal.com Page 3 of 14
Summary
A C18 program (at the minimum) consists of a main program (function) The entire body of main function is enclosed by a pair of braces The variables are declared at the start Any number of variables can be declared Any legal variable name can be used After all variable have been declared, values can be assigned to required variables Every C statement is terminated by a semi-colon.
S Gupta
mcjournal.com
Page 4 of 14
Discussion:
An additional variable y is declared and assigned a value later Example 3 void main (void) { char x; char y; int num; int sum; x = 10; y = 5; num = 12345; sum = num + 1000; }
Discussion:
An additional variable sum is declared and is assigned a value in an algebraic expression
S Gupta
mcjournal.com
Page 5 of 14
Discussion:
Both variables x and y are of type char and can thus be declared in a single statement.
Example 3b void main (void) { char x, y; int num, sum; x = 10; y = 5; num = 12345; sum = num + 1000; }
Discussion:
Here x and y are declared in a single statement as both are of type char Similarly, both num and sum are of type int and are declared in a single statement.
S Gupta
mcjournal.com
Page 6 of 14
Discussion:
num is an int type and cannot be declared in the same statement as char types
S Gupta
mcjournal.com
Page 7 of 14
Discussion:
Here x is first declared as a char type variable and in the next statement it is used again and declared as an int type. It is not permitted.
Notes:
A variable name can again be used in another function. These variables with same names, but in different functions will behave in a manner similar to two different people with same name but in different households
S Gupta
mcjournal.com
Page 8 of 14
Program: // Name: SG // pgm1.c #include <p18F2520.h> void main (void) { TRISCbits.TRISC0 = 0; LATCbits.LATC0 = 1; }
Discussion:
// Name: SG This line is a comment line It is used to provide general information, such as programmers name, date, program name etc. It is ignored by C18 compiler A comment line starts by two front slashes // Any number of comment lines can be used A comment line can be used at the end of any C statement. (another form of comment can even be inserted in the middle of a C statement) // pgm1.c This is also a comment line It is providing the name of the program file. A C18 program file will have .c extension #include <p18F2520.h> This line indicates the name of file that is to be included along with the main program The file contains name and corresponding addresses in PIC18F2520 hardware The addresses of TRISC and LATC registers and their bits are contained in this file. This file is part of C18 compiler and is contained in ..MCC18/h folder.
S Gupta
mcjournal.com
Page 9 of 14
S Gupta
mcjournal.com
Page 10 of 14
// set RC0 as an output // set RC0 High to turn on LED // set RC0 Low to turn off LED
Discussion:
The program operation is as follows: It first sets the direction of RC0 as output. Pin level of RC0 is set to high to turn on LED Pin level of RC0 is then set low to turn off LED
S Gupta
mcjournal.com
Page 11 of 14
// set RC0 as an output // set RC0 High to turn on LED // Insert one instruction cycle delay // set RC0 Low to turn off LED
Discussion:
The program improves on pgm2.c by allowing LED to stay on for 1 instruction cycle, before turning off the LED. Considering typical clock frequencies, it is still too fast for human eye, but it can be easily tested in simulator.
S Gupta
mcjournal.com
Page 12 of 14
Discussion:
The program essentially uses the code in pgm3.c and encases all the statements that flash the LED (turn the LED on, an instruction cycle delay, and turn the LED off) inside a while loop. LATCbits.LATC0 = 1; is first executed Nop (); is the next statement to execute LATCbits.LATC0 = 0; is the last statement to execute On reaching the closing brace, the execution is repeated starting with the first statement.
S Gupta
mcjournal.com
Page 13 of 14
(Modified pgm1.c)
void main (void) { TRISCbits.TRISC0 = 0; LATCbits.LATC0 = 1; LATCbits.LATC0 = 0; while (1); } // set RC0 as an output // set RC0 High to turn on LED // set RC0 Low to turn off LED // keep executing this loop forever
Explanation
The developed program is burnt in program memory (Flash)to be executed on a POR. In practically all cases, the program placed in program memory is much smaller than the program memory capacity. On a POR, the MCU reads the first instruction from location 0x0000 and starts executing the instruction. Once a microcontroller has completed executing an instruction, it reads the next instruction as laid out in the program and executes it. It continues to execute one instruction after another tirelessly. In a program such as the original pgm1.c, there are only 3 C statements. The MCU is supposed to stop execution after completing the 3rd C statement. Unfortunately, the MCU will strive to read instructions from the next program memory location and execute it. Even though, these memory locations may not contain valid instructions, MCU will do its best to interpret these as codes and execute these. This can result is unexpected outputs to be generated at Port pins, resulting in unsafe operation. By placing a while (1); statement, the process repeated execute this C statement is prevented from trying to execute code from the subsequent program memory locations.
Practical Situation
It is very rare in a real life situation, where MCU is only required to execute a small program just once. In practically all cases, the program will contain a loop that will execute a number of C statements on a repeated basis.
S Gupta
mcjournal.com
Page 14 of 14