Text preview for : PWM.pdf part of PIONEER KEH-1850 KEH-1800 KEH-1800 X1M/UC KEH-1850 X1M/UC XM/ES X1M/ES ETC... ORDER NO. CRT2266



Back to : Pioneer Keh 1800 Keh 1850 | Home

AN654
PWM, a Software Solution for the PIC16CXXX
Author: Ole Röpcke Consultant, Europe

METHODS
Before the solution is revealed, we must first examine the various software methods used to generate variable length pulses. In the following explanations, the unit of time will be the length of an Instruction Cycle (TCY). We will use TCY because one instruction (if the program counter is not changed) executes in one TCY and Timer0 (without prescaling) increments every TCY. This provides us with a simple method to control a potentially complex timing sequence. Making use of the time needed to execute an instruction provides a very simple method of generating an pulse. Example 1 (Method A) shows an instruction sequence, which will generate a high pulse of 99 TCY on pin 3 of PORTA. The pulse length is controlled by the value of register LENGTH in steps of 3 TCY. This is the computing time needed by one program loop. The drawbacks of this method are an excessive use of computing time and a poor PWM resolution.

INTRODUCTION
The low cost, high performance features of a PIC16CXXX microcontroller make it a suitable device for automatic control technology applications. Sometimes, an additional PWM output is needed. For some devices, such as the PIC16C71, the addition of a software PWM adds the missing element. It is possible to use Timer0 (which also provides the system clock) and its corresponding interrupt to generate a PWM output with a duty cycle that can vary from nearly 10% to 90%. However, some applications require a greater duty cycle range. This application note provides a software solution for a more accurate and flexible PWM output, which is characterized by the following: 1. 2. 3. 4. 5. PWM frequency up to 19.6 kHz (20 MHz crystal). Variable duty cycle from 0% to 100%. 8-bit resolution. PWM step size of 1 TCY (Instruction Cycle Time). Simultaneous generation of a system time clock.
. . . PORTA LENGTH COUNTER . . . movlw movwf . . . movf movwf bsf Loop decfsz goto bcf . . . COUNTER,F Loop PORTA,3

EXAMPLE 1:

USING TCY AS A TIME BASE FOR PULSE GENERATION (METHOD A)

equ equ equ

04h 0ch 0dh

34 LENGTH

; value for pulse length of 99 Tcy ;

LENGTH,W COUNTER PORTA,3

; write value to the loop counter ; ; start high pulse ; counting pulse length ; ; end high pulse

© 1997 Microchip Technology Inc.

DS00654A-page 1

AN654
However, the architectural features of Microchip's midrange microcontrollers allow us to proceed in another direction. Example 2 shows an instruction sequence (Method B), which enables us to generate a high pulse with lengths varying from 1 to 5 TCY. The addition of any number to the file register PCL increases the program counter and skips a predetermined number of instructions (depending on the number added to PCL). The length of the high pulse is the same as the computing time consumed by the number of executed BSF instructions and is controlled by the value of file register LENGTH. If LENGTH is set to 4, 4 BSF instructions will be skipped and a high pulse of 1 TCY will be generated. If LENGTH is set to 0, no instructions will be skipped and the length of the pulse remains 5 TCY. A special effect takes place, when LENGTH is set to 5. All BSF instructions are skipped and therefore no high pulse occurs. This instruction sequence is able to generate pulses of various lengths including the length 0 TCY. Between each length the step size is only 1 TCY. This method will generate a long pulse if LENGTH is set to a small value, and a short pulse if LENGTH is set to a large value. The drawback of this method is that it is suitable only for short pulses. Long pulses require an excessive amount of ROM and computing time.

EXAMPLE 2:
. . . PORTA LENGTH . . . movlw movwf . . . movf addwf bsf bsf bsf bsf bsf bcf . . .

PULSE GENERATION OPTIMIZED TO DEVICE ARCHITECTURE (METHOD B)

equ equ

04h 0ch

4 LENGTH

; value for pulse length of 1 Tcy ;

LENGTH,W PCL,F PORTA,3 PORTA,3 PORTA,3 PORTA,3 PORTA,3 PORTA,3

; ; ; ; ; ; ; ;

This is an indirectly addressed relative jump start high pulse start high pulse start high pulse start high pulse start high pulse end high pulse 5 4 3 2 1 Tcy Tcy Tcy Tcy Tcy

DS00654A-page 2

© 1997 Microchip Technology Inc.

AN654
A third method (Method C) uses the Timer0 Interrupt. After the port is set, the timer is loaded with an adjusted number corresponding to the desired pulse length. If the timer overflows, the interrupt service routine starts and resets the port. The number, which has to be loaded, is defined by the following factors: · the counting direction of the timer (up/down) · the time between loading the timer and setting the port · the timer clock cycles needed to write to the timer · the computing time between timer overflow and executing the first instruction of the interrupt service routine · the computing time of the interrupt service routine until the port is reset · the desired pulse length · an additional correction number, which is related to the method of calculation Method C is able to generate very different pulse lengths without wasting any computing time. This method is specifically useful to generate long pulses. When the prescaler is not used, the available step size is 1 TCY. But this procedure is unsuitable for generating very short pulses, because every interrupt service routine needs a minimum of computing time for execution, which defines the minimum pulse length. Every PWM signal is a continuous succession of high and low pulses. The length of each pulse is defined by the desired duty cycle. Adding the length of a low and a high pulse gives us the PWM frequency, which should be kept constant. Method C will only work if each of the pulses are longer than the minimum computing time of the interrupt service routine. The interrupt service routine has to find the required pulse and set or reset the port. In addition to that the interrupt service routine has to calculate the number required by the timer and write that value to the timer. Whether it is feasible to use the timer interrupt depends on the desired pulse length and the known minimum computing time of the interrupt service routine. If Method C will not give the desired PWM signal, one has to fall back on methods A or B as described in Example 1 and Example 2. Each method has its advantages and disadvantages. Because all three methods are software based, it is possible to change methods at any time. Therefore the microcontroller can determine, even during the interrupt service routine, which method is best suited to generate the next pulse. This is the underlying concept for this application. The software PWM module uses methods B and C, which are shown in Table 1. If possible, both pulses are generated by the timer interrupt and two timer interrupts occur during one PWM period. If a very short pulse is needed, the interrupt service routine will generate this short pulse using method B. Afterwards it writes a new number corresponding to the next long pulse to the timer. Here, only one interrupt occurs during one PWM period. Knowing the computing time of each part of the interrupt service routine ensures that a change in methods will not visibly affect the PWM signal or vary the PWM frequency. Now, let us have a look at a simple application of this PWM software module. The circuit diagram is shown in Figure 1. Potentiometer R1 adjusts voltage from 0V to VDD. A PIC16C71 converts this voltage to an 8-bit value thirty times per second and sends it to PORTB. The eight LEDs connected to PORTB show the result. The PWM signal value, which is output on pin RA3, is shown by the intensity of the connected LED. If the value is equal to 0, a continuous low signal is generated. If the value is equal to 255 a continuous high signal is generated. The method of operation of the program is the same as the described procedure, which is shown by Table 1. The file register COUNTER is the file register of the system clock. It is incremented by the PWM module at the rising edge of the PWM signal. Bit 7 is toggled each 255 · 128 · TCY and is used as the system clock bit. The file register PWMDESIRED contains the desired PWM value in the range of 0 to 255. Register PWMMAX and PWMHELP are needed by the PWM module. They must not be modified by any other part of the program except during initialization. The constant PWMADJUSTVAL contains the previously described factors, which have to be taken into account while calculating the adjusted timer value. The constant PWMMAXVAL controls the maximum pulse length, which is generated by making use of the time needed to execute BSF/BCF instructions. If the interrupt service routine is modified, then both constants will also have to be modified. The comments in the code explain each instruction sequence.

CONCLUSION
This software based PWM module is able to generate a PWM frequency of up to 19.6 kHz with a variable duty cycle of 0% to 100%. The consumption of RAM is 3 bytes, the consumption of ROM less than 10% (nearly 100 instructions) and the consumption of computing time, in the worst case, amounts to 57/266 = 22.4%. The remaining computing time is more than the total available computing time of a 8051 microcontroller with a 12 MHz crystal.

© 1997 Microchip Technology Inc.

DS00654A-page 3

AN654
FIGURE 1:
VDD VDD

PIC16C71
0.1 µF 5 k 17 18 1 680

RA0 RA1 RA2 RA3 RA4 MCLR OSC1

VDD VSS RB0 RB1 RB2 RB3 RB4 RB5

14 5 6 7 8 9 10 11 12 13 0.1 µF 680 X 8

LED
VDD

2 3 4.7k 4

33 pF

16 4 MHZ 15

OSC2

33 pF

RB6 RB7

LEDs

TABLE 1: OPERATION MODES OF THE SOFTWARE PWM MODULE
Duty Cycle Range 0% .. 10% 10% . . 90% 90% . . 100% High Pulse Skipping (Method B) Timer Interrupt Timer Interrupt Low Pulse Timer Interrupt Timer Interrupt Skipping (Method B)

DS00654A-page 4

© 1997 Microchip Technology Inc.

AN654
APPENDIX A: PWM.ASM
MPASM 01.40 Released PWM.ASM 10-2-1996 10:29:19 PAGE 1

LOC OBJECT CODE VALUE

LINE SOURCE TEXT

00000088 000000A0 00000000 00000003 00000017 00000002 00000089 00000000

0000000C 0000000D 0000000E 0000000F 00000010 00000011 00000012

00000016

00001 00002 00003 00004 00005 00006 00007 00008 00009 00010 00011 00012 00013 00014 00015 00016 00017 00018 00019 00020 00021 00022 00001 00002 00142 00023 00024 00025 00026 00027 00028 00029 00030 00031 00032 00033 00034 00035 00036 00037 00038 00039 00040 00041 00042 00043 00044 00045 00046 00047 00048 00049 00050 00051 00052 00053 00054 00055 00056

;****************************************************************** ; Filename: PWM16CXX.ASM ;****************************************************************** ; Author: Ole Ropcke ; Company: private ; Revision: RevA1 ; Date: 01-4-96 ; Assembled using MPASM rev 01.21 ;****************************************************************** ; Include files: ; p16c71.inc ;****************************************************************** ; Required hardware: ;****************************************************************** ; What's changed: ; Date Description of Change ; xx-xx-xx ;****************************************************************** LIST P=16C71, R=DEC include P16c71.INC LIST ; P16C71.INC Std Hdr File, Version 1.00 Microchip Technology, Inc. LIST ;****************************************************************** ; Definitions ;****************************************************************** ; ; I/O, Interrupt and Option Definitions ; OPTIONVAL equ 10001000b ; portB no pull-up, tmr0 int. INTCONVAL equ 0a0h ; set GIE, T0IE ; port A: UINPBIT equ 00h ; analog input for desired PWM PWMOUTBIT equ 03h ; PWM output TRISAVAL equ 00010111b ; A3 output ADCON1VAL equ 2 ; A0,A1 analog; A2,A3 digital ADCON0VAL equ 10001001b ; fosc/32, channel 0 ; port B: TRISBVAL equ 0 ; LED outputs ;-----------------------------------------------------------------; Register Definitions ; STACKW equ 0ch ; stack to push/pop the W-register STACKS equ 0dh ; stack to push/pop the STATUS-reg COUNTER equ 0eh ; counter: input frequency ; f1 = crystalfreq. / 4 / 255 COUNTER2 equ 0fh ; counter2: input frequency ; f2 = f1 / 128 PWMDESIRED equ 10h ; desired PWM value 0..255 PWMMAX equ 11h ; register to support generating PWM ; You have to put PWMMAXVAL into it! PWMHELP equ 12h ; register to support generating PWM ; used as temp storage of PWMDESIRED ;-----------------------------------------------------------------; PWM-module-constant PWMADJUSTVAL equ .22

© 1997 Microchip Technology Inc.

DS00654A-page 5

AN654
00057 00058 00059 00060 00061 00062 00063 00064 00065 00066 00067 00068 00069 00070 00071 00072 00073 00074 00075 00076 00077 00078 00079 00080 00081 00082 00083 00084 00085 00086 00087 00088 00089 00090 00091 00092 00093 00094 00095 00096 00097 00098 00099 00100 00101 00102 00103 00104 00105 00106 00107 00108 00109 00110 00111 00112 00113 00114 00115 00116 00117 00118 00119 00120 00121 00122 ; correction number, defined by the following factors: ; time from timer interrupt to executing PC 004 + 3 cycles ; computing time from PC=004 to required edge +18 cycles ; lost timer cycles due to writing the timer + 2 cycles ; cal desired PWM value to timer loading value + 2 cycles ; time from timer loading to gen required edge - 1 cycle ; valid value for hardware (unknown diff to the data sheet) ; 3+18+0+2-1=22 ; valid value for PICSIM version 5.11 (error of PICSIM): ; 0+18+2+2-1=21 PWMMAXVAL equ .29 ; loading value for PWMMAX ; If n is the maximum length of a high pulse, which has to be ; generated by the skipping method, then is PWMMAXVAL = n+1. ; The max length of a low pulse using the skip method is n-1. ;================================================================== ORG 0 goto PowerOn ;--- PWM-Generator -----------------------------------------------ORG 04h ; timer interrupt btfsc TMR0,0 ; compensate comp time of 1/2 cyc goto PwmInt ; instruc when timer int occured PwmInt movwf STACKW ; copy W register... to "stack" swapf STACKW,F ; ...to "stack" swapf STATUS,W ; copy STATUS register... movwf STACKS ; ...to "stack" bcf INTCON,T0IF ; clear interrupt flag btfsc PORTA,PWMOUTBIT ; which edge is required? goto Lowpulse ; -> goto falling edge Highpulse comf PWMDESIRED,W ; get desired PWM value movwf PWMHELP ; store val for the foll low pulse addwf PWMMAX,F ; calc number of inst's to skip btfss STATUS,C ; which method to use? goto HighImpInt ; -> using interrupt HighImpShrt movf PWMMAX,W ; get number of inst's to skip addwf PCL,F ; skip n instructions bsf PORTA,PWMOUTBIT ; rising edge, 28 cycles hi pulse bsf PORTA,PWMOUTBIT ; 27 cycles bsf PORTA,PWMOUTBIT ; 26 cycles bsf PORTA,PWMOUTBIT ; 25 cycles bsf PORTA,PWMOUTBIT ; 24 cycles bsf PORTA,PWMOUTBIT ; 23 cycles bsf PORTA,PWMOUTBIT ; 22 cycles bsf PORTA,PWMOUTBIT ; 21 cycles bsf PORTA,PWMOUTBIT ; 20 cycles bsf PORTA,PWMOUTBIT ; 19 cycles bsf PORTA,PWMOUTBIT ; 18 cycles bsf PORTA,PWMOUTBIT ; 17 cycles bsf PORTA,PWMOUTBIT ; 16 cycles bsf PORTA,PWMOUTBIT ; 15 cycles bsf PORTA,PWMOUTBIT ; 14 cycles bsf PORTA,PWMOUTBIT ; 13 cycles bsf PORTA,PWMOUTBIT ; 12 cycles bsf PORTA,PWMOUTBIT ; 11 cycles bsf PORTA,PWMOUTBIT ; 10 cycles bsf PORTA,PWMOUTBIT ; 9 cycles bsf PORTA,PWMOUTBIT ; 8 cycles bsf PORTA,PWMOUTBIT ; 7 cycles bsf PORTA,PWMOUTBIT ; 6 cycles bsf PORTA,PWMOUTBIT ; 5 cycles bsf PORTA,PWMOUTBIT ; 4 cycles bsf PORTA,PWMOUTBIT ; 3 cycles bsf PORTA,PWMOUTBIT ; 2 cycles

0000001D

0000 0000 2870 0004 0004 0005 0006 0006 0007 0008 0009 000A 000B 000C 000D 000D 000E 000F 0010 0011 0012 0012 0013 0014 0015 0016 0017 0018 0019 001A 001B 001C 001D 001E 001F 0020 0021 0022 0023 0024 0025 0026 0027 0028 0029 002A 002B 002C 002D 002E

1801 2806 008C 0E8C 0E03 008D 110B 1985 2840 0910 0092 0791 1C03 2836 0811 0782 1585 1585 1585 1585 1585 1585 1585 1585 1585 1585 1585 1585 1585 1585 1585 1585 1585 1585 1585 1585 1585 1585 1585 1585 1585 1585 1585

DS00654A-page 6

© 1997 Microchip Technology Inc.

AN654
002F 1585 0030 1185 0031 0A8E 0032 0912 0033 3E1B 00123 00124 00125 00126 00127 00128 00129 00130 00131 00132 00133 00134 00135 00136 00137 00138 00139 00140 00141 00142 00143 00144 00145 00146 00147 00148 00149 00150 00151 00152 00153 00154 00155 00156 00157 00158 00159 00160 00161 00162 00163 00164 00165 00166 00167 00168 00169 00170 00171 00172 00173 00174 00175 00176 00177 00178 00179 00180 00181 00182 00183 00184 00185 00186 00187 00188 bsf bcf incf comf addlw PORTA,PWMOUTBIT PORTA,PWMOUTBIT COUNTER,F PWMHELP,W PWMADJUSTVAL+5 ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; 1 cycle fall edge;start of the following low pulse using the interrupt trigger COUNTER, cause there was a rising edge get required low pulse length calculate timer loading value Edge was generated 5 cycles before usual point of time. put value into timer low pulse using int is running high pulse using interrupt calculate timer loading value put value into timer generate rising edge trigger counter, because there was a rising edge "repair"... ...support register restore... ...STATUS register restore W register return to main program get required pulse length calc number of inst's to skip which method is to use? ->using interrupt get number of inst's to skip skip n instructions falling edge, 27 cycles low pulse 26 cycles 25 cycles 24 cycles 23 cycles 22 cycles 21 cycles 20 cycles 19 cycles 18 cycles 17 cycles 16 cycles 15 cycles 14 cycles 13 cycles 12 cycles 11 cycles 10 cycles 9 cycles 8 cycles 7 cycles 6 cycles 5 cycles 4 cycles 3 cycles 2 cycles 1 cycle rising edge; start of the next high pulse using the interrupt get desired PWM value store val for the next lo pulse calculate timer loading value Edge was gen'd 5 cycles before usual point of time.

0034 0035 0036 0036 0037 0038 0038 0039 003A 003B 003C 003D 003E 003F 0040 0040 0041 0042 0043 0044 0044 0045 0046 0047 0048 0049 004A 004B 004C 004D 004E 004F 0050 0051 0052 0053 0054 0055 0056 0057 0058 0059 005A 005B 005C 005D 005E 005F 0060 0061

0081 2869 3E16 0081 1585 0A8E 301C 0091 0E0D 0083 0E0C 0009 0912 0791 1C03 2867 0811 0782 1185 1185 1185 1185 1185 1185 1185 1185 1185 1185 1185 1185 1185 1185 1185 1185 1185 1185 1185 1185 1185 1185 1185 1185 1185 1185 1185 1585

movwf goto HighImpInt addlw movwf HighImpInt2 bsf incf movlw movwf swapf movwf swapf retfie Lowpulse comf addwf btfss goto LowImpShrt movf addwf bcf bcf bcf bcf bcf bcf bcf bcf bcf bcf bcf bcf bcf bcf bcf bcf bcf bcf bcf bcf bcf bcf bcf bcf bcf bcf bcf bsf comf movwf addlw

TMR0 LowImpInt2 PWMADJUSTVAL TMR0 PORTA,PWMOUTBIT COUNTER,F PWMMAXVAL-1 PWMMAX STACKS,W STATUS STACKW,W

PWMHELP,W PWMMAX,F STATUS,C LowImpInt PWMMAX,W PCL,F PORTA,PWMOUTBIT PORTA,PWMOUTBIT PORTA,PWMOUTBIT PORTA,PWMOUTBIT PORTA,PWMOUTBIT PORTA,PWMOUTBIT PORTA,PWMOUTBIT PORTA,PWMOUTBIT PORTA,PWMOUTBIT PORTA,PWMOUTBIT PORTA,PWMOUTBIT PORTA,PWMOUTBIT PORTA,PWMOUTBIT PORTA,PWMOUTBIT PORTA,PWMOUTBIT PORTA,PWMOUTBIT PORTA,PWMOUTBIT PORTA,PWMOUTBIT PORTA,PWMOUTBIT PORTA,PWMOUTBIT PORTA,PWMOUTBIT PORTA,PWMOUTBIT PORTA,PWMOUTBIT PORTA,PWMOUTBIT PORTA,PWMOUTBIT PORTA,PWMOUTBIT PORTA,PWMOUTBIT PORTA,PWMOUTBIT PWMDESIRED,W PWMHELP PWMADJUSTVAL+5

0062 0910 0063 0092 0064 3E1B

© 1997 Microchip Technology Inc.

DS00654A-page 7

AN654
0065 0066 0067 0067 0068 0069 0069 006A 006B 006C 006D 006E 006F 0070 0070 0181 0071 0190 0072 1185 0081 2838 3E16 0081 1185 301D 0091 0E0D 0083 0E0C 0009 00189 00190 00191 00192 00193 00194 00195 00196 00197 00198 00199 00200 00201 00202 00203 00204 00205 00206 00207 00208 00209 00210 00211 00212 00213 00214 00215 00216 00217 00218 00219 00220 00221 00222 00223 00224 00225 00226 00227 00228 00229 00230 00231 00232 00233 00234 00235 00236 00237 00238 00239 00240 00241 00242 00243 00244 00245 00246 00247 00248 00249 00250 00251 movwf TMR0 ; put value into timer goto HighImpInt2 ; high pulse using int is running LowImpInt ; low pulse using interrupt addlw PWMADJUSTVAL ; calculate timer loading value movwf TMR0 ; put value into timer LowImpInt2 bcf PORTA,PWMOUTBIT ; generate falling edge movlw PWMMAXVAL ; "repair" ... movwf PWMMAX ; ...support register swapf STACKS,W ; restore ... movwf STATUS ; ...STATUS register swapf STACKW,W ; restore W register retfie ; return to main program ;--- power on routine --------------------------------------------PowerOn ; configuration of the PWM module clrf TMR0 ; reset timer clrf PWMDESIRED ; reset value of PWM is 0 bcf PORTA,PWMOUTBIT ; reset PWM-port before port A is ; changed from input to output to ; suppress an uncontrolled spike movlw PWMMAXVAL ; set support register movwf PWMMAX ; ; configuration of the Pic bsf STATUS,RP0 ; register page 1 movlw TRISAVAL ; configurate ... movwf TRISA ; ...port A movlw TRISBVAL ; configurate ... movwf TRISB ; ...port B movlw ADCON1VAL ; set inputs of ... movwf ADCON1 ; ...adc movlw OPTIONVAL ; configurate ... movwf OPTION_REG ; ...Pic bcf STATUS,RP0 ; register page 0 movlw INTCONVAL ; configure interrupts and ... movwf INTCON ; ...enable interrupts ;--- main idle ---------------------------------------------------Idle clrwdt ; toggle watchdog btfss COUNTER,07h ; if MSB isn't set, ... goto Idle ; ...then waiting incf COUNTER2,F ; ...else toggle COUNTER2 ; If the crystal freq is 4 MHz, ; the toggle freq is nearly 30.6 Hz bcf COUNTER,07h ; reset MSB movlw ADCON0VAL ; set ... movwf ADCON0 ; ...ADC configuration WaitNoInt movf TMR0,W ; waiting until enough time sublw 0d0h ; for one conversion before start btfss STATUS,C ; of the next timer interrupt. goto WaitNoInt ; (Conv can be disturbed by ; an interrupt. bsf ADCON0,GO ; start ADC WaitAdc btfsc ADCON0,GO ; waiting until ADC ... goto WaitAdc ; ... is ready movf ADRES,W ; put result into W-reg and then movwf PWMDESIRED ; desired PWM-value into PWMDESIRED movwf PORTB ; show result at port B (LEDs) goto Idle ; ;-----------------------------------------------------------------END

0073 301D 0074 0091 0075 0076 0077 0078 0079 007A 007B 007C 007D 007E 007F 0080 0081 0081 0082 0083 0084 1683 3017 0085 3000 0086 3002 0088 3088 0081 1283 30A0 008B

0064 1F8E 2881 0A8F

0085 0086 0087 0088 0088 0089 008A 008B 008C 008D 008D 008E 008F 0090 0091 0092

138E 3089 0088 0801 3CD0 1C03 2888 1508 1908 288D 0809 0090 0086 2881

DS00654A-page 8

© 1997 Microchip Technology Inc.

AN654
MEMORY USAGE MAP ('X' = Used, '-' = Unused) 0000 : X---XXXXXXXXXXXX XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX 0040 : XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXX 0080 : XXXXXXXXXXXXXXXX XXX------------- ---------------- ---------------All other memory blocks unused. Program Memory Words Used: Program Memory Words Free: 144 880

Errors : Warnings : Messages :

0 0 reported, 0 reported,

0 suppressed 0 suppressed

© 1997 Microchip Technology Inc.

DS00654A-page 9

AN654
APPENDIX B: FLOWCHARTS
FIGURE B-1: MAIN ROUTINE

RESET

Initialize PWM Module

Initialize Device

Toggle WDT

No

COUNTER bit7 = 1?

Yes Calculate time until next interrupt

No

Enough time for conversion?

Yes

Start ADC

No

ADC ready?

Yes Copy ADRES to PWMDESIRED and to PORTB (LEDs)

DS00654A-page 10

© 1997 Microchip Technology Inc.

AN654
FIGURE B-2: PWM GENERATOR ROUTINE
PWMInt

Push W and STATUS register

low

Is PWM output high or low?

high

Calculate required pulse lengths and put low pulse length into PWMHELP

Get required low pulse length from PWMHELP

calculate number n of BSF instructions to skip

calculate number n of BSF instructions to skip

Yes

n < 29?

No

No

n < 29?

Yes

toggle system clock divider

toggle system clock divider

calculate required timer value for low pulse

calculate required timer value for high pulse

calculate required timer value for low pulse

calculate required timer value for high pulse

Generate timer value

Generate timer value

put value into timer

put value into timer

Pop W and STATUS registers

Pop W and STATUS registers

Return

Return

© 1997 Microchip Technology Inc.

DS00654A-page 11

AN654
NOTES:

DS00654A-page 12

© 1997 Microchip Technology Inc.

WORLDWIDE SALES & SERVICE
AMERICAS
Corporate Office
Microchip Technology Inc. 2355 West Chandler Blvd. Chandler, AZ 85224-6199 Tel: 602-786-7200 Fax: 602-786-7277 Technical Support: 602 786-7627 Web: http://www.microchip.com

ASIA/PACIFIC
Hong Kong
Microchip Asia Pacific RM 3801B, Tower Two Metroplaza 223 Hing Fong Road Kwai Fong, N.T., Hong Kong Tel: 852-2-401-1200 Fax: 852-2-401-3431

EUROPE
United Kingdom
Arizona Microchip Technology Ltd. Unit 6, The Courtyard Meadow Bank, Furlong Road Bourne End, Buckinghamshire SL8 5AJ Tel: 44-1628-851077 Fax: 44-1628-850259

France
Arizona Microchip Technology SARL Zone Industrielle de la Bonde 2 Rue du Buisson aux Fraises 91300 Massy, France Tel: 33-1-69-53-63-20 Fax: 33-1-69-30-90-79

Atlanta
Microchip Technology Inc. 500 Sugar Mill Road, Suite 200B Atlanta, GA 30350 Tel: 770-640-0034 Fax: 770-640-0307

India
Microchip Technology India No. 6, Legacy, Convent Road Bangalore 560 025, India Tel: 91-80-229-0061 Fax: 91-80-229-0062

Boston
Microchip Technology Inc. 5 Mount Royal Avenue Marlborough, MA 01752 Tel: 508-480-9990 Fax: 508-480-8575

Korea
Microchip Technology Korea 168-1, Youngbo Bldg. 3 Floor Samsung-Dong, Kangnam-Ku Seoul, Korea Tel: 82-2-554-7200 Fax: 82-2-558-5934

Germany
Arizona Microchip Technology GmbH Gustav-Heinemann-Ring 125 D-81739 Müchen, Germany Tel: 49-89-627-144 0 Fax: 49-89-627-144-44

Chicago
Microchip Technology Inc. 333 Pierce Road, Suite 180 Itasca, IL 60143 Tel: 630-285-0071 Fax: 630-285-0075

Italy
Arizona Microchip Technology SRL Centro Direzionale Colleone Palazzo Taurus 1 V. Le Colleoni 1 20041 Agrate Brianza Milan, Italy Tel: 39-39-6899939 Fax: 39-39-6899883

Shanghai
Microchip Technology RM 406 Shanghai Golden Bridge Bldg. 2077 Yan'an Road West, Hongiao District Shanghai, PRC 200335 Tel: 86-21-6275-5700 Fax: 86 21-6275-5060

Dallas
Microchip Technology Inc. 14651 Dallas Parkway, Suite 816 Dallas, TX 75240-8809 Tel: 972-991-7177 Fax: 972-991-8588

Singapore
Microchip Technology Taiwan Singapore Branch 200 Middle Road #10-03 Prime Centre Singapore 188980 Tel: 65-334-8870 Fax: 65-334-8850

JAPAN
Microchip Technology Intl. Inc. Benex S-1 6F 3-18-20, Shin Yokohama Kohoku-Ku, Yokohama Kanagawa 222 Japan Tel: 81-4-5471- 6166 Fax: 81-4-5471-6122 5/8/97

Dayton
Microchip Technology Inc. Two Prestige Place, Suite 150 Miamisburg, OH 45342 Tel: 937-291-1654 Fax: 937-291-9175

Los Angeles
Microchip Technology Inc. 18201 Von Karman, Suite 1090 Irvine, CA 92612 Tel: 714-263-1888 Fax: 714-263-1338

Taiwan, R.O.C
Microchip Technology Taiwan 10F-1C 207 Tung Hua North Road Taipei, Taiwan, ROC Tel: 886 2-717-7175 Fax: 886-2-545-0139

New York
Microchip Technology Inc. 150 Motor Parkway, Suite 416 Hauppauge, NY 11788 Tel: 516-273-5305 Fax: 516-273-5335

San Jose
Microchip Technology Inc. 2107 North First Street, Suite 590 San Jose, CA 95131 Tel: 408-436-7950 Fax: 408-436-7955

Toronto
Microchip Technology Inc. 5925 Airport Road, Suite 200 Mississauga, Ontario L4V 1W1, Canada Tel: 905-405-6279 Fax: 905-405-6253

All rights reserved. © 1997, Microchip Technology Incorporated, USA. 5/97

M

Information contained in this publication regarding device applications and the like is intended for suggestion only and may be superseded by updates. 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, under any intellectual 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.

© 1997 Microchip Technology Inc.