Você está na página 1de 3

PROJECT 0

My 1st PIC PROJECT, LOAD A SIMPLE LED BLINK


In this project, we are going to do a simple LEDs blinking using the LEDs on SK40C In here, we are using both o! the LEDs and blink it like a police car "esides on learning electronic parts, #ou will also learn about some coding in C using $I%&EC$ C in '(L)" COMPONENT NEEDED CODE OVERVIEW Configuration for PIC16F887 ,igure below shows the Con!iguration !or (IC4.,551 &he code abo2e can be !ound at the Con!iguration bits /C*+,I<40 in (IC4.,551 data sheet

$S >D&DIS LEDs *+ SK40C CONNECTION ,or this project, the LEDs was alread# connected to the pins -"./(*-&" .0 and -"1/(*-&" 10 So, #ou ma# onl# ha2e to con!igure the I3* pins as an output in programming part &o program the (IC4.,551, connect the 6IC00" to the ICS( (rogrammer 'ake sure the power connection is correct and *+ !or SK40C *pen '(L)" and include the (roject70 c and click to compile and build $E8 !ile )!ter 9"uild Success!ul:, the (ICKit; should automaticall# run and program the $E8 !ile into (IC4.,551 ,inall#, the LED4 and LED; will be blink like the police car "*-DIS 'CL-E+ L?(DIS

E=ternal Cr#stal in $igh Speed >atchdog &imer Disable "rown *ut -eset Disable 'CL- ,unction Enabled Low ?oltage (rogramming Disable

(>-&E+ (ower 6p &imer Enable

system.h

on 6)-& please re!er to (roject74E 2oid dela#7ms/unsigned int ui72alue0F ,unction protot#pe is the declaration o! a !unction that omits the !unction bod# but does speci!# the !unctionGs name, arit#, argument t#pes and return t#pe &he s#mbol 9 F : should put at the end o! this line MAIN PROGRAM

@de!ine 78&)L7,-EA ;0000000 "esides that, same step b# @de!ine the cr#stal !reBuenc#/78&)L7,-EA0 according to #our e=ternal cr#stal !reBuenc# using In here, the de!ault e=ternal cr#stal !reBuenc# using are ;0000000 /;0'$C0 @de!ine S>4 -"0 In here, we ha2e alread# de!ine the switch on SK40C board as -"0 6ser are no need to de!ine again in the main coding @de!ine LED4 -". In !igure abo2e, using the code @de!ine, we can replace the -". and -"1 as LED4 and LED; "# doing this, we can easil# remember and the I3* port we are using @de!ine LCD7E -"D @de!ine LCD7-S -"4 @de!ine LCD D)&) (*-&D "esides that, user are not reBuired again to de!ine the LCD pin !or SK40C 6ser onl# reBuired to include the s#stem h !ile ,urther detail on LCD please re!er to (roject7; @de!ine &8 -C. @de!ine -8 -C1 6ser are not reBuired again to de!ine the serial comunication /6)-&0 pin !or SK40C 6ser onl# reBuired to include the s#stem h !ile ,urther detail

2oid main /2oid0 In !igure abo2e is the main program 4st we ha2e to t#pe 92oid main/2oid0: at the 4st line to tell the micro%controller that this is the starting point o! the program $ere, 2oid main, tell the compiler the name o! the !unctin which is main and it donGt return an# data/2oid0 (*-&) H 0F Clear the (*-& to pre2ent !rom the data latch on the (IC 'C6 &-IS) H 0b00000000F is the &ri%S&)&E -egister that declare the I3* ports as an I+(6& or *6&(6& b# /4HI+(6&0 and /0H*6&(6&0 E g &-IS" H 0b00004444 is to set the (*-&"I1J4K to *6&(6& and (*-&IEJ0K as I+(6& LED4 H 0F &hen, we make the LED4 o!! b# gi2ing it a 0 at initial state as show abo2e So as the LED; too &o make it on, just change the 0 to 4

while/40 &o make the program loop !ore2er, we use while/40 !unction while construct consists o! a block o! code and a condition &he condition is e2aluated, and i! the condition is true, the code within the block is e=ecuted In here, we put /40 to indicated that the condition is true dela#7ms/4000F is a !unction call is an e=pression containing a simple t#pe name and a parenthesiCed argument list &he argument list can contain an# number o! e=pression separated b# commas It can also be empt# In here, we put the arguments list as 400 which is 400ms

2oid dela#7ms/unsigned int ui72alue0 is an !unction that will be call and per!orm in the main !unction 92oid dela#7ms:, here we are telling the compiler that out !unction is name dela#7ms and it donGt return an# data /2oid0 /unsigned int ui72alue0 is the parameter use to insert 2alue depending to our reBuirement 9int: mean integer which the minimum allow range are between %E;1.1 to ME;1.1/; "#tes0 9unsigned int: mean that the data we can insert is between 0 to .DDED &here are man# data t#pe that we can assign and use that act as the temporar# space !or micro%controller to store the data while/ui72alue%% K 00 in this condition, i! the ui72alue is subtract b# one at the time and check i! the ui72alue isGt still greater then 0 &he code within the while loop will be running !or man# times until the ui72alue is less then 0 77dela#7ms/40F &his is the macro !rom $I%&EC$ complier which will generate 4ms dela# base on 2alue o! 78&)L7,-EA

Comment line // In the code, we can !ound the // on the right hand side o! the code &he // is simpl# a comment in the code and is ignore b# the compiler )n# code that behind the // is ignore b# compiler and is just simpl# there !or #ou, or e2er#one to reads the code Comments are essential in the code to help #ou to better understanding on what going on and how the code works Comments can be put a!ter the command as in the ne=t line o! the program *n the other hand, #ou can also put comments into the block statement b# using /* and */ EgJ 3L )ll the code within the slash and asterisks will be ignore b# compiler L3

Você também pode gostar