Incoming Pulse Monitoring code - comments and suggestions welcome


Results 1 to 4 of 4

Threaded View

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


    Did you find this post helpful? Yes | No

    Default

    Hi jamie,

    It's nice to see more examples with Instant Interrupts popping up.

    I think using the CCP to generate an interrupt on every transition of the Input is slowing things down.

    If you handle the "Input to Inverted Output" in the main loop, It should go alot faster than 2khz.
    Then the interrupts can just handle the timer overflows.

    Here's a possibility ...
    Code:
    '------------------------------------------------
    '  	Pulse Monitor
    '  	PIC16F628A @ 4mhz
    '
    '
    '	Input Signal Port B.3
    '	Output Signal Port B.5
    '------------------------------------------------
    
    
    INCLUDE "DT_INTS-14.bas"        ' Base Interrupt System
    INCLUDE "ReEnterPBP.bas"        ' Include if using PBP interrupts
    
    
    CMCON = 7                       ' set portA to digital
    TRISA = %00100000               ' Port A all output but MCLR
    TRISB = %00001000               ' Port B all output but 3 (CCP)
    T1CON = %00110000               ' Set Timer 1, Off and 1/8 prescale (fire every 520ms)
    T2CON = %00000011               ' Set Timer 2, Off and 1/16 prescae (fire every 4ms)
    T1count     VAR BYTE            ' Timer 1 count (1 count = 0.52 sec)
    i           VAR BYTE            ' i is the incrementer for the pulse generator
    
    InPIN       VAR PORTB.3         ' Input Pin
    OutPIN      VAR PORTB.5         ' Output Pin
    TMR1ON      VAR T1CON.0
    TMR2ON      VAR T2CON.2
    LastState   VAR BIT
    
    ASM
    INT_LIST  macro     ; IntSource,      Label,   Type, ResetFlag?
            INT_Handler    TMR1_INT,   _nospeed,    PBP,    yes
            INT_Handler    TMR2_INT,  _generate,   	PBP,    yes
        endm
        INT_CREATE                  ; Creates the interrupt processor
    
        INT_ENABLE   TMR1_INT       ; enable timer1 interrupts (CCP timer)
        INT_ENABLE   TMR2_INT       ; enable timer2 interrupts (pulse generator)
    ENDASM
    
    
    Low OutPIN                      ' start with Output LOW
    T1count = 0                     ' set variable	
    TMR1H   = 0                     ' Clear high byte of TMR1 counter
    TMR1L   = 0                     ' Clear low byte
    i       = 0                     ' set variable
    TMR1ON  = 1                     ' Turn TMR1 on here
    
       
    '=======================================================================================
    '---[MainLoop handles the Inverted transfer From InPIN to OutPIN] ----------------------
    MainLoop:
        if InPIN <> LastState then  ' If Input State Changed 
            LastState = InPIN
            TMR2ON = 0              ' Stop Timer 2
            OutPIN = LastState ^ 1  ' Output = inverted state of Input
            TMR1H = 0               ' Clear high byte of TMR1 counter
            TMR1L = 0               ' Clear low byte
            T1count = 0
        endif
    GOTO MainLoop
        
    '=======================================================================================
    '---[TMR1 - interrupt handler - Called when Timer1 Overflows @ 520ms] ------------------
    nospeed:
        T1count = T1count + 1       ' increment every 520ms
    
        If T1count >= 3 then        ' no pulses for 1560ms, start generating them
            TMR2ON = 1              ' Set Timer 2 on
            T1count = 0             ' reset the TMR1 count
        endif
    @ INT_RETURN
    
    '==========================================================================================
    '---[TMR2 - interrupt handler - This will fire every 4.103ms]------------------------------
    ' send a 20ms pulse every  (12 * 4ms) 48ms, gives a frequency of ~ 15hz
    generate:
        i = i + 1                   ' increment tmr2 count
    
        if i < 12 then              ' Pin is low for 1-11
            OUTPIN = 0
        else
            OutPin = 1              ' Pin is high for 5 ints  (20ms)
        endif
        If i = 17 then i = 0        ' restart cycle, total = 69.751ms = 14.3hz 
    @ INT_RETURN
    Last edited by Darrel Taylor; - 4th March 2007 at 06:58. Reason: Changed INT_DISABLE TMR2_INT - to - INT_ENABLE
    DT

Similar Threads

  1. Pushbutton code routine suggestions?
    By jessey in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 3rd September 2005, 01:02

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