PICkit™ 3 Debug Express PIC18F45K20 – MPLAB® C Lessons © 2009 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 DEBUG EXPRESS Table of Contents Chapter 1. Introduction 1.1 Before Beginning the Lessons ....................................................................... 7 Chapter 2. PIC18FXXXX Microcontroller Architectural Overview 2.1 Memory Organization ..................................................................................... 9 2.2 Program Memory ............................................................................................ 9 2.2.1 Data Memory .................................
PICkit™ 3 Debug Express 3.6 Lesson 6: Using PICkit 3 Debug Express .................................................... 37 3.6.1 Resources Reserved by the PICkit 3 Debug Express ...............................37 3.6.1.1 General Resources ....................................................................37 3.6.1.2 Program and Data Memory Resources .....................................37 3.6.2 Selecting PICkit 3 as a Debugger in the MPLAB IDE ................................38 3.6.
PICkit™ 3 DEBUG EXPRESS 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. This number is located on the bottom of each page, in front of the page number.
PICkit™ 3 Debug Express 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 PICkit™ 3 Debug Express. Other useful documents are listed below. The following Microchip documents are available and recommended as supplemental reference resources.
PICkit™ 3 Debug Express 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 Debug Express NOTES: DS41370C-page 6 © 2009 Microchip Technology Inc.
PICkit™ 3 DEBUG EXPRESS Chapter 1. Introduction The following series of lessons covers the basics of developing applications for the Microchip PIC18 series of microcontrollers. Working with the MPLAB® IDE, MPLAB® C Compiler for the PIC18, and the PICkit™ 3 Development Programmer/Debugger is introduced in a series of lessons that cover fundamental microcontroller operations, from simply turning on an LED to creating Interrupt Service Routines.
PICkit™ 3 Debug Express FIGURE 1-1: DS41370C-page 8 MPLAB C COMPILER INSTALLATION CONFIGURATION OPTIONS © 2009 Microchip Technology Inc.
PICkit™ 3 DEBUG EXPRESS Chapter 2. PIC18FXXXX Microcontroller Architectural Overview This section provides a simple overview of the PIC18FXXXX microcontroller architecture. 2.1 MEMORY ORGANIZATION The PIC18FXXXX microcontrollers are Harvard architecture microprocessors, meaning that program memory and data memory are in separate spaces. This allows faster execution as the program and data busses are separate and dedicated, so one bus does not have to be used for both memory types.
PICkit™ 3 Debug Express 2.2.1 Data Memory Data memory is called “file register” memory in the PIC18XXXX family. It consists of up to 4096 bytes of 8-bit RAM. Upon power-up, the values in data memory are random. Data is organized in banks of 256 bytes, requiring that a bank (the upper 4 bits of the register address) be selected with the Bank Select Register (BSR). Special areas in Bank 0 and in Bank 15 can be accessed directly without concern for banking. These special data areas are called Access RAM.
PICkit™ 3 DEBUG EXPRESS Chapter 3. PICkit™ 3 Debug Express Lessons Connect the PICkit™ 3 Programmer/Debugger to a PC USB port, and connect the demo board to the PICkit via header P1, labeled ICSP™. 3.1 LESSON 1: HELLO LED This first lesson shows how to create a MPLAB C compiler project in the MPLAB® IDE and turn on a demo board LED using the PIC18F45K20. Key Concepts - Use the MPLAB IDE Project Wizard to create a new project for a microcontroller.
PICkit™ 3 Debug Express FIGURE 3-1: 3.1.1.2 WIZARD STEP ONE: SELECT PIC18F45K20 DEVICE STEP TWO: SELECT A LANGUAGE TOOLSUITE This PIC18F microcontroller project will be in C, so select the “Microchip C18 Toolsuite” from the “Active Toolsuite:” dropdown box, as shown in Figure 3-2. Click Next to continue. FIGURE 3-2: DS41370C-page 12 WIZARD STEP TWO: SELECT TOOLSUITE © 2009 Microchip Technology Inc.
PICkit™ 3 Debug Express Lessons 3.1.1.3 STEP THREE: CREATE A NEW PROJECT Create the project file in the existing directory for Lesson 1. Browse to the directory folder C:\Lessons\PICkit 3 Debug Express Lessons\01 Hello LED and name the project Lesson 1 LED. Save the project and then click Next to continue, as shown below in Figure 3-3. FIGURE 3-3: 3.1.1.
PICkit™ 3 Debug Express FIGURE 3-4: WIZARD STEP FOUR: ADD EXISTING FILES 3.1.1.5 SUMMARY In the final Wizard Dialog window, verify the project parameters and click Finish. To view the Project window in the MPLAB IDE, select from the menu View>Project. The Project window (see Figure 3-5) shows the workspace file name (Lesson 1 LED.mcw) in the title bar, and the project file (Lesson 1 LED.mcp) at the top of the file tree view.
PICkit™ 3 Debug Express Lessons FIGURE 3-5: THE PROJECT WINDOW To complete the project setup, we will add a Linker Script and microcontroller header file to the project. A Linker Script is required to build the project. It is a command file for the linker, and defines options that describe the available memories on the target microcontroller. There are four example linker files for the microcontroller: 18f45k20.lkr Basic Linker Script file for compiling a memory image in non-extended processor mode.
PICkit™ 3 Debug Express FIGURE 3-6: ADD FILES TO PROJECT FIGURE 3-7: NEW PROJECT FILES Select Project > Save Project to save the new project configuration. 3.1.2 Exploring the Lesson 1 Source Code Double-click the 01 Hello LED.c source file name to open the lesson source code file in an MPLAB IDE editor window. DS41370C-page 16 © 2009 Microchip Technology Inc.
PICkit™ 3 Debug Express Lessons FIGURE 3-8: /** C O N LESSON 1 “HELLO LED” SOURCE CODE F I G U R A T I O N B I T S ******************************/ #pragma config FOSC = INTIO67 #pragma config WDTEN = OFF, LVP = OFF /** I N C L U D E #include "p18f45K20.h" S **************************************************/ /** T I void { D E main C L A R A O N S *******************************************/ (void) TRISD = 0b01111111;// LATDbits.
PICkit™ 3 Debug Express 3.1.3 Building and Programming the Lesson 1 Code Build the lesson code in an executable memory image by selecting Project > Build All in the MPLAB IDE. The memory image is stored in a .hex file in the project directory. The results of the build will be shown in the Output window in the MPLAB IDE workspace under the Build tab. The calls to the MCC18 compiler and Linker are shown, along with any errors that may occur.
PICkit™ 3 Debug Express Lessons FIGURE 3-10A: OUTPUT WINDOW PICkit 3 PROGRAMMER FIGURE 3-10B: PICkit 3 PROGRAMMER POWER SUPPLY Program the built code into the PIC® microcontroller by selecting menu Programmer > Program. The results of the programming operation will appear in the Output window as shown in Figure 3-11. Congratulations! You have created, built, programmed, and executed your first Microchip PIC18F project! © 2009 Microchip Technology Inc.
PICkit™ 3 Debug Express FIGURE 3-11: OUTPUT WINDOW PICkit 3 PROGRAMMING RESULTS Note: DS41370C-page 20 If an error occurs during programming, consult the PICkit 3 help file in the MPLAB IDE. Select Help > Topics… then under the “Programmers” heading select “PICki t 3 Programmer” and click OK. On the Contents tab, select the “Troubleshooting” section for information. © 2009 Microchip Technology Inc.
PICkit™ 3 Debug Express Lessons 3.2 LESSON 2: BLINK LED This lesson discusses the Configuration bits of the PIC18FXXXX microcontrollers and how to set them in an MPLAB C source file. It also presents using a library function and shows how delays can be used to blink an LED on the demo board. Key Concepts - Open existing project work spaces by selecting File > Open Workspace… in the MPLAB IDE.
PICkit™ 3 Debug Express FIGURE 3-12: /** C O N LESSON 2 “BLINK LED” CONFIGURATION BIT DEFINITIONS F I G U R #pragma config FOSC #pragma #pragma #pragma #pragma #pragma #pragma #pragma #pragma #pragma #pragma config config config config config config config config config config PWRT = WDTEN MCLRE STVREN CP0 = CPB = WRT0 = WRTB = EBTR0 = EBTRB = = AT I O INTIO67, N B I FCMEN = T S OFF, ******************************/ IESO = OFF OFF, BOREN = SBORDIS, BORV = 30 = OFF, WDTPS = 32768 =
PICkit™ 3 Debug Express Lessons The Configuration bit settings that are important for this lesson project and are different from the default values are: FOSC = INTIO67 This sets the PIC18F45K20 to run using the internal oscillator, so no crystal or external oscillator is needed. The default frequency is 1 MHz. The oscillator is covered in more detail in Lesson 9. It also sets OSC1 and OSC2 pins to be used as the RA6 and RA7 I/O port pins as the OSC pin functions are not needed.
PICkit™ 3 Debug Express Headers for the libraries can be found in the MCC18 header directory C:\MCC18\h. The source code for most of the libraries can be found in C:\MCC18\src, and the libraries themselves are in C:\MCC18\lib. For more detailed information on the included library functions see the “MPLAB C18 C Compiler Libraries” document (DS51297).
PICkit™ 3 Debug Express Lessons 3.3 LESSON 3: ROTATE LED This lesson builds on the previous two lessons to introduce defining global variables and code sections, and to add rotation to the LED display. It will light up LED 0, then shift it to LED 1, then to LED 2 and on up to LED 7, and back to LED 0. In this and following lessons, please open the lesson workspace in the MPLAB IDE upon starting the lesson.
PICkit™ 3 Debug Express For a list of data types supported by MPLAB C, their sizes and limits, see Section 2.1 of the “MPLAB C18 C Compiler User’s Guide” (DS51288). 3.3.2 Allocating Program Memory Program memory will most often be used for program instructions and constant data. The source code for Lesson 3 includes examples of both, as shown in Figure 3-17.
PICkit™ 3 Debug Express Lessons FIGURE 3-18: PROGRAM MEMORY “LED_LOOKUPTABLE” ARRAY VALUES The directive #pragma code is then used to specify the following section, beginning with the main () declaration, will be executable instructions to place in program memory. Since an optional section name and address are not specified, the code instructions will be placed at the first available address by the linker.
PICkit™ 3 Debug Express FIGURE 3-19: LESSON 3 “ROTATE LED” SOURCE CODE /** V A R I A B L E S *************************************************/ #pragma udata // declare statically allocated uninitialized variables unsigned char LED_Number; // 8-bit variable /** D E C L A R A T I O N S *******************************************/ // declare constant data in program memory starting at address 0x180 #pragma romdata Lesson3_Table = 0x180 const rom unsigned char LED_LookupTable[8] = {0x01, 0x02, 0x04, 0x08, 0x
PICkit™ 3 Debug Express Lessons 3.4 LESSON 4: SWITCH INPUT The demo board switch is used in the lesson to rotate the LEDs once on each press. Key Concepts - The directive #define can be used to give SFR registers and bits more meaningful names. - I/O pins that share an analog input channel must be configured as digital pins if used as digital inputs using SFR ADCON1, or they will always read ‘0’. - The PORTx SFRs are used to read the logic state on an input port pin.
PICkit™ 3 Debug Express 3.4.2 Switch Debouncing Mechanical switches are frequently encountered in embedded processor applications, and are inexpensive, simple and reliable. However, such switches are also often very electrically noisy. This noise is known as switch bounce, whereby the connection between the switch contacts makes and breaks several, perhaps even hundreds, of times before settling to the final switch state.
PICkit™ 3 Debug Express Lessons FIGURE 3-22: LESSON 4 “SWITCH INPUT” SOURCE CODE /** V A R I A B L E S *************************************************/ #pragma udata // declare statically allocated uinitialized variables unsigned char LED_Display; // 8-bit variable /** D E C L #pragma code// void { main A R A declare T I O N executable S *******************************************/ instructions (void) unsigned char LED_Display 0; initialize // PORTD // // // // enable PORTB internal pullups
PICkit™ 3 Debug Express In the PIC18F45K20, the RB0 pin is shared with analog input AN12. Such pins must be configured as either digital or analog inputs. This is important because RB0 will be used as a digital input pin to read the state of the switch in register PORTB. If RB0 is configured as an analog input, it will always read ‘0’ and not the actual state of the switch. Pins are configured as analog or digital in the SFRs ANSEL and ANSELH.
PICkit™ 3 Debug Express Lessons 3.5 LESSON 5: USING TIMER0 Timer0 is used to time delays while rotating the demo board LEDs, instead of using program loop delays. The demo board switch reverses the direction of the rotation. Key Concepts - Timer0 is hardware counter implemented in the microcontroller that can count clock cycles or external events. - Using a timer instead of processor delay loops frees up the processor to do useful work instead of counting cycles.
PICkit™ 3 Debug Express FIGURE 3-25: T0CON: TIMER0 CONTROL REGISTER To use Timer0 to replace the software delay Delay1KTCYx(50), it should be set up so it overflows about every 200 to 300 ms. Let’s go over the T0CON bit settings to make this happen: T08BIT = 0 Timer0 is configured as a 16-bit timer/counter to illustrate the buffering of TMR0H. T0CS = 0 Timer0 runs off the internal instruction clock. At Fosc = 1MHz, the instruction clock is 250kHz.
PICkit™ 3 Debug Express Lessons T0PS2:T0PS0 = 000 Since the prescaler is not assigned, these bits are “don’t care.” And finally: TMR0ON = 0 This bit turns the timer on and off. It’s set to zero now as the timer will be turned on once it is has been set up. To configure Timer0 with these settings, the binary value 0b0000100 is written to T0CON. The PIC18F45K20 has 3 other configurable timers: Timer1, Timer2 and Timer3.
PICkit™ 3 Debug Express Line 5 sets bit 7, TMR0ON, of the T0CON register to turn on the timer so it begins incrementing. Using one of the SFR unions to access bits, like T0CONbits.TMR0ON, can change bits without affecting the other bits. Be aware that some cases using an SFR union to access a bit may affect other bits. What actually happens during this instruction execution is the register is read, the bit is modified, and the entire register is re-written. This operation is called Read-Modify-Write.
PICkit™ 3 Debug Express Lessons 3.6 LESSON 6: USING PICkit 3 DEBUG EXPRESS This lesson covers using the PICkit 3 as an In-Circuit-Debugger (ICD). It uses the same MPLAB IDE workspace and project as Lesson 5. Set T0CON assignment back to the “no prescale” statement if it was changed in the last lesson. Key Concepts - An In-Circuit-Debugger like PICkit 3 uses some on-chip resources to enable debugging.
PICkit™ 3 Debug Express Note: 3.6.2 An ICD ‘i’ Linker Script must be used when debugging, as discussed in Section 3.1.1 of this document. The lesson projects already use the correct Linker Script, 18f45k20 i.lkr. Selecting PICkit 3 as a Debugger in the MPLAB IDE The PICkit 3 cannot be used as a programmer and debugger at the same time, so if PICkit 3 is currently selected as a programmer, selecting it as a debugger will cause it to be disabled as a programmer.
PICkit™ 3 Debug Express Lessons 3.6.3.2 STEP Stepping, also known as single-stepping, allows the code to be executed one statement at a time. There are three step options: Step Into This will step through statements one at a time until a function call is reached. When Step Into is selected on a function call, the debugger will step to the first statement in the called function. Shortcut key is . FIGURE 3-28: STEP INTO Step Over This will step through statements one at a time.
PICkit™ 3 Debug Express FIGURE 3-30: C018 START-UP LIBRARY CODE 3.6.4 Using Breakpoints When debugging code, a “breakpoint” can be added to a program statement. When running the program, the debugger will halt the target upon reaching the breakpoint statement. In the MPLAB IDE 05 Timer.c source code, place the editor cursor on line 111, SwitchPressed = TRUE;, and right-click to open the contextual menu. Select Set Breakpoint as shown in Figure 3-31.
PICkit™ 3 Debug Express Lessons FIGURE 3-31: SET BREAKPOINT ON LINE 111 FIGURE 3-32: BREAKPOINT SET The statement we’ve placed the breakpoint on will be executed when the Demo Board Switch button is pressed. Select Debugger > Run to begin program execution. The demo board LEDs will rotate as the code runs since the breakpoint statement has not been executed yet. Press the Demo Board Switch button. The program will halt on the breakpoint statement, as shown in Figure 3-33.
PICkit™ 3 Debug Express FIGURE 3-33: BREAKPOINT HALT The number of breakpoints that can be set at once in a program depends on the PIC18FXXXX device being debugged. Select menu Debugger > Breakpoints… This will open a dialogue box to show the currently set breakpoints. The Silicon Debug Resource Toolbar provides information on the total number of breakpoints available for the selected device (“HW BP”) and the number of used breakpoints (“Used”).
PICkit™ 3 Debug Express Lessons Note: 3.6.5 The number of active breakpoints can affect using the Step Into and Step Over functions. When these functions are used, a breakpoint is set at the next statement to step to. If all breakpoints are currently used and none are available, the MPLAB IDE is not able to set a breakpoint on the next C statement. Instead, it must step through each assembly instruction until the next statement is reached.
PICkit™ 3 Debug Express FIGURE 3-36: Note: WATCH VARIABLES The “Value” fields in the Watch window, File Register window, and Special Function Register windows may not be valid immediately after first being opened. Step the code once to update the values. For each watch variable, the Watch window displays the File Register Address, the Symbol Name (variable name), and current Value. The value display format can be changed by right-clicking on a value and selecting Properties from the pop-up menu.
PICkit™ 3 Debug Express Lessons 3.7 LESSON 7: ANALOG-TO-DIGITAL CONVERTER (ADC) Lesson 7 builds on the previous lesson by using the on-chip ADC to read the demo board potentiometer voltage. The result is used to vary the LED rotation time delay so that the potentiometer controls the LED rotation speed. Key Concepts - An Analog-to-Digital Converter is used to convert an analog voltage level into a digital number representing the voltage.
PICkit™ 3 Debug Express #2: The VCFGx bits in ADCON1 can select the ADC voltage references to use the AN2 and AN3 pins, VDD and VSS, or some combination. Since the demo board does not have voltage references connected to AN2 and AN3, the ADC will be referenced to VDD and VSS. This means an ADC result of ‘0’ corresponds to 0 Volts, or VSS. A result of ‘1023’ corresponds to about 3.3 Volts, or VDD.
PICkit™ 3 Debug Express Lessons The ACTQx bits determine the acquisition time, and should take into account the internal acquisition time Tacq of the ADC, data sheet parameter 132, and the settling time of the application circuit connected to the ADC pin. From the data sheet, the internal acquisition time Tacq = 1.4us over temperature. The application circuit is an RC network formed by the potentiometer and capacitor C3, which has a very long settling time.
PICkit™ 3 Debug Express #5: To begin an ADC conversion, set bit 1 of ADCON0, the GO/DONE bit. When the conversion is done the hardware will clear that bit, so the GO/DONE may then be polled to wait for the conversion to complete. Once the conversion is complete and GO/DONE = 0, the ADC conversion result may be read from ADRESH and ADRESL. 3.7.3 Exploring the Lesson 7 Source Code Open the lesson source files 07 ADC.c and 07 ADC.h in an MPLAB editor window if they are not already open.
PICkit™ 3 Debug Express Lessons See Section 19.0 “10-Bit Analog-to-Digital Converter (A/D) Module” in the PIC18F45K20 Data Sheet (DS41303) for more information on the ADC peripheral. Note: If TMR0L is added to the Watch window, it will cause incorrect operation when stepping through the following 2 lines of code: TMR0H = ADC_Convert(); TMR0L = 0; This is caused by the buffered nature of TMR0H.
PICkit™ 3 Debug Express When an interrupt occurs and the Master Interrupt signal is asserted, the PIC microcontroller finishes executing the current instruction, stores the next address on the Return Address Stack, and then jumps to an interrupt vector. At the interrupt vector it begins executing a function designated as the Interrupt Service Routine. When this function exits, program execution returns to the address stored on the Return Address Stack.
PICkit™ 3 Debug Express Lessons FIGURE 3-41: /** I N T DEFINE INTERRUPT VECTORS E R R U P T S ***********************************************/ //---------------------------------------------------------------------------// High priority interrupt vector #pragma code InterruptVectorHigh = 0x08 void InterruptVectorHigh (void) { _asm goto InterruptServiceHigh //jump to interrupt routine _endasm } //---------------------------------------------------------------------------// Low priority interrupt
PICkit™ 3 Debug Express associated interrupt enable bits are in the PIEx registers, and the IPRx register bits set the interrupt priority as low or high. For detailed information the bits in these registers, see Section 9.0 “Interrupts” of the PIC18F45K20 Data Sheet (DS41303). FIGURE 3-43: LESSON 8 INTERRUPT INITIALIZATIONS // Set up switch INTCON2bits.INTEDG0 INTCONbits.INT0IF INTCONbits.
PICkit™ 3 Debug Express Lessons FIGURE 3-44: LESSON 8 INTERRUPT SFRS In the Lesson 8 source code, all the statements to change the rotation direction are in the INT0 switch interrupt function, and the statements to rotate the LED display are in the TMR0 interrupt function. All that remains in the main program is a while() loop that updates the PORTD register with LED_Display.
PICkit™ 3 Debug Express 3.9 LESSON 9: INTERNAL OSCILLATOR Using the on-chip internal oscillator and PLL (Phase Locked Loop) of the PIC18F45K20 is discussed. Clocks from 31 kHz up to 64 MHz can be generated without requiring external oscillator components. Key Concepts - To use the internal oscillator block, set the OSC Configuration bits to INTIO67 or INTIO7. The latter outputs the clock signal CLKO on the RA6 pin.
PICkit™ 3 Debug Express Lessons 3.9.2 Configuring the Internal Oscillator The internal oscillator block is selected as the primary oscillator in the Configuration bits. The OSC bits in the CONFIG1H Configuration Word are set to either INTIO67 or INTIO7. When INTIO67 is selected, the internal oscillator is the primary oscillator with the external oscillator pins OSC2 and OSC1 available as RA6 and RA7 IO.
PICkit™ 3 Debug Express The IRFCx bits may be changed by software during program execution, allowing the program to “throttle” the microcontroller execution speed to current processing needs. This can save on power consumption when fast clock speeds aren’t required. The OSTS and IOFS bits are read-only Status bits. The PIC18F45K20 has the option to start-up running off the internal oscillator until an external oscillator circuit has stabilized.
PICkit™ 3 Debug Express Lessons 3.9.3 Exploring the Lesson 9 Source Code The Lesson 9 program code has a simple background loop in the main() function that displays a binary count on the demo board LEDs, as shown in Figure 3-46. Each count increment is delayed by 64,000 instruction cycles. As the clock frequency is changed, the instruction rate changes and so the total time in seconds of the delay gets shorter as the clock frequency increases.
PICkit™ 3 Debug Express 3.10 LESSON 10: USING INTERNAL EEPROM The PIC18F45K20 microcontroller includes 256 bytes of on-chip EEPROM for data storage. This lesson discusses reading and writing the internal EEPROM in software. Key Concepts - The 4 SFRs that control EEPROM operations are EECON1, EECON2, EEDATA and EEADR. - The internal EEPROM is written and read one byte at a time. - To write EEPROM, a short code sequence must be written to EECON2 immediately before starting the write operation.
PICkit™ 3 Debug Express Lessons A read of an EEPROM byte begins by clearing the EEPGD bit in EECON1. This selects the data EEPROM array for access. The CFGS bit should also be cleared during an EEPROM access; it is only set to access the Configuration bit locations. The byte address of the data EEPROM location to be read is loaded into the EEADR register. The RD bit in EECON1 is then set to execute the read.
PICkit™ 3 Debug Express For more information on the data EEPROM memory see Section 7.0 of the PIC18F45K20 Data Sheet (DS41303). FIGURE 3-51: DATA EEPROM WRITE void EEPROM_Write(unsigned char address, unsigned char databyte) { // writes the "databyte" value to EEPROM at the address given // location in "address". EECON1bits.EEPGD = 0; // Set to access EEPROM memory EECON1bits.CFGS = 0; // Do not access Config registers EEDATA = databyte; // Load EEDATA with byte to be written EEADR = address; EECON1bits.
PICkit™ 3 Debug Express Lessons 3.11 LESSON 11: PROGRAM MEMORY OPERATIONS Topics covered in this include reading, writing and erasing locations in the Flash program memory, protecting areas of program memory in the Configuration bits, and considerations for using C pointers to program memory. Key Concepts - Pointers declared with the ROM keyword point to program memory locations. - The EECON1 and EECON2 SFRs control program memory erase and write operations.
PICkit™ 3 Debug Express FIGURE 3-52: PROJECT CODE MODEL SETTINGS Once a pointer to program memory has been declared, it can be pointed to a declared location in program memory, for example a #pragma romdata array, or an explicit address. #pragma romdata mystrings = 0x100 rom char hello_str[] = "Hello!"; rom_pointer char letter = = hello_str; *rom_pointer // = &hello_str[0] The first letter ‘H’ of the hello_str[] array in program memory is now pointed to by rom_pointer.
PICkit™ 3 Debug Express Lessons 3.11.1 Erasing and Writing Flash Program Memory Unlike writing Data EEPROM Memory, writing Flash program memory requires that the locations being written are erased first. When erased, a program memory location has all bits set to ‘1’. Thus an erased byte has the hex value 0xFF. Writing a program memory location sets the appropriate bits to ‘0’, but a write cannot set a bit ‘1’.
PICkit™ 3 Debug Express When the 32 locations are written with the pointer, they are not actually written to program until the completion of the entire sequence. The pointer writes actually store the data in 32 temporary hardware registers. When the actual write sequence is executed, it is the contents of this 32-byte buffer that is written to the program memory array. For example, we might use a for loop to write the contents of a RAM array to these buffers using a ROM pointer.
PICkit™ 3 Debug Express Lessons FIGURE 3-53: EXAMPLE PROGRAM MEMORY WRITE FUNCTION unsigned char ProgMemWr32(unsigned int address, unsigned char *buffer_ptr) { // NOTE: program memory must also be erased first. near rom unsigned char *ptr; char i; ptr = (rom unsigned char *)(address & 0xFFE0);// ensure write starts for (i = 0; i < 32; EECON1bits.CFGS = 0; EECON1bits.FREE = 0; EECON1bits.WREN = 1; // execute code sequence, = 0; EECON2 = 0x55; EECON2 = 0xAA; EECON1bits.WR = 1; INTCONbits.
PICkit™ 3 Debug Express 3.11.
PICkit™ 3 Debug Express Lessons Step into the following statement and through the function, which erases a 64-byte block of memory that the strings are stored in. // Erase the 64 bytes starting at 0x100 ProgMemErase64(0x100); After completing the erase, select menu Debugger > Read. In the Program Memory window, the 64 bytes of program memory starting at address 0x0100 where the strings were stored have been erased, as shown in Figure 3-56.
PICkit™ 3 Debug Express 3.12 LESSON 12: USING THE CCP MODULE PWM This lesson gives a brief introduction to using the Pulse Width Modulation (PWM) functionality of the Capture/Compare/PWM (CCP) peripheral of the PIC18F45K20. Key Concepts - The PWM time base (frequency) is determined by Timer2 and the PR2 Special Function Register. - PWM operation of the CCP module is selected in the CCPxCON SFR.
PICkit™ 3 Debug Express Lessons Timer2 also has a postscaler, but the postscaler does not affect the CPP module operation PWM time base, so its settings are “don’t care.” The Timer2 module also has a Period Register, known as PR2. This Special Function Register is the maximum to which Timer2 can count before being reset to 0. Normally, an 8-bit timer would count up to 255 before resetting to 0 and beginning to count again. With the PR2 register, the timer counts up to the value in PR2.
PICkit™ 3 Debug Express FIGURE 3-60: SIMPLIFIED PWM BLOCK DIAGRAM Now that the frequency has been determined, it is necessary to set up the CCP1 module for PWM using the CCP1CON register. Bits CCP1Mx determine the module mode; there is only one value to select for PWM, CCP1Mx = 0b11xx where the ‘x’ bits are “don’t care”, so 0b1100 will work. The two DC1Bx bits in CCP1CON are the 2 Least Significant bits of the 10-bit PWM duty cycle value. The 8 Most Significant of the 10 bits are written to CCPR1L.
PICkit™ 3 Debug Express Lessons FIGURE 3-61: CCPXCON: CCPX CONTROL REGISTER For more information on Timer2 see Section 13.0 “Timer2 Module” of the PIC18F45K20 Data Sheet (DS41303). More info on the CCP module PWM functionality can be found in Section 15.0 “Capture/Compare/PWM (CCP) Module”, and Section 15.4 “PWM Mode”. 3.12.3 Exploring the Lesson 12 Source Code The PWM signal from the CCP1 module is normally output on the CCP1/RC2 pin. However, this pin is not connected to any demo board LEDs.
PICkit™ 3 Debug Express Finally, the CCP1 module is configured for PWM operation with a duty cycle of 50% as described previously in this lesson: CCPR1L = 0x7D; // The 8 most significant bits of the period are 0x7D CCP1CON = 0b01001100; // The 2 LSbs are 0b00, and CCP1Mx = 110 for PWM At this point in the program in the module running, generating and outputting a PWM signal on RD7/P1D with 50% duty cycle at 62.5 Hz. To make the LED get brighter and then dimmer, we have a loop that changes the duty cycle.
© 2009 Microchip Technology Inc. 1 2 P2 PWR +V Rf 1MΩ RA7 C5 22pF X1 10 MHz C4 22pF CEXT 0.1μF C2 0.
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://support.microchip.com Web Address: www.microchip.