Please help me understand this code


Closed Thread
Results 1 to 15 of 15

Hybrid View

  1. #1
    Join Date
    May 2009
    Location
    Montreal, QC, Canada
    Posts
    118

    Default Please help me understand this code

    Hi,
    From the code bellow, I am expecting to have the DEBUG line running only when the variable "ReadFlag" is set and it does, so far so good.
    But once it ran, I clear ReadFlag and it should become set again ONLY if I=10 and this should happen only after the Interrupt has ran 10 times right? Well it is not happening.

    In my DEBUG line "I" shows up as 0,2,4,5 randomly
    I am not using variable "I" anywhere else other than:
    Code:
    I VAR BYTE
    I = 0
    at the top of my program.


    Can someone help me understand this?

    Code:
    Goto main 
        
    Main:
        DO while readflag = 1 
            debug dec Fvalue," i=",dec i," R=",dec readflag,10,13  ; Display on terminal if ReadFlag = 1
            readflag = 0            ; do not update until the next 10 readings
            Fvalue = 0
        LOOP
    goto main
    
    '---[INT - interrupt handler]------Signal found at RB0, take a reading
    SignalFound:
        T1CON.0 = 0                             ; Stop timer1
        i = i + 1                               
        FValue = TMR1H * 256 + TMR1L + fvalue   ; Add 10 readings to FValue
        if i = 10 then
            fvalue = fvalue / 10                ; get the average of one reading
            i = 0                               ; Reset counter
            ReadFlag = 1                        ; FValue is ready to read 
        endif        
        TMR1H = 21                              ; reset timer1 to 60mS
        TMR1L = 167
        T1CON.0 = 1                             ; Start timer1
    @ INT_RETURN
    Thanks

    Mike

  2. #2
    Join Date
    May 2009
    Location
    Montreal, QC, Canada
    Posts
    118


    Did you find this post helpful? Yes | No

    Default

    Getting even more confusing, I tried:
    Code:
    DO while I = 0
    And still the DEBUG line shows up even when: I > 0

  3. #3
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    In my DEBUG line "I" shows up as 0,2,4,5 randomly
    You probably have several timer interrupts while it's inside your DO LOOP, so I ends up with unexpected values.

    Example: With i = i + 1 being first in your timer interupt, it's changing the value of i on every interrupt, and sometimes even during the process of PBP trying to output everything with DEBUG.

    When DEBUG actually starts, your interrupt is still changing I before PBP outputs it.
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  4. #4
    Join Date
    Nov 2005
    Location
    Bombay, India
    Posts
    966


    Did you find this post helpful? Yes | No

    Default

    Try this. Changes are in Bold

    Code:
    '---[INT - interrupt handler]------Signal found at RB0, take a reading
    SignalFound:
        T1CON.0 = 0                             ; Stop timer1
        i = i + 1                               
        FValue = TMR1H * 256 + TMR1L + fvalue   ; Add 10 readings to FValue
        if i >= 10 then                        ' COMPARISON CHANGED TO >=
            fvalue = fvalue / i                ; get the average of one reading CHANGED HERE
            i = 0                               ; Reset counter
            ReadFlag = 1                        ; FValue is ready to read 
        endif        
        TMR1H = 21                              ; reset timer1 to 60mS
        TMR1L = 167
        T1CON.0 = 1                             ; Start timer1
    @ INT_RETURN
    Last edited by Jerson; - 29th April 2010 at 02:26.

  5. #5
    Join Date
    May 2009
    Location
    Montreal, QC, Canada
    Posts
    118


    Did you find this post helpful? Yes | No

    Default

    Hi Bruce and Jersen,

    That is really fast !

    Jerson,
    I was able to observe the proper values by moving the DEBUG line inside the INT handler.

    Is there a more efficient way to observe variables values within a program?
    It seams like LCDOUT and DEBUG are taking too long to operate and are giving false values.

    Thank you

    Mike

  6. #6
    Join Date
    May 2009
    Location
    Montreal, QC, Canada
    Posts
    118


    Did you find this post helpful? Yes | No

    Default

    Bruce,

    I came across this link you wrote Entitled "Using MicroCode Studio ICD
    With The PIC16F628 Microcontroller" http://www.rentron.com/PicBasic/MCS_X3.htm
    Would this enable me to view each step of a program as well as where the PIC timer is at while executing a specific line of code?

    Mike

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