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. All of these methods, to our
knowledge, require using the Microchip products in a manner outside the operating specifications contained in Microchip’s Data
Sheets. Most likely, the person doing so is engaged in theft of intellectual property.
•Microchip is willing to work with the customer who is concerned about the integrity of their code.
•Neither Microchip nor any other semiconductor manufacturer can guarantee the security of their code. Code protection does not
mean that we are guaranteeing the product as “unbreakable.”
Code protection is constantly evolving. We at Microchip are committed to continuously improving the code protection features of our
products. Attempts to break Microchip’s code protection feature may be a violation of the Digital Millennium Copyright Act. If such acts
allow unauthorized access to your software or other copyrighted work, you may have a right to sue for relief under that Act.
Information contained in this publication regarding device
applications and the like is provided only for your convenience
and may be superseded by updates. It is your responsibility to
ensure that your application meets with your specifications.
MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED,
WRITTEN OR ORAL, STATUTORY OR OTHERWISE,
RELATED TO THE INFORMATION, INCLUDING BUT NOT
LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE,
MERCHANTABILITY OR FITNESS FOR PURPOSE.
Microchip disclaims all liability arising from this information and
its use. Use of Microchip’s products as critical components in
life support systems is not authorized except with express
written approval by Microchip. No licenses are conveyed,
implicitly or otherwise, under any Microchip intellectual property
rights.
Trademarks
The Microchip name and logo, the Microchip logo, Accuron,
dsPIC, K
EELOQ, microID, MPLAB, PIC, PICmicro, PICSTART,
PRO MATE, PowerSmart, rfPIC, and SmartShunt are
registered trademarks of Microchip Technology Incorporated
in the U.S.A. and other countries.
AmpLab, FilterLab, Migratable Memory, MXDEV, MXLAB,
PICMASTER, SEEVAL, SmartSensor and The Embedded
Control Solutions Company are registered trademarks of
Microchip Technology Incorporated in the U.S.A.
Analog-for-the-Digital Age, Application Maestro, dsPICDEM,
dsPICDEM.net, dsPICworks, ECAN, ECONOMONITOR,
FanSense, FlexROM, fuzzyLAB, In-Circuit Serial
Programming, ICSP, ICEPIC, Linear Active Thermistor,
MPASM, MPLIB, MPLINK, MPSIM, PICkit, PICDEM,
PICDEM.net, PICLAB, PICtail, PowerCal, PowerInfo,
PowerMate, PowerTool, rfLAB, rfPICDEM, Select Mode,
Smart Serial, SmartTel, Total Endurance and WiperLock are
trademarks of Microchip Technology Incorporated in the
U.S.A. and other countries.
SQTP is a service mark of Microchip Technology Incorporated
in the U.S.A.
All other trademarks mentioned herein are property of their
respective companies.
Microchip received ISO/TS-16949:2002 quality system certification for
its worldwide headquarters, design and wafer fabrication facilities in
Chandler and Tempe, Arizona and Mountain View, California in
October 2003. The Company’s quality system processes and
procedures are for its PICmicro
devices, Serial EEPROMs, microperipherals, nonvolatile memory and
analog products. In addition, Microchip’s quality system for the design
and manufacture of development systems is ISO 9001:2000 certified.
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. The numbering convention for the DS number is
“DSXXXXXA”, where “XXXXX” is the document number and “A” is the revision level of the
document.
INTRODUCTION
This chapter contains general information that will be useful to know before using the
MCP402X Digital Potentiometer Evaluation Board. Items discussed in this chapter
include:
• Document Layout
• Conventions Used in this Guide
• Recommended Reading
• The Microchip Web Site
• Customer Support
• Document Revision History
DOCUMENT LAYOUT
This document describes how to use the MCP402X Digital Potentiometer Evaluation
Board. The manual layout is as follows:
• Chapter 1. “Product Overview” – Important information about the MCP402X
Digital Potentiometer Evaluation Board.
• Chapter 2. “Installation and Operation” – Includes instructions on how to get
started with this evaluation board.
• Appendix A. “Schematic and Layouts” – Shows the schematic and layout
diagrams for the MCP402X Digital Potentiometer Evaluation Board.
• Appendix B. “Bill Of Materials (BOM)” – Lists the parts used to build the
MCP402X Digital Potentiometer Evaluation Board.
• Appendix C. “Using the BFMP Programmer to Power the Board” – Shows
how to interface the BFMP board with the MCP402X Digital Potentiometer Evaluation Board.
• Appendix D. “Potential Programming Issue of the MCP402XEV” – Discusses
possible programming issues with Rev 1 and Rev 2 of the MCP402X Digital
Potentiometer Evaluation Board.
• Appendix E. “00066_MCP402XEV.ASM Source Code” – Provides information
about the application firmware, as well as the location of the source code.
Choice of mutually exclusive
arguments; an OR selection
Represents code supplied by
user
MPLAB® IDE User’s Guide
“Save project before build”
File>Save
‘b00100, ‘b10
any valid filename
0xFFFF, 0x007A
[options]
errorlevel {0|1}
var_name...]
void main (void)
{ ...
}
only
compiler...
RECOMMENDED READING
This user's guide describes how to use the MCP402X Digital Potentiometer Evaluation
Board. The following Microchip documents are available and recommended as
supplemental reference resources.
“Low-Cost, 64-Step Volatile Digital POT in SOT-23”
Data Sheet
“Low-Cost NV Digital POT in SOT-23 with WiperLock™ Technology”
“6-Pin, 8-Bit Flash Microcontrollers”
Data Sheet (DS41239)
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.
Accessible by using your favorite Internet browser, the web site contains the following
information:
• Product Support – Data sheets and errata, application notes and sample
programs, design resources, user’s guides and hardware support documents,
latest software releases and archived software
• General Technical Support – Frequently Asked Questions (FAQs), technical
support requests, online discussion groups, Microchip consultant program
member listing
• Business of Microchip – Product selector and ordering guides, latest Microchip
press releases, listing of seminars and events, listings of Microchip sales offices,
distributors and factory representatives
CUSTOMER SUPPORT
Users of Microchip products can receive assistance through several channels:
• Distributor or Representative
• Local Sales Office
• Field Application Engineer (FAE)
• Technical Support
• Development Systems Information Line
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.
Technical support is available through the web site at: http://support.microchip.com
Preface
DOCUMENT REVISION HISTORY
Revision B (December 2005)
• Updated to include MCP401X devices
• Enhancement of Operation Description
• Added Appendix C. “Using the BFMP Programmer to Power the Board” -
Programming the MCP402X Digital Potentiometer Evaluation Board
• Added Appendix D. “Potential Programming Issue of the MCP402XEV”- A
description of potential programming issues (and their solutions) with the
MCP402X Digital Potentiometer Evaluation Board
This chapter provides an overview of the MCP402X Digital Potentiometer Evaluation
Board and covers the following topics:
• What is the MCP402X Digital Potentiometer Evaluation Board?
• What the MCP402X Digital Potentiometer Evaluation Board includes
1.2WHAT IS THE MCP402X DIGITAL POTENTIOMETER EVALUATION BOARD?
The MCP402X Digital Potentiometer Evaluation Board (MCP402XEV) allows the
system designer to quickly evaluate the operation of Microchip Technology’s
MCP401X/2X Digital Potentiometer products.
The MCP402X Digital Potentiometer Evaluation Board PCB was designed to support:
• 8-pin, 150 mil SOIC MCP4021-XXX/SN devices
• SOT-23-6 MCP4022-XXX/OT devices
• SOT-23-6 MCP4023-XXX/OT devices
• SOT-23-5 MCP4024-XXX/OT devices
• 8-pin, 150 mil SOIC MCP4011-XXX/SN devices
• SOT-23-6 MCP4012-XXX/OT devices
• SOT-23-6 MCP4013-XXX/OT devices
• SOT-23-5 MCP4014-XXX/OT devices
In addition to supporting the MCP401X/2X, the MCP402XEV also utilizes the SOT-23-6
PIC10F20X microcontroller.
The PIC10F20X is supplied with example firmware that debounces the INCR and
DECR push buttons and generates the simple U/D
MCP401X/2X to increment and decrement the potentiometer’s wiper. It also generates
the simple U/D
potentiometer’s wiper.
protocol required by the MCP402X to lock and unlock the
The MCP402X Digital Potentiometer Evaluation Board provides a tested,
out-of-the-box example of a MCP401X/2X application. The circuit description is
described in Section 2.2 “PCB Description”, while the test and operating instructions
are described in Section 2.3 “Test and Operating Instructions”. A supplied blank
PCB allows rapid prototyping of the designer’s specific MCP401X/2X device, along
with other desired passive components (resistor and capacitors) and connection posts.
2.2PCB DESCRIPTION
The MCP402X Digital Potentiometer Evaluation Board has the following features:
• 150 mil, 8-pin SOIC pinout (U1) supports the MCP4021 and MCP4011 devices
• SOT-23-6 pinout (U2) supports the MCP4022/23/24 and MCP4012/13/14 devices
• SOT-23-6 pinout (U3) supports the PIC10F20X devices
• Connection terminals can be left unpopulated for easy connection using small
alligator clip leads (clamped across the edge of the board) or populated with either
through-hole or surface-mount terminals
• Footprints for optional passive components for:
- Power supply filtering
- Device bypass capacitor
- Terminal “A” pull-up resistor
- Terminal “B” pull-down resistor
• Footprints for two switches:
- INCR button operation can be detected by the PIC10F20X to generate
Increment commands (move wiper toward terminal A)
- DECR button operation can be detected by the PIC10F20X to generate
Decrement commands (move wiper toward terminal B)
• Button sequence instructions are printed on the back of the PCB
Appendix A.2 “Schematic” illustrates the schematic for the MCP402XEV.
MCP401X/2X EVALUATION
BOARD USER’S GUIDE
Note:The PIC10F20X firmware (00066_MCP402XEV.HEX) must be
programmed into the microcontroller before the MCP402XEV is functional.
The MCP4021/11-XXXI/SN is an 8-pin digital potentiometer device with terminals A, B
and W available on the device pins. Footprint U1 supports the 150 mil SOIC package.
Resistors R2 and R3 are 2.5 k
MCP4021-103I/SN digital potentiometer. This creates a “windowed” voltage divider
with a transfer function illustrated in Equation 2-1.
EQUATION 2-1:WINDOWED POTENTIOMETER – VOLTAGE DIVIDER
CALCULATION
V
wiper
Note 1:DO NOT populate U2 if using U1.
2:The MCP4011 has high-voltage tolerant pins and, therefore, accepts
high-voltage Increment and Decrement commands. Since this device is
nonvolatile, the WiperLock™ Technology feature is not present.
The MCP4022/12-XXXI/OT is a 6-pin, stand-alone digital rheostat with terminals A and
W available on the device pins. Footprint U2 supports the SOT-23-6 package. An
“INCR” command moves the wiper toward terminal A, thus causing the resistance
across the rheostat to decrease. A “DECR” command moves the wiper toward terminal
B, thus causing the resistance across the rheostat to increase.
Note 1:DO NOT populate U1 if using U2.
2:The included MCP4021 digital potentiometer samples could be used to
evaluate the MCP4022 simply by not populating R3 and letting terminal
B float.
3:The MCP4012 has high-voltage tolerant pins and, therefore, accepts
high-voltage Increment and Decrement commands. Since this device is
nonvolatile, the WiperLock™ Technology feature is not present.
The MCP4023/13-XXXI/OT is a 6-pin, grounded digital potentiometer with terminals A
and W available on the device pins. Footprint U2 supports the SOT-23-6 package.
Populating R2 will create a voltage divider with a transfer function illustrated in
Equation 2-2.
EQUATION 2-2:GROUNDED POTENTIOMETER – VOLTAGE DIVIDER
CALCULATION
V
wiper
VDDRwb⋅
-------------------------R2 R
+
5.0V d 10k
-----------------------------------------------
==
nom
Where:
d = the wiper setting (0 to 63)
Note 1:DO NOT populate U1 if using U2.
2:The included MCP4021 digital potentiometer samples could be used to
evaluate the MCP4023 simply by shorting terminal B to GND.
3:The MCP4013 has high-voltage tolerant pins and, therefore, accepts
high-voltage Increment and Decrement commands. Since this device is
nonvolatile, the WiperLock™ Technology feature is not present.
⋅63⁄
()
⋅
2.5k10k+
2.2.4Evaluating the MCP4024 and MCP4014
The MCP4024/14-xxxI/OT is a 5-pin, grounded digital rheostat with the W terminals
available on the device pins. Footprint U2 supports the SOT-23-5 package. An “INCR”
command moves the wiper toward terminal A, thus causing the resistance across the
rheostat to increase. A “DECR” command moves the wiper toward terminal B, thus
causing the resistance across the rheostat to decrease.
Note 1:DO NOT populate U1 if using U2.
2:The included MCP4021 digital potentiometer samples could be used to
evaluate the MCP4024 simply by not populating R
float and by shorting terminal B to GND.
3:The MCP4014 has high-voltage tolerant pins and, therefore, accepts
high-voltage Increment and Decrement commands. Since this device is
nonvolatile, the WiperLock™ Technology feature is not present.
The populated PCB is configured to create a voltage divider from VDD to VSS using a
2.5 kΩ pull-up resistor (R2), the MCP4021-103I/SN (10 kΩ) and a 2.5 kΩ pull-down
resistor (R3). To quickly evaluate the digital potentiometer’s performance, the following
test equipment is required:
• 2.7V to 5.5V power supply
• Voltmeter or Digital Multimeter (DMM)
Figure 2-1 shows the Digital Potentiometer Evaluation Board PCB, components and
the connection points that will be used in the step-by-step demonstration.
2.3.2MCP402X Digital Potentiometer Evaluation Board Firmware
Operation
The 00066_MCP402XEV.HEX firmware programmed into the PIC10F20X provides a
very simple interface to operate the MCP402X digital potentiometer. There are ten
basic commands supported for the user to exercise the MCP402X digital
potentiometer.
Table 2-1 shows the firmware commands of the Digital Potentiometer Evaluation
Board.
Table 2-2 provides step-by-step instructions to perform a demonstration of the
MCP402X Digital Potentiometer Evaluation Board. Measuring Ratiometric Voltage
TABLE 2-2:MEASURING RATIOMETRIC VOLTAGE
Step ActionResult
1Power up the populated Digital Potentiometer
Evaluation Board.
Voltage calculations use a system voltage of 5V.
Board can be powered via a valid voltage (such as 5V)
on the VDD and GND PCB Pads, or by connecting a
BFMP programmer that is connected to an active PC’s
USB port (powered via the USB connection).
2Set DMM to measure the required voltage range
(0V to 5.5V).
Connect the DMM ground connection to the
evaluation board GND terminal and the DMM V/Ω
connection to terminal W of the evaluation board.
3Turn on DMM.On DMM:
4On the Digital Potentiometer Evaluation Board:
Implement Command #5 (see Table 2-1).
5On the Digital Potentiometer Evaluation Board:
Implement Command #4 (see Table 2-1).
6On the Digital Potentiometer Evaluation Board:
Implement Command #6 (see Table 2-1).
7On the Digital Potentiometer Evaluation Board:
Implement Command #6 (see Table 2-1) 15 more
times.
8On the Digital Potentiometer Evaluation Board:
Turn off/disconnect power supply.
9On the Digital Potentiometer Evaluation Board:
Reapply power to Digital Potentiometer Evaluation
Board.
10On the Digital Potentiometer Evaluation Board:
Implement Command #3 (see Table 2-1).
—
—
DMM will display the voltage determined by the
current non-volatile setting of the wiper.
On the Digital Potentiometer Evaluation Board:
WiperLock™ Technology is disabled and the wiper is
incremented from power-on position.
On DMM:
Voltage increments approximately 0.05V.
On the Digital Potentiometer Evaluation Board:
Wiper is forced to the full-scale position
(Wiper W = node A).
On DMM:
Voltage equals approximately 4.17V.
On the Digital Potentiometer Evaluation Board:
Wiper is decremented one position from the full-scale
position.
On DMM:
Voltage equals approximately 4.12V.
On the Digital Potentiometer Evaluation Board:
Wiper is decremented to the 25% position from
node A (full-scale) to node B (zero-scale).
On DMM:
Voltage equals approximately 3.33V.
On DMM:
Voltage equals approximately 0V.
On DMM:
Voltage equals approximately 3.33V. This shows that
the MCP4021 retained the last selected wiper setting.
On the Digital Potentiometer Evaluation Board:
Wiper is incremented one position and the wiper is
locked (WiperLock Technology is enabled).
Table 2-3 shows the relation between the wiper position and the voltage on the wiper
when 5.0 volts is applied to the MCP402X Digital Potentiometer Evaluation Board
where the resistors R2 and R3 are 2500Ω and the MCP4011 resistor value (R
10 kΩ. Equation 2-4 shows the formula for these calculations.
EQUATION 2-4:CALCULATING THE VOLTAGE ON THE MCP402X DIGITAL
The typical step resistance for the 10 kΩ MCP4021 is approximately 158.73Ω. With a
5V supply and a 15 kΩ total resistance (2.5 kΩ + 2.5 kΩ + 10 kΩ), the delta voltage per
step is approximately 0.053V.
TABLE 2-3:WIPER POSITION AND CORRESPONDING VOLTAGE
The 000066_MCP402XEV.ASM source code was written using assembly language for
the PIC10F206 microcontroller. Refer to Appendix E. “00066_MCP402XEV.ASM Source Code”. The firmware initializes its I/O so that it can poll the INCR and DECR
buttons every millisecond. If the button has been depressed for 40 out of 50 ms, it is
considered to be debounced and asserted. The button state is used to determine if a
new “Action State” is desired. This simple state machine then decodes the desired
action. Once the desired command has been determined, a subroutine that performs
the desired command is executed. The low-voltage commands are driven directly from
the PIC10F206 I/O, but the high-voltage commands require some additional hardware
to generate the required 8V signal.
When a high-voltage command is required, the PIC10F206’s unique OSCOUT feature
is utilized. The OSCOUT function allows the instruction clock to be driven on
GP2/OSCOUT under software control. A simple charge pump consisting of 2 – 0.01 µF
capacitors and a BAV99 dual diode is constructed as seen in Appendix A.2 “Schematic”. When the OSCOUT is enabled, a 1 MHz clock drives the charge
pump that produces an 8.4V signal on the MCP401X/2X’s CS
V
= 5.0V). Utilizing such a charge pump requires special attention to the timing of the
DD
CS
& U/D signals.
Refer to the comments in the 00066_MCP402XEV.ASM source code for more details
on the firmware’s operation. Refer to Appendix E. “00066_MCP402XEV.ASM Source
Appendix C. Using the BFMP Programmer to Power the Board
C.1POWERING THE BOARD USING THE BFMP PROGRAMMER
The BFMP programmer (PG164101) uses a 6-pin interface for programming. The
PICkit™ 2 Flash Starter Kit (PG164120 or DV164120) uses the same interface.
The MCP402X Digital Potentiometer Evaluation Board (MCP402XEV) plugs into the
BFMP (or PICkit™ 2 Flash Starter Kit) in the orientation shown in Figure C-1.
USB Cable to PC
BFMP Board
(Top-side)
Pin #1Pin #1
FIGURE C-1:BFMP Hardware and MCP402X Digital Potentiometer Evaluation
Board.
These programmers use the PICkit™ 1 Baseline Flash Programmer software. This GUI
should be configured as shown in Figure C-2. Selecting Device Power causes the
BFMP to power the attached board all the time (not just during programming). This
allows the MCP402X Digital Potentiometer Evaluation Board to be easily demonstrated
without needing to carry a power supply (although a DMM is still required).
Appendix D. Potential Programming Issue of the MCP402XEV
D.1INTRODUCTION
When programming the MCP402XEV with a Microchip programmer (such as the BPMF
or MPLAB
ICSPDAT pin. This issue relates to PCBs marked 104-00066 and 104-00066R2.
Due to the current path to ground that is present with the D1 and R1 components
through capacitor C2, the programming voltage on the ICSPDAT signal may not meet
the voltage requirements of the PIC10F206 GP1 pin.
To address this when programming the board, lift up one of the terminals of capacitor
C2 (see Figure D-1). This will stop the ICSPDAT signal voltage from being loaded down
while capacitor C2 is charging. The schematic is shown in Figure D-2.
After programming, capacitor C2 must be reconnected to the circuit. This is because
this circuit generates the high voltage for the high voltage commands.
®
ICD 2), the PIC10F206 may not program correctly due to the load on the
This capacitor connection needs
to be opened for programming.
After programming has been
completed, this capacitor needs
to be reconnected in the circuit.
This appendix documents the example PIC10F20X source code used on the populated
PCB. The source code is described in detail by the comments within the.ASM file. The
00066_MCP402XEV.ASM, PIC10F206.INC and MCP402XEV.HEX files can be found
on the Microchip web site (www.microchip.com) and on the included CD.
Software License Agreement
The software supplied herewith by Microchip Technology Incorporated (the “Company”) is intended and supplied to you, the
Company’s customer, for use solely and exclusively with products manufactured by the Company.
The software is owned by the Company and/or its supplier, and is protected under applicable copyright laws. All rights are reserved.
Any use in violation of the foregoing restrictions may subject the user to criminal sanctions under applicable laws, as well as to civil
liability for the breach of the terms and conditions of this license.
THIS SOFTWARE IS PROVIDED IN AN “AS IS” CONDITION. NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR
SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
TABLE E-1:00066_MCP402XEV.ASM SOURCE CODE
TITLE “MCP402XEV Firmware”
;********************************************************************************************
; Author: Jim Simons
; Tools: MPLAB(r) 7.10 using MPLINK(tm)
; Editor: ComicSans 8pt w/8 character Tabs
; Files: 00066_MCP402XEV.asm
; p10f206.inc
;
; Description: Exercise the MCP402x devices using the MCP402x EV board.
; Simply use the INCR & DECR buttons to execute the INCR & DECR serial commands.
;
; Serial Commands Action State
; _______________ ______ _____
; LV Increment w/EE Depress UP, Release UP LVwEE = 1 3
; LV Decrement w/EE Depress DOWN, Release DOWN LVwEE = 1 4
; LV Increment w/o EE Depress UP, Release UP LVwEE = x 3
; LV Decrement w/o EE Depress DOWN, Release DOWN LVwEE = x 4
; LV 64 Increments Depress UP, Hold for 2s, Release UP 1
; LV 64 Decrements Depress DOWN, Hold for 2s, Release DOWN 2
; HV Incr w/o WL Depress UP, Depress DOWN, Release UP, Release DOWN 11
; HV Decr w/o WL Depress DOWN, Depress UP, Release DOWN, Release UP 12
; HV 64 Incr w/o WL Depress UP, Depress DOWN, Hold for 2s, Release UP, Release DOWN 5
; HV 64 Decr w/o WL Depress DOWN, Depress UP, Hold for 2s, Release DOWN, Release UP 6
; HV Incr w/WL Depress UP, Depress DOWN, Release DOWN, Release UP 13
; HV Decr w/WL Depress DOWN, Depress UP, Release UP, Release DOWN 14
;
;**********************************************************************************************
; General Pupose Register Definitions
;**********************************************************************************************
GPRS UDATA
State res 1 ; variable to track the “state” value
; this code functions as a state machine
; bits 3:0 stores the state 0-15 indicator
; bits 7:6 stores the debounced button states
OldState res 1 ; allows program to see if State changed
Exit res 1 ; variable to count the number ms to test for a button press
DebounceUP res 1 ; variable to count the number of ms UP was pressed
DebounceDOWN res 1 ; variable to count the number of ms DOWN was pressed
DLYCNT1 res 1 ; variable for the delay subroutines
DLYCNT2 res 1 ; variable for the delay subroutines
#define UD GPIO,1 ; GP1 tied to UD & has “INCR” switch connected via 2.2k resistor
#define CS GPIO,0 ; GP0 tied to CS & has “DECR” switch connected via 2.2k resistor
#define UP GPIO,1 ; Momentary switch UP
#define DOWN GPIO,0 ; Momentary switch DOWN
#define CP GPIO,2 ; OSCOUT pin for Charge Pump
#define PowerDown GPIO,3 ; Vpp for programming & PowerDown input to put uC to SLEEP
#define HV_CS OSCCAL,FOSC4 ; Drives CS to HVDD (drive CS high first)
#define UP_State State,7 ; Debounced UP State - using bits 5:4 are critical
#define DOWN_State State,6 ; Debounced DOWN State - using bits 5:4 are critical
;***********************************************************************************************
; Reset Code
;***********************************************************************************************
RESET CODE ; processor reset vector + 1
;ResetVector ; Device resets to 0x1FF to get the factory calibration
goto Start ; Lower half of memory reserved for subroutines
;***********************************************************************************************
; Main Code
;***********************************************************************************************
MAIN CODE
Start
Init_Oscillator
; Internal RC calibration value is placed at location 0xFF by Microchip
; as a movlw k, where the k is a literal value.
movwf OSCCAL ; update register with factory cal value
; bcf OSCCAL,FOSC4 ; make sure the FOSC4 is disabled to shut charge pump off
StateMachineReset
movlw b’11110011’ ; Disable the Comparator & make inputs digital
movwf CMCON0
; bcf CMCON0, CMPON ; needs to be on to get GP2 as a General Purpose Output
movlw b’10011010’ ; disable Wake-Up on Pin Change,
; enable weak pullups for button inputs
; T0CS must be 0 for GP2 TRIS to be active
; set up for 72ms WDT
option
Init
clrf State
clrf OldState
call InitGPIO ; make GP0/DECR/CS & GP1/INCR/UD outputs, block undesired U/D commands
ButtonCheck ; Debounce Buttons
btfss PowerDown ; If pulled low, put uC to SLEEP
goto LowPowerMode
bsf UP_State
bsf DOWN_State ; initialize Button_State to indicate no button pressed
movlw .50 ; debounce the buttosns for 50mS to see if pressed
movwf Exit
movlw .40 ; if pushed for 40 of 50 tests, then valid UP
movwf DebounceUP
movwf DebounceDOWN
DebounceLoop
call GPIO_TestUP ; make GP1/INCR/UD an input (w/pu enabled)
movlw .99
call VAR5TcyDELAY ; 5 + (99*5) = 500us
btfss UP
decfsz DebounceUP,f
goto CheckDOWN
bcf UP_State ; UP Button Pressed
CheckDOWN
call GPIO_TestDOWN ; make GP0/DECR/CS an input (w/pu enabled)
movlw .99
call VAR5TcyDELAY ; 5 + (99*5) = 500us
btfss DOWN
decfsz DebounceDOWN,f
goto DebounceLoopTest
bcf DOWN_State ; DOWN Button Pressed
DebounceLoopTest
decfsz Exit,f
goto DebounceLoop
call InitGPIO ; make GP0/DECR/CS & GP1/INCR/UD outputs, block undesired U/D commands
movf State,w
call DetermineState ; subroutine to determine the next state
movwf State
xorwf OldState,w
btfss STATUS,Z
call PerformState ;State changed, now do something
movlw .5
call VAR1000TcyDELAY ; Make sure EE Write Cycle has finished
movf State,w
movwf OldState
goto ButtonCheck ; Loop for button presses
;***********************************************************************************************
; State Machine Code
;***********************************************************************************************
State0 goto StateMachineReset ; State 0, no action, shouldn’t get here
HV_DecrExit clrwdt
call GPIO_TestUP ; make GP1/INCR/UD an input (w/pu enabled)
movlw .10
call VAR1000TcyDELAY ; add a 10mS delay for settling time
btfss UP ; if UP gets released, exit
goto HV_DecrExit
call GPIO_TestDOWN ; make GP0/DECR/CS an input (w/pu enabled)
movlw .10
call VAR1000TcyDELAY ; add a 10mS delay for settling time
btfss DOWN ; if DOWN gets released, exit
goto HV_DecrExit
clrf State
retlw .6
State7
State8
State9
State10
State11 ; HV Increment w/o WL
call InitGPIO ; make GP0/DECR/CS & GP1/INCR/UD outputs, block undesired U/D commands
movlw .1
call HV_Increment_WL_Disable
clrf State
retlw .11
State12 ; HV Increment w/o WL
call InitGPIO ; make GP0/DECR/CS & GP1/INCR/UD outputs, block undesired U/D commands
movlw .1
call HV_Decrement_WL_Disable
clrf State
retlw .12
State13 ; HV Decrement w/WL
call InitGPIO ; make GP0/DECR/CS & GP1/INCR/UD outputs, block undesired U/D commands
call HV_Increment_WL_Enable
clrf State
retlw .13
State14 ; HV Decrement w/WL
call InitGPIO ; make GP0/DECR/CS & GP1/INCR/UD outputs, block undesired U/D commands
call HV_Decrement_WL_Enable
clrf State
retlw .14
State15
goto StateMachineReset
;***********************************************************************************************
; Low Power Mode
;***********************************************************************************************
LowPowerMode
movlw b’11111000’ ; make all I/O pins an output
tris GPIO
movlw b’11111111’
movwf GPIO ; output high
movlw b’10011111’ ; disable Wake-Up on Pin Change,
; enable weak pullups for button inputs
; T0CS must be 0 for GP2 TRIS to be active
; set up for 2.3ms WDT
option
sleep ; put into lowest power setting
goto Start ; shouldn’t get here
;***********************************************************************************************
; State Machine Subroutines
;***********************************************************************************************
DetermineState
swapf State,f
rrf State,f
rrf State,w
andlw b’00111111’ ; mask invalid states
addwf PCL,f
S0_00 retlw .0 ; both buttons pressed at same time, shouldn’t be here
S0_01 retlw .1 ; UP button pressed, change state
S0_10 retlw .2 ; DOWN button pressed, change state
S0_11 retlw .0 ; no change
S1_00 retlw .5 ; both buttons pressed, change state
S1_01 retlw .1 ; no change
S1_10 retlw .0 ; shouldn’t be here, reset state
S1_11 retlw .3 ; UP button released, change state
S2_00 retlw .6 ; both buttons pressed, change state
S2_01 retlw .0 ; shouldn’t be here, reset state
S2_10 retlw .2 ; no change
S2_11 retlw .4 ; DOWN button released, change state
S3_00 retlw .0 ; shouldn’t be here, reset state
S3_01 retlw .0 ; shouldn’t be here, reset state
S3_10 retlw .0 ; shouldn’t be here, reset state
S3_11 retlw .0 ; shouldn’t be here, reset state
S4_00 retlw .0 ; shouldn’t be here, reset state
S4_01 retlw .0 ; shouldn’t be here, reset state
S4_10 retlw .0 ; shouldn’t be here, reset state
S4_11 retlw .0 ; shouldn’t be here, reset state
S5_00 retlw .5 ; no change
S5_01 retlw .7 ; DOWN button released, change state
S5_10 retlw .9 ; UP button released, change state
S5_11 retlw .0 ; shouldn’t be here, reset state
S6_00 retlw .6 ; no change
S6_01 retlw .10 ; DOWN button released
S6_10 retlw .8 ; UP button released
S6_11 retlw .0 ; shouldn’t be here, reset state
S7_00 retlw .5 ; both buttons pressed again, change state
S7_01 retlw .7 ; no change
S7_10 retlw .0 ; shouldn’t be here, reset state
S7_11 retlw .13 ; UP button released, change state
S8_00 retlw .6 ; both buttons pressed again, change state
S8_01 retlw .0 ; shouldn’t be here, reset state
S8_10 retlw .8 ; no change
S8_11 retlw .14 ; DOWN button released, change state
S9_00 retlw .5 ; both buttons pressed again, change state
S9_01 retlw .0 ; shouldn’t be here, reset state
S9_10 retlw .9 ; no change
S9_11 retlw .11 ; DOWN button released, change state
S10_00 retlw .6 ; both buttons pressed again, change state
S10_01 retlw .10 ; no change
S10_10 retlw .0 ; shouldn’t be here, reset state
S10_11 retlw .12 ; DOWN button released, change state
S11_00 retlw .0 ; shouldn’t be here, reset state
S11_01 retlw .0 ; shouldn’t be here, reset state
S11_10 retlw .0 ; shouldn’t be here, reset state
S11_11 retlw .0 ; shouldn’t be here, reset state
S12_00 retlw .0 ; shouldn’t be here, reset state
S12_01 retlw .0 ; shouldn’t be here, reset state
S12_10 retlw .0 ; shouldn’t be here, reset state
S12_11 retlw .0 ; shouldn’t be here, reset state
S13_00 retlw .0 ; shouldn’t be here, reset state
S13_01 retlw .0 ; shouldn’t be here, reset state
S13_10 retlw .0 ; shouldn’t be here, reset state
S13_11 retlw .0 ; shouldn’t be here, reset state
S14_00 retlw .0 ; shouldn’t be here, reset state
S14_01 retlw .0 ; shouldn’t be here, reset state
S14_10 retlw .0 ; shouldn’t be here, reset state
S14_11 retlw .0 ; shouldn’t be here, reset state
S15_00 retlw .0 ; shouldn’t be here, reset state
S15_01 retlw .0 ; shouldn’t be here, reset state
S15_10 retlw .0 ; shouldn’t be here, reset state
S15_11 retlw .0 ; shouldn’t be here, reset state
PerformState
movf State,w
andlw b’00001111’ ; mask all states >15
addwf PCL,f ; jump to desired State
retlw .0 ; State 0, no action
goto State1 ; (2s for repeating LV increment)
goto State2 ; (2s for repeating LV decrement)
goto State3
goto State4
goto State5 ; (2s for repeating HV increment)
goto State6 ; (2s for repeating HV decrement)
retlw .7 ; State 7, no action
retlw .8 ; State 8, no action
retlw .9 ; State 9, no action
retlw .10 ; State 10, no action
goto State11
goto State12
goto State13
goto State14
goto StateMachineReset ; State 15... shouldn’t be here
;***********************************************************************************************
InitGPIO
movlw b’00001011’ ; initialize UP and DOWN switches to be high & CP to be low.
movwf GPIO
movlw b’11111100’ ; make GP0/DECR/CS & GP1/INCR/UD outputs, block undesired U/D commands
tris GPIO
retlw b’11000000’
GPIO_TestUP
movlw b’00001011’ ; initialize UP and DOWN switches to be high & CP to be low.
movwf GPIO
movlw b’11111100’ ; make both GP0/DECR/CS & GP1/INCR/UD outputs, prevent undesired U/D
commands
tris GPIO
movlw b’11111110’ ; make GP1/INCR/UD an input (w/pu enabled)
tris GPIO
retlw b’10000000’
GPIO_TestDOWN
movlw b’00001011’ ; initialize UP and DOWN switches to be high & CP to be low.
movwf GPIO
movlw b’11111100’ ; make both GP0/DECR/CS & GP1/INCR/UD outputs, prevent undesired U/D
commands
tris GPIO
movlw b’11111101’ ; make GP0/DECR/CS an input (w/pu enabled)
tris GPIO
retlw b’01000000’
DLOOP_HVIWL
clrwdt ;or NOP
nop
decfsz DLYCNT1,f
goto DLOOP_HVIWL
bcf UD
goto $+1 ; creates a 3us total delay
bsf UD
goto $+1 ; creates a 3us total delay
bcf UD
goto $+1 ; creates a 3us total delay
bcf HV_CS ; shut off 1Mhz Charge Pump
bcf CS ; helps pull the signal down from HV
goto $+1
bsf CS
retlw 0x04 ; Follow w/long delay to discharge pump & EEPROM Write Cycle
HV_Decrement_WL_Enable
bcf UD
bsf CS
goto $+1 ; creates a 3us total delay
bsf HV_CS ; starts 1Mhz Charge Pump
movlw .100 ; 5*100 = 500us delay for Charge Pump ramp time
movwf DLYCNT1
DLOOP_HVDWL
clrwdt ;or NOP
nop
decfsz DLYCNT1,f
goto DLOOP_HVDWL
bsf UD
goto $+1 ; creates a 3us total delay
bcf HV_CS ; shut off 1Mhz Charge Pump
bcf CS ; helps pull the signal down from HV
goto $+1
bsf CS
retlw 0x05 ; Follow w/long delay to discharge pump & EEPROM Write Cycle
;***********************************************************************************************
; VARIABLE DELAY SUBROUTINES
;***********************************************************************************************
; DLYCNT1 = F9h = 249d DLYCNT2 = W
; DELAY = T((4 DLYCNT1 + 4) DLYCNT2 + 4)
;
; ex. To create a 300ms delay when using a 4Mhz osc, 300-250 = 50
; movlw .50 ;load .50 into WREG
; call VAR1000TcyDELAY ;call VAR1000TcyDELAY = 50ms delay w/4MHz Osc
; call VAR1000TcyDELAY ;call VAR1000TcyDELAY = 250ms delay w/4MHz Osc
; ;total = 300ms delay
;************
; The value in W at the time of the CALL = x. Delay = 1000Tcy*x
VAR1000TcyDELAY
movwf DLYCNT2 ; LOADS CONTROLLING DLY # INTO PRIMARY COUNTER
DLOOP2
movlw .249 ; MAXIMIZES THE SECONDARY DLY COUNTER
movwf DLYCNT1
DLOOP1
clrwdt ;or NOP
decfsz DLYCNT1,f ; DECREMENT AND TEST SECONDARY LOOP FOR ZERO
goto DLOOP1 ; CONTINUE SECONDARY LOOP
decfsz DLYCNT2,f ; DECREMENT AND TEST PRIMARY DLY COUNTER
goto DLOOP2 ; CONTINUE PRIMARY LOOP
retlw .250 ; preload W for the next CALL VAR1000TcyDELAY
;************
; VARIABLE 5 Tcy DELAY UP TO 256*5Tcy+5Tcy
; DLYCNT1 = W
; DELAY = T(1 + 5 DLYCNT1 - 1) + CALL + RETLW
;
; ex. To create a 250us delay, (250/5)-1 = 49
; movlw .49 ;load .49 into WREG
; call VAR5TcyDELAY ;call VAR5TcyDELAY
;************
; The value in W at the time of the CALL = x. Delay = 5*Tcy + 5Tcy
VAR5TcyDELAY
movwf DLYCNT1 ; LOADS CONTROLLING DLY # INTO PRIMARY COUNTER
DLOOP3 clrwdt ;or NOP
nop
decfsz DLYCNT1,f ; DECREMENT AND TEST ZERO
goto DLOOP3 ; CONTINUE LOOP
retlw .250 ; preload W for the next CALL VAR5TcyDELAY
;***********************************************************************************************