Pulse Capture and byte building

THE BOOK of DT's INTERRUPTS is an organization of interrupt service routines and other works written by Darrel Taylor, RIP.

Interrupt routines are arranged per Darrel's original list. Darrel Taylor's Instant Interrupts are an extension of the work of Tim Box whom in October of 2002 wrote INT_CTRL.pbp

Many of the forum members felt this is a long overdue project.

will be a valuable resource and a tribute to Darrel Taylor the "Fanatical Contributor".

THE BOOK of DT's INTERRUPTS is located at http://dt.picbasic.co.uk/

+ Reply to Thread
Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2008
    Michigan, USA

    Default Pulse Capture and byte building

    Hi All,

    I'm at a loss as why this is only making one pass through, any ideas?
    I'm sending it a long (~100uS) or short (~50uS) pulse on RB.0 (INT) and it will grab the timer value for the appropriate length pulse.
    That part is doing fine. Problem is that it will only make one pass, and then nothing. By one pass, I mean it will INT and start the timer, and on the next INT, it will grab the timer value and report it both on the LCD and out serially. The REM'd code is my attempt to build a byte from the captured bits, but so far, I can't get past single captures.
    <body><!--StartFragment--><pre><code><font color="#0000FF"><i>'****************************************************************
    '*  Name    : PulseGrab.BAS                                     *
    '*  Author  : boroko                                            *
    '*  Notice  : Copyright (c) 2009 Controlled Surroundings Inc.   *
    '*          : All Rights Reserved                               *
    '*  Date    : 7/19/2009                                         *
    '*  Version : 1.0                                               *
    '*  Notes   : testing on a 16F877A @ 20mHz on Lab-X1            *
    '*          : Does one capture and displays, but no more.                                            *
    '*          : it DOES differentiate the pulse length
    ' Test bed to build pulse length receiver code. 
    ' Short or long pulse on RB.0  ~ 50uS and ~100uS for &quot;1&quot; and &quot;0&quot;
    ' Display on a LCD and Transmit to test.
    </i></font><b>INCLUDE </b><font color="#FF0000">&quot;AllDigital.pbp&quot; 
    </font><b>INCLUDE </b><font color="#FF0000">&quot;DT_INTS-14.bas&quot;     </font><font color="#0000FF"><i>' Base Interrupt System
    </i></font><b>INCLUDE </b><font color="#FF0000">&quot;ReEnterPBP.bas&quot;     </font><font color="#0000FF"><i>' Include if using PBP interrupts
    </i></font><b>DEFINE  </b>OSC 20
    <b>DEFINE  </b>LCD_DREG        PORTD
    <b>DEFINE  </b>LCD_DBIT        4
    <b>DEFINE  </b>LCD_RSREG       PORTE
    <b>DEFINE  </b>LCD_RSBIT       0
    <b>DEFINE  </b>LCD_EREG        PORTE
    <b>DEFINE  </b>LCD_EBIT        1
    <b>DEFINE </b>HSER_RCSTA 90h       <font color="#0000FF"><i>' enable serial port, %10010000
    </i></font><b>DEFINE </b>HSER_TXSTA 24h       <font color="#0000FF"><i>' enable transmit,  BRGH hi, %00100100 
    </i></font><b>DEFINE </b>HSER_BAUD 19200       <font color="#0000FF"><i>' set baudrate to 19200                   
    </i></font><b>DEFINE </b>HSER_CLOERR  1       <font color="#0000FF"><i>' automatic clear overrun error
    </i></font>T1CON   =   %00001000        <font color="#0000FF"><i>'TMR1 1:1 PS, Osc En, Int Clk, off to start
                ' returning 259 for short and 767 for long pulse @ 1:1
    </i></font>Time       <b>VAR   WORD         
    </b>CntState   <b>VAR   BIT
    LCDOUT </b>$fe, 1                 <font color="#0000FF"><i>'blank flash to see if LCD updated
    </i></font><b>PAUSE </b>100
    <b>LCDOUT </b>$fe, 1, <font color="#FF0000">&quot;PulseGrab SubTest&quot;
    </font><b>HSEROUT </b>[<font color="#FF0000">&quot;PulseGrab SubTest&quot;</font>,13]
    <b>PAUSE </b>500
    <b>LCDOUT </b>$FE,1
    <font color="#0000FF"><i>'*********************************************************
    </b><font color="#008000">INT_LIST  macro   </font><font color="#0000FF"><i>; IntSource,        Label,  Type, ResetFlag?
            </i></font><font color="#008000">INT_Handler    INT_INT,    _pulse,   PBP,  no
        INT_CREATE               </font><font color="#0000FF"><i>; Creates the interrupt processor
        </i></font><font color="#008000">INT_ENABLE   INT_INT     </font><font color="#0000FF"><i>; enable external (INT) interrupts
    </b><font color="#0000FF"><i>'*****************************************************************
         <b>PAUSE </b>10
    <font color="#0000FF"><i>'     if (Time &gt;230)&amp;&amp;(time &lt; 280) then    ' short pulse (1) received
    '       PulseIn = 1                      ' maybe change window to improve
    '       gosub CharBuild                  ' save the character
    '       endif
    '    if (Time &gt;750)&amp;&amp;(time &lt; 800) then   ' long pulse (0) received
    '       PulseIn = 0                      ' maybe change window to improve
    '       gosub CharBuild                  ' save the character
    '       endif
    '     else 
    '       PulseIn = 8                         ' If nothing else, then no data
                ' ADD counter HERE to allow for a small gap before dumping
    '       charcnt = 0                         ' Dump the counter and
    '       char = 0                            '  dump the character received  
    '     endif
    </i></font><b>GOTO </b>Main
    <font color="#0000FF"><i>''***************************************************************
    ' INT Handler
    '****** Pulse capture using External Interrupt courtesy of DT *****************
        <b>IF </b>INTCON.1=1 <b>THEN         </b><font color="#0000FF"><i>' External Interrupt happened
            </i></font><b>IF </b>CntState=0 <b>THEN     </b><font color="#0000FF"><i>' 
                </i></font>T1CON.0=1          <font color="#0000FF"><i>' Enable Timer 
                </i></font>OPTION_REG.6 = 0   <font color="#0000FF"><i>' Next trigger is Falling edge
                </i></font>INTCON.1=0         <font color="#0000FF"><i>' Reset ExtInt Flag 
                </i></font>CntState =1
                </b>Time.LowByte = TMR1L   <font color="#0000FF"><i>' Store the captured value in period variable
                </i></font>Time.HighByte = TMR1H            
                T1CON.0=0           <font color="#0000FF"><i>' Timer is OFF 
                </i></font>INTCON.4=0          <font color="#0000FF"><i>' ExtInt is OFF 
                </i></font>TMR1L=0             <font color="#0000FF"><i>' Reset Timer 
                </i></font>TMR1H=0             <font color="#0000FF"><i>' Remember Timer is OFF 
                </i></font>INTCON.1=0          <font color="#0000FF"><i>' Reset ExtInt Flag 
                </i></font>OPTION_REG.6 = 1    <font color="#0000FF"><i>' Next trigger is Rising edge
                <b>LCDOUT </b>$fe, 1, <font color="#FF0000">&quot;Int&quot;              </font><font color="#0000FF"><i>' Show that we had an Interrupt 
                </i></font><b>LCDOUT </b>$fe,$c0,<font color="#FF0000">&quot;time &quot;</font>,#Time      
                <b>HSEROUT </b>[<font color="#FF0000">&quot;Int&quot;</font>,13,10]
                <b>HSEROUT </b>[#Time,13]
    </b><font color="#008000">@ INT_RETURN
    </font><font color="#0000FF"><i>'***************************************************************
    '                    Subs
    '      char.0 = PulseIn                      ' Put the pulse value in bit 0 
    '      Char = Char &lt;&lt;1                       ' rotate RX bit into byte
    '         CharCnt = CharCnt+1                 ' increment counter to track fill
    '      if CharCnt = 7 then                    ' RX byte is filled
    '         RXbyte = Char                       ' valid Comm byte received
    '         Lcdout $fe, 1, &quot;RXbyte= &quot;,RXbyte    ' Show what we got
    '         hserout [&quot;RXbyte= &quot;,#RXbyte,13,10]  ' transmit what we got
    '      endif
    'ACprog:                                      ' look for lost power programming             
    '         Lcdout $fe, 1, &quot;AC in&quot;              ' Show that we dropped into &quot;AC&quot;
    '         hserout [&quot;AC in&quot;,13,10]
    '         ' add test to gather data
    To give proper credit, this is based on one of Darrel's suggestions in another thread concerning pulse timing.

    Any suggestions?

  2. #2
    Join Date
    Jul 2003
    Colorado Springs

    Did you find this post helpful? Yes | No


    Because you turn OFF the interrupts after the the first sample??
    INTCON.4=0          ' ExtInt is OFF

  3. #3
    Join Date
    Feb 2008
    Michigan, USA

    Did you find this post helpful? Yes | No

    Default Thanks

    NOTE TO SELF: Sleep is a good thing.

    It took me a moment to see it even after you pointed it out. I kept "seeing" it without realizing that it wasn't getting turned back on.


Similar Threads

  1. mS Timer
    By whmeade10 in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 8th September 2020, 12:12
  2. Using hardware capture
    By Bruce in forum Code Examples
    Replies: 14
    Last Post: - 25th March 2012, 05:52
  3. RC Servo decoding/encoding using 12F683
    By ScaleRobotics in forum Code Examples
    Replies: 13
    Last Post: - 14th September 2010, 00:49
  4. Interrupt RPM and Taylors Elapsed time on 18F4620
    By Tobias in forum mel PIC BASIC Pro
    Replies: 70
    Last Post: - 3rd February 2010, 16:12
  5. Replies: 3
    Last Post: - 13th September 2008, 17:40

Posting Permissions

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