|
|
Part: AN428
Category:
Description:
Company:
Datasheet: Download AN428 datasheet File size : 50 kB
Request For quote: Find where to buy AN428
Datasheet text preview:
Philips Semiconductors Microcontroller Products
Application note
Using the ADC and PWM of the 83C752/87C752
AN428
The Philips 83C752/87C752 is a single-chip control-oriented microcontroller. It is an 80C51 derivative, having the same basic architecture and powerful instruction set in a small 28-pin package. As "add-on" functions to a standard microcontroller, it offers an I2C small area network port, a five-channel multiplexed 8-bit analog-to-digital converter (ADC), and a pulse width modulation (PWM) output. The part is essentially the popular 8XC751 with the addition of the ADC and the PWM output. There are many control applications for which this microcontroller can provide an almost-complete, low-cost solution. The A/D converter can monitor analog voltages of up to five sources. The PWM output can be used to generate an analog control voltage with the addition of a simple integrator circuit. Another potential use for the PWM output is as a driver of power-switching circuits for DC motor speed control. The analog-to-digital converter has 8-bit resolution, and the conversion takes 40 machine cycles. A multiplexer selects one out of five input pins. The operation of the A/D
converter and the multiplexer is controlled by the ADCON register. The repetition frequency of the PWM output pulses is determined by an 8-bit prescaler, programmed at register PWMP. The duty cycle of these pulses is determined by the contents of a compare register, PWM. In order to implement the pulse width modulator, the prescaler output drives an 8-bit counter. When the counter value matches the contents of the compare (PWM) register, the PWM output is set high, and when the counter reaches zero, the output is set low. The counter is modulo 255, so the duty cycle generated will be the PWM contents multiplied by 1/255. The enclosed listing demonstrates usage of the A/D converter and the PWM. In order to communicate with the outside world, the program sends messages on a software-driven RS-232 port. The routines for sending messages via a software-controlled serial port can be quite useful, and for further discussion on those, please refer to Application Note 423: "Software Driven Serial Communication Routines for the 83C751 and 83C752 Microcontrollers."
Bit 5 of port 1 is used for the RS-232 communications, and in order to hook the microcontroller to a terminal, a buffer (e.g., MC1488) is needed. Timer 0 is used as the baud rate generator, where the timer value is defined by the symbol BaudVal. The programmed value will generate a 9600 baud rate with a 16MHz crystal. The program, after initialization and sending a message to the terminal, scans all five A/D channel inputs and outputs the voltage read on the serial port, as a hexadecimal value. Circuit operation can be verified by comparing channel voltages with the reading at the terminal. The program follows with an infinite loop in which channel 0 of the A/D converter is read, and its value is used to program the PWM. A simple verification of the duty cycle can be done with a voltmeter: since it acts as an integrator, its reading will be proportional to the duty cycle. Reading of a voltmeter on the PWM output should be proportional to the channel 0 input voltage. If the analog reference voltage AVCC, which is full-scale of the A/D measurement, is set to be exactly as VCC, the PWM output will track channel 0 within about 20mV.
December 1990
1
Revision date: June 1993
Philips Semiconductors Microcontroller Products
Application note
Using the ADC and PWM of the 83C752/87C752
AN428
DEMO752C
87C752 A/D and PWM Demonstration
12/03/90
PAGE 1
FF75
0010 0012 0013 0014 0020 0000 0001 0095
0000 0000 0135 000B 000B 01C5 002B 002B 0199
1; 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
;************************************************************************** ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; 87C752 A/D and PWM Demonstration Program This program first reads all five A/D channels and outputs the values in hexadecimal as RS232 data. Next, the PWM output is set to reflect the value on A/D channel 0, and again outputs the A/D value to RS232. Note that the A/D value is inverted before being moved to the PWM compare register in order to compensate for the inversion on the PWM output pin. This process is repeated continuously. Thus, a voltage may be applied to ADC0 (P1.0, pin 13) to vary the PWM pulse width. A simple test of this function is to measure the voltage on ADC0 and PWM with a voltmeter. A typical voltmeter will integrate the waveform on the PWM output and show a voltage within about 20mV of that on ADC0. The RS232 output appears on Port 1 pin 5, which must be buffered with an MC1488 or perhaps a MAX232 chip prior to being connected to a terminal. The transmission rate will be 9600 baud when the 87C752 is operated from 16MHz crystal.
;************************************************************************** $Title(87C752 A/D and PWM Demonstration) $Date(12/03/90) $MOD752 ;************************************************************************** BaudVal EQU 139 ;Timer value for 9600 baud @ 16 MHz. ;(one bit cell time) ;Data for RS232 transmit routine. ;RS232 transmit bit count. ;Holds next value for updating the PWM. ;Holds last A/D conversion result.
XmtDat BitCnt PWMVal ADVal Flags TxFlag ADFlag TxD
DATA DATA DATA DATA DATA BIT BIT BIT
10h 12h 13h 14h 20h Flags.0 Flags.1 P1.5
;Transmitinprogress flag. ;Indicates A/D conversion complete. ;Port bit for RS232 transmit.
;************************************************************************** ; Interrupt Vectors ORG AJMP ORG AJMP ORG AJMP 0 Reset 0BH Timr0 2Bh ADInt ;Reset vector.
;Timer 0 interrupt. ;(used as a baud rate generator) ;A/D conversion complete interrupt.
December 1990
2
Philips Semiconductors Microcontroller Products
Application note
Using the ADC and PWM of the 83C752/87C752
AN428
DEMO752C
87C752 A/D and PWM Demonstration
12/03/90
PAGE 2
0033 0033 01A3
0035 0038 003B 003E
758130 752000 758800 75A882
0041 90011B 0044 310A 0046 0048 0049 004B 004C 004F 0051 0052 0054 0057 0059 005A 005C 005D 0060 0063 7900 E9 118D FA 900152 310A E9 11EC 900161 310A EA 11EC 09 B905E8 90014F 310A
0065 0068 006B 006E 0071 0074 0076 0078 007B 007D 007F 0082 0084
758FFF 758E00 751300 75FE01 75A8CA 7400 1186 3001FD E514 11EC 900165 310A 80EE
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
ORG AJMP
33h PWMInt
;PWM interrupt.
;***************************************************************************** Reset: MOV MOV MOV MOV MOV ACALL MOV MOV ACALL MOV MOV ACALL MOV ACALL MOV ACALL MOV ACALL INC CJNE MOV ACALL SP,#30h Flags,#0 TCON,#00h IE,#82h DPTR,#Msg1 Mess R1,#0 A,R1 ADConv R2,A DPTR,#Msg2 Mess A,R1 PrByte DPTR,#Msg3 Mess A,R2 PrByte R1 R1,#5,Loop1 DPTR,#CRLF Mess
;Clear RS232 flags. ;Set up timer controls. ;Enable timer 0 interrupt. ;Point to message string. ;Send message. ;Start with A/D channel 0. ;Start A/D conversion.
Loop1:
;Point to message string. ;Send message. ;Print channel #. ;Point to message string. ;Send message.
;Print A/D value. ;Advance R1 value. ;Point to message string.
; Now use A/D channel 0 value to control the PWM. MOV MOV MOV MOV MOV Loop2: MOV ACALL JNB MOV ACALL MOV ACALL SJMP PWMP,#0FFh PWCM,#0 PWMVal,#0 PWENA,#1 IE,#0CAh A,#0 ADStart ADFlag,$ A,ADVal PrByte DPTR,#Msg4 Mess Loop2 ;Set PWM slow frequency. ;Set initial PWM value. ;Default starting value for the PWM. ;Start PWM ;Now enable the A/D and PWM interrupts. ;Read A/D channel 0. ;Start A/D conversion. ;Wait for A/D conversion complete. ;Get A/D result to print. ;Print PWM value. ;Point to message string.
; A/D Conversion Routines. ; The following shows two ways to use the A/D. Both routines are used by ; different portions of the sample program. ; ; ; Method 1: This version of the routine starts the conversion and then returns. The mainline program can detect when the conversion is complete by checking the A/D conversion complete flag (ADFlag) which is 3
December 1990
Philips Semiconductors Microcontroller Products
Application note
Using the ADC and PWM of the 83C752/87C752
AN428
DEMO752C
87C752 A/D and PWM Demonstration
12/03/90
PAGE 3
0086 0088 008A 008C
C201 4428 F5A0 22
008D 008F 0091 0093 0096 0098
4428 F5A0 E5A0 30E4FB E584 22
0099 009B 009D 009E 00A0 00A2
E584 F514 F4 F513 D201 32
00A3 85138E 00A6 32
00A7 11AD 00A9 2000FD 00AC 22
00AD 00AF 00B2 00B5 00B8 00BB 00BE 00C0 00C2 00C4
F510 75120A 758CFF 758A75 758DFF 758B75 D28C C295 D200 22
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
; ;
set by the A/D interrupt service routine. A/D data must be read by the calling routine. ADFlag A,#28h ADCON,A ;Clear A/D conversion complete flag. ;Add control bits to channel #. ;Start conversion.
ADStart: CLR ORL MOV RET
; ; ;
Method 2: This is an alternative version of the A/D routine which starts the conversion and then waits for it to complete before returning. A/D data is returned in the ACC. ORL MOV MOV JNB MOV RET A,#28h ADCON,A A,ADCON ACC.4,ADC1 A,ADAT ;Add control bits to channel #. ;Start conversion. ;Wait for conversion complete. ;Read A/D.
ADConv: ADC1:
; A/D interrupt service routine. ADInt: MOV MOV CPL MOV SETB RETI A,ADAT ADVal,A A PWMVal,A ADFlag ;Read A/D data. ;Save A/D data for print routine. ;Complement the value for the PWM. ;Set new value for PWM update. ;Tell main that new A/D data is ready.
; PWM interrupt service routine allows updating the PWM synchronously. PWMInt: MOV RETI PWCM,PWMVal ;Update PWM duty cycle.
; Send a byte out RS232 and wait for completion before returning. XmtByte: ACALL JB RET RSXmt TxFlag,$ ;Send ACC to RS232 output. ;Wait for transmit complete.
; Begin RS232 transmit. RSXmt: MOV MOV MOV MOV MOV MOV SETB CLR SETB RET XmtDat,A BitCnt,#10 TH,#High BaudVal TL,#Low BaudVal RTH,#High BaudVal RTL,#Low BaudVal TR TxD TxFlag ;Save data to be transmitted. ;Set bit count. ;Set timer for baud rate. ;Also set timer reload value. ;Start timer. ;Begin start bit. ;Set transmitinprogress flag.
December 1990
4
Philips Semiconductors Microcontroller Products
Application note
Using the ADC and PWM of the 83C752/87C752
AN428
DEMO752C
87C752 A/D and PWM Demonstration
12/03/90
PAGE 4
00C5 00C7 00C9 00CC 00CE 00D0 00D2
C0E0 C0D0 200007 C28C D0D0 D0E0 32
00D3 D51204 00D6 C200 00D8 80F2 00DA 00DC 00DF 00E1 00E3 00E5 00E6 00E8 00EA E512 B40104 D295 80EB E510 13 F510 9295 80E2
00EC 00EE 00EF 00F1 00F3 00F5 00F7 00F9
C0E0 C4 11FA 11A7 D0E0 11FA 11A7 22
00FA 00FC 00FF 0102 0105 0107 0109
540F 30E308 20E203 30E102 2407 2430 22
010A 010C 010E 010F
C0E0 7800 E8 93
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
; Timer 0 timeout: RS232 receive bit or transmit bit. Timr0: PUSH PUSH JB CLR POP POP RETI ACC PSW TxFlag,TxBit TR PSW ACC
T0Ex1: T0Ex2:
;Is this a transmit timer interrupt? ;Stop timer.
; RS232 transmit bit routine. TxBit: DJNZ CLR SJMP MOV CJNE SETB SJMP MOV RRC MOV MOV SJMP BitCnt,TxBusy TxFlag T0Ex1 A,BitCnt A,#1,TxNext TxD T0Ex2 A,XmtDat A XmtDat,A TxD,C T0Ex2 ;Decrement bit count, test for done. ;End of stop bit, release timer. ;Stop timer and exit. ;Get bit count. ;Is this a stop bit? ;Set stop bit. ;Exit. ;Get data. ;Advance to next bit. ;Send data bit. ;Exit.
TxBusy:
TxNext:
; Print byte routine: print ACC contents as ASCII hexadecimal. PrByte: PUSH SWAP ACALL ACALL POP ACALL ACALL RET ACC A HexAsc XmtByte ACC HexAsc XmtByte
;Print nibble in ACC as ASCII hex.
; Hexadecimal to ASCII conversion routine. HexAsc: ANL JNB JB JNB ADD ADD RET A,#0FH ACC.3,NoAdj ACC.2,Adj ACC.1,NoAdj A,#07H A,#30H ;Convert a nibble to ASCII hex.
Adj: NoAdj:
; Message string transmit routine. Mess: Mesl: PUSH MOV MOV MOVC ACC R0,#0 A,R0 A,@A+DPTR 5
;R0 is character pointer (string ; length is limited to 256 bytes). ;Get byte to send.
December 1990
Others parts begin by an
AN-1 AN-2 AN-3 AN-4 AN-5 AN-6 AN-7 AN-8
|
|
|