PICkit™ 3 Starter Kit User’s Guide 2012 Microchip Technology Inc.
Note the following details of the code protection feature on Microchip devices: • Microchip products meet the specification contained in their particular Microchip Data Sheet. • Microchip believes that its family of products is one of the most secure families of its kind on the market today, when used in the intended manner and under normal conditions. • There are dishonest and possibly illegal methods used to breach the code protection feature.
PICkit™ 3 STARTER KIT USER’S GUIDE Table of Contents Chapter 1. Overview 1.1 1.2 1.3 1.4 1.5 1.6 1.7 Introduction ............................................................................................. 13 Highlights ................................................................................................ 13 What’s New ............................................................................................ 13 Included Items ..................................................................
PICkit™ 3 STARTER KIT USER’S GUIDE 2.17 MPASM Assembler Directives ................................................................ 36 2.17.1 2.17.2 2.17.3 2.17.4 2.17.5 2.17.6 Banksel .................................................................................................36 cblock .....................................................................................................36 Org (addr) ..............................................................................................37 End .
3.7 3.8 3.9 Lesson 6: Debounce ............................................................................... 62 3.7.1 3.7.2 3.7.3 3.7.4 3.7.5 3.7.6 3.7.7 3.7.8 Lesson 7: Reversible Variable Speed Rotate ......................................... 64 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.8.6 3.8.7 3.11 3.12 Introduction ............................................................................................ 67 Hardware Effects ...........................................................................
PICkit™ 3 STARTER KIT USER’S GUIDE 3.13.4 3.13.5 3.13.6 3.13.7 3.13.8 3.14 New Registers ........................................................................................83 New Registers ........................................................................................85 New Instructions: ...................................................................................86 Assembly Language ..............................................................................87 C Language ...........
PICkit™ 3 STARTER KIT USER’S GUIDE Preface NOTICE TO CUSTOMERS All documentation becomes dated, and this manual is no exception. Microchip tools and documentation are constantly evolving to meet customer needs, so some actual dialogs and/or tool descriptions may differ from those in this document. Please refer to our web site (www.microchip.com) to obtain the latest documentation available. Documents are identified with a “DS” number.
PICkit™ 3 Starter Kit User’s Guide CONVENTIONS USED IN THIS GUIDE This manual uses the following documentation conventions: DOCUMENTATION CONVENTIONS Description Arial font: Italic characters Initial caps Quotes Underlined, italic text with right angle bracket Bold characters N‘Rnnnn Text in angle brackets < > Courier New font: Plain Courier New Represents Referenced books Emphasized text A window A dialog A menu selection A field name in a window or dialog A menu path MPLAB® IDE User’s Guide ...
Preface WARRANTY REGISTRATION Please complete the enclosed Warranty Registration Card and mail it promptly. Sending in the Warranty Registration Card entitles users to receive new product updates. Interim software releases are available at the Microchip web site. RECOMMENDED READING This user’s guide describes how to use the PICkit™ 3 Starter Kit User’s Guide. Other useful documents are listed below. The following Microchip documents are available and recommended as supplemental reference resources.
PICkit™ 3 Starter Kit User’s Guide THE MICROCHIP WEB SITE Microchip provides online support via our web site at www.microchip.com. This web site is used as a means to make files and information easily available to customers.
Preface CUSTOMER SUPPORT Users of Microchip products can receive assistance through several channels: • • • • Distributor or Representative Local Sales Office Field Application Engineer (FAE) Technical Support Customers should contact their distributor, representative or field application engineer (FAE) for support. Local sales offices are also available to help customers. A listing of sales offices and locations is included in the back of this document.
PICkit™ 3 Starter Kit User’s Guide NOTES: DS41628B-page 12 2012 Microchip Technology Inc.
PICkit™ 3 STARTER KIT USER’S GUIDE Chapter 1. Overview 1.1 INTRODUCTION This chapter introduces the hardware that is included in the kit, as well as a quick start to downloading and installing the accompanying software. 1.2 HIGHLIGHTS This chapter discusses: - 1.3 What’s New Included Items The Low Pin Count (LPC) Board Hardware Software Overview Running the Demonstrations WHAT’S NEW This kit is an update to the PICkit™ 2 Starter Kit.
PICkit™ 3 Starter Kit User’s Guide The software associated with the kit supports the PIC16F1829 and PIC18F14K22. The software is intended to run on these two devices, although the software can be easily ported to other devices. 1.5 THE LOW PIN COUNT BOARD Support for 18-pin devices requires some board modifications. 14- and 20-pin PIC devices will have full access to all of the human interface devices. If an 8-pin part is used, then the LEDs will have to be bridged to the necessary pins on the PIC MCU.
Overview 1.6 SOFTWARE OVERVIEW This guide will assume that the reader has a basic knowledge of electronics. The reader does not need to have any programming experience with a PIC MCU before reading, although a basic knowledge of programming and what the difference between a bit and byte will help. The software is written in both assembly and ‘C’ in the MPLAB X and MPLAB 8 integrated design environment (IDE).
PICkit™ 3 Starter Kit User’s Guide NOTES: DS41628B-page 16 2012 Microchip Technology Inc.
PICkit™ 3 STARTER KIT USER’S GUIDE Chapter 2. PIC® MCU Architecture 2.1 INTRODUCTION This chapter describes the architecture of the enhanced mid-range PIC16F1829 (DS41440), as well as the PIC18 (DS41365). 2.2 CORE BASICS Enhanced PIC16 and PIC18 devices use a modified Harvard architecture, meaning the code memory and data memory are independent. This allows faster execution because code instructions and data can be accessed simultaneously.
PICkit™ 3 Starter Kit User’s Guide FIGURE 2-2: SIMPLIFIED ENHANCED MID-RANGE PIC® MCU DATA BLOCK DIAGRAM Program Flash Memory RAM EEPROM PORTA OSC2/CLKOUT OSC1/CLKIN Timing Generation PORTB CPU INTRC Oscillator Figure 2-1 PORTC MCLR PORTD Note SR Latch ADC 10-Bit Timer0 Timer1 Timer2 Timer4 Timer6 Comparators LCD ECCP1 ECCP2 ECCP3 CCP4 CCP5 MSSP EUSART 1: DS41628B-page 18 PORTE See applicable chapters for more information on peripherals. 2012 Microchip Technology Inc.
PIC® MCU Architecture FIGURE 2-3: SIMPLIFIED PIC18 BLOCK DIAGRAM Data Bus<8> Table Pointer<21> Data Latch 8 8 inc/dec logic PCLATU PCLATH 21 PORTA Data Memory (512/768 bytes) Address Latch 20 PCU PCH PCL Program Counter 12 Data Address<12> 31-Level Stack 4 BSR Address Latch STKPTR Program Memory 12 FSR0 FSR1 FSR2 Data Latch 4 Access Bank 12 PORTB 8 inc/dec logic Table Latch Instruction Bus <16> RA0 RA1 RA1 RA3 RA4 RA5 RB4 RB5 RB6 RB7 Address Decode ROM Latch IR Instruction Deco
PICkit™ 3 Starter Kit User’s Guide 2.3 DATA/PROGRAM BUS The data bus is connected to the outside world via port pins, as well as all of the peripheral registers (timers, ADC, PWM). The program bus connects to the Flash memory where the program is stored. This is where assembled code is programmed to. 2.4 ACCUMULATOR There is only one accumulator – the working register (WREG). The accumulator handles all data bus related tasks, such as mathematical operations.
PIC® MCU Architecture 2.6 BYTE All byte instructions on the enhanced PIC16 contain a 6-bit opcode, 7-bit file address, and a destination bit. All PIC18 byte instructions contain a 6-bit opcode, 8-bit file address, a destination bit, and a RAM access bit.The sum of all the bit field sizes confirms that the PIC16 enhanced core does indeed have a 14-bit wide word size for instructions. Likewise, the same can be seen for the PIC18 for its 16-bit wide word length.
PICkit™ 3 Starter Kit User’s Guide 2.9 CONTROL Instructions that dictate what address the PC will select in program memory are called control instructions. This would include call, goto, and branch. Each has a unique word length. Please refer to the “Instruction Set Summary” chapter in any PIC device data sheet for more information. DS41628B-page 22 2012 Microchip Technology Inc.
PIC® MCU Architecture FIGURE 2-4: ENHANCED PIC16 GENERAL FORMAT FOR INSTRUCTIONS Byte-oriented file register operations 13 8 7 6 OPCODE d f (FILE #) Example Instruction 0 ADDWF MYREG, W d = 0 for destination W d = 1 for destination f f = 7-bit file register address Bit-oriented file register operations 13 10 9 7 6 OPCODE b (BIT #) f (FILE #) 0 b = 3-bit bit address f = 7-bit file register address Literal and control operations General 13 OPCODE 8 7 0 k (literal) BSF MYREG, BIT k = 8-bit immed
PICkit™ 3 Starter Kit User’s Guide FIGURE 2-5: PIC18 GENERAL FORMAT FOR INSTRUCTIONS Byte-oriented file register operations 15 10 9 8 7 OPCODE d a Example Instruction 0 f (FILE #) ADDWF MYREG, W, B d = 0 for result destination to be WREG register d = 1 for result destination to be file register (f) a = 0 to force Access Bank a = 1 for BSR to select bank f = 8-bit file register address Byte to Byte move operations (2-word) 15 12 11 OPCODE 15 0 f (Source FILE #) 12 11 MOVFF MYREG1, MYREG2 0 f (Desti
PIC® MCU Architecture 2.10 STACK LEVEL The PIC18 has a deeper stack level of 31, whereas the enhanced core has 16. A deeper stack allows the PIC device to make more calls in the software before returning to the original address where the first call was made. A call or goto modifies the program counter to point to a different place in code. Without these, the code would execute from the top to the bottom. The lessons will show the significance of this.
PICkit™ 3 Starter Kit User’s Guide 2.12.4 Revision ID There are five bits in each PIC MCU that indicate the silicon revision of the PIC device. These bits are read-only and found in the DEVID register. There are usually multiple revisions of silicon for each PIC device. The errata document, which points out any errors and their temporary work-arounds, should be read alongside the data sheet. The PIC18 has a program bus that is 21 bits wide, whereas the enhanced core is only 15 bits wide.
PIC® MCU Architecture Changing pages is necessary in the enhanced core when changing execution from one page to another. None of the lessons for the enhanced PIC16 occupy more than one page and, therefore, page changes are not necessary. If the code does overflow into another page, the assembler will give a warning, indicating that a pagesel may be required.
PICkit™ 3 Starter Kit User’s Guide 2.13.1 Core Registers The core registers contain the registers that directly affect the basic operation of the PIC device, repeated at the top of every data memory bank. Here are three examples of the 12 core registers: 1. STATUS 2. WREG 3. INTCON The STATUS register contains the arithmetic status of the ALU. The WREG register is used to move bits in and out of registers.
PIC® MCU Architecture FIGURE 2-8: ENHANCED MID-RANGE BANKED MEMORY PARTITIONING 7-bit Bank Offset Memory Region 00h 0Bh 0Ch Core Registers (12 bytes) Special Function Registers (20 bytes maximum) 1Fh 20h General Purpose RAM (80 bytes maximum) 6Fh 70h Common RAM (16 bytes) 7Fh Addresses 70h-7Fh are shared by all of the banks. This is useful for storing a few bytes of RAM without the need to switch banks each time the byte is used. Figure 2-9 shows the first eight banks on the PIC16F1829.
PIC16F1829 MEMORY MAP – THE CORRECT BANK MUST BE SELECTED BEFORE WRITING/READING FROM A REGISTER BANK 0 BANK 1 BANK 2 BANK 3 BANK 4 BANK 5 280h 281h 282h 283h 284h 285h 286h 287h 288h 289h 28Ah 28Bh 28Ch 28Dh 28Eh 28Fh 290h 291h 292h 293h 294h 295h 296h 297h 298h INDF0 INDF1 PCL STATUS FSR0L FSR0H FSR1L FSR1H BSR WREG PCLATH INTCON — — — — — CCPR1L CCPR1H CCP1CON PWM1CON CCP1AS PSTR1CON — CCPR2L BANK 6 300h 301h 302h 303h 304h 305h 306h 307h 308h 309h 30Ah 30Bh 30Ch 30Dh 30Eh 30Fh 310h 311h 312h 313
PIC® MCU Architecture When using the PIC16F1829 in assembly, the reader will be constantly referring back to Figure 2-9 to make sure that the right bank is selected before writing to an SFR. For PIC18 devices, the banking situation was streamlined so that the user does not have to switch banks when using the access SFRs. The data memory is configured with an Access Bank, which allows users to access a mapped block of memory without specifying a Bank Select Register (BSR).
PICkit™ 3 Starter Kit User’s Guide FIGURE 2-10: PIC18F14K22 DATA MEMORY MAP BSR<3:0> = 0000 = 0001 = 0010 = 0011 = 0100 = 0101 = 0110 = 0111 = 1000 = 1001 = 1010 = 1011 = 1100 = 1101 = 1110 = 1111 00h Access RAM FFh 00h GPR Bank 0 000h 05Fh 060h 0FFh 100h Bank 1 Bank 2 Bank 3 Bank 4 Bank 5 Bank 6 Bank 7 Bank 8 FFh 00h 1FFh 200h FFh 00h 2FFh 300h FFh 00h 3FFh 400h FFh 00h 4FFh 500h FFh 00h 5FFh 600h FFh 00h 6FFh 700h FFh 00h Unused Read 00h 8FFh 900h FFh 00h 9FFh A0
PIC® MCU Architecture FIGURE 2-11: Address FFFh PIC18F14K22 SPECIAL FUNCTION REGISTER MAP – ALL OF THESE ARE IN BANK 15 WHICH IS INCLUDED IN THE “ACCESS RAM” Name Address TOSU FD7h Name TMR0H Address FAFh Name SPBRG Address F87h Name Address Name — (2) F5Fh —(2) (2) F5Eh —(2) FFEh TOSH FD6h TMR0L FAEh RCREG F86h — FFDh TOSL FD5h T0CON FADh TXREG F85h —(2) F5Dh —(2) F84h — (2) F5Ch —(2) — (2) F5Bh —(2) FFCh FFBh STKPTR FD4h PCLATU FD3h — (2) OSCCON FACh
PICkit™ 3 Starter Kit User’s Guide 2.15 DATA EEPROM MEMORY The data EEPROM is a nonvolatile memory array, separate from both the data RAM, and program memory, which is used for long-term storage of program data. The EEPROM is not directly mapped in either the register file or program memory space, but is indirectly addressed through special SFRs. The EEPROM is readable and writable during normal operation. The PIC16F1829 and PIC18F14K22 have 256 bytes of EEPROM on board.
PIC® MCU Architecture FIGURE 2-13: XC8 OPERATION Notice how the output is the same for both the compiler/assembler – a hex file. The assembly that the compiler generates can be seen in the disassembly window inside of the MPLAB® IDE. FIGURE 2-14: DISASSEMBLY FIGURE Figure 2-14 shown above shows part of the disassembly of lesson 5. The ‘C’, which is indented, is easier to understand and write.
PICkit™ 3 Starter Kit User’s Guide 2.16.3 Numbers in the Assembler Unless otherwise specified, the assembler assumes any numeric constants in the program are hexadecimal (base 16). Binary (base 2), octal (base 8), decimal (base 10), and ASCII coding are also supported. TABLE 2-1: NUMBERS IN THE ASSEMBLER Radix Format Hexadecimal # or 0x# or H’#’ 12 or 0x12 or H’12’ Decimal .# or D’#’Octal or O’#’ .12 or D’12’Octal or O’12’ Binary B’#’ B’00010010’ ASCII A’#’ or ‘#’ A’c’ or ‘c’ 2.16.
PIC® MCU Architecture 2.17.3 Org (addr) Org tells the assembler where to start generating code at addr. Normally, the lessons would start code at address 0x0000. 2.17.4 End End tells the assembler to stop assembling. There must be one at the end of the program. It does not necessarily have to be at the end of the file, but nothing after the end statement will be assembled. 2.17.5 Errorlevel This is used to suppress warnings that the assembler may give.
PICkit™ 3 Starter Kit User’s Guide NOTES: DS41628B-page 38 2012 Microchip Technology Inc.
PICkit™ 3 STARTER KIT USER’S GUIDE Chapter 3. Lessons All of the following 13 lessons will include important code snippets, as well as new registers and instructions for each PIC MCU. Each lesson introduces either a new peripheral or feature. There may be slight differences between the PIC16 and PIC18 in each lesson, but the differences are pointed out and explained. The enhanced PIC16 is explained first, followed by the PIC18.
LESSONS # Lesson New Modules 1 Hello World 2 Blink ALU 1. 2. 3. 4. Latch New Concepts Port GPR SRF ACCESS RAM Oscillator Basics of PIC MCU programming 1. 2. 3.
Lessons 3.2 LESSON 1: HELLO WORLD (TURN ON AN LED) 3.2.1 Introduction The first lesson shows how to turn on an LED. 3.2.2 Hardware Effects DS1 will light up and stay lit indefinitely. 3.2.3 Summary The LEDs are connected to input-outpins (I/O) RC0 through RC3. First, the I/O pin must be configured for an output. In this case, when one of these pins is driven high (RC0 = 1), the LED will turn on. These two logic levels are derived from the power pins of the PIC MCU.
PICkit™ 3 Starter Kit User’s Guide 3.2.5 New Instructions All of the instructions for the enhanced mid-range or PIC18 can be studied in detail in the “Instruction Set Summary” chapter in each corresponding PIC® microcontroller data sheet. This document will briefly explain the importance of each. 3.2.5.
Lessons 3.2.6 Assembly 3.2.6.1 ENHANCED MID-RANGE EXAMPLE 3-4: #include
PICkit™ 3 Starter Kit User’s Guide This is a very important directive that is used the most in the enhanced mid-range core. This is an instruction to the assembler and linker to generate bank selecting code to set the bank to the one containing the TRISC register. In our case, that is Bank 1. This takes one instruction cycle. bcf TRISC, 0 This allows pin RC0 to be an output. A ‘1’ in the register configures the pin for an input and a ‘0’ for output.
Lessons 3.2.7 C Language The reader should notice that the PIC16 and PIC18 source code for the ‘C’ language is very similar. 3.2.7.1 ENHANCED MID-RANGE EXAMPLE 3-6: #include
PICkit™ 3 Starter Kit User’s Guide 3.2.7.2 PIC18 There is nothing different from the PIC16 version, except for the Configuration Words. For more information, see the PIC18F14K22 data sheet. DS41628B-page 46 2012 Microchip Technology Inc.
Lessons 3.3 LESSON 2: BLINK 3.3.1 Introduction This lesson blinks the same LED used in the previous lesson (DS1). This may seem trivial, but it requires a deep understanding on how the PIC MCU executes each instruction if using the assembly version. 3.3.2 Hardware Effects DS1 blinks at a rate of approximately 1.5 seconds. 3.3.3 Summary One way to create a delay is to spend time decrementing a value.
PICkit™ 3 Starter Kit User’s Guide In assembly, this is the most common instruction. Data is typically moved into WREG, where operations can be performed or moved into another register. 3.3.5.1.2 movwf Similar to movlw, data is moved from WREG to another register. EXAMPLE 3-7: movwf OPTION_REG Before Instruction: OPTION_REG = 0xFF W = 0x4F After Instruction: OPTION_REG = 0x4F W = 0x4F 3.3.5.1.3 decfsz Use this to decrement a register by one.
Lessons This configures the PIC MCU to run at 500 kHz. The working register (WREG) is used to move bytes into the register. Upon default, if this register was not written to, the PIC16F1829 would also run at 500 kHz. Other PIC devices are different, however, so this should always be written to in the first few lines of code.
PICkit™ 3 Starter Kit User’s Guide The variables, Delay1 and Delay2 will rollover from 0 to 255. This is why it is unnecessary to assign a value to the Delay1 and Delay2 variables before decrementing. 3.3.6.2 PIC18 While the Enhanced Core has its 16 bytes of general purpose RAM that is shared between all banks, the PIC18 has its equivalent at locations 0x00->0x5F. It gives the user access to 96 bytes, which the user can access without specifying the bank.
Lessons 3.4 LESSON 3: ROTATE 3.4.1 Introduction Rotate the lit LED between the four available LEDs. 3.4.2 Hardware Effects LEDs rotate from right to left. 3.4.3 Summary This lesson will introduce shifting instructions as well as bit-oriented skip operations to move the LED display. 3.4.4 New Registers 3.4.4.0.1 BOTH TABLE 3-7: NEW REGISTERS FOR BOTH DEVICES Register Purpose STATUS Used to check ALU status 3.4.4.0.
PICkit™ 3 Starter Kit User’s Guide 3.4.5.2 ENHANCED MID-RANGE TABLE 3-9: NEW INSTRUCTIONS FOR ENHANCED MID-RANGE Instruction English Logical shift right lsrf 3.4.5.3 Shift bits to the right PIC18 TABLE 3-10: NEW INSTRUCTIONS FOR PIC18 Instruction English Purpose Rotate right through carry rrcf 3.4.5.3.1 Purpose Shift bits to the right lsrf/rrcf The difference between a logical shift and a shift through carry is that a logical shift right will shift in a ‘0’ from the left.
Lessons DS1 is connected to RC0 and DS2 to RC1 and so forth. A shift to the right would actually be turning on the LEDs from right to left. This can be better explained in the following figures.
PICkit™ 3 Starter Kit User’s Guide The PIC18 can rotate through carry or not. If not, the LSb would simply be loaded into the MSb. The program needs to use the carry bit to test if it rotated the LED out of the displayable range, much like in the PIC16. The only difference is now the carry bit MUST be cleared when it rotates out of the display. If it is not cleared, the program will light up DS3 as intended, but soon all LEDs will be lit since LATCbits.
Lessons 3.5 LESSON 4: ANALOG-TO-DIGITAL CONVERSION 3.5.1 Introduction This lesson shows how to configure the ADC, run a conversion, read the analog voltage controlled by the potentiometer (RP1) on the board, and display the high order four bits on the display. 3.5.2 Hardware Effects The top four MSbs of the ADC are mirrored onto the LEDs. Rotate the potentiometer to change the display. 3.5.
PICkit™ 3 Starter Kit User’s Guide The PIC18F14K22 has a slightly different ANSEL register, but the functionality is the same. The top row of each register screen shot in every PIC microcontroller data sheet and in this document indicates more information on the functionality of each bit, such as its default state. The bit ANSA0, is read/writable, and will default to an analog input both on Power-on Reset (POR) and Brown-out Reset (BOR).
Lessons 3.5.5 New Instructions 3.5.5.1 BOTH TABLE 3-18: NEW INSTRUCTIONS FOR BOTH DEVICES Instruction English Swapf WREG with register SWAPF 3.5.5.1.1 Purpose Swap nibbles swapf This allows nibbles to be switched. A nibble consists of four bits and a byte contains two nibbles. For example: EXAMPLE 3-19: movlw b’01100110’ TABLE 3-19: BEFORE SWAPF Registers Value WREG B’01100110’ ADC is performed. ADRESH is full with ADC result of b'10100011’.
PICkit™ 3 Starter Kit User’s Guide 3.5.6 Assembly 3.5.6.
Lessons ADRESH >> 4; TABLE 3-23: //grab the top 4 MSbs TEMPORARY WORKSPACE REGISTER AFTER SHIFT ADRESH – after shift Bit # MSb (7) 6 5 4 3 2 1 LSb (0) value 0 0 0 0 0 1 0 1 Now LATC can be assigned to the temporary workspace register, since all of the LEDs are on pins . The rest of the bits in PORTC can be ignored.
PICkit™ 3 Starter Kit User’s Guide 3.6 LESSON 5: VARIABLE SPEED ROTATE 3.6.1 Introduction This lesson combines all of the previous lessons to produce a variable speed rotating LED display that is proportional to the ADC value. The ADC value and LED rotate speed are inversely proportional to each other. 3.6.2 Hardware Effects Rotate the POT counterclockwise to see the LEDs shift faster. 3.6.3 Summary A crucial step in this lesson is to check if the ADC value is 0.
Lessons Calls use one stack level. Remember that the PIC18 has a stack size of 31 levels, whereas the enhanced core has 16 levels. Anytime a call is performed, the return address will be pushed to the stack, then the program counter will go to the location in program memory where the label is located. It is important to note that stack depth should not be exceeded. For instance, performing 17 embedded call statements on the PIC16F1829 without returning at least once will cause a Stack Overflow.
PICkit™ 3 Starter Kit User’s Guide EXAMPLE 3-25: tstfsz return Delay2 ;if the ADC result is NOT '0', then simply return to MainLoop ;return to MainLoop If Delay2 is ‘0’, then the return instruction will not be executed, and instead skipped. 3.6.5.2.2 rcall A relative call should be used if the location to jump to is within 1K of the current location of the Program Counter. The reason is that rcall consumes only one word of program spaces, whereas a regular call takes two words. 3.6.6 Assembly 3.6.6.
Lessons 3.7 LESSON 6: DEBOUNCE 3.7.1 Introduction Mechanical switches play an important and extensive role in practically every computer, microprocessor and microcontroller application. Mechanical switches are inexpensive, simple and reliable. In addition, switches can be very noisy. The apparent noise is caused by the closing and opening action that seldom results in a clean electrical transition.
PICkit™ 3 Starter Kit User’s Guide 3.7.3 Summary This lesson uses a simple software delay routine to avoid the initial noise on the switch pin. The code will delay for only 5 ms, but should overcome most of the noise. The required delay amount differs with the switch being used. Some switches are worse than others. This lesson also introduces the #define preprocessing symbol in both ‘C’ and assembly. Hard coding pin locations is bad practice.
Lessons 3.8 LESSON 7: REVERSIBLE VARIABLE SPEED ROTATE 3.8.1 Introduction This lesson combines all of the previous lessons in using the button to reverse the direction of rotation when the button is pressed. The speed of rotation is controlled using the potentiometer. 3.8.2 Hardware Effects LEDs will rotate at a speed that is proportional to the ADC value. The switch will toggle the direction of the LEDs. 3.8.
PICkit™ 3 Starter Kit User’s Guide The debounce routine is more in-depth in this lesson because we need to keep in mind of the scenario of the switch being held down for long periods of time. If SW1 is held down, the LEDs would change direction rapidly, making the display look like it is out of control. The above flowchart will only change direction on the first indication of a solid press and then ignore the switch until it is released and pushed again.
Lessons EXAMPLE 3-31: RotateLeft: bcf rrcf btfss bra bsf bcf bra STATUS, C LATC,f STATUS,C MainLoop LATC, 3 STATUS, C MainLoop ;clear the carry ;rotate the LEDs (through carry) and turn on the next LED to the right ;did the bit rotate into the carry (i.e. was DS1 just lit?) ;yes, it did and now start the sequence over again by turning on DS4 ;clear the carry ;repeat this program forever 3.8.7 C Language 3.8.7.1 BOTH This version utilizes global variables.
PICkit™ 3 Starter Kit User’s Guide 3.9 LESSON 8: PULSE-WIDTH MODULATION (PWM) 3.9.1 Introduction This lesson does not rely on any of the previous lessons, but does use the same coding techniques and information learned thus far. In this lesson, a PIC MCU generates a PWM signal that lights an LED with the POT thereby controlling the brightness. 3.9.2 Hardware Effects Rotating the POT will adjust the brightness of a single LED. 3.9.
Lessons FIGURE 3-8: PWM ANALYSIS Period Pulse Width TMRx = PRx TMRx = CCPRxH:CCPxCON<5:4> TMRx = 0 The PWM period is specified by the PRx register. Timer 2/4/6 is used to count up to the value in CCPRxH combined with two LSbs in CCPxCON. CCPRxL is used to load CCPRxH. One can think of CCPRxL as a buffer which can be read or written to, but CCPRxH is read-only. When the timer is equal to PRx, the following three events occur on the next increment cycle: 1. TMRx is cleared 2. The CCPx pin is set 3.
PICkit™ 3 Starter Kit User’s Guide Both devices are using some features of the enhanced PWM module. The PIC16 will operate the CCP module in single output since the CCP2 P2A pin connects directly to DS4. The PIC18 will operate the CCP module in Full-Bridge mode in order to modulate P1D on DS3. Maximum resolution is achieved when the PRx register is set to 0xFF, or rather 255, the maximum value an 8-bit number can hold. Below is a scope capture of the PWM signal when the LED is dimly lit.
Lessons 3.9.5 Assembly 3.9.5.1 ENHANCED MID-RANGE EXAMPLE 3-34: call A2d movf banksel movwf ADRESH, w CCPR2L CCPR2L ;begin the Analog to Digital conversion ;ADRESH and ADRESL are now both full of the ADC result! ;Get the top 8 MSbs (remember that the ADC result is LEFT justified!) This fills the eight MSbs in the PWM register. The next few lines can be commented out and still provide the same perceived output.
PICkit™ 3 Starter Kit User’s Guide 3.10 LESSON 9: TIMER0 3.10.1 Introduction This lesson will produce the same output as 3.4 “Lesson 3: Rotate ”. The only difference is that this version uses Timer0 to provide the delay routine. 3.10.2 Hardware Effects LEDs rotate from right to left, similar to Lesson 3. 3.10.3 Summary Timer0 is a counter implemented in the processor. It may be used to count instruction cycles or external events, that occur at or below the instruction cycle rate.
Lessons 3.10.4.0.2 OPTION_REG This register controls Timer0 settings as well as some miscellaneous features, such as weak pull-ups, which will be used in later lessons. 3.10.4.1 PIC18 TABLE 3-31: NEW REGISTERS FOR PIC18 Register T0CON Purpose Timer0 configuration 3.10.4.1.1 T0CON The T0CON register and OPTION_REG are similar with respect to the prescaler settings and Timer0 assignment bits. The enhanced mid-range Timer0 is always enabled, so there is no need to enable it.
PICkit™ 3 Starter Kit User’s Guide 3.11 LESSON 10: INTERRUPTS AND PULL-UPS 3.11.1 Introduction This lesson introduces interrupts and how they are useful. It also introduces internal weak pull-ups that are available on most PIC devices. This lesson expands on the previous lessons, but mostly Lessons 9 and 3. 3.11.2 Hardware Effects LEDs rotate at a constant speed and the switch reverses their direction. 3.11.3 Summary Two new concepts are introduced: interrupts and weak pull-ups. 3.11.3.
Lessons The firmware within the Interrupt Service Routine (ISR) should determine the source of the interrupt by polling the interrupt flag bits. The serviced interrupt flag bits must be cleared before exiting the ISR to avoid repeated interrupts.
PICkit™ 3 Starter Kit User’s Guide 3.11.4 New Registers 3.11.4.1 BOTH TABLE 3-32: NEW REGISTERS FOR BOTH DEVICES Register WPUA Purpose Weak pull-up enable This enables the individual internal pull-up circuitry for each pin on PORTA. 3.11.4.2 ENHANCED MID-RANGE TABLE 3-33: NEW REGISTERS FOR ENHANCED MID-RANGE Register Purpose IOCAN Interrupt-on-change PORTA negative edge IOCAF Interrupt-on-change PORTA flags 3.11.4.2.
Lessons 3.11.5 New Instructions 3.11.5.1 BOTH TABLE 3-35: NEW INSTRUCTIONS FOR BOTH DEVICES Instruction retfie 3.11.5.1.1 English Return from interrupt Purpose Return to normal execution retfie The retfie instruction exits the ISR by popping the previous address from the stack and setting the GIE bit. The PIC18 requires the retfie, fast instruction to restore the saved context, whereas the enhanced mid-range does not have this distinction. 3.11.6 Assembly 3.11.6.
PICkit™ 3 Starter Kit User’s Guide EXAMPLE 3-40: #ifdef PULL_UPS banksel WPUA bsf WPUA, 2 ;enable the weak pull-up for the switch banksel OPTION_REG bcf OPTION_REG, NOT_WPUEN ;enable the global weak pull-up bit ;this bit is active HIGH, meaning it must be cleared for it to be enabled #endif The #ifdef is a preprocessor directive, which will look to see if the directive, in this case PULL_UPS, is defined. If so, the code between the #ifdef and #endif will be assembled.
Lessons 3.12 LESSON 11: INDIRECT ADDRESSING 3.12.1 Introduction This lesson covers a very important topic of indirect addressing. The code uses indirect addressing to implement a moving average filter. This lesson adds a moving average filter to the Analog-to-Digital code in Lesson 4. The moving average keeps a list of the last ADC values (n) and averages them together. The filter needs two parts: A circular queue and a function to calculate the average.
PICkit™ 3 Starter Kit User’s Guide The addressing modes are: 1. 2. 3. 4. Inherent Literal Direct Indirect 3.12.3.1 INHERENT AND LITERAL Many PIC device control instructions do not need any argument at all; they either perform an operation that globally affects the device or they operate implicitly on one register. This addressing mode is known as Inherent Addressing. Examples include SLEEP and RESET, which are used in the EEPROM lesson.
Lessons 3.12.4 New Registers 3.12.4.1 BOTH TABLE 3-36: NEW REGISTERS FOR BOTH DEVICES Register Purpose INDFx Virtual indirect register FSRx Holds target address of virtual register 3.12.4.1.1 INDFx/FSRx Because indirect addressing on both the PIC18 and enhanced mid-range core use the full address range, data RAM banking is not necessary. The FSR registers on the PIC18 form a 12-bit address while the enhanced mid-range forms a 16-bit address.
PICkit™ 3 Starter Kit User’s Guide 3.12.6 Assembly Language 3.12.6.1 BOTH EXAMPLE 3-43: FilterInit: movlw low Queue ;point to the Queue holding the ADC values movwf FSR0L movlw high Queue movwf FSR0H Here, FSR0 is pointed towards the Queue location. Figure 3-16 explains the code in Figure 3-43. It is important to note that FSR0 is two bytes wide in order to address locations in program memory across multiple pages.
Lessons EQUATION 3-5: Before Rotate right: b'00001010'= d'10' After Rotate right: 3.12.7 C language 3.12.7.1 BOTH b'00000101'= d'5' Pointers in ‘C’ are constructed by using the INDF/FSR pair on the PIC16/PIC18 to achieve the effect.
PICkit™ 3 Starter Kit User’s Guide 3.13 LESSON 12: LOOK-UP TABLE 3.13.1 Intro It is sometimes useful to implement a table to convert from one value to another. Expressed in a high-level language it might look like this: EQUATION 3-6: y = function(x); That is, for every value of x, the function returns the corresponding y value. Look-up tables are a fast way to convert an input to meaningful data because the transfer function is pre-calculated and “looked up”, rather than calculated on the fly.
Lessons 3.13.4.1.2 PCLATH: The contents of PCLATH and PCLATU (if using the PIC18) are transferred to the program counter by any operation that writes PCL. Similarly, the upper two bytes of the program counter are transferred to PCLATH and PCLATU by an operation that reads PCL. This is useful for the computed offsets to the PC that are used in this lesson. For the PIC18, the PC increments by two to address sequential instructions in program memory. The PC increments by one in the enhanced mid-range core.
PICkit™ 3 Starter Kit User’s Guide 3.13.5 New Registers 3.13.5.1 ENHANCED MID-RANGE TABLE 3-39: NEW REGISTERS FOR ENHANCED MID-RANGE Register Purpose EEADRx Address to read/write in program or EEPROM memory EEDATx 2-byte word that holds 14-bit data for read/write EECON1 Control register for memory access Flash program memory is also writable during normal operation. This is commonly referred to as “Self-modifying code” or “Self-write”.
Lessons 3.13.6 New Instructions: 3.13.6.1 BOTH: TABLE 3-41: NEW INSTRUCTIONS FOR BOTH DEVICES Instruction English Return with literal in WREG retlw Purpose Table look-ups 3.13.6.1.1 retlw The WREG register is loaded with the 8-bit literal specified as 8 bits in the instruction word. The program counter is then loaded from the top of the stack (the return address). Recall that the PIC MCU utilizes a modified Harvard architecture.
PICkit™ 3 Starter Kit User’s Guide FIGURE 3-19: TABLE POINTER USED TO READ ONE BYTE OF DATA – ANSWER IS REFLECTED IN ‘TABLAT’ 3.13.7 Assembly Language 3.13.7.1 ENHANCED MID-RANGE There are three methods of accessing constants in program memory: 1. Computed goto 2. Indirect Addressing 3. Table Reads The code will implement 3 different ways to utilize these methods: 1. Computed goto a) Program Counter b) BRW instruction 2. Indirect Addressing a) FSR/INDF 3.
Lessons the lower eight bits is not automatically carried over into PCLATH. Because of this, be sure to check the Carry flag in the STATUS register immediately after the table offset addition, so that the PCLATH can be modified accordingly.
PICkit™ 3 Starter Kit User’s Guide EXAMPLE 3-49: banksel movlw movwf movlw addwf btfsc incf movwf EEADRL high TableStart EEADRH low TableStart temp, w STATUS, C EEADRH, f EEADRL ; bank 3 ; ; Store MSb of address ; bcf bsf bcf bsf nop nop bsf movf return ;movwf ;movf ;movwf EECON1, EECON1, INTCON, EECON1, INTCON, GIE EEDATL, w ; ; ; ; ; ; ; ; PROG_DATA_LO EEDATH,W PROG_DATA_HI ; Store in user location ; Get MSb of word ; Store in user location CFGS EEPGD GIE RD ; Store LSb of address Do not select
Lessons 3.13.7.3 PROGRAM COUNTER Like the Enhanced Core implementation, the differences being that PCLATU is used to address the full 21 bits, and that the ADC value is multiplied by two, because the 16-bit PIC18 instructions are byte-addressable on the even addresses.
PICkit™ 3 Starter Kit User’s Guide EXAMPLE 3-53: const unsigned char gray_code[] = { //lookup table for binary->gray code 0b0000,0b0001,0b0011,0b0010,0b0110, 0b0111,0b0101,0b0100,0b1100,0b1101, 0b1111,0b1110,0b1010,0b1011,0b1001, 0b10000 }; For PIC18 devices, the compiler will recognize the “char” type as a byte and assign two byte addressable data points per program memory word. Notice the data space difference if this ‘const’ keyword is forgone.
Lessons 3.14 LESSON 13: EEPROM 3.14.1 Introduction This lesson provides code for writing and reading a single byte onto the on-board EEPROM. EEPROM is nonvolatile memory, meaning that it does not lose its value when power is shut off. This is unlike RAM, which will lose its value when no power is applied. The EEPROM is useful for storing variables that must still be present during no power. It is also convenient to use if the entire RAM space is used up.
PICkit™ 3 Starter Kit User’s Guide 3.14.4 New Registers 3.14.4.1 BOTH TABLE 3-44: NEW REGISTERS FOR BOTH DEVICES Register Purpose EECON2 Performs the required write sequence 3.14.4.1.1 EECON2 In order to write to EEPROM, a special sequence must be performed on EECON2. This register is only used for EEPROM writes and nothing else. 3.14.5 New Instructions 3.14.5.1 BOTH TABLE 3-45: NEW INSTRUCTIONS FOR BOTH DEVICES Instruction SLEEP English Go to Sleep Purpose Low-power operation 3.14.5.1.
Lessons EXAMPLE 3-55: ;REQUIRED SEQUENCE for EEPROM write movlw 0x55 movwf EECON2 movlw 0xAA movwf EECON2 bsf EECON1, WR ;begin write When the WR bit is set, it remains set until the write to EEPROM is complete. 3.14.7 C Language 3.14.7.1 BOTH There are two functions that the XC8 compiler provides, which greatly simplify EEPROM reads and writes. EXAMPLE 3-56: eeprom_read() eeprom_write(,); Use this to read and write single bytes from EEPROM. 2012 Microchip Technology Inc.
PICkit™ 3 Starter Kit User’s Guide NOTES: DS41628B-page 96 2012 Microchip Technology Inc.
GND 1K SW-B3F1000 SW1 RA2 PICkit® 10K R1 VPP/MCLR VDD GND ICSPDAT ICSPCLK NC P1 R2 1 2 3 4 5 6 1 2 +V +V GND +V RA4_T1G RA5 GND CCW 2 CW RB7 RC7 RC6 RC3 RC4 RC5 RA3_MCLR_VPP POT-3352E 10K RP1 JP5 RA0_ICSPDAT RA1_ICSPCLK RA4_T1G RA3_MCLR_VPP GND 3 1 2 1 P2 GND 1K C2 2012 Microchip Technology Inc. 9 8 7 6 5 4 3 2 1 +V DIP20 1828 DIP14 1823 DIP8 1822 0.
PICkit™ 3 Starter Kit User’s Guide A.1 USEFUL MPLAB® X SHORTCUTS MPLAB X provides several new features to the MPLAB IDE. It is based on the existing development platform, Netbeans. The following is a list of useful shortcuts available while developing code: 1. ALT+SHIFT+’F’ a) Auto Format code 2. CTRL+E a) Delete the currently selected line and move up the next 3. CTRL+SEMICOLON a) Append a semicolon to the end of the currently selected line 4.
Block Diagram and MPLAB® X Shortcuts EXAMPLE A-1: Bad: #define flowRate 0x20 flowRate equ 0x20 Good: Cblock 0x20 flowRate flowTotal endc 2012 Microchip Technology Inc.
PICkit™ 3 Starter Kit User’s Guide NOTES: DS41628B-page 98 2012 Microchip Technology Inc.
Worldwide Sales and Service AMERICAS ASIA/PACIFIC ASIA/PACIFIC EUROPE Corporate Office 2355 West Chandler Blvd. Chandler, AZ 85224-6199 Tel: 480-792-7200 Fax: 480-792-7277 Technical Support: http://www.microchip.com/ support Web Address: www.microchip.