FACT002 [ETC]

Fact 3 Documentation ; 其实3文档\n
FACT002
型号: FACT002
厂家: ETC    ETC
描述:

Fact 3 Documentation
其实3文档\n

文件: 总4页 (文件大小:52K)
中文:  中文翻译
下载:  下载PDF数据表文档文件
CARE AND FEEDING OF THE PIC16C74  
AND ITS PERIPHERALS  
By: Robert Angelo  
The PIC16C74 is one of the latest mid-range  
microcontrollers from Microchip Technology Inc. In this  
article we will be addressing a few of the new features and  
peripherals of this new part. The main focus will be on the  
A/D (Analog-to-Digital) Converter, the SCI-Serial  
Communication Interface, and the PWM-Pulse Width  
Modulator. Our intention is to give you a small program that  
initializes these peripherals as well as exercise them. A  
schematic is provided. The PICDEM-2 board from  
Microchip will run this program. The second trimpot does  
not exist on the PICDEM-2 board, so the second A/D value  
may float around. The second trimpot is only used to show  
a method of changing A/D input pins. If you are using the  
PICDEM-2 board, then the LED and a current limiting  
resistor must be connected to the PWM output. When the  
program is run, the RS-232 terminal will display two A/D  
values. The brightness of an LED is adjusted using pulse  
width modulation. The duty cycle is determined by the  
trimpot setting.  
A/D Converter Mysteries  
The A/D converter and its eight input channels will be our  
first topic. Setting up the A/D converter involves two special  
function registers:  
• ADCON0  
• ADCON1  
In the program included with this article is a code segment  
initad that sets up the A/D. ADCON0 is the work horse  
register for this peripheral. This register is used to select the  
conversion clock frequency and channel. This register is  
also where we signal the start of a conversion and detect  
the completion of a conversion. ADCON1 has only one  
purpose in life for this part, and that is A/D port  
configuration. When ADCON1 is used it does not override  
the TRISA register controls. The TRISA register must be set  
up. Once these registers are set up, all the program has to  
do is select the desired pin and set the GO/DONE bit in  
ADCON0. The program then waits for the conversion  
complete bit, GO/DONE, to be cleared by hardware. Then  
the ADRES (A/D conversion result register) register is read.  
The value from the first pot's conversion is then used to  
adjust the PWM pulse width thereby adjusting the LED  
brightness.  
Assumptions  
Although dangerous, sometimes we need to make  
assumptions. For this discussion on the PIC16C74, let us  
agree that RA0 and RA1 will be connected through a series  
resistor to the wipers on two potentiometers with the other  
ends connecting across VDD and ground (see schematic).  
The oscillator clock will be 4 MHz. First we'll read an A/D  
input, send its result out the serial port (to be displayed on  
a PC terminal program), and then switch to the next  
channel. We will adjust the PWM output pulse width to  
match the first potentiometer. Each time we are ready to  
begin a new sequence we will first send a pair of sync bytes  
to signal the receiving processor. To simplify our discussion,  
we will forgo using interrupts and we will do this in a polled  
fashion. The watch dog timer is disabled for this program.  
FIGURE 1:  
PWM PULSE WIDTH  
Period  
Period  
To ensure there are no surprises, it is a good idea to  
initialize every special function register (SFR) and data  
register to some known value prior to use.  
Duty  
Duty  
®
1996 Microchip Technology Inc.  
FACT003-page 1  
This document was created with FrameMaker 4 0 4  
Pulse Width Modulation (PWM)  
FIGURE 2:  
SERIAL COMMUNICATIONS  
INTERFACE MODULE  
The PORTC-1 pin is used as the PWM output. The registers  
that need to be set up for this PWM operation are:  
• TRISC  
• T2CON  
• CCPR2L  
• PR2  
• CCP2CON.  
The code initpwm is an example of what might be done  
to initialize the PWM module. TRISC was cleared earlier  
thus setting PORTC as output. By writing a "4" to the  
T2CON register, we will set the prescaler equal to 0 and  
select TIMER2 operation. Writing a 0fh to the CCP2CON  
register selects PWM mode and standard resolution. The  
0fh written to the CCPR2L register sets the high period to a  
low value initially. Setting the PR2 register to ffh allows the  
CCPR2L value (from the A/D converter result) to approach  
a 100% duty cycle. Now we can control the brightness of the  
LED attached to this pin by adjusting the pot on pin RA0 and  
writing the A/D result to the CCPR2L register as already  
described earlier.  
First global interrupts are disabled. The initsci code  
does the serial port setup and the sendat code handles the  
actual sending of the data.  
The SCI is setup for 2400 baud, 8 data bits and 1 stop bit  
with no parity. A terminal program, such as TERMINAL in  
Windows“, set to the same settings can be used to see our  
output. If you use the Windows terminal program, then set  
the communications parameters to 2400 baud, 8 data bits,  
1 stop bit, no parity and hardware handshake.  
Tying The Pieces Together  
SCI  
The main loop for getting the process running and restarting  
it again is mloop. The adcnvrt routine handles port pin  
selection and actual conversion control. The dopwm routine  
handles updating the PWM duty cycle register CCPR2L.  
The routine sendatchecks transmit ready status and loads  
the transmit buffer when the status reports ready. You will  
notice there is no error recovery routine. It is up to the user  
to determine.  
The Serial Communications Interface Module is our RS-232  
communications channel. We will configure the SCI as an  
asynchronous full duplex serial port. This is done with the  
routine at initsci in the program provided. There are a  
few fine points to remember relative to this peripheral. The  
baud rate is determined by a dedicated eight bit baud rate  
generator and can be used to derive standard baud rate  
frequencies from the oscillator. Since we are not using  
interrupts, there are only five registers to deal with:  
Here is what the program will do:  
Once all peripherals have been initialized two sync bytes  
"< >" are sent to the terminal. The A/D conversion results  
are then sent and the LED brightness is adjusted to match  
the RA0 trimpot setting. To simplify displaying A/D values,  
only the highest nibble is used, and thirty is added to put it  
into an ASCII range.  
• RCSTA - receiver status  
• TXSTA - transmitter status  
• TXREG - transmit buffer  
• RCREG - receive buffer  
• SPBRG - to set the baud rate generator  
®
FACT003-page 2  
1996 Microchip Technology Inc.  
Care and Feeding of PIC16C74 Source Code  
Note: This source code can be downloaded from our BBS from the "PICTIPS" library as filenameC74feed.ZIP.  
LIST P=16C74  
INCLUDEP16CXX.INC  
;new include file that comes with MPASM (on BBS)  
;a/d converter pin count register  
;a/d converter pin work register  
Adcnt equ  
Adcntw equ  
20h  
21h  
Temp  
equ  
22h  
;temporary data holding register seems we always need one  
org  
0
goto  
org  
bcf  
init  
5h  
;go to where our code really begins  
;begin program above interrupt service vector address  
;make sure we don't get interrupted  
init  
INTCON,7  
PORTA  
PORTB  
PORTC  
PORTD  
PORTE  
Adcnt  
Adcntw  
Temp  
STATUS,RP0  
TRISB  
TRISC  
TRISD  
TRISE  
clrf  
clrf  
clrf  
clrf  
clrf  
clrf  
clrf  
clrf  
bsf  
clrf  
clrf  
clrf  
clrf  
;don't rely on anything, set port latches where you want them  
;clear RAM registers we will be using  
;switch to page 1 to access trisX registers  
;set all ports outputs  
;just for this program to minimize current  
; and prevent pins from floating  
movlw 0Bh  
movwf TRISA  
;set analog inputs as inputs, the rest as outputs  
;
bcf  
STATUS,RP0  
initad movlw 0C1h  
movwf ADCON0  
;Internal RC A/D clock, input channel 0 , A/D on  
;(user must wait for specified period before sampling)  
;select page 1 of the SFRs  
bsf  
STATUS,RP0  
movlw 4  
movwf ADCON1  
;setup a/d inputs on RA0, RA1 and RA3 with Vref = Vdd  
;we are still in page 1 of the SFRs  
;setup 2400 baud  
initscimovlw  
19h  
movwf SPBRG  
movlw 20h  
;setup for async operations  
movwf TXSTA  
bcf  
STATUS,RP0  
;back to page 0 for a moment  
movlw 80h  
movwf RCSTA  
;enable serial port operations and the associated pins  
clrf  
clrf  
TXREG  
RCREG  
;clear our serial port buffers for start up  
;setup T2CON with prescaler = 0 and timer2 on  
;setup capture/compare to PWM mode standard resolution  
;set compare register to half for now  
initpwmmovlw 4h  
movwf T2CON  
movlw 0fh  
movwf CCP2CON  
movlw 0fh  
movwf CCPR2L  
bsf  
STATUS,RP0  
;select page 1 for the PR2 register  
movlw 0ffh  
movwf PR2  
bcf  
STATUS,RP0  
mloop movlw 0dh  
;send a carriage return character  
call  
sendat  
movlw 3ch  
;begin main loop for data gathering and serial transmission  
;these are our sync bytes to tell receiving micro a new  
;sequence is beginning  
call  
sendat  
movlw 3eh  
call  
clrf  
adloop call  
movf  
sendat  
Adcnt  
adcnvrt  
Adcnt,0  
;our first time through select AN0 pin  
;go do a conversion and send the result  
;get Adcnt into the W register  
xorlw 2  
btfss STATUS,2  
;(# determines number of AD inputs to scan)  
;have we sampled all of the pins yet?  
;go adjust the PWM output  
goto  
goto  
dopwm  
mloop  
;all done go do it again  
®
1996 Microchip Technology Inc.  
FACT003-page 3  
adcnvrtmovf  
Adcnt,0  
;get a/d count value  
movwf Adcntw  
;put in work register  
bcf  
rlf  
rlf  
rlf  
STATUS,0  
Adcntw,1  
Adcntw,1  
Adcntw,1  
;clear the carry flag for the upcoming rotate operations  
;rotate left and leave the number in adcntw  
;need to do it three times to put the count in the right  
;position to select the next A/D pin  
;load the initial ADCON0 value excepting channel select  
;set the pin select bits we want  
;set the new ADCON0 with new channels selected  
;wait about twenty micro seconds  
;start conversion  
movlw 0C1h  
iorwf Adcntw,0  
movwf ADCON0  
call  
bsf  
incf  
wait  
ADCON0,2  
Adcnt  
;increment pin counter register  
adwait btfsc ADCON0,2  
goto adwait  
;wait for conversion done  
;not done yet  
swapf ADRES,0  
andlw 0Fh  
addlw 30h  
;conversion done, swap result nibbles into W register  
;mask off the upper nibble to limit number to an ascii range  
;convert to ascii character to make it visible on terminal  
;select page one  
sendat bsf  
STATUS,RP0  
btfss TXSTA,1  
;check transmit status ready to send  
;if not ready go try again  
;back to page 0  
goto  
bcf  
sendat  
STATUS,RP0  
movwf TXREG  
return  
;transmit buffer empty send new data  
dopwm movf  
movwf CCPR2L  
goto adloop  
ADRES,0  
;get the a/d conversion value  
;put the value into the PWM duty cycle register  
wait  
movlw 08h  
movwf Temp  
decfsz Temp  
;do a wait loop of before using a/d converter  
w1  
goto  
return  
end  
w1  
;end of program  
FIGURE 3:  
PIC16C74 DEMO SCHEMATIC  
®
FACT003-page 4  
1996 Microchip Technology Inc.  

相关型号:

FAD1-03010BALW11

FAN AXIAL 30X10MM 5VDC WIRE
QUALTEK

FAD1-03010BAMW11

FAN AXIAL 30X10MM 5VDC WIRE
QUALTEK

FAD1-03010BBAW11

FAN AXIAL 30X10MM BALL 5VDC WIRE
QUALTEK

FAD1-03010BBLW11

FAN AXIAL 30X10MM BALL 5VDC WIRE
QUALTEK

FAD1-03010BBMW11

FAN AXIAL 30X10MM BALL 5VDC WIRE
QUALTEK

FAD1-03010BHAW11

FAN AXIAL 30X10MM 5VDC WIRE
QUALTEK

FAD1-03010BHHW11

FAN AXIAL 30X10MM 5VDC WIRE
QUALTEK

FAD1-03010BHLW11

FAN AXIAL 30X10MM 5VDC WIRE
QUALTEK

FAD1-03010BHMW11

FAN AXIAL 30X10MM 5VDC WIRE
QUALTEK

FAD1-03010CAAW11

FAN AXIAL 30X10MM 12VDC WIRE
QUALTEK

FAD1-03010CAHW11

FAN AXIAL 30X10MM 12VDC WIRE
QUALTEK

FAD1-03010CBAW11

FAN AXIAL 30X10MM 12VDC WIRE
QUALTEK