C Language QR v2
C Language QR v2
C Language QR v2
Quick Reference
V1.2
Nov 2008
Information contained in this publication regarding device applications and the like is intended through suggestion only and may
be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. No
representation or warranty is given and no liability is assumed by Cytron Technologies Incorporated with respect to the accuracy
or use of such information, or infringement of patents or other intellectual property rights arising from such use or otherwise. Use
of Cytron Technologies’s products as critical components in life support systems is not authorized except with express written
approval by Cytron Technologies. No licenses are conveyed, implicitly or otherwise, under any intellectual property rights.
ROBOT . HEAD to TOE
Quick Reference – C Language
1st question, Why C language? Because C offers unmatched power and flexibility in
programming microcontrollers.
a. Comments
b. Include header
file
c. Configuration
bits
d. Include libraries,
function declaration,
global variable
e. Function name
Begin of
Block
f. Function
body
End of block
a. C comments
/* Put your comments here. This is actually the format of C comments. It may takes
multiple lines, as long as it is end with */
OR
// Put your comments after “//”, this only allow one line.
// If you need multiple lines you will need “//” in front of each line.
// This is C++ format of putting comments.
As the file placed before the program proper, it is called a header file (with the file
extension .h).
c. Configuration bits
A macro that writes a value into the special register that controls the core executing
operations of the microcontroller. It start with two “_” and followed with
CONFIG(configuration bits);
If the configuration bits are not specified correctly, the PIC MCUs might not run the
application code properly
e. Function name
All C programs are written in terms of functions. It is the basic building block in C
programming. C program must have at least the function main( ). The void means the
main( ) function doesn't return any value when it exit (finish running)..
f. Function body
Every function, including main( ), must have a body enclosed in braces { }.The function
body (also called block) can be of any size. The curly braces are to signify the block of
codes belonging to main( ). Do take note that there MUST NOT be a semicolon after the
closing brace.
3. Radix format
Radix format is the way a value is being presented in C language
There are four method to present value:
Radix Format Comments
Binary 0bnumber or 0Bnumber MPASM binary is in format B’number’.
Valid Invalid
4.2 Keywords, also called reserved words, have standard, predefined meanings and must be
used only for their intended purpose.
asm auto break case char
class const continue default delete
do double else enum extern
far float for friend goto
huge if inline int interrupt
long near new operator private
protected public register return short
sizeof static struct switch this
typedef union unsigned virtual void
volatile while
4.3 Variables are identifiers whose value may change during the course of execution of a
program. Before variables can be used in C program, it must be declare or define.
Variable is define based on data types.
type variable_name;
Example:
float amount; //Declares amount as a floating variable
Array
Used to store related items in the same block of memory and under a specified name.
Declared by specifying its type, name and the number of elements it will store. The general
form of the declaration is
Example:
unsigned int Total [5]; //Declares an array of unsigned int
//with the name Total, and
//it will store 5 elements.
or
unsigned int days[4] = {1,200,3,365};
If ‘A’ is being stored, the single quote ‘ mark will convert the character into ASCII code,
which is decimal 65 (0x41) for character A.
To store character:
unsigned char name[4] = {‘O’,’b’,’e’,’r’};
is the same as:
unsigned char name[4] = {79, 98, 101, 114};
Noted that 4 ASCII character stored in 5 elements, why? Because C will need to terminate
the string with ‘/0’ or NULL or 0 at the last element. No { } is required for string.
Pointer
Pointer is used to position a memory.
The value inside pointer is used as address of memory.
type *pointer_name;
5. Operators
PICC Lite Compiler recognizes following operators:
- Arithmetic Operators
- Assignment Operators
- Bitwise Operators
- Logical Operators
- Relational Operators
expression1 = expression2
Operator Meaning
&& AND
|| OR
! NOT
Operator Meaning
6. Statements
Statements can be roughly divided into:
- Labeled Statements
- Selection Statements
- Iteration Statements (Loops)
- Jump Statements
Label_identifier: statement;
Example 1:
loop : Display(message);
goto loop; //infinite loop that call Display function
Example 2:
Use in switch statement…….
if Statement
if (expression) statement1 [else statement2]
else keyword with alternative statement is optional
switch Statement
switch (expression)
{
case expression_1 :
statement sequence
case expression_2 :
statement sequence
.
.
default:
statement sequence
}
While Statements
while (expression) statement
Example:
while ((sensor1==0) && (sensor2==1) && (sensor3==0))
{
Motor1 = 100; // Motor’s PWM value
Motor2 = 100; // moving forward
}
Do Statements
do statement;
while (expression);
Example:
unsigned char i, s;
s = i = 0;
do
{
s = s + 2;
i = i + 1;
}
while( i < 7); // at the end, s = 14
For Statements
for ([initialization];[ condition-exp];[ increment-exp])
statement
Example:
for (i = 1 ; i <= 20 ; i++)
{
lcd_char(i); // display i on LCD
delay(1000);
} //i will be display from 1 until 20
Break Statements
A break terminates the nearest enclosing loop. Applicable for switch, for, while and do
block
Example:
unsigned char j = 0, s = 1;
while (1) {
if (j == 4) break;
s = s * 2;
j++;
} // result s = 16
Continue Statements
Unlike the break statement, continue does not force the termination of a loop, it skip the rest
of statements and jump to the first statement in the loop.
Example:
unsigned char Sum = 0, i = 0;
for (i=1 ; i<=10; i++)
{
if (i==5) continue;
Sum = Sum + i;
} //This will add up 1, 2, 3, 4, 6, 7, 8, 9, 10
Goto Statements
The goto statement has the form
goto label;
Example:
TOGGLE:
RTC++;
if (RB0 == 1)
goto TOGGLE; //if RB0=1, program jump to TOGGLE
else if (RB1 == 1)
RTC--;
else
RTC = 0xFF;
Return Statements
The return statement has the form
return expression;
To terminate execution of the current function and optionally pass the return value contained
in the expression. The value return must be of the same type or convertible to the same types
as the function.
Example:
void main()
{
…
c = Calculate(a, b);
…
}
7. Defining functions
function_type function_name(parameter-list);
Example:
unsigned char Cal (unsigned char a, unsigned char b);
Function Definition
C program function definition takes the form
Example:
Prepared by
Cytron Technologies Sdn. Bhd.
19, Jalan Kebudayaan 1A,
Taman Universiti,
81300 Skudai,
Johor, Malaysia.
URL: www.cytron.com.my
Email: [email protected]
[email protected]