Microchip Quick Start Guide - Universidad De Sonora

2y ago
16 Views
2 Downloads
397.77 KB
14 Pages
Last View : 1m ago
Last Download : 3m ago
Upload by : Kaden Thurman
Transcription

Microchip MicrocontrollerQuick Start GuideSource: Microchip PIC18F1220/1320 Data Sheet, DS39605CBy Joseph Sturtevant and Charles Meyer

Microchip Microcontroller TutorialPage 2Introduction:This tutorial will cover the basics of operation and development for the PIC-18F1220Microcontroller using the MPLAB Integrated Development Environment (IDE) made byMicrochip Technology Inc.Table of Contents:I. Introduction to MPLAB1. MPLAB Setup2. MPLAB Interface3. A Simple Program4. The MPLAB Debugger5. Optional - The C18 CompilerII. Introduction to the PIC18F12201. PIN Configuration2. Basic Input/Output3. Memory Organization4. Instruction Set5. Further ReadingIII. Programming the PIC18F1220Copyright 2006 Joseph Sturtevant, Charles Meyer33346788910111112

Microchip Microcontroller TutorialI.Page 3Introduction to MPLABMPLAB is an Integrated Development Environment (IDE) in which you can writeand debug programs for a PIC Microcontroller. MPLAB also allows you to set certainconfiguration options for your chip.1.MPLAB SetupThe complete MPLAB software package can be downloaded online free of charge /MP731 Full.zipTo install MPLAB, extract MP731 Full.zip and run the program MP731 full install.exeRun through the setup program and install MPLAB using all default settings and options.2.MPLAB InterfaceBelow is a screenshot of the MIPS IDE Interface:Execution Breakpoint:Halts program execution onthis line when simulating.(To set a breakpoint, doubleclick the line of code youwish to set the breakpoint on.)Current Execution Arrow:Shows which line of codeprogram execution iscurrently on.“Build All” Button:Use this button to compile yourprogram before simulating it oruploading to hardware.“Debug” Buttons:Use these buttons to run or step through your code when you simulate it.The buttons are (in order):Run, Halt, Animate, Step Into, Step Over, Step Out, and ResetWorkspace WindowList of all files included inyour project.Stimulus ControllerChange the value on theinput pins as your code runs.Code WindowContains the actualprogram code.Output WindowDisplays errorand\or successmessages.Copyright 2006 Joseph Sturtevant, Charles MeyerWatch WindowSee the value of differentregisters as your code runs.

Microchip Microcontroller Tutorial3.Page 4A Simple ProgramThe following instructions will show you how to implement a simple counter using thePIC18F1220 Microcontroller:1. In MPLAB, select Project Project Wizard from the menu. This will start theMPLAB Project Wizard which will walk you through creating a project.a. In Step 1 of the Project Wizard, select the microcontroller you will run yourfinished program on. (In this case, select the PIC18F1220).b. In Step 2 of the Project Wizard, select the Microchip MPASM Toolsuite asyour Active Toolsuite. Note: If you were using the C Compiler you wouldselect a different Toolsuite.c. In Step 3 of the Project Wizard, select a name and location for your project.d. In Step 4 of the Project Wizard, select any files you wish to add to your project.(You will not be adding any at this time, so just skip this step.)2. Once you have finished the Project Wizard, select Project Add New File to Projectfrom the menu to add a new source file to your project. Call the file “main.asm” andsave it in the same location as your project. You should see that the file main.asmhas been added to your project under the “Source Files” heading. This is the file thatwill contain your program code. Note: The .asm extension denotes that this is anassembly code file.Copyright 2006 Joseph Sturtevant, Charles Meyer

Microchip Microcontroller TutorialPage 53. Copy the following code and paste it into your newly created main.asm ----------------------------------------; FILE: main.asm; AUTH: Joseph Sturtevant; DATE: 5-18-06; DESC: Sample Code for PIC18F1220; NOTE: Tested on PIC18F1220; REFs: Data Sheet adix#define#define#define#define#definep UTTEMP; Set processor type; Sets the default radix for data x0800x0810x0820x083org 0x000; Set the program origin for subsequent code at the address OVWF0x00COUNTLASTIN; W 0; COUNT WREG; LASTIN OPORTA, INPUTINPUT, 0LASTIN, 00x1INPUT, LASTINTEMPTEMP, 0IncrementLoop;;;;;;;INPUT PORTAW PORTAW W XOR LASTINW W AND 0x1LASTIN PORTATEMP WIf TEMP 0 0 Then Skip Next CommandIncrement:MOVFADDLWMOVWFMOVWFRETURNCOUNT, 01COUNTPORTB;;;;W COUNTW W 1COUNT WPORTB WendInitialize PORTA by clearing output data latchesInitialize PORTB by clearing output data latchesSet all A\D Converter Pins asdigital I/O pinsValue used to initialize data directionSet Port B RB 7:0 as outputsValue used to initialize data directionSet Port A Pin 0 RA 0 as input; Indicates the end of the program.4. Click the “Build All” button on the toolbar to compile your project. If it compilessuccessfully, it will print a success message in the Output window.5. The next section will show you how to simulate your program.Copyright 2006 Joseph Sturtevant, Charles Meyer

Microchip Microcontroller Tutorial4.Page 6The MPLAB DebuggerThe following instructions will show you how to simulate your counter program usingthe MPLAB Debugger:1. Select Debugger Settings from the menu.a. Select the Animation / Realtime Updates tab.b. Check the Enable Realtime watch updatescheckbox and change the update rate to 1 x100msecs.Note: This allows you to see the values of theregisters specified in the Watch window as yourprogram runs.2. Select Debugger Select Tool MPLAB Sim from the menu.3. Select View Watch from the menu. This allows you to watch the values in differentRegisters\Memory Locations as the program runs.4. Add PORTA, PORTB and WREG to the Watch window by selecting them from theSFR (Special Function Registers) dropdown list and clicking the “Add SFR” button.5. Add COUNT to the Watch window by selecting it from the Add Symbol dropdownlist and clicking the “Add Symbol” button.6. Select Debugger Stimulus Controller New Scenario from the menu. TheStimulus Controller allows you to change the value your simulated microcontrollersees on its input pins.7. Add the toggling of Pin 0 on Port A to your list of Stimuli by selecting RA0 in thePin/SFR column in the first row of the Stimulus Controller window. Then selectToggle under the action column.8. Click the Run button on the toolbar to begin simulating your program.9. Press the Fire button on the stimulus controller to toggle Bit 0 on Port A. You shouldsee the value on Port B in the watch window increment with each toggle.10. Click the Halt button on the toolbar to stop the simulation.11. Note: You may also use the Step Into button on the toolbar to simulate your code oneline at a time.Copyright 2006 Joseph Sturtevant, Charles Meyer

Microchip Microcontroller Tutorial5.Page 7Optional – The C18 CompilerThe C18 Compiler is an ANSI compliant C compiler written for the PIC18 seriesmicrocontrollers. A free version can be download online /MPLAB-C18-Student-Edition-doc-v3 02.exeIt is fully functional for 60 days, after which it will no longer fully optimize your code.(This should not be an issue for most people.)The only thing different when using the C18 Compiler are:1. When creating the Project through the “New Project Wizard”, select the“Microchip C18 Toolsuite” in step two.2. Source code files will have the extensions .c (source files) and .h (header files).3. You must include a linker script with your project. To do this, complete thefollowing steps:a. Right-click on the “Linker Scripts” folder in the Project Window andselect “Add Files” from the menu that appears.b. Navigate to the location you installed the C18 Compiler to and then the lkrsubfolder. Add the linker script for your microcontroller (in this case itwould be the 18f1220.lkr file.)4. The following source code will perform the same function as the assembly ****************** File:main.c* Project:Counter (Sample for PIC18F1220)* Author:Joseph Sturtevant* ****************************/#include p18f1220.h //TRISA, TRISB, PORTA, PORT are already defined in p18f1220.hvoid main(void){unsigned char input;unsigned char lastinput 0x00;unsigned char count 0x00;ADCON1 0x7F;TRISA 0x01;TRISB 0x00;while(1){input PORTA;input input & 0x01;if(input ! lastinput){count ;PORTB count;}lastinput input;}}For further reading regarding the C18 compiler, see the PDF files under the C18Compiler heading in Help Topics on the MPLAB menu.Copyright 2006 Joseph Sturtevant, Charles Meyer

Microchip Microcontroller TutorialII.Page 8Introduction to the PIC18F1220The PIC18F1220 is a RISC (Reduced Instruction Set) microcontroller made byMicrochip Technology Inc. The data sheet with the complete specifications is availableonline at: 605c.pdfThe specifications sheet is the best resource to go to for any in depth explanations aboutanything relating to the PIC18F1220. This tutorial merely intends to give a simplifiedoverview. Note: The PIC18F1220 and PIC18F1320 are nearly identical. The primarydifference between them is that the 18F1320 has more memory.1.Pin ConfigurationThe following is a diagram from Page 4 on the Data Sheet:Power:Pin 5Pin 14Input\Output:Pin 1Pin 2Pin 6Pin 7Pin 3Pin 4Pin 15Pin 16(0 V)( 5 V)PORTA Bit 0PORTA Bit 1PORTA Bit 2PORTA Bit 3PORTA Bit 4PORTA Bit 5PORTA Bit 6PORTA Bit 7Pin 8Pin 9Pin 17Pin 18Pin 10Pin 11Pin 12Pin 13PORTB Bit 0PORTB Bit 1PORTB Bit 2PORTB Bit 3PORTB Bit 4PORTB Bit 5PORTB Bit 6PORTB Bit 7Note 1: In code, the Port A and Port B pins are represented by the registers PORTA and PORTB.Note 2: Some pins may not be available for input/output if they are already used for other functions.Copyright 2006 Joseph Sturtevant, Charles Meyer

Microchip Microcontroller Tutorial2.Page 9Basic Input\OutputHandling Input and Output on the PIC is as simple as writing to or reading from a registerin memory. Configuring the bits in this manner, however, is somewhat more difficult.Below is pin configuration code from the counter program:CLRFCLRFPORTBPORTBThese lines clears the data in PORTA and PORTB.MOVLW0x7FThis line sets the W register (accumulator) to value 0x7F 01111111The accumulator is a register on which every operation is performed. For example, ifyou wished to add 2 to PORTA, you would have to move the value in PORTA to W, add2 to W, and move the value in W back to PORTA.MOVWFADCON1This line copies the contents of the W register to the ADCON1 register.The ADCON1 register is one of three register that control the operation of the 18F1220’sbuilt in Analog to Digital Converter (ADC). Setting the value of ADCON1 to 0x7F tellsthe PIC that pins 1, 2, 6, 7, 8, 9 and 10 will be used for input\output rather than for theADC. (See Data Sheet pg. 158)MOVLW0x00This line sets the W register (accumulator) to value 0x00 00000000MOVWFTRISBThis line copies the contents of the W register to the TRISB register.The TRISB register is the control register for PORTB. The bits in TRISB signify whichway data is flowing in PORTB (1 Input, 0 Output).MOVLW0x01This line sets the W register (accumulator) to value 0x01 00000001MOVWFTRISAThis line copies the contents of the W register to the TRISA register.The TRISA register is the control register for PORTA. The bits in TRISA signify whichway data is flowing in PORTA (1 Input, 0 Output).Now having configured the input\output pins appropriately, you may write or read fromthem just as you would any register. For example:MOVLWMOVWF0xABPORTAWill set the value displayed on the 8 pins of Port A to 0xAB (hex).Copyright 2006 Joseph Sturtevant, Charles Meyer

Microchip Microcontroller Tutorial3.Page 10Memory OrganizationThe PIC18F1220 has a large number of registers available for use. Below is a diagramtaken from pg. 50 in the Data Sheet:It shows the basic layout ofmemory in themicrocontroller. Theregisters you will be usingthe most are: Addresses 0xF80 through0xFFF. These are theSpecial FunctionRegisters (SFR) such asPORTA, PORTB,TRISA, TRISB, etc. Addresses 0x000 through0x0FF. These are theAccess Ram and GeneralPurpose Registers (GPR)which you will use forstoring data for yourprograms.More information on theMemory Map andRegisters can be found onpgs. 49-56 in the DataSheet.The list of SFRs and their respective memory locations can be found on pg. 51 in theData Sheet. A further breakdown of each pin in each SFR can be found on pgs. 52-53.Note: It is a good idea to print out these three pages for reference when programming thePIC18F1220.In the Counter example earlier in the tutorial, preprocessor commands (#define) as wellas the equ statement were used to give names to different addresses in memory. The#define statement can be used to give a name to any value or location and makes formore readable code. The equ statement behaves similarly to the #define statement onlyit allows for memory locations defined via the equ statement to be added to the watchwindow using the Add Symbol button and dropdown list.Copyright 2006 Joseph Sturtevant, Charles Meyer

Microchip Microcontroller Tutorial3.Page 11Instruction SetThe instruction set for the PIC18F1220 can be found on pgs. 196-198 in the Data Sheet.Note: It is a good idea to print out these three pages for reference when programming thePIC18F1220 in assembly language. Details on each instruction as well as examples canbe found on pgs. 199-234. The PIC instruction set is not difficult to use for anyone whois comfortable programming in assembly language. The most notable about the PICarchitecture is its use of an accumulator register to perform all data manipulationfunctions (arithmetic, bitwise, etc.) Examples of different instructions can be found inthe code for the Counter program earlier in the tutorial.4.Further ReadingThis tutorial has only scratched the surface of the PIC18F1220’s functionality. Thosewishing to find out more about how to use other features can consult the Data Sheet orlook for further documentation and\or application notes (App Notes) online. A list ofsome additional features you may wish to explore are:Oscillator configurations: Begins on pg. 13 in the Data Sheet.The PIC18F1220 can be used with a variety of different oscillator configurations.The most advanced would be an external oscillator which can allow for clockspeeds of up to 20 MHz. The most simplistic would be using the PIC’s internaloscillator which allows for clock speeds of up to 8 MHz.Interrupts: Begins on pg. 75 in the Data Sheet.The PIC18F1220 has the ability to handle a variety of interrupts. Interrupts canbe triggered by different events such as the expiration of a timer, the changing ofa value or an error. Interrupts call Interrupt Service Routines which are simplyblocks of code which run whenever the interrupt occurs. The PIC18F1220 allowsfor varying priority interrupts so that less important interrupts do not interrupt the“servicing” of more important ones.Timers: Begins on pg. 101 in the Data Sheet.Timers can be configured to count in various ways and trigger interrupts whenvarious values are reached.Pulse Width Modulation: Begins on pg. 117 in the Data Sheet.Pulse Width Modulation (PWM) is commonly used for communication and motorcontrol. More info on PWM can be found here:http://en.wikipedia.org/wiki/Pulse-width modulationAnalog to Digital Converter: Begins on pg. 157 in the Data Sheet.An Analog to Digital Converter allows the microcontroller to convert an analogvoltage to a digital number which can be analyzed by the microcontroller. ThePIC18F1220 has seven Analog to Digital inputs and describes the analog signal asa 10bit number.Copyright 2006 Joseph Sturtevant, Charles Meyer

Microchip Microcontroller TutorialPage 12III. Programming the PIC18F1220 (using the ICD 2)The following circuit shows how to connect to the PIC18F1220 in order to program itusing the MPLAB ICD 2:123456Side View of RJ-45Jack on ICD 2Connect the ICD 2 To Your Computer:1. Install MPLAP before you plug in the ICD 22. Plug the ICD 2 into the USB port (MPLAB should not be running)3. If the New Hardware Found Wizard asks “Can Windows connect to WindowsUpdate ” select ‘No, not at this time’ and click “Next”.4. Select ‘Install the software automatically’ and click “Next”.5. Windows should install the drivers that were preloaded when you installedMPLAB. Click “Finish”.To Set Up the ICD 2 in MPLAB:1.2.3.4.5.6.Start MPLAB and open your projectSelect Programmer Select Tool MPLAB ICD 2Click “Next”.Select ‘USB’ and click “Next”.Select “Target has own Power supply” and click “Next”Select the default options on next two screens and select nextCopyright 2006 Joseph Sturtevant, Charles Meyer

Microchip Microcontroller TutorialPage 13Setting the Configuration Bits on the PIC18F1220:1.2.3.4.5.Open Your projectSelect Configure Configuration BitsUnder oscillator select “INT RC-Port on RA6, Port on RA7”Under “Watchdog timer” select “Disabled”under Master Clear Enable select “MCLR disabled, RA5 input enabled”(unlessyou need an external reset switch or the In-Circuit Debugger, in which case youneed to need to hook the input switch circuit above to MCLR)6. Close the Configuration pits windowTo Program Your Device Using the ICD 2:1.2.3.4.5.6.7.8.Compile the codeConnect the ICD 2 to your PC and select it as your programmer (instructions above).Connect the PIC18f1220 to the ICD 2 using the schematic aboveSupply power to the PIC18f1220Select Programmer ConnectMPLAB should complete a self diagnosticSelect Programmer ProgramThe program should start running on the processor immediately (unless youhave a external reset switch in witch case you need to selectProgrammer Release from Reset)Copyright 2006 Joseph Sturtevant, Charles Meyer

Microchip Microcontroller TutorialPage 14To Use the ICD 2 as an In-Circuit Debugger:1. You Cannot use pin RA5 as an I/O pin when debugging2. Compile the code3. Select Debugger Select Tool MPLAB ICD 24. Configure the Configuration Bits as above but do not set “MCLR disabled”.5. Connect the ICD 2 to your PC and select it as your programmer (instructions above)6. Connect the PIC18f1220 to the ICD 2 using the schematic above7. Supply power to the PIC18f12208. Select Debugger Connect9. MPLAB should complete a self diagnostic10. Select Debugger Program11. MPLAB will then program you chip with a special operating system that willallow you to debug it.12. You can then use watches and breakpoints the same way you use thesimulator. There are often delays as data is sent to and from the processor.13. After you are done debugging switch back to program mode and reprogramyou chip.For a more in depth step by step tutorial or more information on how to use the In-CircuitDebugging feature, see the MPLAB ICD 2 In-Circuit Debugger User’s Guide that isincluded with the ICD 2.Copyright 2006 Joseph Sturtevant, Charles Meyer

Microchip Technology Inc. Table of Contents: I. Introduction to MPLAB 3 1. MPLAB Setup 3 2. MPLAB Interface 3 3. A Simple Program 4 4. The MPLAB Debugger 6 5. Optional - The C18 Compiler 7 II. Introduction to the PIC18F1220 8 1. PIN Configuration 8 2. Basic Input/Output 9 3.

Related Documents:

María M. Arana/Universidad del Este Dr. Alex Betancourt/Universidad de Puerto Rico-RP Dr. Gabriel De La Luz/Universidad de Puerto Rico-RP Dr. Jorge F. Figueroa/Universidad del Este Dra. Yolanda López/Universidad del Este Dr. Jaime Partsch/Universidad del Este Dr. Guillermo Rebollo/Universidad Metropolitana Dra.

hidpri.h \PIC32 Solutions\Microchip\USB\hid_device_driver Private function and macro definitions hid.h \PIC32 Solutions\Microchip\Include\USB USB HID include file hiddesc.h \PIC32 Solutions\Microchip\Include\USB HID specific descriptor defines hidreport.h \PIC32 Solutions\Microchip

Test the Smart MicroChip Sensor in a high pressure and temperature lab environment. Q12 0 6.6 Build a downhole logging tool to power the Smart MicroChip Sensor and assimilate their data. Q13 50 6.7 Report the results of injecting the Smart MicroChip Sensor into the formation (small-scale fr

Microchip Technology Inc. 2355 W. Chandler Blvd. Chandler, AZ 85224 www.microchip.com Don’ M N I MoSolutions Published six times a year, MicroSolutions is a valuable resource for product and technology news about Microchip’s innovative solutions. Subscribe today to receive email notifications when each new issue of MicroSolutions is ready. Use

Microchip's free TCP/IP Stack, and more specifically, how you can use Dynamic Variables to present data from your system to a web browser. My name is Elliott Wood, and I'm an Applications Engineer at Microchip's headquarters in Chandler, AZ. I am one of the main developers of Microchip's

143-37 Hyundai Tower Unit 902 Samsung-dong Kangnam-gu Seoul Korea 135-090 Tel: (82-2) 553-5693 Fax: (82-2) 552-4942 www.genesis-microchip.com / info@genesis-microchip.com.com Genesis Microchip Publication PRELIMINARY DATA SHEET gm2121 SXGA LCD Monitor Controller with Integrated Analog Interface and Dual

many members of the Microchip User's forum and some ideas taken out from the latest release (v4.02) of the original Microchip TCP/IP stack code. Note: The current version DOES NOT support the HI-TECH or CCS compilers yet. Thanks to all members of the Mi

A Reader’s Guide to Contemporary Literary Theoryis a classic introduction to the ever-evolving field of modern literary theory, now expanded and updated in its fifth edition. This book presents the full range of positions and movements in contemporary literary theory. It organises the theories into clearly defined sections and presents them in an accessible and lucid style. Students are .