You could simplify this a great deal if your PIC has Timer1 available, and Compare,
Capture, PWM .. by just using the Compare peripheral.
Here's an example:
Code:
DEFINE OSC 4
Match VAR WORD
Match = 20000 ' 20,000 * 1uS = 20mS until compare match
CCPR1H = Match.HighByte' Load compare register high
CCPR1L = Match.LowByte ' Load compare register low
CCP1CON = %00001011 ' Compare mode, auto reset Timer1
'/ TRIS & Port Setup
PORTB.0=1
TRISB = $FE ' PORTB.0 output, rest inputs
'/ disable interrupts
INTCON.7 = 0 ' Disable global ints
INTCON.6 = 0 ' Disable peripheral ints
PIE1.0 = 0 ' Disable Timer1 int
PIE1.2 = 0 ' Disable CCP1 int
T1CON = %00000000 ' Timer1 off, internal clock, 1:1 prescale
TMR1H = 0 ' Clear Timer1 high
TMR1L = 0 ' Clear Timer1 low
T1CON.0 = 1 ' Turn on Timer1
LOOPS: ' NOTE: PIR1.2 is the CCP1IF compare interrupt flag bit.
' this bit is set when Timer1 reaches the value in
' CCPR1L & CCPR1H
IF PIR1.2 = 1 THEN ' If CCP1 to TMR1 compare match occured
PORTB = PORTB ^ 1 ' Toggle PORTB.0
PIR1.2 = 0 ' Clear compare interrupt flag bit
ENDIF
GOTO LOOPS
END
Just 1 single interrupt could take care of your 20mS timing VS a bunch to accumulate the
total 20mS time period.
A single interrupt every 20mS & no reloading anything. Timer1 is automatically cleared on
match, and the CCPR1L/CCPR1H registers contain the value loaded into them until you
change it.
Bookmarks