MPLAB IDE Tutorial PDF
MPLAB IDE Tutorial PDF
MPLABIDE(IntegratedDevelopmentEnvironment)isusedforediting,compilingcodesaswellas
simulatingthemusinganinbuiltsimulator.
Inthistutorial,wewillcoverthefollowingsteps(specificallytoprogramthePIC16F877A)
1) Settingupanewproject
Selectingthedevice
SettingupLanguageTools
Namingtheproject
AddingRequiredfilestotheProject
2) Editing&CompilingtheCode
ViewingWindows
CheckingtheConfigurationBits
Locatingthemaincodearea
Locatingthevariabledefinitionarea
LocatingtheISR(InterruptServiceRoutine)
Buildingthecode
3) TestingthecodeusingtheSimulator
MPLABIDEwhenopened,lookslikethis:
Step1)Settingupanewproject
a) SelectingtheDevice
Goto:Project>ProjectWizardasshown
ClickNextontheWelcomeScreen.
Youarriveatadeviceselectionwindow.MakesurethattheDeviceselectedisPIC16F877A.
ClickontheNextButton.
b) SettinguptheLanguageTools
Youdontneedtodoanythinghere.Acceptthedefaultsettings&justhittheNextbutton.
c) NamingtheProject
Youwillencounterthefollowingwindow:
ClickontheBrowseButton.
WhileinsidetheBrowseWindow:
InitiallyCreateanewfolder(withanyname)insomelocation.Fore.g.TestFolder.
Thenclickonthisfoldertogoinsideit,typeanydesirablenamethatyouwantyour
programtohave(fore.g.Test),andthenclickonSaveButton.
Yourwindowwillnowlooklikethis.HittheNextbutton.
d) AddingrequiredfilestotheProject
Wewillneedtocopytwofilestoourproject.
Youwillhavearrivedatawindowthatshowsatreetypestructureoffoldersonthecomputer.
Usingthearrows,migratetothefolder:
C:\ProgramFiles\Microchip\MPASMSuite
Youwillseethefollowingfolderstructure:
Firstly,addthetemplatefiletoyourprojectasfollows
Usingarrows,enterthefolder:
C:\ProgramFiles\Microchip\MPASMSuite\Template\Object
Findthefile:16F877ATMPO.ASM(becarefultochoosetherightfile)
SelectthisfileandclickonAdd>>sothatthefileenterstherightcolumn(withanA)
DONOTclickonNext.
Secondly,addalinkerfiletoyourprojectasfollows(inthesamewindow):
Usingarrows,enterthefolder:
C:\ProgramFiles\Microchip\MPASMSuite\LKR
Findthefile:16f877a.lkr
(becarefulandDONOTCONFUSEwithothersimilarfileslike16f877.lkr,16f877_g.lkr,
16f877ai.lkr,16f877a_g.lkr,etc.)
SelectthisfileandagainclickonAdd>>sothatitenterstherightcolumn(withanA)
DONOTclickonNext.
MoveyourmousePointerovertheAsintherightcolumn,andkeeponclickingonthemuntil
youseeaCintheirplace.(asyouclickontheA,youwillgothroughU,S,andthenC)
Cstandsforcopy.(sincewewanttocopythefilestoourproject)
Yourfinalwindowshouldlooklikebelow:
Now,hittheNextButton
YouwillarriveattheSummarywindow.ClickonFinish
Step2)Editing&Compilingthecode
a) ViewingWindows
Youwillarriveatseeminglyblankwindowasfollows:
FromtheMenubarabove,Goto:View>Project.
Theprojectwindowlooksasfollows:
Inthissmallwindow,youwillseethefile16F877ATMPO.ASM.
DoubleClickonthisfiletoopentheEditorwindow,whereyouwillwriteyourcodes.
b) CheckingtheConfigurationBitString
MaximisetheEditorwindowtoviewthedefaultcodeinthetemplate.
Itsaprettylongcode.Scrollup/downtolocatethescriptthatisshownasfollows:
Locatetheselinesinthecode
list
#include
p=16f877a
<p16f877a.inc>
;listdirectivetodefineprocessor
;processorspecificvariabledefinitions
__CONFIG_CP_OFF&_WDT_OFF&_BODEN_OFF&_PWRTE_ON&_HS_OSC&_WRT_OFF&_LVP_OFF&_CPD_OFF
EnsurethatthisConfigurationStringinthecodeisexactlyasitiswrittenabove.
Especiallylookoutfor:HS_OSC...andLVP_OFF.
Ifnot,thencorrectit.
c) LocatingtheMaincodeArea:
Scrollup/downinthecodetofindthelinethatcontains
MAIN_PROG
CODE
start
;Whatevercodethatyouwishtorunfortheexperiment,shouldbewritteninthisplace
;entirely(excepttheInterruptcode).
END
d) LocatingtheVariableDefinitionArea
Payattentiontotheparthighlightedinthisframe.Locatethislineinyourcode.
Thevariable(register)countisdefinedhere.Similarlyyoucandefinemorevariablesbelowit.
e) LocatingtheISR(InterruptServiceRoutine)
Locatethefollowingpartofcodeinyourtemplate.
;interruptvectorlocation
INT_VECTORCODE0x0004
INTERRUPT
movwf
w_temp
;saveoffcurrentWregistercontents
movf
STATUS,w
;movestatusregisterintoWregister
movwf
status_temp
;saveoffcontentsofSTATUSregister
movf
PCLATH,w
;movepclathregisterintowregister
movwf
pclath_temp
;saveoffcontentsofPCLATHregister
;isrcodecangohereorbelocatedasacallsubroutineelsewhere
movf
pclath_temp,w
;retrievecopyofPCLATHregister
movwf
PCLATH
;restorepreisrPCLATHregistercontents
movf
status_temp,w
;retrievecopyofSTATUSregister
movwf
STATUS
;restorepreisrSTATUSregistercontents
swapf
w_temp,f
swapf
w_temp,w
;restorepreisrWregistercontents
retfie
;returnfrominterrupt
Whatevercodeyouwishtorunwhenanyinterruptiscalled,needstobewrittenintheplaceof
thecommentedline:
;isrcodecangohereorbelocatedasacallsubroutineelsewhere
f) Building(Compiling)theCode
Afteryoufinishwritingthedesiredcodeforaparticularexperiment,youneedtocompilethecode
forerrors.
OntheMenuBar,goto:Project>BuildAll
Ifyourcodehasnoerrorsinit,youwillgetthemessageBUILDSUCCEEDED,intheOutput
Window.
Toreturntotheeditorwindow,Goto:Window>MPLABIDEEditor.
Fromthispointonwards,itisconvenienttoresizethewindows,sothatyoucanviewallof
themsimultaneously.
Step3)TestingthecodeusingSimulator
ThisisthemostimportantstepbeforeyouburnyourprogramintothemicrocontrollerIC.
Thesimulatorenablesyoutosimulateandcheckyourcodeifitisworkingfine,ratherthan
feedingitintotheICandthenwonderingwhytheprogramdoesntwork.
a) SelectingtheDebugger(Simulator)
OntheMenuBar,goto:Debugger>SelectTool>MPLABSIM
b) ViewingtheWatchWindow
OntheMenuBar,goto:View>Watch
Resize(RestoreDown)theWatchwindow,inordertoseeallthewindowssimultaneously.
Thisisnecessaryinordertowatchtheprogramrun&outputsimultaneouslyonthesimulator.
c) SelectingtheRegistersforaWatch
Thereare2pulldownlistsintheWatchwindow.OnecontainsalltheSFRs(SpecialFunction
Registers)
Selectallthoseregistersthatyouareusinginyourprogram(e.g.TRIS,PORT,WREG,STATUS)
Fore.g.towatchthevalueofWREG(Accumulator),selectWREGfromthedropdownlistand
thenclickonAddSFR.Similarlyforothers.
Similarlyyoucanwatchanyvariablesthatyouareusinginyourprogram.Fore.g.count.
d) CheckingtheProgramSequence
Yourresizedwindowswilllooklikethis.Trytolocatethe2buttonshighlitedinthepicture:
Alsolocatethegreenarrowpointingtosomelineoftheprogram.
Makesureyouhavecompiledyourprogram(usingBuildAllOption)
Now,keepingalltheregistersonwatchintoview,alongwithsomevisiblepartofthecode;
presstheStepIntoButton.Youwillseethegreenarrowshiftbelow.
KeeponpressingtheStepIntoButtontoseehoweachinstructionshiftsthegreenarrow
(ProgramCounter)tosomeposition;andatthesametimewatchifthevaluesofthe
RegistersintheWatchwindowchangeasdesired.
PresstheResetButtontorestarttheprogramfromthebeginning.
Ifyouhaveintroducedanydelaysinyourprogram,youwillneedtoreducethedelaycycles
inordertoseethesimulation;andthenagainincreasethedelaycycles(time)whilefeeding
theprogramintotheactualmicrocontroller.
Step4)FeedingtheProgramintotheMicrocontroller
Ifyouaresurethatyourprogramisworkingfineinthesimulator,thenproceedasfollows
ChangethevaluesofDelaysifrequired.
Compiletheprogramforonefinaltime(inordertoensurethatallrecentchangeshavebeentaken
intoaccount)
Now,inMyComputerlocatetheactualfolderwhereyouhavesavedyourproject.
Insidethisfolder,youwillseemanyfilesasfollows:
Locatethefilewithyourprogramname&a.hexextension.
ThisistheactualfilethatneedstobefeededtotheMicrocontrollersoftware.
Usethis.hexfileinthesoftwareusedforprogrammingthePICIC.