Sunteți pe pagina 1din 12

GettingStartedwithMPLABIDEv8.

56andanIntroductiontotheMicrochipMCC18CCompiler

Coventry University EC AEE


Getting Started with MPLAB IDE v8.56 and an Introduction
to the Microchip MCC18 C Compiler

Table of Contents
1

MPLABIDEv8.56andMicrochipMCC18CCompiler..............................................................................................2
1.1

BuildingProjects...............................................................................................................................................2

1.2

Simulation.........................................................................................................................................................7

DownloadingtheProgramtotheBoardusingMPLABICD3...................................................................................8

DigitalIO....................................................................................................................................................................8

3.1

HardwareApparatus.........................................................................................................................................8

3.2

CCodeListing..................................................................................................................................................10

ADC..........................................................................................................................................................................10
4.1

HardwareApparatus.......................................................................................................................................10

4.2

CCodeListing..................................................................................................................................................12

Page1of12

GettingStartedwithMPLABIDEv8.56andanIntroductiontotheMicrochipMCC18CCompiler

1 MPLAB IDE v8.56 and Microchip MCC18 C Compiler


1.1 Building Projects
ThissectionwillguideyoustepbystepthroughbuildingaprojectinMPLABIDE.

Beginanewprojectbyusingthenewprojectwizard
Menu>Project>ProjectWizard

OpenMPLABIDEv8.56
byusingthedesktopshortcuticonorthroughStart>
Programs>Microchip>MPLABIDEv8.56>MPLAB
IDE.exe

ClickNext

SelectMicrochipC18toolsuitefromthedropdownlist
(sothisprojectisexpectingCcodeandnotassembly)

SelectPIC18F4520
ClickNext

Ifredcrossesappearthetoolsuiteneedstobefound
manually.
ItshouldbelocatedontheApps(driveD:/MCC18/)

Browseforeachpartofthetoolsuiteandselectthe
correctlocationsbeforecontinuing

ClickNext
Page2of12

GettingStartedwithMPLABIDEv8.56andanIntroductiontotheMicrochipMCC18CCompiler

Thisisforaddingexistingfileswhichwehavenoneso
leaveblank
ClickNext

Findadirectorytouseasaworkspace(alocationto
saveourprojectfiles)andgivetheprojectaname
ClickNext

NowweshouldseeourprojectstructureintheProject
viewwindow

ClickFinish

Menu>File>New

Thiswillbeourmain.cfilewhichwillcontainourmain
method.

Page3of12

GettingStartedwithMPLABIDEv8.56andanIntroductiontotheMicrochipMCC18CCompiler

Menu>File>SaveAs

Nameitmain.candclicksave

Nowweneedtoaddthemain.cfileintoourproject

IntheProjectviewwindowRightClickSourceFiles>
AddFiles

Selectournewlycreatedmain.cfile
ClickOpen

Ourmain.cfileisaddedtoourprojectwindowunder
SourceFiles

Writeashortprogram
AfullcodelistingcanbefoundinAppendixADCcode
atthebackofthishandout

Page4of12

GettingStartedwithMPLABIDEv8.56andanIntroductiontotheMicrochipMCC18CCompiler

Nextweneedtoaddthelinkerscript

IntheProjectviewwindowRightClickLinkerScript>
AddFiles

ItshouldbelocatedontheAppsdrive(D:/MCC18/)
Select18f4520.lkr
ClickOpen

Noticethelinkerfileshasbeenaddedtoourproject

NowwearepresentedwiththeBuildoptionsscreen.
Thisiswherewecansetsearchpathsforourproject.

Nextweneedtoviewthebuildoptions
Menu>Project>BuildOptions>Project
Checkeachofthefollowingdropdownsandmakesure
wehaveapathlistedsimilartotheexamplesbelow.
WecanaddnewpathsbyclickingNew>and
browsingforafolder

OutputDirectory:
F:\MPLABWorkspace\Output

IncludeSearchPath:
D:\MCC18\h

LibrarySearchPath:
D:\MCC18\lib

LinkerScriptSearchPath:
D:\MCC18\lkr

ClickOK

Page5of12

GettingStartedwithMPLABIDEv8.56andanIntroductiontotheMicrochipMCC18CCompiler

Withallthesearchpathssetupcorrectlywecan
compileourfile

Menu>Project>BuildAll

TheOutputwindowshoulddisplayBUILDSUCCEEDED

Page6of12

GettingStartedwithMPLABIDEv8.56andanIntroductiontotheMicrochipMCC18CCompiler

1.2 Simulation
ThissectionwillguideyoustepbystepthroughsimulatingaprojectinMPLABIDE.

Withabuiltprojectsimulationcanbeperformed

Menu>Debugger>SelectTool>MPLABSIM

Wecanwatchwhatishappeningtovariablesinour
programtocheckiftheyarechangingasexpected
Menu>View>Watch

SelectTRISAfromthedropdown
SelectAddSFR(SpecialFunctionRegister)toadditto
thewatchwindow
DothesameforTRISBandTRISD

Breakpointscanbeusedtopausetheprogramatkey
pointsandareusefulwhenusedinconjunctionwith
thewatchwindowtochecklinesofcodeareoperating
asweexpectthemto
Beforestartingthesimulationaddthreebreakpointsin
ourprogramwherewesetTRISA,TRISBandTRISDby
doubleclickinginthelefthandgreymargin

Thecommandsinthedebuggertoolbarcomeinuseful
whensimulatinganddebugging

SelectRuntobeginrunningtheprogramindebug
mode

Whenthefirstbreakpointisreachedtheprogramwill
pause
Thenextlinetobeprocessedisshownbythegreen
arrowinthemargin
Byusingthedebugcommandsstepthroughthe
programandkeepandeyeonthewatchwindowto
checkifthevariablevaluesupdateasexpected
Page7of12

GettingStartedwithMPLABIDEv8.56andanIntroductiontotheMicrochipMCC18CCompiler

2 Downloading the Program to the Board using MPLAB ICD 3

Menu>Programmer>SelectProgrammer>MPLABICD
3

RemoveanyIOportconnectionstothePICboardbefore
programmingtheboard

PlugintheICDUSBtoPCandthecabletothe
developmentboard

TargetDetected

VoltageCautionWindow
SelectOK

Programmingcomplete

YoucanplugbackintheIOconnectionstothePIC
developmentboardandcheckfunctionality

Whenreprogrammingtheboardbesuretoremovethe
IOportconnectionsbeforehand.

3 Digital IO
ThistutorialaimstoguideyouthroughtheDigitalIOdevelopmentforthePIC18toreaddigitalinputanddisplay
digitaloutput.

3.1 Hardware Apparatus


PCwithMPLABandUSBport
ICD3,USBleadandconnectingcable
5voltfixedPSUandconnectingleads
PIC18F4520developmenttargetboard
PIC18IOprototypingboardandconnectingcableforPortDoutput
AdditionalportIOboardforPortBinputsandconnectingcableforPortBinput
8bitdigitaloutput(10LEDbargraphdisplay(weonlyusethefirst8bitsinthisexample))
8bitdigitalinput(8bitswitchnetwork)
Resistornetwork(toactaspullupresistors)

Page8of12

GettingStartedwithMPLABIDEv8.56andanIntroductiontotheMicrochipMCC18CCompiler

Page9of12

GettingStartedwithMPLABIDEv8.56andanIntroductiontotheMicrochipMCC18CCompiler

3.2 C Code Listing

//DigitalIO.c
//8bitinputofPortDareoutputtoPortB

#pragmaconfigOSC=HS//setOSCmodetoHS
#pragmaconfigWDT=OFF//setwatchdogtimeroff
#pragmaconfigLVP=OFF//lowvoltageprogrammingoff
#pragmaconfigDEBUG=OFF//compilewithoutextradebugcompilecode

#include<p18f4520.h>//deviceusedisthePIC18F4520

voidmain(void)
{

LATB=0x00;//initlatchportB

LATD=0x00;//initlatchportD

TRISB=0x00;//configportBasoutput

TRISD=0xFF;//configportDasinput

while(1)//loopforever

PORTB=PORTD;//passtheportDinputtoPortB

}//while

}//main

4 ADC
ThistutorialaimstoguideyouthroughtheADCinputdevelopmentforthePIC18toreadanalogueinputanddisplay
digitaloutput.

4.1 Hardware Apparatus


PCwithMPLABandUSBport
ICD3,USBleadandconnectingcable
5voltfixedPSUandconnectingleads
PIC18F4520developmenttargetboard
PIC18IOprototypingboardandconnectingleadforPort
Doutput
ADCpotentiometersboardandconnectingcable
10LEDbargraphdisplay(weonlyusethefirst8bitsin
thisexample)
2x270Rresistors

Page10of12

GettingStartedwithMPLABIDEv8.56andanIntroductiontotheMicrochipMCC18CCompiler

Page11of12

GettingStartedwithMPLABIDEv8.56andanIntroductiontotheMicrochipMCC18CCompiler

4.2 C Code Listing

//ADCInterface.c
//ADCInterfacingexamplefor10bitADC
//ThefollowingexampleshowstheprocedureofsettingupandusingthePICADC
//PICADCCh0isusedastheanalogueinput.All10bitsareshownonPortsD(08)andPortB(bits9and10)

#pragmaconfigOSC=HS//setoscmodetoHS
#pragmaconfigWDT=OFF//setwatchdogtimeroff
#pragmaconfigLVP=OFF//LowVoltageProgrammingOff
#pragmaconfigDEBUG=OFF//CompilewithoutextraDebugcompileCode

#include<p18f4520.h>//DeviceusedisthePICF452
#include<adc.h>
#include<stdlib.h>
#include<delays.h>

voidmain(void)
{

//SetPORTBasDigitalOutputstodisplayonlythe8LSBADCvalues

TRISB=0x00;//ConfigurePortBasO/P

TRISA=0xFF;//ConfigurePortAasI/PsforADC

TRISD=0X00;//ConfigurePortDasallO/P

//ConfiguretheADC

//ThefollowingconfiguretheADCmoduleoperationalmode

//OpenADCconfigurestheoperationoftheADCwithanumberofarguments

//ADC_FOSCselectstheADCclocksourceandhasanumberofsetvalues

//ADC_RIGHT_JUSTselectADCresultjustification

//ADC_0_TADsetstheA/Dacquisitiontimeselect

//ADC_CH0selectChannel0

//ADC_INT_OFFInterruptsaredisabled

//ADC_VREFPLUS_VDDsetADC+referencevoltageconfigurationtoVDD

//ADC_VREFMINUS_VSSsetADCreferencevoltageconfigurationtoVSS

//0b1011aretheA/DcontrolRegister1configbitssettingthereferenceandfunctionofportbits

OpenADC(ADC_FOSC_32&ADC_RIGHT_JUST&ADC_20_TAD,ADC_CH0&ADC_INT_OFF&ADC_VREFPLUS_VDD&
ADC_VREFMINUS_VSS,0b1011);

SetChanADC(ADC_CH0);//Selectsthepinusedasi/ptothe

while(1)

ConvertADC();//startA/Dconversion

while(BusyADC());//waitforcompletion

//GetresultsandstoreatPortsD&B

LATD=ADRESL;//ResultsRegisterLowdisplayed,leastsignonPortD

LATB=ADRESH;//ResultsRegisterHighdisplayedonPortB

CloseADC();

}//while

}//main

Page12of12

S-ar putea să vă placă și