Alain...Thanks for the reply. Below is the code for the ISR ande DS. Again both work fine, but running together, timer runs faster. Throughout the code I have tried to keep the instructions as short as possible since pbp has to finish with the instruction line before it can service the interrupt. for instance, instead of PAUSE 2000, I loop 2000 times on a PAUSE 1. But the DEBUG, OWIN and OWOUT may take a long time to execute and could be missing interrupts. But I would think this would make the clock run slower.
I calculate that I should get a Timer2 interrupt every 4 msec (prescale 1:1, postscale 1:16, 4 MHz and PR2=255). this means I would loop about 250 interrupts to get 1 sec. I find I have to loop about 1960 times. So this tells me I am missing interrupts. My guess is that some of these I/O statements may be taking longer than 4 msec and I miss an interrupt. But why I get more with the DS readout is a mystery right now. I will try to time some of the statements.
Anyway here are the code snippets for the ISR and DS:
DISABLE
Self_Test_Counter:
' Interupt Service Routine for Self Test timer interupt
' want to do a diagnostic every hour, but do it every minute
' for debugging prposes
' SecLoop is a constant defined earlier as 1960. when I calculate
' what it should be, I get a number more like 245. So this could be a clue!
' check that this was a Timer2 interupt
if PIR1.1=0 then ExitInterrupt
Ticks = Ticks + 1 ' Count pieces of seconds
If Ticks < SecLoop Then ExitInterrupt
' One second elasped - update time
Ticks = 0
Second = Second + 1
If Second >= 60 Then
Second = 0
' temp set flag every minute for testing
DoSelfTest = 1 ' Set update flag
Minute = Minute + 1
If Minute >= 60 Then
Minute = 0
DoSelfTest = 1 ' Set update flag
endif
Endif
PIR1.1 = 0 ' Reset timer2 interrupt flag
ExitInterrupt:
Resume
ENABLE
DS readout....
' One-wire temperature for DS18B20
Get_Temp:
OWOut Temp_DQ, 1, [$CC, $44] ' Start temperature conversion
waitloop:
OWIn Temp_DQ, 4, [Busy] ' Check for still busy converting
If Busy = 0 Then waitloop
OWOut Temp_DQ, 1, [$CC, $BE] ' Read the temperature
OWIn Temp_DQ, 2, [temperature.LOWBYTE, temperature.HIGHBYTE]
gosub Convert_Temp
return
Convert_Temp:
Sign = "+"
IF Sign_Bit = 1 THEN Sign="-"
' must disable interupts between dummy multiply and DIV32
DISABLE
Dummy = 625 * temperature ' Multiply to load internal registers with 32-bit value
TempC = DIV32 10 ' Use Div32 value to calculate precise deg C
Dummy = 1125 * temperature
TempF = DIV32 100
ENABLE
IF TempF >6795 THEN ' Over 99.5 deg F..?
TempF = TempF + 3200
debug "F = ",Sign,DEC TempF DIG 4,DEC TempF DIG 3,DEC TempF DIG 2,".",DEC2 TempF, 13
ELSE
TempF = TempF + 3200
debug "F = ",Sign,DEC TempF DIG 3,DEC TempF DIG 2,".",DEC2 TempF,13
ENDIF
TempC = (temperature & $0FF0) >> 4 ' Mask middle 8-bits, shift into lower byte
Float = ((temperature.Lowbyte & $0F) * 625) ' Lower 4-bits of result * 625
debug "C = ",Sign,DEC TempC,".",DEC Float,13
RETURN
Bookmarks