Documentos de Académico
Documentos de Profesional
Documentos de Cultura
AN746
Potentiometers to a PICmicro® Microcontroller
MCP41XXX and MCP42XXX potentiometers are 8-bit
Author: Craig L. King & Ezana Haile or 256 tap potentiometers. All 8 bits in the data byte are
Microchip Technology Inc. wiper data bits. Depending on the state of P0 and P1 in
the command byte, the data byte sets the wiper’s posi-
OVERVIEW tion or positions.
The MCP41XXX and MCP42XXX family of digital The four command bits to consider are bits 4:5 (C0:C1)
potentiometers communicate using a standard 3-wire and bits 0:1 (P0:P1). C0 and C1 determine which com-
SPI™ compatible interface. This application note will mand is being issued. For the MCP41XXX and
discuss communications between these devices and a MCP42XXX devices, there are three possible com-
PIC16F876 microcontroller. The code supplied with mands:
this application note will include both absolute and relo-
• Write new data to potentiometer(s)
catable assembly code, written for both hardware SPI
and firmware SPI implementations. • Shutdown potentiometer(s)
• NOP (No Operation).
COMMUNICATION The MCP42XXX devices contains two potentiometers,
Instructions for the MCP41XXX and MCP42XXX P0 and P1. P0 uses pins 5, 6 and 7. P1 uses pins 8, 9
devices consist of 16 clock cycles or two bytes. and 10. Using these two bits, the user can select either,
Figure 1 shows the format of these two bytes using a both or neither potentiometer. A ‘1’ for either P1 or P0
standard 3-wire SPI interface. The first byte is the com- will cause the data to be written to the respective data
mand byte which must contain four bits to program the register and a ‘0’ for P1 or P0 will cause no change. The
state of the digital potentiometer. The command byte MCP41XXX devices contain only one potentiometer.
determines the operation that is performed as well as For these devices, P1 is a don’t care.
identifies which potentiometer will execute the com-
mand (MCP42XXX devices contain two potentiome-
ters). The second byte is the data byte. The
CS
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
SCK
Command Byte Data Byte
Don’t Don’t Channel
Care Command Care Select New Potentiometer Data
Bits Bits Bits Bits
SI X X C1 C0 X X P1* P0 D7 D6 D5 D4 D3 D2 D1 D0
FIGURE 1: Instruction sequence and command byte summary for MCP41XXX AND MCP42XXX DIGITAL
potentiometers
#include <p16f876.inc>
ERRORLEVEL -302
__CONFIG _BODEN_OFF & _PWRTE_OFF & _CP_OFF & _WDT_OFF & _XT_OSC
;*******************************************************************************************
;******************** PROGRAM ORIGIN *****************************************************
;*******************************************************************************************
ORG 0X00
;-------------------------------------------------------------------------------------------
;-------------------- PORTB AND SPI SETTING ----------------------------------------------
;-------------------------------------------------------------------------------------------
;-------------------------------------------------------------------------------------------
;-------------------- PROGRAM ROUTINE ----------------------------------------------------
;-------------------------------------------------------------------------------------------
;-------------------------------------------------------------------------------------------
;-------------------- TRANSMISSION SUBROUTINE --------------------------------------------
;-------------------------------------------------------------------------------------------
END
#include <p16f876.inc>
ERRORLEVEL -302
__CONFIG _BODEN_OFF & _PWRTE_OFF & _CP_OFF & _WDT_OFF & _XT_OSC
CBLOCK H’20’
OUT, COUNT ;VARIABLES USED TO TRANSMIT SERIAL DATA
ENDC
;*******************************************************************************************
;******************** EQUATES ************************************************************
;*******************************************************************************************
;*******************************************************************************************
;******************** PROGRAM ORIGIN *****************************************************
;*******************************************************************************************
ORG 0x00
;-------------------------------------------------------------------------------------------
;-------------------- PORT SETTING -------------------------------------------------------
;-------------------------------------------------------------------------------------------
;-------------------------------------------------------------------------------------------
;-------------------- PROGRAM ROUTINE ----------------------------------------------------
;-------------------------------------------------------------------------------------------
;*******************************************************************************************
END
#include <p16f876.inc>
ERRORLEVEL -302
__CONFIG _BODEN_OFF & _PWRTE_OFF & _CP_OFF & _WDT_OFF & _XT_OSC
;*******************************************************************************************
;******************** PROGRAM ORIGIN *****************************************************
;*******************************************************************************************
prog1 code
;-------------------------------------------------------------------------------------------
;-------------------- PORTB AND SPI SETTING ----------------------------------------------
;-------------------------------------------------------------------------------------------
;-------------------------------------------------------------------------------------------
;-------------------- PROGRAM ROUTINE ----------------------------------------------------
;-------------------------------------------------------------------------------------------
;-------------------------------------------------------------------------------------------
;-------------------- TRANSMISSION SUBROUTINE ---------------------------------------------
;-------------------------------------------------------------------------------------------
END
#include <p16f876.inc>
ERRORLEVEL -302
__CONFIG _BODEN_OFF & _PWRTE_OFF & _CP_OFF & _WDT_OFF & _XT_OSC
udata
;*******************************************************************************************
;******************** EQUATES ************************************************************
;*******************************************************************************************
;*******************************************************************************************
;******************** PROGRAM ORIGIN *****************************************************
;*******************************************************************************************
prog1 code
;-------------------------------------------------------------------------------------------
;-------------------- PORT SETTING -------------------------------------------------------
;-------------------------------------------------------------------------------------------
;-------------------------------------------------------------------------------------------
;-------------------- PROGRAM ROUTINE ----------------------------------------------------
;-------------------------------------------------------------------------------------------
;*******************************************************************************************
END
LIBPATH .
DS00746A-page 16
APPENDIX F:
SCHEMATIC
U1 +5V
+5V U3 POR U2
2 Vin Reset 1 1 MCLR/Vpp/THV RA0 28 1 CS VDD 14
2
RC3 27 SCK
3 12 +5V
RC5 26 SI SHDN
4 11
GND
VSS RS
5 10
PB1 PB0
+5V 6 9 VOLT PA1
3
PW1 PW0
7 8 Meter
8 PA1 PA0
VSS I
+5V 9 +5V
OSC1/CLKIN VDD 20 MCP42XXX PW1
VSS 19 10K POT
4
Y1
+
2 3
VDD
GND CLK VOLT VOLT PB1
Meter Meter
PIC16F876 -
01/30/01
All rights reserved. © 2001 Microchip Technology Incorporated. Printed in the USA. 3/01 Printed on recycled paper.
Information contained in this publication regarding device applications and the like is intended through suggestion only and may be superseded by
updates. It is your responsibility to ensure that your application meets with your specifications. No representation or warranty is given and no liability is
assumed by Microchip Technology Incorporated with respect to the accuracy or use of such information, or infringement of patents or other intellectual
property rights arising from such use or otherwise. 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, except as maybe explicitly expressed herein, under any intellec-
tual property rights. The Microchip logo and name are registered trademarks of Microchip Technology Inc. in the U.S.A. and other countries. All rights
reserved. All other trademarks mentioned herein are the property of their respective companies.