DS18B20 interfers with Timer 2 interrupts


Results 1 to 8 of 8

Threaded View

  1. #3
    Join Date
    Feb 2009
    Posts
    36


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Acetronics View Post
    Hi, John

    Could we have a piece of code :

    just DS 1820 and ISR parts ...

    Thanks

    Did you remember DS needs a certain amount of time for its conversion AND OnInterrupt waits for the current command to be completed ???

    Alain
    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
    Last edited by John_Mac; - 20th February 2009 at 17:01. Reason: added comments on interrupt timing

Similar Threads

  1. PICkit 2 and RBC interrupts
    By jderson in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 4th April 2009, 02:33
  2. Clock using Instant Interrupts
    By PICpocket in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 16th February 2009, 21:43
  3. help: TMR0 interrupts disabling PORTAchange interrupts???
    By xnihilo in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 19th August 2008, 15:10
  4. Totally Baffled with Elapsed Timer
    By CocaColaKid in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 11th June 2008, 21:01
  5. Microcontroller with 2 way paging application problem
    By oneohthree in forum mel PIC BASIC Pro
    Replies: 30
    Last Post: - 20th April 2007, 17:27

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts