Capture without CCP


Closed Thread
Results 1 to 6 of 6
  1. #1
    Join Date
    Feb 2008
    Location
    Michigan, USA
    Posts
    231

    Default Capture without CCP

    Is it possible to capture the TMR1 counts without a CCP and still let the counter continue?

    The problem: Trying to use a 18 pin chip for space, currently a 18F1330.

    I need the Power Control PWM as I need at least 2 different PWMs, three would be better.

    The TMR1 is using a prescaler of 1:1 and a preload of 5543 (@8mHz) for a cycle of about 30 mS.

    That cycle sends a ultrasonic transmit pulse and starts looking for a return echo.
    INT2_INT (eventually CMP2_INT) is used to catch the echo and grab the count from TMR1. I spent the last 6 hours learning that when you load TMR1, you need to load TMR1H first, before TMR1L. I won't tell you how many times I read the datasheet before I realized that one. I have the cycle stable now, but the capture of the elapsed time is still wonked. I can't see where.

    I could go to a 18F2331 and get CCP and PCPWM, but that puts me at 28 pin and I really don't have the room.

    If anyone sees something that here, I sure would appreciate the advice.
    Code:
    '******************************************************************
    '*  Name    : CS-Sonic1330_2f.BAS                                 *
    '*  Author  : Mark A.Rokus                                        *
    '*  Notice  : Copyright (c) 2009 Controlled Surroundings Inc.     *
    '*          : All Rights Reserved                                 *
    '*  Date    : 3/25/2009                                           *
    '*  Version : 1.0                                                 *
    '*  Notes   : 18F1330 @ 8mHz, PBP 2.50b, MCS 3.0.0.0
    '*  Pulse out on PORTB.0 every ~30 mS (TMR1: _Transmit)
    '*  external circuit echos back pulse on PORTB.2 (INT2)
    '*  between 3mS and 23mS. After transmit, enable INT2_INT and 
    '*  get count to "EchoTime".  Send # out on HSER to debug.    
    '*   This is the core of the code that is failing.  Increasing the 
    '*   result count with every echo while the return pulse is steady????          
    '****************************************************************
    DEFINE OSC 8 
    DEFINE HSER_RCSTA 90h
    DEFINE HSER_TXSTA 24h
    DEFINE HSER_BAUD 9600
    DEFINE HSER_CLOERR  1       ' automatic clear overrun error
    clear                 
                                 
    TRISB   = %01000100      ' PWM1,3,5 outputs,RB2=INT, RB0,2,3: DIO
    ADCON1  = %00001111      ' Set up ADCON1 no matter what you're doing!!!!!!                      
    T0CON   = %10001000      ' On, 16 bit, Prescaler Bypassed (1:1) 
    T1CON   = %10000001      ' 16bit, 1:1 ps, 30 mS INT   
    CMCON   = %00000100      ' CMEN2 to enable the comparator
    CVRCON  = %10100010      ' REF on, REF voltage @ 1.563
    RCON    = %00011111      ' NO Priority for interrupts
    OSCCON  = %11111110      ' INTOSC, 8MHz, 
    
     hserout [" 1330-2g",13,10]
    '::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
    INCLUDE "DT_INTS-18.bas"     ' Base Interrupt System
    INCLUDE "ReEnterPBP-18.bas"  ' Include if using PBP interrupts
    '::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
        '   Variable definition
    ':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 
    EchoTime     Var    word
    PL1lo        con    $A7             ' Preload for TMR1 LO byte,  Cycle time
    PL1hi        con    $15             ' Preload TMR1 HI byte ,PL =5543/ 1:1 
           
    ASM
    INT_LIST  macro    ; IntSource,        Label,  Type, ResetFlag?
            INT_Handler   INT2_INT,   _GetEcho,    PBP,  yes   ;straight I/P
            INT_Handler   TMR1_INT,  _Transmit,   PBP,  yes
        endm
        INT_CREATE               ; Creates the interrupt processor
    ENDASM
    '@    INT_ENABLE  INT2_INT
    @    INT_ENABLE  TMR1_INT     ; enable Timer 1 interrupts 
    '***************************************************************************
    Start:
           
    Mainloop:
     
       hserout [#EchoTime,13,10]
       pause 500
    
       goto Mainloop
    '*********** Int handlers **************************************************
    
    '---[INT - interrupt handler]-----------------------------------------------
    '  TMR1 cnt up from start of TX. INT when RX, get counts into EchoTime
    GetEcho:   
       T1CON.0=0                     ' Stop TMR1
       echoTime.Highbyte=TMR1H       ' capture the counts
       echoTime.Lowbyte=TMR1L
    '   echoTime.Highbyte=TMR1H       ' capture the counts
       T1CON.0=1                     ' Restart the TMR1 to finish cycle  
    @ INT_RETURN
    
    '---[TMR1 - interrupt handler]-------------------------------
    ' TMR1 = 16 bit, 1:1, 65535 - 5543(preload) ~30 mS cycle  
    Transmit:
    @ INT_DISABLE   INT2_INT     ; Disable external ints to stop Echo RX
    'T1CON   = %10000001      ' 16bit, 1:1 ps, 30 mS INT
             TMR1H = PL1hi            'Preload HI byte
             TMR1L = PL1lo            'Preload LO byte = 
    
    asm 
         BSF 	PORTB,0
         NOP     ; need 7 NOPs between changes for 125kHz @ 8mHZ
         NOP    
         NOP
         NOP
         NOP
         NOP
         NOP
         BCF    PORTB,0
         NOP     
         NOP     
         NOP
         NOP
         NOP
         NOP
         NOP
    endasm         
    
    @ INT_ENABLE   INT2_INT                          
    @ INT_RETURN
    
    end
    Thanks
    Bo

  2. #2
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,620


    Did you find this post helpful? Yes | No

    Default Did you read zeu whole Book ???

    Hi, Boroko

    I think in the Microchip datas ( Midrange manual ), there's a chapter on how to use timers as "free running timers"

    Example 12-2: Reading a 16-bit Free-Running Timer

    Example 12-3: Writing a 16-bit Free Running Timer


    Ok ... ASM, but is it really a problem ??? certainly not with Darrel's I.I.

    Alain
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

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


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by boroko View Post
    I spent the last 6 hours learning that when you load TMR1, you need to load TMR1H first, before TMR1L.
    Yes, with RD16 set, you load the highbyte first, then when you load the lowbyte, both bytes a written to the timer at the same time.

    But when reading a timer with RD16, you have to read the lowbyte first. That puts the highbyte in a buffer that can be read in the next instruction.

    If you read the highbyte first, you get the value that was left in the buffer the last time you read/wrote the timer, then the lowbyte of the current timer value.

    Also, when using RD16, you don't need to stop the timer when reading or writing to the timer.

    For something like this where you need Critical timing ... the interrupt handlers should be ASM. Saving/Restoring PBP's system variables to get in/out of a PBP interrupt will throw the results off considerably.

    None of the statements in your handlers use any PBP system variables. So you can just change the handers "type" to ASM without actually having to write it in ASM.

    hth,
    DT

  4. #4
    Join Date
    Feb 2008
    Location
    Michigan, USA
    Posts
    231


    Did you find this post helpful? Yes | No

    Default tidbits..

    Thanks guys,

    Alain, I had not thought to look at the mid-range ref manual. Seeing how they handled it helped to see it better. The longer I'm at the less I'm scared by the asm. I may not be able to write it very well, but it is getting easier to see how it works. I'm not religious about a particular method. I'm interested in results and PBP has given me the best chance to get powerful ones with the least amount of pain.

    Darrel, thanks for going over these details. I was having a blind spot pulling them out of the data sheet.

    It may seem like a small thing to you guys, but your assistance has helped me (and I'm sure others) get through things that in the past, would have pushed me to give up on out of pure frustration.

    Bo

    Wohoo! Stable numbers echoing back! Thank you so much.....
    Last edited by boroko; - 26th March 2009 at 23:13. Reason: update:

  5. #5
    Join Date
    Feb 2008
    Location
    Michigan, USA
    Posts
    231


    Did you find this post helpful? Yes | No

    Default

    None of the statements in your handlers use any PBP system variables. So you can just change the handers "type" to ASM without actually having to write it in ASM.
    I guess that means I don't need to include ReEnterPBP-18.bas either. Do I understand that correctly?

    Bo

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


    Did you find this post helpful? Yes | No

    Default

    Correct!

    Including the file will use some additional RAM and program space that isn't needed.

    But it won't adversely affect the program if it's there.
    <br>
    DT

Similar Threads

  1. mS Timer
    By whmeade10 in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 8th September 2020, 13:12
  2. Using hardware capture
    By Bruce in forum Code Examples
    Replies: 14
    Last Post: - 25th March 2012, 06:52
  3. Measuring change of frequency with PIC
    By Aussie in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 19th July 2007, 02:47
  4. continious counting process (capture)
    By asynch in forum General
    Replies: 1
    Last Post: - 17th February 2006, 08:42
  5. CCP question
    By jamie_s in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 5th December 2005, 18:08

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