PICDEM™ Lab Development Board User’s Guide © 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.
PICDEM™LAB DEVELOPMENT BOARD USER’S GUIDE Table of Contents Chapter 1. Overview 1.1 Introduction ..................................................................................................... 5 1.2 Highlights ........................................................................................................ 5 1.3 PICDEM™ Lab Development Kit Contents .................................................... 5 1.4 PICDEM™ Lab Development Board Construction and Layout ...................... 6 1.
PICDEM™Lab Development Board User’s Guide Chapter 4. Comparator Peripheral Labs 4.1 Introduction ................................................................................................... 61 4.2 Comparator Labs .......................................................................................... 61 4.2.1 Reference Documentation .........................................................................61 4.2.2 Comparator Labs ......................................................................
PICDEMTM LAB DEVELOPMENT BOARD 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.
PICDEMTM Lab Development Board 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 RECOMMENDED READING This user’s guide describes how to use the PICDEM™ Lab Development Kit. Other useful documents are listed below. The following Microchip documents are available and recommended as supplemental reference resources. Readme Files For the latest information on using other tools, read the tool-specific Readme files in the Readmes subdirectory of the MPLAB® IDE installation directory.
PICDEMTM Lab Development Board User’s Guide 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.
PICDEMTM LAB DEVELOPMENT BOARD USER’S GUIDE Chapter 1. Overview 1.1 INTRODUCTION The PICDEM Lab Development Board supports Microchip’s 8, 14, 18 and 20-pin 8-bit MCUs including accommodation for PIC10F products in the 8-pin PDIP package. Dual-row expansion headers on either side of each socket provide connectivity to all pins on the connected PIC® MCU.
PICDEMTM Lab Development Board User’s Guide • (10) 1” Jumper Wires • (4) IRFD9020 P-CH MOSFETs • (4) IRFD010 N-CH MOSFETs • (2) 100kΩ Potentiometers 3. PICkit™ 2 Programmer/Debugger with USB Cable 4.
Overview 3. 32 kHz Crystal Oscillator (Y1) and Connection Header (J7) 4. VDD Connect/Disconnect Jumpers (J3, J4, J5) a) J3 jumper connects/disconnects VDD1 supply to PIC16F690 MCU in socket U2 b) J4 jumper connects/disconnects VDD2 supply to PIC16F819 MCU in socket U3 c) J5 jumper connects/disconnects VDD3 supply to PIC10F206 MCU in socket U5 5. Battery Clip Connection (BT1) for 9V Battery and Jumpers (J14) J14 jumpers connect/disconnect battery terminals for use in future lab on battery chargers. 6.
PICDEMTM Lab Development Board User’s Guide 2. Using a 9V battery connected to connector BT1 Ensure that connect/disconnect jumpers J14 are in place. 3. A PICkit™ 2 Programmer/Debugger connected to any one of the three PICkit™ Programmer/Debugger connectors J13, J12 and J6 (recommended for low-power applications only). Note: When using the PICkit™ 2 Programmer/Debugger as the power source, the variable VDD potentiometer (R1) will not vary the supply voltage.
Overview FIGURE 1-2: CONNECTING THE PICkit™ PROGRAMMER/DEBUGGER TO AN ICSP™ CONNECTOR r we e t o P rg Ta sy Bu PICkit™ Programmer/Debugger Denotes Pin 1 ICSP™ CONNECTOR The PICkit™ 2 Programmer/Debugger is then connected to an available USB port on the PC using the included USB cable. 1.7 SOLDERLESS PROTOTYPING AREA STRIP CONFIGURATION The solderless prototyping area contains a variety of strips under the perforated plastic block.
PICDEMTM Lab Development Board User’s Guide NOTES: DS41369A-page 10 © 2009 Microchip Technology Inc.
PICDEMTM LAB DEVELOPMENT BOARD USER’S GUIDE Chapter 2. Getting Started 2.1 INTRODUCTION This chapter is intended to prepare the reader to complete the labs in the remaining chapters of this user’s guide. 2.2 PREREQUISITES The labs contained within this lab manual assumes the user: 1. Has a basic understanding of the C programming language. 2. Understands basic circuit analysis. 3.
PICDEMTM Lab Development Board User’s Guide 1. Initalize(): - Initializes the microcontroller, the peripherals used in the application and any global variables used by multiple functions. 2. Get_Inputs(): - Obtains any input information either on-chip (from internal registers, etc...) or off-chip (pin voltage levels). 3. Decide(): - Makes decisions based on the input information gathered in the previous function to manipulate global variables. 4.
Getting Started 1. Using a PC that is connected to the internet, navigate to the MPLAB® IDE download page at the following url: www.microchip.com/mplab This page outlines the MPLAB IDE and also features downloadable plug-ins, User’s Guides and other useful information. 2. Scroll down to the Downloads section of the page and select the latest full release zip file for MPLAB IDE vx.xx. (See Figure 2-2.) FIGURE 2-2: MPLAB ZIP FILE 3. When prompted open the .
PICDEMTM Lab Development Board User’s Guide FIGURE 2-3: HI TECH INSTALLER WINDOW 12. In the next window, accept the terms of the license agreement and click Next to continue. (See Figure 2-4.) FIGURE 2-4: HI TECH LICENSE AGREEMENT 13. In the next window, select any components in addition to the HI-TECH C® Pro for the PIC10/12/16 MCU Family Compiler to install (additional components are not required to complete the labs in this user's guide) and click Next to continue. (See Figure 2-5.
Getting Started FIGURE 2-5: HI TECH COMPONENTS 14. In the next window, choose the language of preference, select the Add to environment path radio button and click Next to continue. (See Figure 2-6.) FIGURE 2-6: HI TECH LANGUAGE PREFERENCES The installation process will now begin. 15. Once the installation is complete, a confirmation window will open. Select or de-select the Read quick start guide now? radio button and click Finish to proceed. (See Figure 2-7.) © 2009 Microchip Technology Inc.
PICDEMTM Lab Development Board User’s Guide FIGURE 2-7: HI TECH INSTALL CONFIRMATION 16. The MPLAB® Tools Install Shield Wizard Complete window should soon open and prompt the user to restart the computer before using the software. To begin using the tools, select the Yes, I want to restart my computer radio button and click Finish to end the installation process and restart the computer. Following restart, the user will be given the option to view a variety of documentation.
PICDEMTM LAB DEVELOPMENT BOARD USER’S GUIDE Chapter 3. General Purpose Input/Output Labs 3.1 INTRODUCTION The following labs cover some of the fundamental features of the General Purpose Input/Output (GPIO) peripherals available on the PIC16F690. As the name implies, these peripherals are used for general purpose applications that can monitor and control other off-chip devices. Some PIC® microcontrollers have multiple GPIO peripherals on-chip including the PIC16F690 used in the following labs.
PICDEMTM Lab Development Board User’s Guide 3.3 GPIO OUTPUT LABS 3.3.1 Reference Documentation All documentation is available on the PICDEM™ Lab Development Kit accompanying CD-ROM. • PIC16F690 Data Sheet (DS41262) - Section 2.2.2.2: Option Register - Section 2.2.2.
General Purpose Input/Output Labs Special care should be observed when connecting the LED jumper wires to the expansion headers surrounding the PIC16F690, as the PORTC pins are not in sequential order. The 470Ω resistors are used to limit the current across the LEDs to manufacturer specifications. Furthermore, the PIC16F690 Data Sheet electrical specifications (see Section 17.0) specify that each port pin should not source/sink more than 25 mA.
PICDEMTM Lab Development Board User’s Guide Figure 3-2 shows the software control loop that will be implemented in this lab. At device power-up, the first functional block called from the main() is Initialize().
General Purpose Input/Output Labs FIGURE 3-4: STEP TWO 6. In the Step Three: window, use the Browse button and navigate to a new folder on the C:\ drive to store this project. Alternately, the reader may wish to use the C:\PICDEM_Lab\GPIO_Labs\GPIO_Lab1 folder created earlier. (See Figure 3-5.) FIGURE 3-5: STEP THREE 7. In the Step Four: files are added to the project. A Lab_Template.c file has been provided in the C:\PICDEM_Lab\Lab_Template.
PICDEMTM Lab Development Board User’s Guide FIGURE 3-6: STEP FOUR 8. Finally, the Summary window should appear showing the selected device, the tool suite and the new project file name. Click Finish to exit the Project Wizard. FIGURE 3-7: SUMMARY 9. The MPLAB® IDE Workspace should now be open. If the Project window is not visible, it can be opened by selecting View>Project. The Project window should resemble Figure 3-8. DS41369A-page 22 © 2009 Microchip Technology Inc.
General Purpose Input/Output Labs FIGURE 3-8: PROJECT WINDOW 10. Double click on the GPIO_Lab1.c source file in the Project window to open. 11.
PICDEMTM Lab Development Board User’s Guide 12. Copy/paste the code in Example 3-2 into the Do_Outputs() section labeled: //ADD DO_OUTPUTS CODE HERE EXAMPLE 3-2: RC0 RC1 RC2 RC3 RC4 RC5 RC6 RC7 = = = = = = = = DO_OUTPUT() CODE FOR LAB 1 1;//Make 0;//Make 1;//Make 1;//Make 0;//Make 1;//Make 0;//Make 1;//Make RC0 RC1 RC2 RC3 RC4 RC5 RC6 RC7 (pin (pin (pin (pin (pin (pin (pin (pin 16) HIGH (approx. Vdd) 15) LOW (approx. Vss) 14) HIGH (approx. Vdd) 7) HIGH (approx. Vdd) 6) LOW (approx.
General Purpose Input/Output Labs FIGURE 3-9: PICkit 2 PROGRAMMER/DEBUGGER TOOLBAR 1 2 3 4 5 6 7 8 9 Legend: 1. 2. 3. 4. 5. 6. 7. 8. 9. Program the target device Read target device memories Read the target EEDATA memory Verify the contents of the target device Erase the target device memories Verify that target memories are erased Bring target MCLR to VDD Bring target MCLR to VIL Re-establish PICkit 2 connection 8. Press button1 shown in Figure 3-9 to program the PIC16F690. 3.3.4.
PICDEMTM Lab Development Board User’s Guide FIGURE 3-11: MAIN() SOFTWARE CONTROL LOOP FLOWCHART FOR LAB 2 main() Initialize() Do_Outputs() Loop Forever Timing() The Timing() delay routine is shown in Figure 3-12. FIGURE 3-12: TIMING() DELAY ROUTINE FLOWCHART FOR LAB 2 TIMING() Create two 8-bit variables: • delay_var1 = 45571 • delay_var2 = 3 YES delay_var2 - 1 = 0 ? END NO delay_var1 = 45571 NO YES delay_var1 - 1 = 0 ? Two variables are used delay_var1 and delay_var2.
General Purpose Input/Output Labs The Do_Outputs() changes somewhat from the previous lab by implementing the XOR operator to toggle the value in each PORTC bit location each time through the software loop. The XOR operator is implemented in code as follows: RCx ^= 1; This translates to: “Make RCx equal to the current value in RCx XOR’d with 1” When a value is XOR’d with itself, the result is ‘0’ (i.e., 1 XOR’d with 1 = 0, 0 XOR’d with 0 = 0).
PICDEMTM Lab Development Board User’s Guide EXAMPLE 3-5: RC0 RC1 RC2 RC3 RC4 RC5 RC6 RC7 ^= ^= ^= ^= ^= ^= ^= ^= DO_OUTPUT() CODE FOR LAB 2 1;//XOR 1;//XOR 1;//XOR 1;//XOR 1;//XOR 1;//XOR 1;//XOR 1;//XOR current current current current current current current current RC0 RC1 RC2 RC3 RC4 RC5 RC6 RC7 value value value value value value value value with with with with with with with with 1 1 1 1 1 1 1 1 3.
General Purpose Input/Output Labs 3.3.6 Lab 3: Simple Delays Using Timer0 3.3.6.1 NEW REGISTERS USED IN THIS LAB To configure the peripherals used in this lab, the following registers are used: 1. Timer0 Module Register: TMR0 - Holds a count value of the number of selected edge transition of a clock source. 2. OPTION Register: OPTION_REG (Register 5-1 in Section 5 of the PIC16F690 Data Sheet). - Selects clock source used to increment TMR0 result register.
PICDEMTM Lab Development Board User’s Guide Finally, TMR0 is a writable register. Meaning that a value can be added to the register to offset the number of counts it takes for the overflow to occur. Equation 3-3 demonstrates how to calculate the value to preload the TMR0 register with to create a 10mS overflow period.
General Purpose Input/Output Labs EQUATION 3-4: MAXIMUM TMR0 OVERFLOW PERIOD TMR0 Overflow Period = (4/FOSC) x 256 x prescaler using a maximum prescaler setting of 1:256 and the 4MHz internal oscillator TMR0 Overflow Period = 1 μS x 256 x 256 = 65.5mS Therefore, to implement delays greater than 65.5mS, a counter variable is implemented as shown in the flowchart of Figure 3-14 for a 1 second delay.
PICDEMTM Lab Development Board User’s Guide The Initialize() now configures the PIC16F690 peripherals as follows: • PORTC - Set all bits in PORTC high - Make all PORTC pins digital output • Timer0 - Use the internal instruction clock (FOSC/4) as the TMR0 register clock source - Increment TMR0 register on low-to-high transition of FOSC/4 - Assign the prescaler to Timer0 and configure to increment on every 256th transition of FOSC/4 3.3.6.
General Purpose Input/Output Labs EXAMPLE 3-9: INITIALIZE() CODE FOR LAB 3 //Set all PORTC bits HIGH (to a known state) PORTC = 0b11111111; //Configure PORTC's ANALOG/DIGITAL pins as all Digital ANS4 = 0;//Associated with RC0 ANS5 = 0;//Associated with RC1 ANS6 = 0;//Associated with RC2 ANS7 = 0;//Associated with RC3 ANS8 = 0;//Associated with RC6 ANS9 = 0;//Associated with RC7 //Configure PORTC //i.e.
PICDEMTM Lab Development Board User’s Guide 3.3.7 Lab 4: Rotate LEDs 3.3.7.1 OVERVIEW This lab shifts a high bit in the PORTC register from right-to-left each time through the software loop sequentially lighting the LEDs connected to the PORTC pins in 1 second intervals as dictated by the Timing(). This lab adds some new functional blocks to the main() software control loop as shown in Figure 3-15.
General Purpose Input/Output Labs The Decide() first checks the current value in LED_Output for two specific conditions: • Is the Most Significant bit ‘1’? This means that on the next shift, the contents will be all ‘0’s. • Is the value currently a ‘0’? If either condition exists, the function re-initializes LED_Output to set the Least Significant bit. Otherwise, there will be a period when none of the LEDs are lit.
PICDEMTM Lab Development Board User’s Guide 3.3.7.2 PROCEDURE Using the code developed in the previous lab, make the following changes: 1. The LED_Output variable will need to be declared before it can be used. Copy/paste the code in Example 3-11 to the beginning of the source file under the section marked: //-----------------DATA MEMORY-----------------------EXAMPLE 3-11: LED_OUTPUT VARIABLE DECLARATION FOR LAB 4 unsigned char LED_Output;//Variable used to set/clear PORTC bits 2.
General Purpose Input/Output Labs Changes from the previous lab include PORTC initialization so that all bits are ‘0’ except for the Least Significant bit (LSb) and the initialization of the LED_Output variable. 3. Copy/paste the code in Example 3-13 into the Decide() section labeled: //ADD DECISION CODE HERE EXAMPLE 3-13: DECIDE()CODE FOR LAB 4 //First check if LED_Output variable has most significant bit //set to 1 or if LED_Output variable is all 0's.
PICDEMTM Lab Development Board User’s Guide 3.3.7.3 TESTING THE APPLICATION Program the PIC16F690. The LEDs connected to the individual PORTC pins should now all flash on/off sequentially from right-to-left in 1 second intervals. The solution for this project is located in the C:\PICDEM_Lab\GPIO_Labs\GPIO_Lab4\solution directory. 3.4 GPIO INPUT LABS 3.4.1 Reference Documentation PIC16F690 Data Sheet • Section 2: Memory Organization • Section 4: I/O Ports • Section 5: Timer0 Module 3.4.
General Purpose Input/Output Labs FIGURE 3-18: PICDEM LAB SCHEMATIC FOR GPIO INPUT LABS VDD J8 U2 1 2 3 4 5 6 7 8 9 10 RC5 RC4 RC3 RC6 RC7 R9 10KΩ J9 RA2 RC0 RC1 RC2 20 19 18 17 16 15 14 13 12 11 SW1 VSS R8 470Ω R7 470Ω R6 470Ω R5 470Ω R4 470Ω R3 470Ω R2 470Ω R1 470Ω LED8 LED7 LED6 LED5 LED4 LED3 LED2 LED1 VSS The only change from the previous section is the inclusion of a push button connected to RA2 with associated pull-up resistor. 3.4.4 Lab 5: Adding a Push Button 3.4.4.
PICDEMTM Lab Development Board User’s Guide of uncorrected switch bounce can range from being just annoying to catastrophic. The classic solution involves filtering, such as through a resistor-capacitor circuit, or through resettable shift registers. These methods are still effective but they involve additional cost in material, installation and board real estate. Debouncing in software eliminates these additional costs.
General Purpose Input/Output Labs A new function called Get_Inputs() is used to check the RA2 pin voltage. Referring to Figure 3-12, the RA2 pin connected to the push button (SW1) is pulled to VDD using a 10 KΩ resistor. This pull-up resistor eliminates noise on the pin that could trigger “false” push button presses. The second terminal of the push button is connected to VSS.
PICDEMTM Lab Development Board User’s Guide FIGURE 3-21: DELAY_5MS() SOFTWARE FLOWCHART FOR LAB 5 Timer0 configured in the Initialize() as follows: • Using the internal FOSC/4 (4MHz/4) clock source • TMR0 increments on low-to-high transition of FOSC/4 • Prescaler configured for 1:32 Delay_5mS() Clear the Timer0 overflow flag (T0IF) Preload the Timer0 result register (TMR0) with 100 NO T0IF = 1 (i.e.
General Purpose Input/Output Labs FIGURE 3-22: DECIDE() SOFTWARE FLOWCHART FOR LAB 5 Decide() YES direction = 0 ? LED_Output = 0b10000000 or LED_Output = 0b00000000 ? YES NO LED_Output = 0b00000001 or LED_Output = 0b00000000 ? NO YES NO LED_Output = 0b10000000 LED_Output = 0b00000001 Shift contents of LED_Output left by 1 bit position Shift contents of LED_Output right by 1 bit position END Similar to the previous lab, Decide() checks the LED_Output variable value before executing the shift to
PICDEMTM Lab Development Board User’s Guide 1.
General Purpose Input/Output Labs EXAMPLE 3-18: INITIALIZE() FOR GPIO LAB 5 //Clear PORTC to a known state //Set the least significant bit to 1 so that it can be //shifted through PORTC = 0b00000001; //Clear the PORTA register to a known state PORTA = 0b00000000; //Configure PORTC's ANALOG/DIGITAL pins as all Digital ANS4 = 0;//Associated with RC0 ANS5 = 0;//Associated with RC1 ANS6 = 0;//Associated with RC2 ANS7 = 0;//Associated with RC3 ANS8 = 0;//Associated with RC6 ANS9 = 0;//Associated with RC7 //Co
PICDEMTM Lab Development Board User’s Guide 3. Copy/paste the code in Example 3-19 into the Get_Inputs() at the section labeled: //ADD GET_INPUTS CODE HERE EXAMPLE 3-19: GET_INPUTS() CODE FOR GPIO LAB 5 //Check for a push button press (i.e. RA2 = 0) if (RA2 == 0) { Delay_5mS(); //Delay to debounce //Check if push button is still pressed if(RA2 == 0) direction ^= 1; //If so, toggle the //direction bit } //Otherwise keep direction the same as it was else direction = direction; 4.
General Purpose Input/Output Labs 5. The Do_Outputs() code from the previous lab stays the same. 6. Copy/paste the code in Example 3-21 into the Timing() over the code from the previous lab. EXAMPLE 3-21: TIMING() CODE FOR GPIO LAB 5 unsigned int delay_var = 9997; //Keep looping until the delay_var is // equal to zero (should take 10mS) while(--delay_var); Note: This lab now utilizes a 10 mS delay to time the software control loop. 7.
PICDEMTM Lab Development Board User’s Guide 3.4.5 Lab 6: Push Button Interrupt 3.4.5.1 NEW REGISTERS USED IN THIS LAB To configure the peripherals used in this lab, the following registers are used: 1. OPTION Register: OPTION (Register 2-2 in Section 2 of the PIC16F690 Data Sheet) - Selects the edge transition on RA2/INT that will trigger an interrupt. 2. Interrupt Control Register: INTCON (Register 2-3 in Section 2 of the PIC16F690 Data Sheet) - Enable interrupt functionality on the PIC16F690.
General Purpose Input/Output Labs FIGURE 3-23: MAIN() SOFTWARE CONTROL LOOP FLOWCHART FOR GPIO LAB 6 main() Global Variables: • 8-bit variable LED_Output will be used to light the LEDs connected to PORTC • 1-bit variable direction used to determine the direction of the sequential LED flashing 0 = shift PORTC bits right-to-left 1 = shift PORTC bits left-to-right Initialize() Decide() Loop Forever Do_Outputs() Timing() Note the removal of the Get_Inputs() from the previous lab.
PICDEMTM Lab Development Board User’s Guide Referring to the flowchart in Figure 3-24, the PB_PressISR() replaces the Get_Inputs() used in Lab 4 with a few additions. At the beginning of the ISR, the INTE and INTF bits are first checked to ensure that the RA2/INT external interrupt is indeed enabled and that the flag is set indicating that an RA2/INT external interrupt has occurred.
General Purpose Input/Output Labs Note: To indicate a function that should be used whenever an interrupt occurs, the interrupt function qualifier is needed. This qualifier is specific to the HI-TECH C® Compiler. 2. Copy/paste the code in Example 3-24 into the Initialize() over the code from the previous lab. © 2009 Microchip Technology Inc.
PICDEMTM Lab Development Board User’s Guide EXAMPLE 3-24: INITIALIZE() CODE FOR GPIO LAB 6 //Clear PORTC to a known state //Set the least significant bit to 1 so that it can be //shifted through PORTC = 0b00000001; //Clear the PORTA register to a known state PORTA = 0b00000000; //Configure PORTC's ANALOG/DIGITAL pins as all Digital ANS4 = 0;//Associated with RC0 ANS5 = 0;//Associated with RC1 ANS6 = 0;//Associated with RC2 ANS7 = 0;//Associated with RC3 ANS8 = 0;//Associated with RC6 ANS9 = 0;//Associated
General Purpose Input/Output Labs 3. Copy/paste the code in Example 3-25 into the main() over the code from the previous lab to remove the Get_Inputs(). EXAMPLE 3-25: MAIN() CODE FOR GPIO LAB 6 Initialize(); //Initialize the relevant registers while(1) { Decide();//Make any decisions Do_Outputs(); //Perform any outputs Timing();//Sets execution rate of the //Software Control Loop } 4. The remaining code remains unchanged from the previous lab. Compile the project. There should be no errors. 3.4.5.
PICDEMTM Lab Development Board User’s Guide The INTCON register is needed as well to implement the interrupt-on-change feature. Again the GIE bit needs to be set to enable any interrupts used on the microcontroller. To configure for an interrupt-on-change, the PORTA/PORTB Change Interrupt Enable bit (RABIE) must be set along with the individual enable bits in the IOCA register. On an interrupt-on-change for configured pins, the PORTA/PORTB Change Interrupt Flag bit (RABIF) will be set.
General Purpose Input/Output Labs The PB_PressISR() now checks to see if the voltage level on RA2 has changed state from a 1-to-0 or from 0-to-1. Each condition will change the direction that the LEDs flash sequentially. Note that the direction bit toggle used in previous labs has been replaced by assigning either a ‘1’ for the left-to-right direction or ‘0’ for the right-to-left direction.
PICDEMTM Lab Development Board User’s Guide EXAMPLE 3-26: PB_PRESSISR() CODE FOR GPIO LAB 7 //First, check if the interrupt occurred as a result of an //RA2 change interrupt. If so, clear the RABIF flag so //that subsequent interrupts can occur if(RABIE && RABIF) RABIF = 0; //Check the push button connected to RA0 pin.
General Purpose Input/Output Labs EXAMPLE 3-27: INITIALIZE() CODE FOR GPIO LAB 7 //Clear PORTC to a known state //Set the least significant bit to 1 so that it can be //shifted through PORTC = 0b00000001; //Clear the PORTA register to a known state PORTA = 0b00000000; //Configure PORTC's ANALOG/DIGITAL pins as all Digital ANS4 = 0;//Associated with RC0 ANS5 = 0;//Associated with RC1 ANS6 = 0;//Associated with RC2 ANS7 = 0;//Associated with RC3 ANS8 = 0;//Associated with RC6 ANS9 = 0;//Associated with RC7
PICDEMTM Lab Development Board User’s Guide 3. All remaining code from the previous lab is unchanged. Compile the project. There should be no errors. 3.4.6.4 TESTING THE APPLICATION Program the PIC16F690. The LEDs connected to PORTC should now flash sequentially from left-to-right when the push button is released and flash from right-to-left when the push button is pressed. The solution for this project is located in the C:\PICDEM_Lab\GPIO_Labs\GPIO_Lab7\solution directory. 3.4.
General Purpose Input/Output Labs EXAMPLE 3-28: INITIALIZE() CODE FOR GPIO LAB 8 //Clear PORTC to a known state //Set the least significant bit to 1 so that it can be //shifted through PORTC = 0b00000001; //Clear the PORTA register to a known state PORTA = 0b00000000; //Configure PORTC's ANALOG/DIGITAL pins as all Digital ANS4 = 0;//Associated with RC0 ANS5 = 0;//Associated with RC1 ANS6 = 0;//Associated with RC2 ANS7 = 0;//Associated with RC3 ANS8 = 0;//Associated with RC6 ANS9 = 0;//Associated with RC7
PICDEMTM Lab Development Board User’s Guide 3.4.7.4 TESTING THE APPLICATION Program the PIC16F690. The application should operate exactly as it did in the previous lab. Only this time with the absence of the 10 KΩ pull-up resistor. The solution for this project is located in the C:\PICDEM_Lab\GPIO_Labs\GPIO_Lab8\solution directory. DS41369A-page 60 © 2009 Microchip Technology Inc.
PICDEMTM LAB DEVELOPMENT BOARD USER’S GUIDE Chapter 4. Comparator Peripheral Labs 4.1 INTRODUCTION The following labs cover some of the fundamental features of the Comparator 1 peripheral found on the PIC16F690 including some unique applications. These peripherals are very useful mixed signal building blocks as they provide analog functionality independent of program execution.
PICDEMTM Lab Development Board User’s Guide 4.2.4 Lab 1: Simple Compare 4.2.4.1 NEW REGISTERS USED IN THIS LAB To configure the peripherals used in this lab, the following registers are used: 1. Comparator C1 Control Register 0: CM1CON0 (Register 8-1 in Section 8 of the PIC16F690 Data Sheet) - Enables Comparator C1. - Configures Comparator output polarity. - Enables the Comparator result to be available internal only or on the C1OUT pin (pin 17).
Comparator Peripheral Labs FIGURE 4-2: MAIN() SOFTWARE CONTROL LOOP FLOWCHART FOR COMPARATOR LAB 1 main() Initialize() Loop Forever Wait As mentioned in the introduction, comparators on the PIC16F690 are able to function independent of software logic. Therefore, all that is needed is to call the Initialize() from main() to activate the peripheral.
PICDEMTM Lab Development Board User’s Guide 2. Copy/paste the code in Example 4-2 into the main() section labeled: //ADD MAIN CODE HERE EXAMPLE 4-2: MAIN() CODE FOR COMPARATOR LAB 1 Initialize(); //Initialize the relevant registers while(1); Note: The inclusion of the while loop forces the microcontroller to sit and wait at a “known” instruction. At start-up, the contents of the program memory are unknown.
Comparator Peripheral Labs • Fixed 0.6 reference option • Output clamped to VSS The CVREF has 2 ranges with 16 voltage levels in each range. Range selection is controlled by the CVREF Range Selection (VRR) bit in the VRCON (Voltage Reference Control Register) along with the CVREF Value Selection bits (VR<3:0>). The Value Selection bits hold a value based upon some simple calculations to set the internal reference voltage.
PICDEMTM Lab Development Board User’s Guide FIGURE 4-3: SCHEMATIC FOR COMPARATOR LAB 2 VDD J8 U2 1 2 3 4 5 6 7 8 9 10 C12IN0C1OUT J9 20 19 18 17 16 15 14 13 12 11 R3 100KΩ R4 470Ω VSS LED1 VSS 4.2.5.3 PROCEDURE Using the firmware developed in the previous lab, make the following changes: 1. Copy/paste the code in Example 4-3 into the Initialize() over the code from the previous lab. DS41369A-page 66 © 2009 Microchip Technology Inc.
Comparator Peripheral Labs EXAMPLE 4-3: INITIALIZE CODE FOR COMPARATOR LAB 2 //Intialize Comparator 1 as follows: //Turn comparator 1 on C1ON = 1; //Make the comparator output available on the //C1OUT pin C1OE = 1; //Select the internal voltage reference //as the non-inverting reference voltage C1R = 1; //Select the C12IN0- pin as the inverting reference C1CH0 = 0; C1CH1 = 0; //Initialize the internal voltage reference as follows: //Turn on the CVref output and route to the C1Vref input //of comparator 1
PICDEMTM Lab Development Board User’s Guide 4.2.6 Lab 3: Higher Resolution Sensor Readings Using a Single Comparator 4.2.6.1 NEW REGISTERS USED IN THIS LAB To configure the peripherals used in this lab, the following registers are used: 1. Timer1 Control Register: T1CON (Register 6-1 in Section 6 of the PIC16F690 data sheet) - This register is used to control Timer1 and select various features of the module. In this lab the register will be used to enable Timer1 and select the clock source. 4.2.6.
Comparator Peripheral Labs reference, the cycle repeats and the system oscillates. The frequency of this oscillation is dependant on the RC time constant (τ = R x C), or the time it takes to discharge the capacitor to 37% of its initial voltage. As either the resistance or capacitance decreases, so will τ effectively increasing the frequency of the oscillator.
PICDEMTM Lab Development Board User’s Guide The software flowchart for this lab is shown in Figure 4-6. FIGURE 4-6: MAIN() SOFTWARE CONTROL LOOP FLOWCHART FOR COMPARATOR LAB 3 main() Initialize() Loop Forever Wait The Initialize() configures the PIC16F690 as follows: • Configure PORTC pins RC0, RC1, RC2 and RC3 and digital outputs • Comparator 1 - Enable Comparator 1. - Make the Comparator 1 output available on the C1OUT pin configuring TRISA2 as an output.
Comparator Peripheral Labs FIGURE 4-7: TMR0_ISR FLOWCHART FOR COMPARATOR LAB 3 TMR0_ISR() T0IF = 1 ? NO YES Clear T0IF Flag Turn off Timer1 Keep PORTC the same END Assign PORTC the T1MRH value shifted 4 bits to the right Clear the Timer1 result register pair TMR1H:TMR1L Turn on Timer1 The TMR0_ISR() first checks if a Timer0 interrupt has occurred (good programming practice).
PICDEMTM Lab Development Board User’s Guide EXAMPLE 4-4: TMR0_ISR CODE FOR LAB 3 void interrupt TMR0_ISR(void) { //Check if Timer0 interrupt has occurred if(T0IE&&T0IF) { //if so, clear the interrupt flag T0IF = 0; //Turn off Timer1 (stop counting) TMR1ON = 0; //Assign the upper 4-bits of the 16-bit //result to PORTC to light the LEDs connected //RC0,R1,RC2 and RC3 PORTC = TMR1H>>4; TMR0 = 10; //Clear the Timer1 register pair TMR1L = 0; TMR1H = 0; //Turn Timer1 back to start counting again TMR1ON = 1; } e
Comparator Peripheral Labs EXAMPLE 4-5: INITIALIZE CODE FOR COMPARATOR LAB 3 //Configure RC0,RC1,RC2 and RC3 as digital outputs ANSEL = 0b00001111; PORTC = 0; TRISC0 = 0; TRISC1 = 0; TRISC2 = 0; TRISC3 = 0; //Initialize PORTA pin connected to C12IN0TRISA1 = 1; //Make C1OUT pin an output TRISA2 = 0; //Configure the Comparator 1 as follows: //Turn on comparator 1 C1ON = 1; //Make C1OUT available externally C1OE = 1; //Connect the non-inverting reference to CVREF C1R = 1; //Connect the inverting reference to
PICDEMTM Lab Development Board User’s Guide 3. Copy/paste the code in Example 4-6 into the main() over the code from the previous lab: EXAMPLE 4-6: MAIN() CODE FOR COMPARATOR LAB 3 Initialize(); //Initialize the relevant registers while(1); 4. Compile the project. There should be no errors. 4.2.6.4 TESTING THE APPLICATION Program the PIC16F690. Adjust the R1 potentiometer until the LEDs begin to light displaying a binary value.
PICDEMTM LAB DEVELOPMENT BOARD USER’S GUIDE Chapter 5. Analog-to-Digital Converter Peripheral Labs 5.1 INTRODUCTION The Analog-to-Digital Converter (ADC) peripheral allows conversion of an analog input signal to a 10-bit binary value representing that signal so that it can be used in firmware. The following labs cover some of the fundamental features of the Analog-to-Digital Converter (ADC) peripheral found on the PIC16F690 including some unique applications. 5.
PICDEMTM Lab Development Board User’s Guide 5.2.3 Lab 1: Simple ADC 5.2.3.1 NEW REGISTERS USED IN THIS LAB To configure the peripherals used in this lab, the following registers are used: 1. ADC Control Register 0: ADCON0 (Register 9-1 in Section 9 of the PIC16F690 Data Sheet) - Configures ADC conversion result justification. - Select ADC reference voltage. - Selects ADC input channel (i.e., pin with analog voltage to be converted). - Starts ADC conversion and determines when ADC conversion is complete.
Analog-to-Digital Converter Peripheral Labs FIGURE 5-1: SCHEMATIC FOR ADC LAB 1 U2 J8 1 2 3 4 5 6 7 8 9 10 RC0 RC1 RC3 RC2 RB4/AN10 J9 VDD 20 19 18 17 16 15 14 13 12 11 R5 100KΩ VSS R4 470Ω LED4 R3 470Ω LED3 VSS R2 470Ω LED2 VSS R1 470Ω LED1 VSS VSS The voltage reference is software selectable as either VDD or an external voltage applied to the external reference pin 18 (VREF). To minimize circuit complexity, this application makes use of VDD as the reference.
PICDEMTM Lab Development Board User’s Guide The Initialize() configures the peripherals as follows: • Ports - Configure pin 13 as an analog input (using TRISB4). - Clear the PORTC register. - Configure RC0, RC1, RC2 and RC3 pins as digital output. • ADC - Select ADC conversion clock FRC. - Configure voltage reference using VDD. - Select channel 10 as the ADC input channel (Pin 13: RB4/AN10). - Select result format left justified (10-bit result in ADRESH<7:0> and ADRESL<7:6>). - Turn on ADC module.
Analog-to-Digital Converter Peripheral Labs FIGURE 5-3: MAIN() SOFTWARE CONTROL LOOP FLOWCHART FOR COMPARATOR LAB 1 Get_Inputs() Gives ADC capacitor time to charge Delay_1mS() Start conversion by setting GO/DONE bit YES Wait for ADC to complete GO/DONE = 1 ? NO END Following the Get_Inputs(), the 10-bit ADC result is now in the ADRESH:ADRESL registers. The Decide() assigns the ADC result value, shifted four bit positions to the right, to the LED_Output variable.
PICDEMTM Lab Development Board User’s Guide EXAMPLE 5-2: DELAY_1MS() CODE FOR ADC LAB 1 /*--------------------------------------------------------Subroutine: Delay_1mS Parameters: none Returns:nothing Synopsys:Creates a 1mS delay when called ---------------------------------------------------------*/ void Delay_1mS(void) { unsigned int delay_var = 98; //Keep looping until the delay_var is // equal to zero (should take 1mS) while(--delay_var); } 3.
Analog-to-Digital Converter Peripheral Labs EXAMPLE 5-3: INITIALIZE CODE FOR COMPARATOR LAB 1 //Configure Port: //Disable pin output driver (See TRIS register) TRISB4 = 1; // Configure pin as analog ANS10 = 1; //Configure RC0, RC1, RC2 and RC3 as digital output PORTC = 0; TRISC0 = 0; TRISC1 = 0; TRISC2 = 0; TRISC3 = 0; ANS4 ANS5 ANS6 ANS7 = = = = 0; 0; 0; 0; //Configure the ADC module: //Select ADC conversion clock Frc ADCS0 = 1; ADCS1 = 1; ADCS2 = 1; //Configure voltage reference using VDD VCFG = 0; /
PICDEMTM Lab Development Board User’s Guide EXAMPLE 5-4: GET_INPUTS() CODE FOR ADC LAB 1 //Perform an ADC of potentiometer connected to pin 13 //Wait the required acquisition time Delay_1mS(); //Start conversion by setting the GO/DONE bit. GODONE = 1; //Wait for ADC conversion to complete //Polling the GO/DONE bit // 0 = ADC completed // 1 = ADC in progress while(GODONE == 1); 5.
Analog-to-Digital Converter Peripheral Labs 5.2.3.4 TESTING THE APPLICATION Program the PIC16F690. Turning the potentiometer connected to pin 13 should light the LEDs sequentially in a binary fashion. Note that these are the 4 Most Significant bits of the ADC result. Adding 6 more LEDs and I/O pins would allow the complete 10-bit value to be displayed. To determine the significance of each bit in the ADRESH:ADRESL 10-bit result, see Figure 5-4.
PICDEMTM Lab Development Board User’s Guide Since this lab outputs the 4 Most Significant bits of the ADC result and VDD is used as the reference voltage, the LED display should correspond with the following voltage levels shown in Table 5-1. TABLE 5-1: CORRESPONDING VOLTAGE ON PIN 13 RELATED TO LIT LEDS (1 = LED ON, 0 = LED OFF) LED4 LED3 LED2 LED1 pin 13 Voltage 0 0 0 0 < 0.3125V 0 0 0 1 > 0.3125V 0 0 1 0 > 0.625V 0 0 1 1 > 0.9375V 0 1 0 0 > 1.25V 0 1 0 1 > 1.
Analog-to-Digital Converter Peripheral Labs 5.2.4 Lab 2: Audible Temperature Sensor 5.2.4.1 OVERVIEW In this lab, the ADC peripheral on the PIC16F690 is used to alter the frequency of Pulse-Width Modulated Waveform (PWM) in relation to the temperature sensed by a thermistor connected to the input of the ADC peripheral. The PWM waveform is generated by simply toggling the RC0 voltage level high and low.
PICDEMTM Lab Development Board User’s Guide The audible frequency range is between 20-20000Hz. Therefore, the Timing() delays from a maximum of 4.096 mS (244.1Hz) to a minimum of 1.536 mS (651Hz). These values are determined using the internal instruction clock (FOSC/4) as the TMR0 clock source with a prescaler of 1:16. Other values could easily be used as long as the frequency of the PWM remains within the audible range. The software flowchart for this lab is shown in Figure 5-6.
Analog-to-Digital Converter Peripheral Labs Next, the Get_Inputs() performs an ADC on the voltage present on pin 13 using the same code as Lab 1 with one minor change. This application is very dependant on the timing of the software control loop. The 1mS delay used in Lab 1 to allow the hold capacitor on the input of ADC to fully charge to the pin voltage is excessive.
PICDEMTM Lab Development Board User’s Guide EXAMPLE 5-9: INITIALIZE CODE FOR ADC LAB 2 //Configure Port: PORTB = 0; //Disable pin output driver (See TRIS register) TRISB4 = 1; // Configure RB4 as analog pin as analog ANS10 = 1; //Configure RC0, RC1, RC2 and RC3 as digital output PORTC = 0; TRISC0 = 0; ANS4 = 0; //Configure Timer0 as follows: //Select the FOSC/4 internal instruction clock //as the clock source for TMR0 T0CS = 0; //Increment TMR0 value on low-to-high transition //of the FOSC/4 T0SE = 0; //A
Analog-to-Digital Converter Peripheral Labs EXAMPLE 5-10: GET_INPUTS() CODE FOR ADC LAB 2 unsigned char counter = 2; //Give ADC hold capacitor time to charge //This works out to approximately 8uS while(--counter > 0); //Start conversion by setting the GO/DONE bit. GODONE = 1; //Wait for ADC conversion to complete //Polling the GO/DONE bit // 0 = ADC completed // 1 = ADC in progress while(GODONE == 1); 4.
PICDEMTM Lab Development Board User’s Guide 7. Copy/paste the code in Example 5-14 over the main() code from the previous lab: EXAMPLE 5-14: MAIN() CODE FOR ADC LAB 2 Initialize(); //Initialize the relevant registers while(1) { Get_Inputs(); Decide(); Do_Outputs(); Timing(); } 8. Compile the project. There should be no errors. 5.2.4.3 TESTING THE APPLICATION Program the PIC16F690. An audible tone should emit from the speaker.
© 2009 Microchip Technology Inc. C10 15pF XT1 XT1 J1 J17 Y1 J7 9 8 7 6 5 4 3 2 1 32 kHz 2 DJ005B 3 C11 15pF XT2 XT2 RA2 RB3 RB2 RB1 RB0 VPP2 RA4 RA3 1 9 8 7 6 5 4 3 2 2PHDR 9V BT1 RA2 RA0 RA1 RB3 RB2 RB1 RB0/INT Vss MCLR PIC_18P RB4 RB5 RB6 RB7 VDD OSC2/CLKOUT 10 11 12 13 14 15 16 17 18 SP+ SP- 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.