AN840
AN840
AN840
Authors: Mary Tamar Tan, The A/D allows the conversion of an analog input signal
Mark Pallones to a corresponding 8-bit digital number. The output of
Microchip Technology Inc. the internal sample and hold circuit is the input into the
converter, which generates the result via successive
approximation. The analog reference voltage is
INTRODUCTION software-selectable to either the device’s positive
supply voltage (VDD) or the voltage level on the VREF
In choosing the appropriate microcontroller for a
pin. The A/D converter has a unique feature of being
specific application, it is necessary to select one which
able to operate while the device is in Sleep mode.
includes all the needed peripherals. It is also essential
to know how to integrate these peripherals to meet the The A/D module has three registers:
desired output requirements. • A/D Control Register 0 (ADCON0)
The PIC16F7X and PIC16C7X families are both 8-bit • A/D Control Register 1 (ADCON1)
mid-range microcontrollers. The PIC16F7X family is a • A/D Result Register (ADRES)
Flash device, while the PIC16C7X family is a In the sample program (see Appendix A: “Source
one-time-programming (OTP) device. Two code Code in Assembly” or Appendix B: “Source Code
examples are presented in this application note in C”), the code segment, init_ADC, sets up the A/D
covering the Analog-to-Digital Converter (ADC), via ADCON0 and ADCON1. The ADCON0 register
Timer2, Capture/Compare/PWM (CCP) and the controls the operation of the A/D module. This register
Universal Synchronous Asynchronous Receiver is used to select the conversion clock frequency and
Transmitter (USART), for both the PIC16F7X and the analog channel. It is where the start and completion
PIC16C7X families. of conversion is determined. The ADCON1 register
This application note covers three main subjects: configures the functions of the port pins. The
PIC16F7X and PIC16C7X microcontrollers have either
• Configuration of the above-mentioned peripherals
five or eight I/O pins which can be configured as analog
• Integration of these peripherals inputs. It is important to take note that ADCON1 does
• How their operation affects the other peripherals not override the respective TRIS register which is used
Also provided are calculations, data, hardware to configure the data direction of the PORT register.
schematic and source code in C and assembly. The ADCON1, TRISA and TRISE registers control the
operation of the A/D port pins. The port pins that are
desired as analog inputs must have their
A/D MODULE corresponding TRIS bits set (input). The sample
programs in Appendix A: “Source Code in
FIGURE 1: ANALOG-TO-DIGITAL Assembly” and Appendix B: “Source Code in C”
CONVERSION BLOCK use two analog channels, AN0 and AN1, which are
DIAGRAM switched after every conversion is completed.
CAPTURE/COMPARE/PWM MODULE
(CCP)
PIC16F7X and PIC16C7X have two CCP (Capture/
Compare/PWM) modules. Each module contains a
16-bit register which can operate as a 16-bit Capture
register, as a 16-bit Compare register or as a 10-bit
PWM Master/Slave Duty Cycle register. The CCP
modules are identical in operation, with the exception
of the operation of the Special Event Trigger. Each CCP
module has three registers, which are shown below:
FOSC
Baud Rate = -------------------------------------------------------------
-
64 SPBRG Value + 1
FOSC
SPBRG Value = ------------------------------------ –1
64 Baud Rate
Start
No
Configure TŝŵĞƌ2
A/D Done ?
Configure A/D
Yes
16
29
RD6 C6
28 3 2
C1+
VCC
RD5 C1- V+ 0.1 μF
10 27 4 6
RE2 RD4 C2+ V-
9 22 C3
RE1 RD3
8 21 0.1 μF 5
RE0 RD2 C2-
7 20
VDD RA5 RD1
6 19 11 14 1
RA4 RD0 T1IN T1OUT 6
5 10 7 2
RA3 T2IN T2OUT 7
4 26 12 13 3
RA2 RC7 R1OUT RB1IN 8
GND
RP2 3 25 9 8 4
5 kΩ RA1 RC6 R2OUT RB2IN 9
2 24 5
R2 RA0 RC5
23 U2
15
RP1 470 Ω RC4 MA3232C
18
5 kΩ RC3 P1
17 R1 R3
>1 DE9S - FRS
RC2 330 Ω 10 Ω
12 VSS 16
RC1
ϯϭ VSS 1ϱ
LED
W
Brightness
Analog
Input Voltage AͬD
Serial
USART Terminal
Display
MCP2200
Connector
Serial Port
USB Serial
USART USB-to-UART Serial Terminal Program
Converter
PIC16F7X/PIC16C7X
The USART output is sent to the Serial Terminal Since two analog channels are used in the A/D, two
Program by connecting the USB-to-UART Serial values will also be displayed on the PC monitor. A
Converter to the serial port connector included in the Serial Terminal Program is used to capture, control and
PICDEM 2 Plus Demo Board and to the USB port of a debug binary streams of data. It must be setup to
personal computer (see Figure 5). Microchip’s 2400 baud, eight data bits, one Stop bit and no parity to
MCP2200 is used as the USB-to-UART Serial match the USART software configuration. The
Converter for this application. displayed value can also be set to either ASCII, ANSI,
hexadecimal, binary or other types of numerical
representation, depending on the Serial Terminal
Program features.
(A) VAIN0 = 1V, VAIN1 = 0V, DCAVE = 11.82%, VDC = 448.2 mV (B) VAIN0 = 3.5 V, VAIN1 = 0V, DCAVE = 40.45%, VDC = 1.535V
(C) VAIN0 = 1V, VAIN1 = 2V, DCAVE = 29.95 %, VDC = 1.358V (D) VAIN0 = 3.5V, VAIN1 = 2V, DCAVE = 53.82 %, VDC = 2.491V
Analog Input
PWM Output
USART Output
Serial Decoding
CONCLUSION
In order to achieve the desired output values and
display, it is often necessary to configure each
peripheral one step at a time before putting them
together.
This application note describes a method on how the
A/D, Timer2, CCP and USART are configured,
implemented and integrated to achieve the desired
A/D output values and display. Data are also
provided to demonstrate how the peripherals behave
at specific time instants.
;
; Generation Information:
; Device : PIC16F74
; Compiler : MPASM v5.54
; MPLAB : MPLAB X 2.00
;*******************************************************************************
;*******************************************************************************
;Copyright (c) 2013 released Microchip Technology Inc. All rights reserved.
;
;Microchip licenses to you the right to use, modify, copy and distribute
;Software only when embedded on a Microchip microcontroller or digital signal
;controller that is integrated into your product or third party product
;(pursuant to the sublicense terms in the accompanying license agreement).
;
;You should refer to the license agreement accompanying this Software for
;additional information regarding your rights and obligations.
;
;SOFTWARE AND DOCUMENTATION ARE PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND,
;EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF
;MERCHANTABILITY, TITLE, NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE.
;IN NO EVENT SHALL MICROCHIP OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER
;CONTRACT, NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR
;OTHER LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
;INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE OR
;CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT OF
;SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
;(INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
;*******************************************************************************
#include p16f74.inc
__CONFIG _FOSC_RC & _WDTE_OFF & _PWRTE_OFF & _CP_OFF & _BOREN_ON
org 0x00
init_PORTS
banksel TRISA ; goto bank 1
movlw 0x03
movwf TRISA ; set <Ra0:Ra1> as inputs
movlw 0x00
movwf TRISC ; make all PORTC pins as output
banksel RCSTA ; switch to bank 0
bsf RCSTA, SPEN ; enable serial ports
init_PWM
banksel PR2 ; switch to bank 1
movlw 0xFF
movwf PR2 ; set value for TIMER2 Period Register
banksel CCPR2L ; switch to bank 0
movlw 0x0F
movwf CCPR2L ; set initial value for Capture/Compare/PWM Register2
movlw 0x0F
movwf CCP2CON ; select PWM mode
init_TIMER2
movlw 0x00
movwf T2CON ; prescale = 1:1, disable TIMER2
init_USART
banksel SPBRG ; switch to bank 1
movlw 0x19
movwf SPBRG ; baud rate = 2400 for Fosc = 4 MHz
movlw 0x20
movwf TXSTA ; enable asynchronous transmission
LOOP btfss PIR1, TMR2IF ; wait for TMR2 and PR2 to match
goto $-1
bcf PIR1, TMR2IF ; clear TMR2 and PR2 match flag bit
bsf ADCON0, GO ; start an A-to-D Conversion
btfsc ADCON0, GO ; wait for conversion to complete
goto $-1
banksel TXSTA ; switch to bank 1
btfss TXSTA, TRMT ; wait for TRMT flag bit to be set
goto $-1
banksel ADRES ; switch to bank 0
movf ADRES, W ; move ADRES register value to W register
movwf TXREG ; copy value on W register to TXREG
movwf CCPR2L ; copy value on W register to Duty Cycle register
movf ADCON0, W ; move ADCON0 register value to W register
xorlw 0x08
movwf ADCON0 ; change analog input channel
goto LOOP
END
Summary:
This is the main file used for the Application Note AN840
Generation Information :
Device : PIC16F74
Compiler : XC8 v1.30
MPLAB : MPLAB X 2.00
*******************************************************************************/
/*******************************************************************************
Copyright (c) 2013 released Microchip Technology Inc. All rights reserved.
Microchip licenses to you the right to use, modify, copy and distribute
Software only when embedded on a Microchip microcontroller or digital signal
controller that is integrated into your product or third party product
(pursuant to the sublicense terms in the accompanying license agreement).
You should refer to the license agreement accompanying this Software for
additional information regarding your rights and obligations.
SOFTWARE AND DOCUMENTATION ARE PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF
MERCHANTABILITY, TITLE, NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE.
IN NO EVENT SHALL MICROCHIP OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER
CONTRACT, NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR
OTHER LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE OR
CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT OF
SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
(INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
*******************************************************************************/
#include<xc.h>
• 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.
QUALITY MANAGEMENT SYSTEM Microchip received ISO/TS-16949:2009 certification for its worldwide
headquarters, design and wafer fabrication facilities in Chandler and
CERTIFIED BY DNV Tempe, Arizona; Gresham, Oregon and design centers in California
and India. The Company’s quality system processes and procedures
are for its PIC® MCUs and dsPIC® DSCs, KEELOQ® code hopping
== ISO/TS 16949 == 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.