Using Mpl Abide Colour

download Using Mpl Abide Colour

of 12

Transcript of Using Mpl Abide Colour

  • 7/30/2019 Using Mpl Abide Colour

    1/12

    GettingStartedwithMPLABIDEv8.56andanIntroductiontotheMicrochipMCC18CCompiler

    Page1of12

    CoventryUniversityECAEE

    GettingStartedwithMPLABIDEv8.56andanIntroduction

    totheMicrochipMCC18CCompiler

    TableofContents1 MPLAB IDEv8.56andMicrochipMCC18CCompiler..............................................................................................2

    1.1 BuildingProjects...............................................................................................................................................2

    1.2 Simulation.........................................................................................................................................................7

    2 DownloadingtheProgramtotheBoardusingMPLABICD3...................................................................................8

    3

    DigitalIO

    ....................................................................................................................................................................

    8

    3.1 HardwareApparatus.........................................................................................................................................8

    3.2 CCodeListing..................................................................................................................................................10

    4 ADC..........................................................................................................................................................................10

    4.1 HardwareApparatus.......................................................................................................................................10

    4.2 CCodeListing..................................................................................................................................................12

  • 7/30/2019 Using Mpl Abide Colour

    2/12

    GettingStartedwithMPLABIDEv8.56andanIntroductiontotheMicrochipMCC18CCompiler

    Page2of12

    1 MPLABIDEv8.56andMicrochipMCC18CCompiler1.1 BuildingProjectsThissectionwillguideyoustepbystepthroughbuildingaprojectinMPLABIDE.

    OpenMPLABIDEv8.56

    byusingthedesktopshortcuticonorthroughStart>

    Programs>Microchip>MPLABIDEv8.56>MPLAB

    IDE.exe

    Beginanewprojectbyusingthenewprojectwizard

    Menu>Project>ProjectWizard

    ClickNext

    SelectPIC18F4520

    ClickNext

    SelectMicrochipC18toolsuitefromthedropdownlist

    (sothisprojectisexpectingCcodeandnotassembly)

    Ifredcrossesappearthetoolsuiteneedstobefound

    manually.

    ItshouldbelocatedontheApps(driveD:/MCC18/)

    Browseforeachpartofthetoolsuiteandselectthe

    correctlocationsbeforecontinuing

    ClickNext

  • 7/30/2019 Using Mpl Abide Colour

    3/12

    GettingStartedwithMPLABIDEv8.56andanIntroductiontotheMicrochipMCC18CCompiler

    Page3of12

    Findadirectorytouseasaworkspace(alocationto

    saveourprojectfiles)andgivetheprojectaname

    ClickNext

    Thisisforaddingexistingfileswhichwehavenoneso

    leaveblank

    ClickNext

    ClickFinish

    NowweshouldseeourprojectstructureintheProject

    viewwindow

    Menu>File>New

    Thiswillbeourmain.cfilewhichwillcontainourmain

    method.

  • 7/30/2019 Using Mpl Abide Colour

    4/12

    GettingStartedwithMPLABIDEv8.56andanIntroductiontotheMicrochipMCC18CCompiler

    Page4of12

    Menu>File>SaveAs

    Nameitmain.candclicksave

    Nowweneedtoaddthemain.cfileintoourproject

    IntheProjectviewwindowRightClickSourceFiles>

    AddFiles

    Select

    our

    newly

    created

    main.c

    file

    ClickOpen

    Ourmain.c

    file

    is

    added

    to

    our

    project

    window

    under

    SourceFiles

    Writeashortprogram

    Afull

    code

    listing

    can

    be

    found

    in

    Appendix

    ADC

    code

    atthebackofthishandout

  • 7/30/2019 Using Mpl Abide Colour

    5/12

    GettingStartedwithMPLABIDEv8.56andanIntroductiontotheMicrochipMCC18CCompiler

    Page5of12

    Nextweneedtoaddthelinkerscript

    IntheProjectviewwindowRightClickLinkerScript>

    AddFiles

    ItshouldbelocatedontheAppsdrive(D:/MCC18/)

    Select18f4520.lkr

    ClickOpen

    Noticethelinkerfileshasbeenaddedtoourproject

    Nextweneedtoviewthebuildoptions

    Menu>Project>BuildOptions>Project

    NowwearepresentedwiththeBuildoptionsscreen.

    Thisiswherewecansetsearchpathsforourproject.

    Checkeachofthefollowingdropdownsandmakesure

    wehaveapathlistedsimilartotheexamplesbelow.

    WecanaddnewpathsbyclickingNew>and

    browsingforafolder

    OutputDirectory:

    F:\MPLABWorkspace\Output

    IncludeSearchPath:

    D:\MCC18\h

    LibrarySearchPath:

    D:\MCC18\lib

    LinkerScriptSearchPath:

    D:\MCC18\lkr

    ClickOK

  • 7/30/2019 Using Mpl Abide Colour

    6/12

    GettingStartedwithMPLABIDEv8.56andanIntroductiontotheMicrochipMCC18CCompiler

    Page6of12

    Withallthesearchpathssetupcorrectlywecan

    compileourfile

    Menu>Project>BuildAll

    TheOutputwindowshoulddisplayBUILDSUCCEEDED

  • 7/30/2019 Using Mpl Abide Colour

    7/12

    GettingStartedwithMPLABIDEv8.56andanIntroductiontotheMicrochipMCC18CCompiler

    Page7of12

    1.2 SimulationThissectionwillguideyoustepbystepthroughsimulatingaprojectinMPLABIDE.

    Withabuiltprojectsimulationcanbeperformed

    Menu>Debugger>SelectTool>MPLABSIM

    Wecanwatchwhatishappeningtovariablesinour

    programtocheckiftheyarechangingasexpected

    Menu>View>Watch

    SelectTRISA

    from

    the

    drop

    down

    SelectAddSFR(SpecialFunctionRegister)toadditto

    thewatchwindow

    DothesameforTRISBandTRISD

    Breakpointscanbeusedtopausetheprogramatkey

    pointsandareusefulwhenusedinconjunctionwith

    thewatchwindowtochecklinesofcodeareoperating

    asweexpectthemto

    Beforestarting

    the

    simulation

    add

    three

    breakpoints

    in

    ourprogramwherewesetTRISA,TRISBandTRISDby

    doubleclickinginthelefthandgreymargin

    Thecommands

    in

    the

    debugger

    toolbar

    come

    in

    useful

    whensimulatinganddebugging

    SelectRuntobeginrunningtheprogramindebug

    mode

    Whenthefirstbreakpointisreachedtheprogramwill

    pause

    Thenextlinetobeprocessedisshownbythegreen

    arrowinthemargin

    Byusingthedebugcommandsstepthroughthe

    programandkeepandeyeonthewatchwindowto

    checkifthevariablevaluesupdateasexpected

  • 7/30/2019 Using Mpl Abide Colour

    8/12

    GettingStartedwithMPLABIDEv8.56andanIntroductiontotheMicrochipMCC18CCompiler

    Page8of12

    2 DownloadingtheProgramtotheBoardusingMPLABICD3

    Menu>Programmer>SelectProgrammer>MPLABICD

    3

    RemoveanyIOportconnectionstothePICboardbefore

    programmingtheboard

    PlugintheICDUSBtoPCandthecabletothe

    developmentboard

    TargetDetected

    VoltageCautionWindow

    SelectOK

    Programmingcomplete

    YoucanplugbackintheIOconnectionstothePIC

    developmentboardandcheckfunctionality

    Whenreprogrammingtheboardbesuretoremovethe

    IOport

    connections

    beforehand.

    3 DigitalIOThistutorialaimstoguideyouthroughtheDigitalIOdevelopmentforthePIC18toreaddigitalinputanddisplay

    digitaloutput.

    3.1

    HardwareApparatus

    PCwithMPLABandUSBport

    ICD3,USBleadandconnectingcable

    5voltfixedPSUandconnectingleads

    PIC18F4520developmenttargetboard

    PIC18IOprototypingboardandconnectingcableforPortDoutput

    AdditionalportIOboardforPortBinputsandconnectingcableforPortBinput

    8bitdigitaloutput(10LEDbargraphdisplay(weonlyusethefirst8bitsinthisexample))

    8bitdigitalinput(8bitswitchnetwork)

    Resistornetwork(toactaspullupresistors)

  • 7/30/2019 Using Mpl Abide Colour

    9/12

    GettingStartedwithMPLABIDEv8.56andanIntroductiontotheMicrochipMCC18CCompiler

    Page9of12

  • 7/30/2019 Using Mpl Abide Colour

    10/12

    GettingStartedwithMPLABIDEv8.56andanIntroductiontotheMicrochipMCC18CCompiler

    Page10of12

    3.2 CCodeListing

    //DigitalIO.c

    //8bitinputofPortDareoutputtoPortB

    #pragmaconfigOSC=HS//setOSCmodetoHS

    #pragmaconfigWDT=OFF//setwatchdogtimeroff

    #pragmaconfigLVP=OFF//lowvoltageprogrammingoff

    #pragmaconfigDEBUG=OFF//compilewithoutextradebugcompilecode

    #include//deviceusedisthePIC18F4520

    voidmain(void)

    {

    LATB=0x00;//initlatchportB

    LATD=0x00;

    //

    init

    latch

    port

    D

    TRISB=0x00;//configportBasoutput

    TRISD=0xFF;//configportDasinput

    while(1)//loopforever

    {

    PORTB=PORTD;//passtheportDinputtoPortB

    }//while

    }//main

    4 ADCThistutorialaimstoguideyouthroughtheADCinputdevelopmentforthePIC18toreadanalogueinputanddisplay

    digitaloutput.

    4.1 HardwareApparatusPCwithMPLABandUSBport

    ICD

    3,

    USB

    lead

    and

    connecting

    cable

    5voltfixedPSUandconnectingleads

    PIC18F4520developmenttargetboard

    PIC18IOprototypingboardandconnectingleadforPort

    Doutput

    ADCpotentiometersboardandconnectingcable

    10LEDbargraphdisplay(weonlyusethefirst8bitsin

    thisexample)

    2x270Rresistors

  • 7/30/2019 Using Mpl Abide Colour

    11/12

    GettingStartedwithMPLABIDEv8.56andanIntroductiontotheMicrochipMCC18CCompiler

    Page11of12

  • 7/30/2019 Using Mpl Abide Colour

    12/12

    GettingStartedwithMPLABIDEv8.56andanIntroductiontotheMicrochipMCC18CCompiler

    Page12of12

    4.2 CCodeListing

    //ADCInterface.c

    //ADCInterfacingexamplefor10bitADC

    //ThefollowingexampleshowstheprocedureofsettingupandusingthePICADC

    //PICADCCh0isusedastheanalogueinput.All10bitsareshownonPortsD(08)andPortB(bits9and10)

    #pragmaconfigOSC=HS//setoscmodetoHS

    #pragmaconfigWDT=OFF//setwatchdogtimeroff

    #pragmaconfigLVP=OFF//LowVoltageProgrammingOff

    #pragmaconfigDEBUG=OFF//CompilewithoutextraDebugcompileCode

    #include//DeviceusedisthePICF452

    #include

    #include

    #include

    voidmain(void)

    {

    //SetPORTBasDigitalOutputstodisplayonlythe8LSBADCvalues

    TRISB=0x00;//ConfigurePortBasO/P

    TRISA=0xFF;//ConfigurePortAasI/PsforADC

    TRISD=0X00;//ConfigurePortDasallO/P

    //ConfiguretheADC

    //ThefollowingconfiguretheADCmoduleoperationalmode

    //OpenADCconfigurestheoperationoftheADCwithanumberofarguments

    //ADC_FOSCselectstheADCclocksourceandhasanumberofsetvalues

    //ADC_RIGHT_JUST selectADCresultjustification

    //ADC_0_TAD

    sets

    the

    A/D

    acquisition

    time

    select

    //ADC_CH0selectChannel0

    //ADC_INT_OFFInterruptsaredisabled

    //ADC_VREFPLUS_VDD setADC+referencevoltageconfigurationtoVDD

    //ADC_VREFMINUS_VSS setADC referencevoltageconfigurationtoVSS

    //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