You are on page 1of 28

PIC Microcontrollers The basics of C programming language

References: http://www.mikroe.com and the Hi-Tech C Manual Page 1 / 28

EPAI-Fribourg Herv Page - support c!o"t - ver!1!1

PIC Microcontrollers The basics of C programming language


References: http://www.mikroe.com and the Hi-Tech C Manual Page 2 / 28

Table of contents
PROGRAMMI G !A G"AG#$............................................................................................% A&'A TAG#$ O( HIGH#R PROGRAMMI G !A G"AG#$....................................) PR#PROC#$$OR........................................................................................................ )
PR#PROC#$$OR &IR#CTI'# * include......................................................................................... + PR#PROC#$$OR &IR#CTI'# * de,ine........................................................................................... +

TH# -A$IC$ O( C PROGRAMMI G !A G"AG#............................................................... COMM# T$................................................................................................................/0 &ATA T1P#$ I C !A G"AG#................................................................................../0 &eclarin2 'aria3le4............................................................................................. /0 Pointer4............................................................................................................... // Chan2in2 indi5idual 3it4...................................................................................... // &eclaration4........................................................................................................ // CO $TA T$.............................................................................................................../6
I T#G#R CO $TA T$.................................................................................................................. /6 (!OATI G POI T CO $TA T$.................................................................................................... /% CHARACT#R CO $TA T$ 7A$CII CHARACT#R$8..................................................................... /% $TRI G CO $TA T$.................................................................................................................... /% # "M#RAT#& CO $TA T$......................................................................................................... /% 'ARIA-!#$..................................................................................................................................... /0

OP#RATOR$9 OP#RATIO $ A & #:PR#$$IO $................................................./;


ARITHM#TIC OP#RATOR$............................................................................................................ /; A$$IG M# T OP#RATOR$.......................................................................................................... /; I CR#M# T A & &#CR#M# T OP#RATOR$............................................................................ /< R#!ATIO A! OP#RATOR$............................................................................................................ /< !OGIC OP#RATOR$...................................................................................................................... /< -IT=I$# OP#RATOR$................................................................................................................... /) HO= TO "$# OP#RATOR$>........................................................................................................ /)

&ATA T1P# CO '#R$IO ........................................................................................ /+ CO &ITIO A! OP#RATOR$...................................................................................../. PROGRAM !OOP....................................................................................................... 60


=hile !OOP..................................................................................................................................... 60 (or !OOP........................................................................................................................................ 6/ &o-while !OOP................................................................................................................................ 6/ CO &ITIO A! OP#RATOR i,-el4e................................................................................................. /. $witch OP#RATIO ........................................................................................................................ /?

=RITI G CO&# I A$$#M-!1 !A G"AG#............................................................66 ARRA1$...................................................................................................................... 6% POI T#R$.................................................................................................................. 6< (" CTIO $................................................................................................................6)
&#C!ARATIO O( A #= (" CTIO .......................................................................................... 6. (" CTIO !I-RARI#$.................................................................................................................. 6. T=O-&IM# $IO A! ARRA1.......................................................................................................... 6;

EPAI-Fribourg Herv Page - support c!o"t - ver!1!1

PIC Microcontrollers The basics of C programming language


References: http://www.mikroe.com and the Hi-Tech C Manual Page # / 28

PROGRAMMING LANGUAG !
The microcontroller e@ecute4 the pro2ram loaded in it4 (la4h memorA. Thi4 i4 the 4o called e@ecuta3le code compri4ed o, 4eemin2lA meanin2le44 4eBuence o, Cero4 and one4. It i4 or2aniCed in /6-9 /;- or /)-3it wide word49 dependin2 on the microcontrollerD4 architecture. #5erA word i4 con4idered 3A the CP" a4 a command 3ein2 e@ecuted durin2 the operation o, the microcontroller. (or practical rea4on49 a4 it i4 much ea4ier ,or u4 to deal with he@adecimal num3er 4A4tem9 the e@ecuta3le code i4 o,ten repre4ented a4 a 4eBuence o, he@adecimal num3er4 called a He@ code. It u4ed to 3e written 3A the pro2rammer. All in4truction4 that the microcontroller can reco2niCe are to2ether called the In4truction 4et. A4 ,or PIC microcontroller4 the pro2rammin2 word4 o, which are compri4ed o, /; 3it49 the in4truction 4et ha4 %< di,,erent in4truction4 in total.

A4 the proce44 o, writin2 e@ecuta3le code wa4 endle44lA tirin29 the ,ir4t Ehi2herD pro2rammin2 lan2ua2e called a44em3lA lan2ua2e wa4 created. The truth i4 that it made the proce44 o, pro2rammin2 more complicated9 3ut on the other hand the proce44 o, writin2 pro2ram 4topped 3ein2 a ni2htmare. In4truction4 in a44em3lA lan2ua2e are repre4ented in the ,orm o, meanin2,ul a33re5iation49 and the proce44 o, their compilin2 into e@ecuta3le code i4 le,t o5er to a 4pecial pro2ram on a PC called compiler. The main ad5anta2e o, thi4 pro2rammin2 lan2ua2e i4 it4 4implicitA9 i.e. each pro2ram in4truction corre4pond4 to one memorA location in the microcontroller. It ena3le4 a complete control o, what i4 2oin2 on within the chip9 thu4 makin2 thi4 lan2ua2e commonlA u4ed todaA.

Howe5er9 pro2rammer4 ha5e alwaA4 needed a pro2rammin2 lan2ua2e clo4e to the lan2ua2e 3ein2 u4ed in e5erAdaA li,e. A4 a re4ult9 the hi2her pro2rammin2 lan2ua2e4 ha5e 3een created. One o, them i4 C. The main ad5anta2e o, the4e lan2ua2e4 i4 4implicitA o, pro2ram writin2. It i4 no lon2er po44i3le to know e@actlA
EPAI-Fribourg Herv Page - support c!o"t - ver!1!1

PIC Microcontrollers The basics of C programming language


References: http://www.mikroe.com and the Hi-Tech C Manual Page $ / 28
how each command e@ecute49 3ut it i4 no lon2er o, intere4t anAwaA. In ca4e it i49 a 4eBuence written in a44em3lA lan2ua2e can alwaA4 3e in4erted in the pro2ram9 thu4 ena3lin2 it.

$imilar to a44em3lA lan2ua2e9 a 4pecialiCed pro2ram in a PC called compiler i4 in char2e o, compilin2 pro2ram into machine lan2ua2e. "nlike a44em3lA compiler49 the4e create an e@ecuta3le code which i4 not alwaA4 the 4horte4t po44i3le.

(i2ure4 a3o5e 2i5e a rou2h illu4tration o, what i4 2oin2 on durin2 the proce44 o, compilin2 the pro2ram ,rom hi2her to lower pro2rammin2 lan2ua2e.

EPAI-Fribourg Herv Page - support c!o"t - ver!1!1

PIC Microcontrollers The basics of C programming language


References: http://www.mikroe.com and the Hi-Tech C Manual Page % / 28
Here i4 an e@ample o, a 4imple pro2ram written in C lan2ua2e:

EPAI-Fribourg Herv Page - support c!o"t - ver!1!1

PIC Microcontrollers The basics of C programming language


References: http://www.mikroe.com and the Hi-Tech C Manual Page & / 28

A"#ANTAG ! O$ %IG% R PROGRAMMING LANGUAG ! I, Aou ha5e e5er written a pro2ram ,or the microcontroller in a44em3lA lan2ua2e9 then Aou pro3a3lA know that the RI$C architecture lack4 in4truction4. (or e@ample9 there i4 no appropriate in4truction ,or multiplAin2 two num3er49 3ut there i4 al4o no rea4on to 3e worried a3out it. #5erA pro3lem ha4 a 4olution and thi4 one make4 no e@ception thank4 to mathematic4 which ena3le u4 to per,orm comple@ operation4 3A 3reakin2 them into a num3er o, 4imple one4. ConcretelA9 multiplication can 3e ea4ilA 4u34tituted 3A 4ucce44i5e addition 7a @ 3 F a G a G a G ... G a8. And here we are9 Hu4t at the 3e2innin2 o, a 5erA lon2 4torA... &onDt worrA a4 ,ar a4 the hi2her pro2rammin2 lan2ua2e49 4uch a4 C9 are concerned 3ecau4e 4ome3odA ha4 alreadA 4ol5ed thi4 and manA other 4imilar pro3lem4 ,or Aou. It will do to write aI3.

PR PROC !!OR A preproce44or i4 an inte2ral part o, the C compiler and it4 ,unction i4 to reco2niCe and e@ecute preproce44or in4truction4. The4e are 4pecial in4truction4 which do not 3elon2 to C lan2ua2e9 3ut are a part o, 4o,tware packa2e comin2 with the compiler. #ach preproce44or command 4tart4 with E*D. Prior to pro2ram compilation9 C compiler acti5ate4 the preproce44or which 2oe4 throu2h the pro2ram in 4earch ,or the4e 4i2n4. I, anA encountered9 the preproce44or will 4implA replace them 3A another te@t which9 dependin2 on the tApe o, command9 can 3e a ,ile content4 or Hu4t a 4hort 4eBuence o, character4. Then9 the proce44 o, compilation maA 4tart. The preproce44or in4truction4 can 3e anAwhere in the 4ource pro2ram9 and re,er onlA to the part o, the pro2ram ,ollowin2 their appearance up to the end o, the pro2ram.
EPAI-Fribourg Herv Page - support c!o"t - ver!1!1

PIC Microcontrollers The basics of C programming language


References: http://www.mikroe.com and the Hi-Tech C Manual Page ' / 28
PR PROC !!OR "IR CTI# & inclu'e ManA pro2ram4 o,ten repeat the 4ame 4et o, command4 ,or 4e5eral time4. In order to 4peed up the proce44 o, writin2 a pro2ram9 the4e command4 and declaration4 are u4uallA 2rouped in particular ,ile4 that can ea4ilA 3e included in the pro2ram u4in2 thi4 directi5e. To 3e more preci4e9 the *include command import4 te@t ,rom another document9 no matter what it i4 7command49 comment4 etc.89 into the pro2ram.

PR PROC !!OR "IR CTI# & 'efine The #define command pro5ide4 macro e@pan4ion 3A replacin2 identi,ier4 in the pro2ram 3A their 5alue4.

#define symbol sequence_of_characters

#@ample: ... #define PI 3.14 ... A4 the u4e o, anA lan2ua2e i4 not limited to 3ook4 and ma2aCine4 onlA9 thi4 pro2rammin2 lan2ua2e i4 not clo4elA related to anA 4pecial tApe o, computer49 proce44or4 or operatin2 4A4tem4. C lan2ua2e i4 actuallA a 2eneral-purpo4e lan2ua2e. Howe5er9 e@actlA thi4 ,act can cau4e 4ome pro3lem4 durin2 operation a4 C lan2ua2e 4li2htlA 5arie4 dependin2 on it4 application 7thi4 could 3e compared to di,,erent dialect4 o, one lan2ua2e8.

EPAI-Fribourg Herv Page - support c!o"t - ver!1!1

PIC Microcontrollers The basics of C programming language


References: http://www.mikroe.com and the Hi-Tech C Manual Page 8 / 28

T% (A!IC! O$ C PROGRAMMING LANGUAG


The main idea o, writin2 pro2ram in C lan2ua2e i4 to 3reak a 3i22er pro3lem down into 4e5eral 4maller piece4. $uppo4e it i4 nece44arA to write a pro2ram ,or the microcontroller that i4 2oin2 to mea4ure temperature and 4how re4ult4 on an !C& di4plaA. The proce44 o, mea4urin2 i4 per,ormed 3A a 4en4or that con5ert4 temperature into 5olta2e. The microcontroller u4e4 it4 A/& con5erter to con5ert thi4 5olta2e 7analo2ue 5alue8 to a num3er 7di2ital 5alue8 which i4 then 4ent to the !C& di4plaA 5ia 4e5eral conductor4. Accordin2lA9 the pro2ram i4 di5ided in ,our part4 that Aou ha5e to 2o throu2h a4 per the ,ollowin2 order: /. 6. %. ;. Acti5ate and 4et 3uilt-in A/& con5erterJ Mea4ure analo2ue 5alueJ Calculate temperatureJ $end data in the proper ,orm to !C& di4plaA.

A4 4een9 the hi2her pro2rammin2 lan2ua2e4 4uch a4 C ena3le Aou to 4ol5e thi4 pro3lem ea4ilA 3A writin2 ,our ,unction4 to 3e e@ecuted cAclicallA and o5er and o5er a2ain.

EPAI-Fribourg Herv Page - support c!o"t - ver!1!1

PIC Microcontrollers The basics of C programming language


References: http://www.mikroe.com and the Hi-Tech C Manual Page ( / 28
The ,i2ure 3elow illu4trate4 the 4tructure o, a 4imple pro2ram9 pointin2 out the part4 it con4i4t4 o,.

EPAI-Fribourg Herv Page - support c!o"t - ver!1!1

PIC Microcontrollers The basics of C programming language


References: http://www.mikroe.com and the Hi-Tech C Manual Page 1) / 28

COMM NT! Comment4 are part o, the pro2ram u4ed to clari,A the operation o, the pro2ram or pro5ide more in,ormation a3out it. Comment4 are i2nored and not compiled into e@ecuta3le code 3A the compiler. $implA put9 the compiler can reco2niCe 4pecial character4 u4ed to de4i2nate where comment4 4tart and terminate and completelA i2nore4 the te@t in3etween durin2 compilation. There are two tApe4 o, 4uch character4. One de4i2nate4 lon2 comment4 e@tendin2 4e5eral pro2ram line4 7 /I .... I/ 89 while the other de4i2nate4 4hort comment4 takin2 up a 4in2le line 7 // 8. #5en thou2h comment4 cannot a,,ect the pro2ram e@ecution9 theA are a4 important a4 anA other part o, the pro2ram9 and here i4 whA... A written pro2ram can alwaA4 3e impro5ed9 modi,ied9 up2raded9 4impli,ied...It i4 almo4t alwaA4 done. =ithout comment49 trAin2 to under4tand e5en the 4imple4t pro2ram4 i4 wa4te o, time. "ATA T)P ! IN C LANGUAG There are 4e5eral tApe4 o, data that can 3e u4ed in C pro2rammin2 lan2ua2e. A ta3le 3elow 4how4 the ran2e o, 5alue4 which the4e data can ha5e when u4ed in their 3a4ic ,orm. #ARIA(L ! AnA num3er chan2in2 it4 5alue durin2 pro2ram operation i4 called a 5aria3le. $implA put9 i, the pro2ram add4 two num3er4 7num3er/ and num3er689 it i4 nece44arA to ha5e a 5alue to repre4ent what we in e5erAdaA li,e call the 4um. In thi4 ca4e num3er/9 num3er6 and 4um are 5aria3le4. "eclaring #ariables

'aria3le name can include anA o, the alpha3etical character4 A-K 7a-C89 the di2it4 0-? and the under4core character LML. The compiler i4 ca4e 4en4iti5e and di,,erentiate4 3etween capital and 4mall letter4. (unction and 5aria3le name4 u4uallA contain lower ca4e character49 while con4tant name4 contain upperca4e character4. 'aria3le name4 mu4t not 4tart with a di2it. $ome o, the name4 cannot 3e u4ed a4 5aria3le name4 a4 alreadA 3ein2 u4ed 3A the compiler it4el,. $uch name4 are called the key words. T*pe 3it char un4i2ned char 4hort un4i2ned 4hort int un4i2ned int 4hort lon2 un4i2ned 4hort lon2 lon2 un4i2ned lon2 ,loat dou3le !i+e ,bits/ . . /) /) /) /) 6; 6; %6 %6 6; 6; or %6 Arithmetic T*pe un4i2ned inte2er 4i2ned or un4i2ned inte2er un4i2ned inte2er 4i2ned inte2er un4i2ned inte2er 4i2ned inte2er un4i2ned inte2er 4i2ned inte2er un4i2ned inte2er 4i2ned inte2er un4i2ned inte2er real real

EPAI-Fribourg Herv Page - support c!o"t - ver!1!1

PIC Microcontrollers The basics of C programming language


References: http://www.mikroe.com and the Hi-Tech C Manual Page 11 / 28
Pointers A pointer i4 a 4pecial tApe o, 5aria3le holdin2 the addre44 o, character 5aria3le4. In other word49 the pointer Epoint4 toD another 5aria3le. It i4 declared a4 ,ollow4: type_of_variable *pointer_name;

In order to a44i2n the addre44 o, a 5aria3le to a pointer9 it i4 nece44arA to u4e the LFL character and write 5aria3le name preceded 3A the LNL character. In the ,ollowin2 e@ample9 the pointer Emultiple@D i4 declared and a44i2ned the addre44 o, the ,ir4t out o, ei2ht !#& di4plaA4: unsigned int *multiplex; // Declare name and type of pointer multiplex multiplex = !isplay1; // Pointer multiplex is assigned the address of // variable display1

To chan2e the 5alue o, the pointed 5aria3le9 it i4 4u,,icient to write the LIL character in ,ront o, it4 pointer and a44i2n it a new 5alue. *multiplex = "; // Variable display1 is assigned the number 6

$imilarlA9 in order to read the 5alue o, the pointed 5aria3le9 it i4 4u,,icient to write: temp = *multiplex; // The value of variable display1 is copied to temp Changing in'i.i'ual bits There are a ,ew waA4 to chan2e onlA one 3it o, a 5aria3le. The 4imple4t one i4 to 4peci,A the re2i4ter name9 3itL4 po4ition or a name and de4ired 4tate: #!efine $%&'( $') $*3 = ); ... $%&'( = 1; "eclarations #5erA 5aria3le mu4t 3e declared prior to 3ein2 u4ed ,or the ,ir4t time in the pro2ram. $ince 5aria3le4 are 4tored in RAM memorA9 it i4 nece44arA to re4er5e 4pace ,or them 7one9 two or more 3Ate48. 1ou know what tApe o, data Aou write or e@pect a4 a re4ult o, an operation9 while the compiler doe4 not know that. &onDt ,or2et9 the pro2ram deal4 with 5aria3le4 to which Aou a44i2ned the name4 gate, sum, minimum etc. The compiler reco2niCe4 them a4 re2i4ter4 o, RAM memorA. 'aria3le tApe4 are u4uallA a44i2ned at the 3e2innin2 o, the pro2ram. unsigned int +ate1; // Declare name and type of variable gate1 // Clear the bit 3 of P !T" // #et the bit named !$%&'

EPAI-Fribourg Herv Page - support c!o"t - ver!1!1

PIC Microcontrollers The basics of C programming language


References: http://www.mikroe.com and the Hi-Tech C Manual Page 12 / 28
Apart ,rom the name and tApe9 5aria3le4 are u4uallA a44i2ned initial 5alue4 at the 3e2innin2 o, the pro2ram a4 well. It i4 not a Emu4t-doD 4tep9 3ut a matter o, 2ood ha3it4. In thi4 ca4e9 it look4 a4 ,ollow4: unsigned int +ate1; // Declare type and name of the variable signed int start, sum; // Declare type and name of other t(o variables +ate1 = -); // &ssign variable gate1 an initial value

The proce44 o, a44i2nin2 initial 5alue and declarin2 tApe can 3e per,ormed in one 4tep: unsigned int +ate1=-); // Declare type) name and value of variable

I, there are 4e5eral 5aria3le4 3ein2 a44i2ned the 4ame initial 5alue9 the proce44 can 3e e5en 4impli,ied: unsigned int +ate1=+ate-=+ate3=-); signed int start=sm=);

TApe o, 5aria3le i4 not accompanied 3A the EGD or E-D 4i2n 3A de,ault. (or e@ample9 char can 3e written in4tead o, signed char 75aria3le i4 a 4i2ned 3Ate8. In thi4 ca4e the compiler con4ider4 5aria3le po4iti5e 5alue4. I, Aou9 3A anA chance9 ,or2et to declare 5aria3le tApe9 the compiler will automaticallA con4ider it a 4i2ned inte2er. It mean4 that 4uch a 5aria3le will occupA two memorA 3Ate4 and ha5e 5alue4 in the ran2e o, -%6+). to G%6+)+.

CON!TANT! A con4tant i4 a num3er or a character ha5in2 ,i@ed 5alue that cannot 3e chan2ed durin2 pro2ram e@ecution. "nlike 5aria3le49 con4tant4 are 4tored in the ,la4h pro2ram memorA o, the microcontroller ,or the purpo4e o, 4a5in2 5alua3le 4pace o, RAM. The compiler reco2niCe4 them 3A their name and pre,i@ const. INT G R CON!TANT! Inte2er con4tant4 can 3e decimal9 he@adecimal9 octal or 3inarA. In the ,ollowin2 e@ample9 the con4tant MI IM"M will 3e con4idered a 4i2ned inte2er and 4tored within two 3Ate4 o, (la4h memorA 7/) 3it48:

Ra'i/ 3inarA octal decimal he@adecimal

$ormat 03num3er or 0-num3er 0num3er num3er 0@num3er or 0:num3er

/ample 03/00//0/0 0+)% /6? 0@6(

EPAI-Fribourg Herv Page - support c!o"t - ver!1!1

PIC Microcontrollers The basics of C programming language


References: http://www.mikroe.com and the Hi-Tech C Manual Page 1# / 28

const int .I/I.0. = 11)); // Declare constant *+,+*-* $LOATING POINT CON!TANT! (loatin2 point con4tant4 con4i4t o, an inte2er part9 a dot9 a ,ractional part and an optional e or # ,ollowed 3A a 4i2ned inte2er e@ponent. const float 2_.'3 = 3-."); // Declare temperature T.*&/ const !ouble 2_.'3 = 3.-")%1; // Declare the same constant T.*&/

In 3oth e@ample49 a con4tant named T_MAX i4 declared to ha5e the ,ractional 5alue %6.)0. It ena3le4 the pro2ram to compare the mea4ured temperature to the meanin2,ul con4tant in4tead o, num3er4 repre4entin2 it. C%ARACT R CON!TANT! ,A!CII C%ARACT R!A character con4tant i4 a character enclo4ed within 4in2le Buotation mark4. In the ,ollowin2 e@ample9 a con4tant named I_CLASS i4 declared a4 A character9 while a con4tant named II_CLASS i4 declared a4 ( character. const char I_4&'55 = 6'6; // Declare constant +.C%&## const char II_4&'55 = 6*6; // Declare constant ++.C%&##

=hen de,ined thi4 waA9 the e@ecution o, the command4 4endin2 the I_CLASS and II_CLASS con4tant4 to an !C& di4plaA9 will cau4e the character4 A and ( to 3e di4plaAed9 re4pecti5elA. !TRING CON!TANT! A con4tant con4i4tin2 o, a 4eBuence o, character4 i4 called a 4trin2. $trin2 con4tant4 are enclo4ed within dou3le Buotation mark4. const char .essa+e_178 = 9Press the 52'$2 button9; // *essage 1 for %CD const char .essa+e_-78 = 9Press the $I:;2 button9; // *essage 0 for %CD const char .essa+e_378 = 9Press the &%<2 button9; // *essage 3 for %CD

In thi4 e@ample9 4endin2 the Message_ con4tant to an !C& di4plaA will cau4e the me44a2e !"ress the STA#T $utton! to 3e di4plaAed. NUM RAT " CON!TANT! #numerated con4tant4 are a 4pecial tApe o, inte2er con4tant4 which make a pro2ram more comprehen4i5e and ea4ier to ,ollow 3A a44i2nin2 element4 the ordinal num3er4. In the ,ollowin2 e@ample9 the ,ir4t element in curlA 3racket4 i4 automaticallA a44i2ned the 5alue 09 the 4econd one i4 a44i2ned the 5alue /9 the third one the 5alue 6 etc.
EPAI-Fribourg Herv Page - support c!o"t - ver!1!1

PIC Microcontrollers The basics of C programming language


References: http://www.mikroe.com and the Hi-Tech C Manual Page 1$ / 28

enum .=2=$5 >0P, ?=@/, &%<2, $I:;2A; // Declare constant * T !#

On e5erA occurrence o, the word4 !L%&T!9 !#I'(T!9 !)*! and !+,-.! in the pro2ram9 the compiler will replace them 3A the appropriate num3er4 70-%8. OP RATOR!0 OP RATION! AN" 1PR !!ION! An operator i4 a 4Am3ol denotin2 particular arithmetic9 lo2ic or 4ome other operation. There are more than ;0 operation4 a5aila3le in C lan2ua2e9 3ut at mo4t /0-/< o, them are u4ed in practice. #5erA operation i4 per,ormed upon one or more operand4 which can 3e 5aria3le4 or con4tant4. -e4ide49 e5erA operation ,eature4 prioritA e@ecution and a44ociati5itA a4 well. ARIT%M TIC OP RATOR! Arithmetic operator4 are u4ed in arithmetic operation4 and alwaA4 return po4iti5e re4ult4. "nlike unarA operation4 3ein2 per,ormed upon one operand9 3inarA operation4 are per,ormed upon two operand4. In other word49 two num3er4 are reBuired to e@ecute a 3inarA operation. (or e@ample: aG3 or a/3.

Operator G I / O A!!IGNM NT OP RATOR! There are two tApe4 o, a44i2nment4 in C lan2ua2e:


Operation Addition $u3traction Multiplication &i5i4ion Reminder

$imple operator4 a44i2n 5alue4 to 5aria3le4 u4in2 the common LFL character. (or e@ample: a F . Compound a44i2nment4 are 4peci,ic to C lan2ua2e and con4i4t o, two character4 a4 4hown in the ta3le. An e@pre44ion can 3e written in a di,,erent waA a4 well9 3ut thi4 one pro5ide4 more e,,icient machine code. Operator GF -F IF /F OF /ample #@pre44ion a GF . a -F . a IF . a /F . a OF . #Bui5alent aFaG. aFa-. aFaI. aFa/. aFaO.

EPAI-Fribourg Herv Page - support c!o"t - ver!1!1

PIC Microcontrollers The basics of C programming language


References: http://www.mikroe.com and the Hi-Tech C Manual Page 1% / 28

INCR M NT AN" " CR M NT OP RATOR! Increment and decrement 3A / operation4 are denoted 3A LGGL and L--L. The4e character4 can either precede or ,ollow a 5aria3le. In the ,ir4t ca4e 7GG@89 the @ 5aria3le will 3e ,ir4t incremented 3A /9 then u4ed in e@pre44ion. Otherwi4e9 the 5aria3le will 3e ,ir4t u4ed in e@pre44ion9 then incremented 3A /. The 4ame applie4 to the decrement operation. Operator GG -R LATIONAL OP RATOR! Relational operator4 are u4ed in compari4on4 ,or the purpo4e o, comparin2 two 5aria3le4 which can 3e inte2er4 7int8 or ,loatin2 point num3er4 7,loat8. I, an e@pre44ion e5aluate4 to true9 a / i4 returned. Otherwi4e9 a 0 i4 returned. Thi4 i4 u4ed in e@pre44ion4 4uch a4 Ei, the e@pre44ion i4 true then...D /ample GGa aGG --3 3-"escription 'aria3le PaP i4 incremented 3A / 'aria3le P3P i4 decremented 3A /

Operator Q QF R RF FF SF LOGIC OP RATOR!

Meaning i4 2reater than i4 2reater than or eBual to i4 le44 than i4 le44 than or eBual to i4 eBual to i4 not eBual to

/ample 3Qa a QF < aR3 a RF 3 a FF ) a SF 3

Truth con'ition i, b i4 2reater than a I, a i4 2reater than or eBual to 2 i, a I4 le44 than b i, a I4 le44 than or eBual to b i, a I4 eBual to 3 i, a I4 not eBual to b

There are three tApe4 o, lo2ic operation4 in C lan2ua2e: lo2ic A &9 lo2ic OR and ne2ation 7 OT8. (or the 4ake o, clearne449 lo2ic 4tate4 in ta3le4 3elow are repre4ented a4 lo2ic Cero 70F,al4e8 and lo2ic one 7/Ftrue8. !o2ic operator4 return true 7lo2ic /8 i, the e@pre44ion e5aluate4 to non-Cero9 and ,al4e 7lo2ic 08 i, the e@pre44ion e5aluate4 to Cero. Thi4 i4 5erA important 3ecau4e lo2ic operation4 are commonlA u4ed upon e@pre44ion49 not upon 4in2le 5aria3le4 7num3er48 in the pro2ram. There,ore9 lo2ic operation4 re,er to the truth o, the whole e@pre44ion. (or e@ample: 1 && 0 i4 the 4ame a4 (true expression) && (false expression) The re4ult i4 09 i.e. - &a/se in either ca4e.

Operator 0 0 / /
EPAI-Fribourg Herv Page - support c!o"t - ver!1!1

Logical AN" Operand/ Operand6 0 / 0 / Re4ult 0 0 0 /

PIC Microcontrollers The basics of C programming language


References: http://www.mikroe.com and the Hi-Tech C Manual Page 1& / 28
Operator Operand/ 0 BB 0 / / Logical OR Operand6 0 / 0 / Re4ult 0 / / /

Operator C

Logical NOT Operand/ 0 / Re4ult / 0

(IT4I! OP RATOR! "nlike lo2ic operation4 3ein2 per,ormed upon 5aria3le49 the 3itwi4e operation4 are per,ormed upon 4in2le 3it4 within operand4. -itwi4e operator4 are u4ed to modi,A the 3it4 o, a 5aria3le. TheA are li4ted in the ta3le 3elow:

Operan' D EE FF

Meaning -itwi4e complement $hi,t le,t $hi,t ri2ht -itwi4e A &

/ample a F T3 a F 3 RR 6 a F 3 QQ % cFaN3 cFaU3 cFaV3 3F<

Result a F -< a F //00//00 a F 000////0 c F //000000 c F ///0//// c F 00/0//// 3 F ////00// 3 F ////00// a F ///000// 3 F //00//00 a F ///000// 3 F //00//00 a F ///000// 3 F //00//00

B G

-itwi4e OR -itwi4e #:OR

%O4 TO U! OP RATOR!5 #@cept ,or a44i2nment operator49 two operator4 mu4t not 3e written ne@t to each other. x*H1-; // such expression (ill generate an error Operator4 are 2rouped to2ether u4in2 parenthe4e4 4imilar to arithmetic e@pre44ion4. The e@pre44ion4 enclo4ed within parenthe4e4 are calculated ,ir4t. I, nece44arA9 multiple 7ne4ted8 parenthe4e4 can 3e u4ed. #ach operator ha4 it4 prioritA and a44ociati5itA a4 4hown in the ta3le.

EPAI-Fribourg Herv Page - support c!o"t - ver!1!1

PIC Microcontrollers The basics of C programming language


References: http://www.mikroe.com and the Hi-Tech C Manual Page 1' / 28

Priorit* Hi2h

Operators IJ 78 1F . C D KK 11 KIunaryJ 1IunaryJ *Pointer * L H K 1 E F E E= F F= == C= G B BB MN Pointer

Associati.it* ,rom le,t to ri2ht ,rom ri2ht to le,t ,rom le,t to ri2ht ,rom le,t to ri2ht ,rom le,t to ri2ht ,rom le,t to ri2ht ,rom le,t to ri2ht ,rom le,t to ri2ht ,rom le,t to ri2ht ,rom le,t to ri2ht ,rom le,t to ri2ht ,rom ri2ht to le,t ,rom ri2ht to le,t

!ow "ATA T)P CON# R!ION

= K= 1= *= L= L=

= G= B= E= F=

,rom le,t to ri2ht

The main data tApe4 are put in hierarchical order a4 ,ollow4:

I, two operand4 o, di,,erent tApe are u4ed in an arithmetic operation9 the lower prioritA operand tApe i4 automaticallA con5erted into the hi2her prioritA operand tApe. In e@pre44ion4 ,ree ,rom a44i2nment operation9 the re4ult i4 o3tained in the ,ollowin2 waA:

I, the hi2he4t prioritA operand i4 o, tApe dou$/e9 then tApe4 o, all other operand4 in the e@pre44ion a4 well a4 the re4ult are automaticallA con5erted into tApe dou$/e. I, the hi2he4t prioritA operand i4 o, tApe /ong9 then tApe4 o, all other operand4 in the e@pre44ion a4 well a4 the re4ult are automaticallA con5erted into tApe /ong. I, the operand4 are o, /ong or char tApe9 then tApe4 o, all other operand4 in the e@pre44ion a4 well a4 the re4ult are automaticallA con5erted into tApe int.

Auto con5er4ion i4 al4o per,ormed in a44i2nment operation4. The re4ult o, the e@pre44ion ri2ht ,rom the a44i2nment operator i4 alwaA4 con5erted into the tApe o, 5aria3le le,t ,rom the operator. I, the re4ult i4 o, hi2her-ranked tApe9 it i4 truncated or rounded in order to match the tApe o, 5aria3le. =hen con5ertin2 real data into inte2er9 num3er4 ,ollowin2 the decimal point are alwaA4 truncated.

EPAI-Fribourg Herv Page - support c!o"t - ver!1!1

PIC Microcontrollers The basics of C programming language


References: http://www.mikroe.com and the Hi-Tech C Manual Page 18 / 28

int x; // Variable x is declared as integer int x = 3; // Variable x is assigned value 3 x K= 3.14; // ,umber P+ 132134 is added to variable x by performing // the assignment operation /5 The result of addition is 6 instead of expected 62132 To obtain the expected result (ithout truncating the numbers follo(ing the decimal point) common addition should be performed 1x632134) 2 5/ CON"ITIONAL OP RATOR! A condition i4 a common in2redient o, the pro2ram. =hen met9 it i4 nece44arA to per,orm one out o, 4e5eral operation4. In other word4 !If the condition is met 0...1, do 0...1. ,therwise, if the condition is not met, do 0...1! . Conditional operand4 if6else and s7itch are u4ed in conditional operation4. CON"ITIONAL OP RATOR if6else The conditional operator can appear Here i4 an e@ample o, the if operator: ifIexpressionJ operation; in two ,orm4 a4 if and if6else operator.

I, the re4ult o, e2"ression enclo4ed within 3racket4 i4 not 0 7true89 the o"eration i4 per,ormed and the pro2ram proceed4 with e@ecution. I, the re4ult o, e2"ression i4 0 7,al4e89 the o"eration i4 not per,ormed and the pro2ram immediatelA proceed4 with e@ecution. A4 mentioned9 the other ,orm com3ine4 3oth if and else operator4: ifIexpressionJ operation1; else operation-;

I, the re4ult o, e2"ression i4 not 0 7true89 o"eration i4 per,ormed9 otherwi4e o"eration3 i4 per,ormed. A,ter per,ormin2 either operation9 the pro2ram proceed4 with e@ecution. The 4Anta@ o, the if6else 4tatement i4: ifIexpressionJ operation1; else operation-;

EPAI-Fribourg Herv Page - support c!o"t - ver!1!1

PIC Microcontrollers The basics of C programming language


References: http://www.mikroe.com and the Hi-Tech C Manual Page 1( / 28
I, either o"eration or o"eration3 i4 compound9 a 2roup o, operation4 the4e con4i4t o, mu4t 3e enclo4ed within curlA 3racket4. (or e@ample: ifIexpressionJ > operation1; operation07 A else > operation3; operation4; A

The if6else operator can 3e written u4in2 the conditional operator L>:L a4 in e@ample 3elow: Iexpression1JM expression- N expression3

I, e2"ression i4 not 0 7true89 the re4ult o, the whole e@pre44ion will 3e eBual to the re4ult o3tained ,rom e2"ression3. Otherwi4e9 i, e2"ression i4 0 7,al4e89 the re4ult o, the whole e@pre44ion will 3e eBual to the re4ult o3tained ,rom e2"ression4. maximum = Ia F bJM a N b // Variable maximum is assigned the value of // larger variable 1a or b4 !7itch OP RATION "nlike the if6else 4tatement which make4 4election 3etween two option4 in the pro2ram9 the s7itch operator ena3le4 Aou to choo4e 3etween 4e5eral operation4. The 4Anta@ o, the s7itch 4tatement i4: switch IselectorJ > // case constant1N operation1; // ... // break; case constant-N operation-; // ... // break; ... defaultN expecte!_operation;// ... // break; A #elector is of char or int type 8roup of operators are executed if selector and constant1 are e9ual 8roup of operators are executed if selector and constant0 are e9ual

8roup of operators are executed if no constant is e9ual to selector

EPAI-Fribourg Herv Page - support c!o"t - ver!1!1

PIC Microcontrollers The basics of C programming language


References: http://www.mikroe.com and the Hi-Tech C Manual Page 2) / 28
The s7itch operation i4 e@ecuted in the ,ollowin2 waA: se/ector i4 e@ecuted ,ir4t and compared to constant . I, match i4 ,ound9 4tatement4 in that ca4e 3lock are e@ecuted until the $reak keAword or the end o, the s7itch operation i4 encountered. I, no match i4 ,ound9 se/ector i4 ,urther compared to constant3 and i, match i4 ,ound9 4tatement4 in that ca4e 3lock are e@ecuted until the $reak keAword i4 encountered and 4o on. I, the 4elector doe4nDt match anA con4tant9 operation4 ,ollowin2 the 'efault operator are to 3e e@ecuted. PROGRAM LOOP It i4 o,ten nece44arA to repeat a certain operation ,or a couple o, time4 in the pro2ram. A 4et o, command4 3ein2 repeated i4 called the pro2ram loop. How manA time4 it will 3e e@ecuted9 i.e. how lon2 the pro2ram will 4taA in the loop9 depend4 on the condition4 to lea5e the loop. 4hile LOOP The 7hile loop look4 a4 ,ollow4: whileIexpressionJ> comman!s; ... A

The commands are e@ecuted repeatedlA 7the pro2ram remain4 in the loop8 until the e2"ression 3ecome4 ,al4e. I, the e2"ression i4 ,al4e on entrA to the loop9 then the loop will not 3e e@ecuted and the pro2ram will proceed ,rom the end o, the 7hile loop. A 4pecial tApe o, pro2ram loop i4 the end/ess /oo". It i4 ,ormed i, the condition remain4 unchan2ed within the loop. The e@ecution i4 4imple in thi4 ca4e a4 the re4ult in 3racket4 i4 alwaA4 true 7/F/89 which mean4 that the pro2ram reman4 in the 4ame loop: whileI1J> ... // $xpressions enclosed (ithin curly brac:ets (ill be ... // endlessly executed 1endless loop42 A

EPAI-Fribourg Herv Page - support c!o"t - ver!1!1

PIC Microcontrollers The basics of C programming language


References: http://www.mikroe.com and the Hi-Tech C Manual Page 21 / 28

$or LOOP The for loop look4 a4 ,ollow4: forIinitial_expression; con!ition_expression; chan+e_expressionJ > operation; ... A

The e@ecution o, 4uch pro2ram 4eBuence i4 4imilar to the 7hile loop9 e@cept that in thi4 ca4e the proce44 o, 4ettin2 initial 5alue 7initialiCation8 i4 per,ormed within declaration. The initia/_e2"ression 4et4 the initial 5aria3le o, the loop9 which i4 ,urther compared to the condition_e2"ression 3e,ore enterin2 the loop. ,"erations within the loop are e@ecuted repeatedlA and a,ter each iteration the 5alue o, e@pre44ion i4 chan2ed. The iteration continue4 until the condition_e2"ression 3ecome4 ,al4e. forIO=1; OEP; OKKJ> // +ncrease variable : ; times 1from 1 to ;4 and operation; // repeat expression operation every time ... A

,"eration i4 to 3e per,ormed ,i5e time4. A,ter that9 it will 3e 5alidated 3A checkin2 that the e@pre44ion kR< i4 ,al4e 7a,ter < iteration4 kF<8 and the pro2ram will e@it the for loop. "o67hile LOOP The do5whi/e loop look4 a4 ,ollow4: do >

operation; ... A while IchecO_con!itionJ;

In thi4 ca4e9 the o"eration i4 e@ecuted at lea4t once re2ardle44 o, whether the condition i4 true or ,al4e a4 the e@pre44ion check_condition i4 e@ecuted at the end o, the loop. I, the re4ult i4 not 0 7true89 the procedure i4 repeated. In the ,ollowin2 e@ample9 the pro2ram remain4 in 'o67hile loop until the 5aria3le a reache4 /0 7a million iteration48. a = ); do > a = aK1; A while Ia E= 1)J; // peration in progress // #et initial value

// Chec: condition

EPAI-Fribourg Herv Page - support c!o"t - ver!1!1

PIC Microcontrollers The basics of C programming language


References: http://www.mikroe.com and the Hi-Tech C Manual Page 22 / 28
4RITING CO" IN A!! M(L) LANGUAG PIC MC" in4truction4 maA al4o 3e directlA em3edded Win-lineX into C code u4in2 the directi5e4 *a4m9 *enda4m or the 4tatement a4m78J. The *a4m and *enda4m directi5e4 are u4ed to 4tart and end a 3lock o, a44em3lA in4truction4 which are to 3e em3edded into the a44em3lA output o, the code 2enerator. The *a4m 3lock i4 not 4AntacticallA part o, the C pro2ram9 and thu4 it doe4 not o3eA normal C ,low-o,-control rule4. Thi4 mean4 that Aou 4hould not u4e thi4 ,orm o, in-line a44em3lA in4ide C con4truct4 like i,789 while78 and ,or78 4tatement4. Howe5er thi4 i4 the ea4ie4t mean4 o, addin2 multiple a44em3lA in4truction4. The a4m78 4tatement i4 u4ed to9 tApicallA9 em3ed a 4in2le a44em3ler in4truction. Thi4 ,orm look4 and 3eha5e4 like a C 4tatement. OnlA one a44em3lA in4truction maA 3e encap4ulated within each a4m78 4tatement. 1ou can 4peci,A more than one a44em3lA in4truction in4ide one a4m78 4tatement 3A 4eparatin2 the in4truction4 with a Yn character9 7e.2. a4m7Pmo5lw <<Ynmo5w, M@P8J8 althou2h code will 3e more reada3le i, Aou one place one in4truction in each 4tatement and u4e multiple 4tatement4. 1ou maA u4e the a4m7P P8 ,orm o, in-line a44em3lA at anA point in the C 4ource code a4 it will correctlA interact with all C ,low-o,-control 4tructure4. The ,ollowin2 e@ample 4how4 3oth method4 u4ed: unsi+ne! int var; voi! mainIvoi!J> var = 1; #asm LL liOe this... *4< ),3 *'/Q5%&I_varJ $&< I_varJ )Rfh $&< I_varK1J )Rfh #en!asm LL !o it a+ain the other Say... asmI9*4< ),39 J; asmI9*'/Q5%& _fvar9J; asmI9$&< I_varJ )Rfh9 J; asmI9$&< I_varK1J )Rfh9 J; A

EPAI-Fribourg Herv Page - support c!o"t - ver!1!1

PIC Microcontrollers The basics of C programming language


References: http://www.mikroe.com and the Hi-Tech C Manual Page 2# / 28

ARRA)! A 2roup o, 5aria3le4 o, the 4ame tApe i4 called an arraA. #lement4 o, an arraA are called component49 while their tApe i4 called the main tApe. An arraA i4 declared 3A 4peci,Ain2 it4 name9 tApe and the num3er o, element4 it will compri4e: component_type array_name 7number_of_components8;

$uch a complicated de,inition ,or 4omethin2 4o 4imple9 i4nDt it> An arraA can 3e thou2ht o, a4 a 4horter or lon2er li4t o, 5aria3le4 o, the 4ame tApe where each o, the4e i4 a44i2ned an ordinal num3er 7num3erin2 alwaA4 4tart4 at Cero8. $uch an arraA i4 o,ten called a 5ector. The ,i2ure 3elow 4how4 an arraA named she/f which con4i4t4 o, /00 element4.

Arra* 8shelf8 + 6% %; 0 0 /6 ? ... ... 6%

lements of arra* 4hel,Z0[ 4hel,Z/[ 4hel,Z6[ 4hel,Z%[ 4hel,Z;[ 4hel,Z<[ 4hel,Z)[ ... ... 4hel, Z??[

Contents of element + 6% %; 0 0 /6 ? ... ... 6%

In thi4 ca4e9 the content4 o, a 5aria3le 7an element o, the arraA8 repre4ent4 a num3er o, product4 the 4hel, contain4. #lement4 are acce44ed 3A inde@in29 i.e. 3A 4peci,Ain2 their ordinal num3er 7inde@8: shelf748 = 1-; // 10 items is <placed= on shelf >3? temp = shelf 718; // Variable shelf>1? is copied to // variable temp

#lement4 can 3e a44i2ned content4 durin2 arraA declaration. In the ,ollowin2 e@ample9 the arraA named calendar i4 declared and each element i4 a44i2ned 4peci,ic num3er o, daA4: unsi+ne! char calendar 71-8 = >31,-T,31,3),31,3),31,31,3),31,3),31A;

EPAI-Fribourg Herv Page - support c!o"t - ver!1!1

PIC Microcontrollers The basics of C programming language


References: http://www.mikroe.com and the Hi-Tech C Manual Page 2$ / 28

T4O6"IM N!IONAL ARRA) Apart ,rom one-dimen4ional arraA4 which could 3e thou2ht o, a4 a li4t9 there are al4o multidimen4ional arraA4 in C lan2ua2e. In a ,ew ,ollowin2 4entence4 we are 2oin2 to de4cri3e onlA two-dimen4ional arraA4 called matrices which can 3e thou2ht o, a4 ta3le4. A twodimen4ional arraA i4 declared 3A 4peci,Ain2 data tApe o, the arraA9 the arraA name and the 4iCe o, each dimen4ion. !ook at the e@ample 3elow: component_type array_name 7number_of_roSs8 7number_of_columns8;

num$er_of_rows and num$er_of_co/umns repre4ent the num3er o, row4 and column4 o, a ta3le9 re4pecti5elA. int 2able 738748; // Table is defined to have 3 ro(s and 3 columns

Thi4 arraA can 3e repre4ented in the ,orm o, a ta3le.

ta3leZ0[Z0[ ta3leZ/[Z0[ ta3leZ6[Z0[

ta3leZ0[Z/[ ta3leZ/[Z/[ ta3leZ6[Z/[

ta3leZ0[Z6[ ta3leZ/[Z6[ ta3leZ6[Z6[

ta3leZ0[Z%[ ta3leZ/[Z%[ ta3leZ6[Z%[

$imilar to 5ector49 the element4 o, a matri@ can 3e a44i2ned 5alue4 durin2 arraA declaration. In the ,ollowin2 e@ample9 the element4 o, the two-dimen4ional arraA Table are a44i2ned 5alue4. A4 4een9 thi4 arraA ha4 two row4 and three column4: int 2able7-8738 = >>3,4-,1A, >R,R,1UAA;

The matri@ a3o5e can al4o 3e repre4ented in the ,orm o, a ta3le the element4 o, which ha5e the ,ollowin2 5alue4: % ;6 + + / /?

EPAI-Fribourg Herv Page - support c!o"t - ver!1!1

PIC Microcontrollers The basics of C programming language


References: http://www.mikroe.com and the Hi-Tech C Manual Page 2% / 28

POINT R! A pointer i4 a 4pecial tApe o, 5aria3le holdin2 the addre44 o, character 5aria3le4. In other word49 the pointer Epoint4 toD another 5aria3le. It i4 declared a4 ,ollow4: Thi4 i4 how a ,unction look4 like:

type_of_variable *pointer_name;

In order to a44i2n the addre44 o, a 5aria3le to a pointer9 it i4 nece44arA to u4e the LFL character and write 5aria3le name preceded 3A the LNL character. In the ,ollowin2 e@ample9 the pointer Emultiple@D i4 declared and a44i2ned the addre44 o, the ,ir4t out o, ei2ht !#& di4plaA4:

unsigned int *multiplex; // Declare name and type of pointer multiple multiplex = !isplay1; // Pointer multiplex is assigned the address of // variable display1

To chan2e the 5alue o, the pointed 5aria3le9 it i4 4u,,icient to write the LIL character in ,ront o, it4 pointer and a44i2n it a new 5alue.

*multiplex = "; // Variable display1 is assigned the number

$imilarlA9 in order to read the 5alue o, the pointed 5aria3le9 it i4 4u,,icient to write:

temp = *multiplex; // The value of variable display1 is copied to temp

EPAI-Fribourg Herv Page - support c!o"t - ver!1!1

PIC Microcontrollers The basics of C programming language


References: http://www.mikroe.com and the Hi-Tech C Manual Page 2& / 28

$UNCTION! #5erA pro2ram written in C lan2ua2e con4i4t4 o, lar2er or 4maller num3er o, ,unction4. The main idea i4 to di5ide a pro2ram into 4e5eral part4 u4in2 the4e ,unction4 in order to 4ol5e the actual pro3lem ea4ier. -e4ide49 ,unction4 ena3le u4 to u4e the 4kill4 and knowled2e o, other pro2rammer4. (or e@ample9 i, it i4 nece44arA to 4end a 4trin2 to an !C& di4plaA9 it i4 much ea4ier to u4e alreadA written part o, the pro2ram than to 4tart o5er. (unction4 con4i4t o, command4 4peci,Ain2 what 4hould 3e done upon 5aria3le4. TheA can 3e compared to 4u3routine4. A4 a rule9 it i4 much 3etter to ha5e a pro2ram con4i4tin2 o, lar2e num3er o, 4imple ,unction4 than o, a ,ew lar2e ,unction4. A ,unction 3odA u4uallA con4i4t4 o, 4e5eral command4 3ein2 e@ecuted 3A the order theA are 4peci,ied. #5erA ,unction mu4t 3e properlA declared 4o a4 to 3e properlA interpreted durin2 the proce44 o, compilation. &eclaration contain4 the ,ollowin2 element4:

(unction name (unction 3odA !i4t o, parameter4 &eclaration o, parameter4 TApe o, ,unction re4ult

Thi4 i4 how a ,unction look4 like: type_of_result function_name Itype argument1, type argument2,...J > 4omman!; 4omman!; ... A

#@ample: /5 @unction computes the result of division of the numerator number by the denominator denom2 The function returns a real2 5/ real !ivIint number, int !enomJ;

ote that a ,unction doe4 not need to ha5e parameter49 3ut mu4t ha5e 3racket4 to 3e u4ed ,or enterin2 them. Otherwi4e9 the compiler would mi4interpret the ,unction.

EPAI-Fribourg Herv Page - support c!o"t - ver!1!1

PIC Microcontrollers The basics of C programming language


References: http://www.mikroe.com and the Hi-Tech C Manual Page 2' / 28
I, the ,unction9 a,ter 3ein2 e@ecuted9 return4 no re4ult to the main pro2ram or to the ,unction it i4 called 3A9 the pro2ram proceed4 with e@ecution a,ter encounterin2 a clo4in2 curlA 3racket. $uch ,unction4 are u4ed when it i4 nece44arA to chan2e the 4tate o, the microcontroller output pin49 durin2 data tran4,er 5ia 4erial communication9 when writin2 data on an !C& di4plaA etc. The compiler reco2niCe4 tho4e ,unction4 3A the tApe o, their re4ult 4peci,ied to 3e .oi'. oid function_name Itype ar+ument1, type ar+ument-,...J > 4omman!; A

#@ample: oid interruptIJ > cntKK; 2)I< = ); A

// +nterrupt causes cnt to be incremented by 1 // !eset TimerA @lag

EPAI-Fribourg Herv Page - support c!o"t - ver!1!1

PIC Microcontrollers The basics of C programming language


References: http://www.mikroe.com and the Hi-Tech C Manual Page 28 / 28
The ,unction can 3e a44i2ned an ar3itrarA name. The onlA e@ception i4 the name main which ha4 a 4pecial purpo4e. amelA9 the pro2ram alwaA4 4tart4 e@ecution with thi4 ,unction. It mean4 that e5erA pro2ram written in C lan2ua2e mu4t contain one ,unction named LmainL which doe4 not ha5e to 3e placed at the 3e2innin2 o, the pro2ram. I, it i4 nece44arA that called ,unction return4 re4ult4 a,ter 3ein2 e@ecuted9 the return command9 which can 3e ,ollowed 3A anA e@pre44ion9 i4 u4ed: type_of_result function_name Itype ar+ument1, type ar+ument-,...J > 4omman!; ... return expression; A

I, the ,unction contain4 the return command without 3ein2 ,ollowed 3A e2"ression9 the ,unction 4top4 it4 e@ecution when encounter4 thi4 command and the pro2ram proceed4 with e@ecution ,rom the ,ir4t command ,ollowin2 a clo4in2 curlA 3racket. " CLARATION O$ A N 4 $UNCTION Apart ,rom the ,unction4 that C lan2ua2e LautomaticallAL reco2niCe49 there are al4o completelA new ,unction4 3ein2 o,ten u4ed in pro2ram4. #ach Lnon-4tandardL ,unction 4hould 3e declared at the 3e2innin2 o, the pro2ram. The ,unction declaration i4 called a prototApe and look4 a4 ,ollow4: type_of_result function_name Itype1, type-, ..J ;

$UNCTION LI(RARI ! ame4 o, all ,unction4 3ein2 u4ed in C lan2ua2e are 4tored in the ,ile called header. The4e ,unction4 are9 dependin2 on their purpo4e9 4orted in 4maller ,ile4 called li3rarie4. Prior to u4in2 anA o, them in the pro2ram9 it i4 nece44arA to 4peci,A the appropriate header ,ile u4in2 the &inclu'e command at the 3e2innin2 o, the pro2ram. I, the compiler encounter4 an unknown ,unction durin2 pro2ram e@ecution9 it will ,ir4t look ,or it4 declaration in the 4peci,ied li3rarie4.

EPAI-Fribourg Herv Page - support c!o"t - ver!1!1

You might also like