Professional Documents
Culture Documents
METHODS
Author: Ole Röpcke
Consultant, Europe Before the solution is revealed, we must first examine
the various software methods used to generate vari-
able length pulses.
INTRODUCTION In the following explanations, the unit of time will be the
The low cost, high performance features of a length of an Instruction Cycle (TCY). We will use TCY
PIC16CXXX microcontroller make it a suitable device because one instruction (if the program counter is not
for automatic control technology applications. Some- changed) executes in one TCY and Timer0 (without
times, an additional PWM output is needed. For some prescaling) increments every TCY. This provides us with
devices, such as the PIC16C71, the addition of a soft- a simple method to control a potentially complex timing
ware PWM adds the missing element. It is possible to sequence.
use Timer0 (which also provides the system clock) and Making use of the time needed to execute an instruc-
its corresponding interrupt to generate a PWM output tion provides a very simple method of generating an
with a duty cycle that can vary from nearly 10% to 90%. pulse. Example 1 (Method A) shows an instruction
However, some applications require a greater duty sequence, which will generate a high pulse of 99 TCY
cycle range. on pin 3 of PORTA. The pulse length is controlled by the
This application note provides a software solution for a value of register LENGTH in steps of 3 TCY. This is the
more accurate and flexible PWM output, which is char- computing time needed by one program loop.
acterized by the following: The drawbacks of this method are an excessive use of
1. PWM frequency up to 19.6 kHz (20 MHz crys- computing time and a poor PWM resolution.
tal).
2. Variable duty cycle from 0% to 100%.
3. 8-bit resolution.
4. PWM step size of 1 TCY (Instruction Cycle
Time).
5. Simultaneous generation of a system time clock.
EXAMPLE 1: USING TCY AS A TIME BASE FOR PULSE GENERATION (METHOD A)
.
.
.
PORTA equ 04h
LENGTH equ 0ch
COUNTER equ 0dh
.
.
.
movlw 34 ; value for pulse length of 99 Tcy
movwf LENGTH ;
.
.
.
movf LENGTH,W ; write value to the loop counter
movwf COUNTER ;
bsf PORTA,3 ; start high pulse
Loop
decfsz COUNTER,F ; counting pulse length
goto Loop ;
bcf PORTA,3 ; end high pulse
.
.
.
VDD VDD
PIC16C71
5 kΩ
17 14
0.1 µF RA0 VDD
0.1 µF
18 5
RA1 VSS
680 X 8
1 6
RA2 RB0
680 LED
2 7
RA3 RB1
VDD
3 8
RA4 RB2
4.7k
4 9
MCLR RB3
33 pF
16 10
OSC1 RB4
4 MHZ 11
RB5
15
OSC2 12
33 pF RB6
13
RB7
LEDs
00001 ;******************************************************************
00002 ; Filename: PWM16CXX.ASM
00003 ;******************************************************************
00004 ; Author: Ole Ropcke
00005 ; Company: private
00006 ; Revision: RevA1
00007 ; Date: 01-4-96
00008 ; Assembled using MPASM rev 01.21
00009 ;******************************************************************
00010 ; Include files:
00011 ; p16c71.inc
00012 ;******************************************************************
00013 ; Required hardware:
00014 ;******************************************************************
00015 ; What's changed:
00016 ; Date Description of Change
00017 ; xx-xx-xx
00018 ;******************************************************************
00019
00020 LIST P=16C71, R=DEC
00021
00022 include P16c71.INC
00001 LIST
00002 ; P16C71.INC Std Hdr File, Version 1.00 Microchip Technology, Inc.
00142 LIST
00023
00024 ;******************************************************************
00025 ; Definitions
00026 ;******************************************************************
00027 ;
00028 ; I/O, Interrupt and Option Definitions
00029 ;
00000088 00030 OPTIONVAL equ 10001000b ; portB no pull-up, tmr0 int.
000000A0 00031 INTCONVAL equ 0a0h ; set GIE, T0IE
00032 ; port A:
00000000 00033 UINPBIT equ 00h ; analog input for desired PWM
00000003 00034 PWMOUTBIT equ 03h ; PWM output
00000017 00035 TRISAVAL equ 00010111b ; A3 output
00000002 00036 ADCON1VAL equ 2 ; A0,A1 analog; A2,A3 digital
00000089 00037 ADCON0VAL equ 10001001b ; fosc/32, channel 0
00038 ; port B:
00000000 00039 TRISBVAL equ 0 ; LED outputs
00040 ;------------------------------------------------------------------
00041 ; Register Definitions
00042 ;
0000000C 00043 STACKW equ 0ch ; stack to push/pop the W-register
0000000D 00044 STACKS equ 0dh ; stack to push/pop the STATUS-reg
0000000E 00045 COUNTER equ 0eh ; counter: input frequency
00046 ; f1 = crystalfreq. / 4 / 255
0000000F 00047 COUNTER2 equ 0fh ; counter2: input frequency
00048 ; f2 = f1 / 128
00000010 00049 PWMDESIRED equ 10h ; desired PWM value 0..255
00000011 00050 PWMMAX equ 11h ; register to support generating PWM
00051 ; You have to put PWMMAXVAL into it!
00000012 00052 PWMHELP equ 12h ; register to support generating PWM
00053 ; used as temp storage of PWMDESIRED
00054 ;------------------------------------------------------------------
00055 ; PWM-module-constant
00000016 00056 PWMADJUSTVAL equ .22
Errors : 0
Warnings : 0 reported, 0 suppressed
Messages : 0 reported, 0 suppressed
RESET
Initialize Device
Toggle WDT
No
COUNTER
bit7 = 1?
Yes
No Enough
time for
conversion?
Yes
Start ADC
No
ADC ready?
Yes
PWMInt
Is
low PWM output high
high or low?
calculate number n of BSF instructions to skip calculate number n of BSF instructions to skip
Yes No No Yes
n < 29? n < 29?
calculate required timer calculate required timer calculate required timer calculate required timer
value for low pulse value for high pulse value for low pulse value for high pulse
Return Return
M
Tel: 905-405-6279 Fax: 905-405-6253
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.