You are on page 1of 2

C Programming

Quick Reference Guide


for
PIC Micro Controllers
using the
HI-TECH

C Compiler
For a complete reference consult the HI-TECH

manual.
Programming Style
Making your program readable is good programming
practice. A readable program is much easier to
understand, troubleshoot and maintain.
Use blank lines to separate one conceptual
section of a function from another.
Use one line per statement.
Indent each progressive block successively
use auto indent.
Clarify the program low by writing comments
as appropriate.
Do not use a semicolon for an empty statement
in a control structure use { }
Control
for (initial condition ; terminating condition ; step )
{
}

if( condition)
{
}
else
{
}
while (condition)
{
}

do {
statement ;
}while (condition) ; // note the semicolon !

switch ( selector )
{
case selection :
Statement ;
[break] ;
}

Auto Inc & Dec
count = count + 1 ; / / same as
count ++ ; / / see compound
oper at or s
EEPROM Read & Write functions
voi d eepr om_wr i t e(
unsi gned char addr ,
unsi gned char val ue) ;
unsi gned char eepr om_r ead(
unsi gned char addr ) ;
e. g.
eepr om_wr i t e( 3, 27) ;
PORTC = eepr om_r ead( 3) ;
Function Prototype
void identifier (parameters) ;
// these are placed at the start of the program.
Note the semicolon at the end of the line.
ISR Interrupt Service Routine
voi d i nt er r upt I SR ( voi d)
{
i f ( T0I F)
{
T0I F = 0 ; / / r eset f l ag
st at ement ;
}
/ / ot her f l ags checked i f r equi r ed
}
Comments
Comments can be included on a line after // e.g.
PORTC = 0x00 ; / / PORTC set t o zer o
Block header comments can be placed between /* and */
/ *
t empl at e. c J an 2011 Ver si on 1
pl ace comment s her e
*/
Compiler Directives
These are not C Programming statements and therefore no
semicolon at the end of the line
# i ncl ude <ht c. h>
# def i ne ON 1
PORTC = ON ; / / usage
Numbers
Decimal 134 Binary 0b10000111 Hex 0x12
// Binary and Hex start with number zero
Statements
End in a semicolon ;
PORTC = 0xFF ;
Compound statements are grouped with curly braces {}
Types
For complete details of all variable types refer to the HI-
TECH C manual section 3.3
bi t doneFl ag ; / / 1 bi t 2
0
( bl ank i s same as si gned)
si gned char smal l Nr ; / / 1 byt e 2
7

unsi gned char number ; / / 1 byt e 2
8

si gned i nt medSi zeNr ; / / 2 byt es 2
15

unsi gned i nt bi gNum; / / 2 byt es 2
16

si gned l ong l ar ge ; / / 4 byt es 2
31

unsi gned l ong ver yBi g ; / / 4 byt es 2
32

( wi t h deci mal poi nt s)
f l oat l engt h ; / / 4 byt es 1. 0e
38

Quick Reference C Programming for Micro Controllers
Variables
Variable names begin with a letter or the underscore
character. useCamelCase for readability.
Constants
By convention they are capitalised with each word separated
by the underscore. See also compiler directives.
#define BAUD_RATE 9600
See the HI-TECH manual section 3.4.5 for more details.
Arrays
Type identiier [ number of elements] = {irst element
data, next data, } ;
char leddata[2] ={0x7E, 0b00011111 };
char string[] =123;
Modifiers
Constants - ixed values stored in lash memory;
const char leddata[2] ={0x7E, 0b00011111 };
Static variables local variables where value is preserved
between calls to the function.
static char count ;
Volatile used with interupts for variables whose value may
change in the ISR or main program.
volatile int mSec ;
Assignment Operator
A = 5 ;
Relational Operators
Used in conditional testing:
== is equal to
!= is not equal to
> is greater than
< is less than
>= is greater than or equal to
<= is less than or equal to
Arithmetic Operators
The + - * / operators give us add, subtract,
multiply and divide.
Integer Division
Using the / operator will return the whole number.
y =30 / 10 ; // returns a value of 3 to y.
Modulus Division
Using the % operator will return the remainder.
y =27 % 10 ; // returns a value of 7 to y.
Logical Operators
&& AND e.g. r esul t = t r ue && f al se;
|| OR
! NOT
Bitwise Operators
The bitwise operators take the individual bits of the operands
and act on them respectively.
& AND
| OR
^ XOR
~ NOT
>>n shift right n is the number of places to shift
<<n shift left eg PORTC =PORTC <<8 ;
PORTC <<=8 ;

e.g.
0b10101010
& 0b11110000
= 0b10100000
Compound Operators
++ ( i ncr ement )
- - ( decr ement )
+= ( compound addi t i on)
- = ( compound subt r act i on)
*= ( compound mul t i pl i cat i on)
/ = ( compound di vi si on)
&= ( compound bi t wi se and)
| = ( compound bi t wi se or )
eg PORTC += 10 ; / / adds 10 t o por t C

You might also like