Troubled with Instant Ints and TMR2


Closed Thread
Results 1 to 28 of 28

Hybrid View

  1. #1
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    >> So Darrel if i made this with just an ASM interrupt i would be able to get my 125uS since i wouldnt need the Reenter Context Saving...
    Correct.

    >> A bit Array of 120 samples in Assembler .... mmm..... mmmmm....
    In ASM, you would just shift the bits into a byte var. After 8 bit's move to the next byte. After 15 bytes you're done.

    >> By the way would you consider (in my case) the pauseus 125 Take Sample option?

    YES!
    Your program just sits in a loop anyways, waiting for the sample to finish.
    No need to use interrupts, if it's not doing anything in the first place.

    Or, you can still use the timer. Just poll the TMR2IF Flag.
    <br>
    DT

  2. #2


    Did you find this post helpful? Yes | No

    Default Thanks for your advise Darrel

    I will keep working on the interrupt option... This is supposed to detect DTMF (we talk about it before) so for it to work in the background of anything else is the best choice.

    Everything is so complicated in ASM!.. thanks god there is picbasic.

  3. #3
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Here's a quick untested stab at an ASM handler.

    Change the Handler type to ASM
    Code:
    Samples      VAR BYTE[15] BANK0 SYSTEM
    SamplePTR    VAR BYTE     BANK0 SYSTEM
    SamplesAddr  CON EXT
    @SamplesAddr = Samples
    BitCount     VAR BYTE     BANK0 SYSTEM
    ByteCount    VAR BYTE     BANK0 SYSTEM
    FlagSample   VAR BIT
    TMR2ON       VAR T2CON.2
    
    ;----[Start getting the 120 samples]------------------------------------------
    GetSamples:
        FlagSample = 0
        SamplePTR = SamplesAddr
        ByteCount = 15
        BitCount  = 8
        PR2 = 125 : TMR2 = 0 : TMR2ON = 1
    return
        
    ;----[TMR2 Handler]-----------------------------------------------------------
    ASM
    Sample
        MOVE?BB  SamplePTR, FSR      ; Load FSR with pointer to array
        MOVE?TT  CMCON,7, STATUS,C   ; Put sample in carry flag
        rrf      INDF, F             ; Shift in the sample bit
        decfsz   BitCount, F         ; Are all 8 bits in ?
        goto     Int_Done            ;   NO, done for now
        MOVE?CB  8, BitCount         ;   Yes, reset bitcount
        incf     SamplePTR, F        ;     Point to next byte
        decfsz   ByteCount, F        ; Are all 15 bytes done ?
        goto     Int_Done            ;   NO, done for now
        MOVE?CT  1, _FlagSample      ;   Yes, Indicate - Samples Complete
        MOVE?CT  0, _TMR2ON          ; stop the timer
    Int_Done
        INT_RETURN
    ENDASM
    DT

  4. #4


    Did you find this post helpful? Yes | No

    Smile I Will be testing it for the next couple of hours

    Your wish to help is INFITE and appreciated Darrel.

    Thanks Again
    Last edited by Josuetas; - 5th October 2007 at 15:32.

  5. #5
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Ha!

    You are using ....

    LCD_AnyPin
    Instant Interrupts
    Elapsed Timer


    and you showed your "Colors" when posting.

    How could I possibly Not Help??
    <br>
    DT

  6. #6


    Did you find this post helpful? Yes | No

    Default Hey!!!! your right

    Hey!!!!

    Wait a second... your right!!

    Soon iŽll end up looking like a mouse with a red Book in my hands!!

  7. #7


    Did you find this post helpful? Yes | No

    Exclamation Ok it took me more than a couple of hours

    Hi Darrel

    I made some tests on the ASM handler for my TMR2 interrupt, it works ALMOST fine.

    Before posting i double checked this.

    First The timer is interrupting correctly every 0.000125uS and taking the samples, how did i checked this? well first i used HSEROUT to my serial port of the whole sample and i got something. At the Same time i needed to check the timing, so i modified the handler to add a toggle of a pin in the uC after each Sample, then i measured it with the oscilloscope and got the correct frequency.

    Code:
    <html>
    <head></head>
    <body><!--StartFragment--><pre><code><font color="#000000"><b>CLEAR
    
    DEFINE OSC </b>4
    <b>DEFINE HSER_RCSTA </b>90<b>h       </b><font color="#000080"><i>' Set transmit register to transmitter enabled
    </i></font><b>DEFINE HSER_TXSTA </b>24<b>h       </b><font color="#000080"><i>' Set baud rate
    </i></font><b>DEFINE HSER_BAUD </b>9600
    <b>DEFINE HSER_CLROERR </b>1
    
    
    <b>LCD_DB4         VAR PORTA</b>.6 <font color="#000080"><i>;************LCD_AnyPin declarations
    </i></font><b>LCD_DB5         VAR PORTB</b>.6
    <b>LCD_DB6         VAR PORTB</b>.5
    <b>LCD_DB7         VAR PORTB</b>.4
    <b>LCD_RS          VAR PORTA</b>.0
    <b>LCD_E           VAR PORTA</b>.7
    <b>LCD_Lines       CON </b>2    
    <b>LCD_DATAUS      CON </b>50   
    <b>LCD_COMMANDUS   CON </b>2000 
    
    
    <b>INCLUDE </b><font color="#FF0000">&quot;LCD_AnyPin.pbp&quot; 
    </font><b>INCLUDE </b><font color="#FF0000">&quot;DT_INTS-14628.bas&quot;
    </font><b>INCLUDE </b><font color="#FF0000">&quot;ReEnterPBP.bas&quot;
    </font><b>INCLUDE </b><font color="#FF0000">&quot;Elapsed_INT.bas&quot;
    
    
    </font><b>PinActivar      VAR porta</b>.3
    <b>PinDescuelgue   VAR porta</b>.5
    <b>PinPolaridad    VAR porta</b>.4
    <b>InZCD           VAR porta</b>.2
    <b>PinDTMF         VAR portb</b>.7
    <b>PinOffHook      VAR portb</b>.0
    
    
    <b>Samples      VAR BYTE</b>[15] <b>BANK0 SYSTEM
    SamplePTR    VAR BYTE     BANK0 SYSTEM
    SamplesAddr  CON EXT
    </b><font color="#008000">@SamplesAddr = Samples
    </font><b>BitCount     VAR BYTE     BANK0 SYSTEM
    ByteCount    VAR BYTE     BANK0 SYSTEM
    FlagSample   VAR BIT
    TMR2ON       VAR T2CON</b>.2
    
    
    
    <b>NumSamples      VAR BYTE
    ComIn           VAR BYTE
    
    x               VAR BYTE            </b><font color="#000080"><i>'Counter Byte
    
    </i></font><b>ASM
    </b><font color="#008000">INT_LIST  macro    </font><font color="#000080"><i>; IntSource,        Label,  Type, ResetFlag?
            ;INT_Handler     RX_INT,      _Hablar,   PBP,  yes
            </i></font><font color="#008000">INT_Handler   TMR2_INT,       Sample,   ASM,  yes
            </font><font color="#000080"><i>;INT_Handler   TMR1_INT,  _ClockCount,   PBP,  yes
        </i></font><font color="#008000">endm
        INT_CREATE               </font><font color="#000080"><i>; Creates the interrupt processor
    </i></font><b>ENDASM
    </b><font color="#000080"><i>;@    INT_ENABLE  RX_INT     ; Enable Timer 1 Interrupts  
    ;@    INT_ENABLE  TMR1_INT     ; Enable Timer 1 Interrupts  
    </i></font><font color="#008000">@    INT_ENABLE  TMR2_INT     </font><font color="#000080"><i>; Enable Timer 1 Interrupts  
    
    </i></font><b>Config</b>:
            <b>trisb</b>.0=0
            <b>trisa</b>.5=1
            <b>trisa</b>.4=1
            <b>trisa</b>.2=1
            <b>trisb</b>.7=0
            <b>trisb</b>.3=0
            <b>trisa</b>.3=0         
            <b>CMCON </b>= 5
            <b>CMCON</b>.5 = 1         <font color="#000080"><i>'INVERTIDO        
            </i></font><b>VRCON </b>= %11101100
            <b>OPTION_REG </b>= %00000000 
            <b>PAUSE </b>500 : <b>LCDOUT </b>$FE,1 : <b>PAUSE </b>250
            <b>HSEROUT </b>[<font color="#FF0000">&quot;7&quot;</font>]
            <font color="#000080"><i>'gosub ResetTime
            'gosub StartTimer
            ;@    INT_DISABLE  TMR1_INT     ; Enable Timer 1 Interrupts                  
            ;@    INT_DISABLE  RX_INT     ; Enable Timer 1 Interrupts                  
            'HPWM 1,127,420
            </i></font><b>LOW PinDTMF
            HIGH PinOffHook
    main1</b>:
            <b>GOSUB GetSamples
            WHILE FlagSample </b>= 0
            <b>WEND
    GOTO main1
    
    
    MAIN</b>:
            <b>HIGH PinActivar
            GOSUB GetSamples
            WHILE FlagSample </b>= 0 
            <b>WEND
            HSEROUT </b>[<font color="#FF0000">&quot;:&quot;</font>,10,13]
            <b>FOR x </b>= 0 <b>TO </b>14
                  <b>HSEROUT </b>[<b>BIN8 Samples</b>[<b>x</b>],10,13]
            <b>NEXT x
            PAUSE </b>5000
    <b>GOTO MAIN
    
    
    </b><font color="#000080"><i>;----[Start getting the 120 samples]------------------------------------------
    </i></font><b>GetSamples</b>:
        <b>FlagSample </b>= 0
        <b>SamplePTR </b>= <b>SamplesAddr
        ByteCount </b>= 15
        <b>BitCount  </b>= 8
        <b>PR2 </b>= 125 : <b>TMR2 </b>= 0 : <b>TMR2ON </b>= 1
    <b>RETURN
        
    </b><font color="#000080"><i>;----[TMR2 Handler]-----------------------------------------------------------
    'ASM
    'Sample
    '    MOVE?BB  SamplePTR, FSR      ; Load FSR with pointer to array
    '    MOVE?TT  CMCON,7, STATUS,C   ; Put sample in carry flag
    '    rrf      INDF, F             ; Shift in the sample bit
    '    decfsz   BitCount, F         ; Are all 8 bits in ?
    '    goto     Int_Done            ;   NO, done for now
    '    MOVE?CB  8, BitCount         ;   Yes, reset bitcount
    '    incf     SamplePTR, F        ;     Point to next byte
    '    decfsz   ByteCount, F        ; Are all 15 bytes done ?
    '    goto     Int_Done            ;   NO, done for now
    '    MOVE?CT  1, _FlagSample      ;   Yes, Indicate - Samples Complete
    '    MOVE?CT  0, _TMR2ON          ; stop the timer
    'Int_Done
    '    btfsc    PORTA, 3            ; Toggle porta.3 to check Interruption on OScilloscope
    '    goto Int_DNZ                 ; this really looks stupid.. longer than the rest of the
    '    goto Int_DZ                  ; routine :P
    '    Int_DZ
    '    bsf      PORTA, 3
    '    goto Int_Done1
    '    Int_DNZ
    '    bcf      PORTA, 3
    '    Int_Done1
    '    INT_RETURN
    'ENDASM
    
    </i></font><b>ASM
    </b><font color="#008000">Sample
        MOVE?BB  SamplePTR, FSR      </font><font color="#000080"><i>; Load FSR with pointer to array
        </i></font><font color="#008000">MOVE?TT  CMCON,7, STATUS,C   </font><font color="#000080"><i>; Put sample in carry flag
        </i></font><font color="#008000">rrf      INDF, F             </font><font color="#000080"><i>; Shift in the sample bit
        </i></font><font color="#008000">decfsz   BitCount, F         </font><font color="#000080"><i>; Are all 8 bits in ?
        </i></font><font color="#008000">goto     Int_Done            </font><font color="#000080"><i>;   NO, done for now
        </i></font><font color="#008000">MOVE?CB  8, BitCount         </font><font color="#000080"><i>;   Yes, reset bitcount
        </i></font><font color="#008000">incf     SamplePTR, F        </font><font color="#000080"><i>;     Point to next byte
        </i></font><font color="#008000">decfsz   ByteCount, F        </font><font color="#000080"><i>; Are all 15 bytes done ?
        </i></font><font color="#008000">goto     Int_Done            </font><font color="#000080"><i>;   NO, done for now
        </i></font><font color="#008000">MOVE?CT  1, _FlagSample      </font><font color="#000080"><i>;   Yes, Indicate - Samples Complete
        </i></font><font color="#008000">MOVE?CT  0, _TMR2ON          </font><font color="#000080"><i>; stop the timer
    </i></font><font color="#008000">Int_Done
        btfsc    CMCON,7            </font><font color="#000080"><i>; Toggle porta.3 to check Interruption on OScilloscope
        </i></font><font color="#008000">goto Int_DNZ                 </font><font color="#000080"><i>; this really looks stupid.. longer than the rest of the
        </i></font><font color="#008000">goto Int_DZ                  </font><font color="#000080"><i>; routine :P
        </i></font><font color="#008000">Int_DZ
        bcf      PORTA, 3
        goto Int_Done1
        Int_DNZ
        bsf      PORTA, 3
        Int_Done1
        INT_RETURN
    </font><b>ENDASM
            
    Hablar</b>:             <font color="#000080"><i>' FUNCION DE INTERRUPCION DEL PUERTO SERIE, RECIBE CORRECTAMENTE TODOS LOS DATOS
        </i></font><b>WHILE PIR1</b>.5  <font color="#000080"><i>' If RCIF is set, then read RCREG until it's clear
                </i></font><b>ComIn </b>= <b>RCREG
        WEND
        HSEROUT </b>[<b>ComIn</b>]
    <font color="#008000">@ INT_RETURN
    
    </font></code></pre><!--EndFragment--></body>
    </html>
    Then here is what i got from my Serial Port


    11111110
    00000011
    11111000
    00011111
    11000000
    01111111
    00000000
    11111110
    00000111
    11110000
    00011111
    10000000
    11111111
    00000000
    11111100


    My first impression GREAT!! but nono. Why? because The fecuency sampled is 420Hz, considering 0.000125 samples per second the period of the signal should be (1/420Hz)/0.000125uS = 19 samples aproximately. From what i am getting you can see that the period is around 15 samples, which means 533,3 Hz.

    So i Though maybe the comparator is not working correctly...but... Another modification to the handler to make porta.3 follow the comparator output showed this picture. Its a DTMF code not the 420Hz i used as sample.

    <img src="http://www.picbasic.co.uk/forum/attachment.php?attachmentid=2071&amp;stc=1&amp;d=1 191624484" width="160" height="98" />

    So the Comp is working fine, and the sampling is being done a the correct frequency.
    Here i took the 420Hz Comparator Output.

    <img src="http://www.picbasic.co.uk/forum/attachment.php?attachmentid=2072&stc=1&d=119162493 3" width="160" height="98" />

    Could it be... this is my last one... that something is wrong with the Handler... maybe between Byte changes a bit is missing? Obviously i have no idea of what you did there, BY THE WAY you will laugh at my ASM code to check things at porta.3 it has to be awful !!. It works but is longer than the sampling part.

    Hope you can give me any ideas.
    Attached Images Attached Images   
    Last edited by Josuetas; - 6th October 2007 at 00:06.

Members who have read this thread : 1

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