Remain counting while sending out a pulse


Closed Thread
Results 1 to 6 of 6

Hybrid View

  1. #1
    Join Date
    Apr 2006
    Location
    GearSweaterMountain, The Netherlands
    Posts
    52

    Default Remain counting while sending out a pulse

    Hi there,
    I'm learning a lot just browsing this forum, but i'm quite stuck at the moment.
    What i'm trying to do is the following :

    I'm have to build a puls divider. When a jumper is on pin 4(gpio3), it counts a number of pulses on pin 5(gpio2), when the jumper is taken off it divides the number of pulses that were count by 10 and stores that in eeprom.

    It then returns to it's normal routine, counting the pulses. If, say 500 pulses have been counted it must output one 50ms pulse every 50 pulses (500/10=50).

    The problem is that im using pulsout plsout,5000 for the pulse output, but this delays the counting of the pulses with 50ms. Which results in me missing some of the incoming pulses.

    Is there anything i can do to keep counting and give a command to make the pin high for 50ms ??
    Code:
    '——-[ Device Initialization ]————————————————————————————————————
    @ device pic12F635,intrc_osc_noclkout,wdt_off,pwrt_off,mclr_off,bod_off,cpd_off,protect_off
    
    '==== Variables ======================================
    
    INCLUDE "modedefs.bas"
    DEFINE OSC 4 
    
    plsOUT var GPIO.0
    plsIN  var GPIO.2
    LEARN  var GPIO.3
    ser var GPIO.1
    
    i var word
    tel var word
    div var word
    
    output plsOUT
    output ser
    input  plsIN
    input  LEARN
    
    i=0
    tel=0
    div=0
    
    pause 1000
    
    main:
    if learn = 1 then goto reset
    
    read 1,i.lowbyte
    read 2,i.highbyte
    pauseus 50
    div = i/10
    
    pl1:
    if i = 0 then goto reset
    if plsin = 1 then
        tel = tel + 1
        'Serout ser,T9600,[#tel,45,#i,45,#div,13,10]
        if tel >= div then
            pulsout plsout,5000
            tel = 0
        else
    
            goto pl0
        endif
    pl0:
        if learn = 1 then goto reset
        if plsin = 1 then
            PAUSEUS 50
            GOTO PL0
        else
            goto pl1
        endif
    else
        goto PL1
    endif     
    if learn = 1 then goto reset
    goto pl1
    
    reset:
    i=0
    tel=0
    div=0
    
    np:
    if learn = 1 then  
        if plsin = 1 then
            i = i+1
            goto wp
        else
            goto np
        endif      
    wp:
        if learn = 1 then
            if plsin = 0 then
                goto np
            else
                goto wp
            endif 
        else
            write 1,i
            goto setmem
        endif
    else
        goto setmem
    endif
    goto main
    
    setmem:
    if i = 0 then goto np
    write 1,i.lowbyte
    write 2,i.highbyte
    pauseus 50
    setmem2:
    if learn = 0 then
        goto main
    else
        goto setmem2
    endif
    
    theend:
    goto theend
    Last edited by ultiblade; - 10th January 2007 at 11:11.

  2. #2
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,604


    Did you find this post helpful? Yes | No

    Default

    Hi,
    I'd sugest you set up the TMR1 module as an asychcronous counter and feed your pulses to Pin2. The timer module will then count the pulses for you and you can read/reset/preset the count any time you like. The timer will keep on counting no matter what the rest of program is doing. Have a look at section 6 in the datasheet.

    /Henrik Olsson.

  3. #3
    Join Date
    Apr 2006
    Location
    GearSweaterMountain, The Netherlands
    Posts
    52


    Did you find this post helpful? Yes | No

    Default

    Hi Henrik, thanks for replying !

    I have looked at section 6 in the datasheet. Do you have some sample code
    on how to setup pin 2 as an asychcronous counter ?

    Is the counter able to put it's "number of pulses count" into a variable ?

    I have got no clue on how to implement this within my code ...

    Thanks in advance !

  4. #4
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,604


    Did you find this post helpful? Yes | No

    Default

    Hi,
    First you need to set GP5 as an input. Then you set the function of the TMR1 module using the T1CON register. The value of the TMR1 module is can be read/written from/to using TMR1H and TMR1L register.

    Here's a shot at it (untested):
    Code:
    TRISA.5 = 1    'Set GP5 to input.
    
    Count Var Word
    Temp var byte
    
    'Set TMR1 to increment on rising edge of T1CKI pin, no prescaler, no sync to internal clock, TMR 1 is on.
    T1CON = %00000111
    
    Loop:
    Count.highbyte = TMR1H     'Get high byte of counter
    Count.Lowbyte = TMR1L     'Get low byte of counter
    
    
    'Make sure that the counter did not roll over between reads of the high and low byte.
    
    Temp var TMR1H              'Get highbyte again.
    If Temp - Count.HighByte <> 0 then     'The two values differs so we read it again.
      Count.HighByte = TMR1H
      Count.LowByte = TMR1L
    ENDIF
    
    Serout ser,T9600,[#Count,13,10]    'Send count to computer.
    Pause 100
    Goto Loop
    As I said - not tested but should be close to what you want.

    (Edit)
    Oh..I forgot...
    You can stop counting by setting bit0 of T1CON to 0, start again by setting it to 1 and you can set the TMR1H and TMR1L register to what ever you want by writing to them just like any other variable.

    /Henrik Olsson.
    Last edited by HenrikOlsson; - 10th January 2007 at 14:32.

  5. #5
    Join Date
    Apr 2006
    Location
    GearSweaterMountain, The Netherlands
    Posts
    52


    Did you find this post helpful? Yes | No

    Default

    Henrik ! Wow !

    Compiled your code, and it counts great !
    Many thanks for getting me started !

  6. #6
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,604


    Did you find this post helpful? Yes | No

    Smile

    Hi,
    Compiled your code, and it counts great !
    Woohoo....I don't think that has ever happend to me before..... :-)
    You're welcome!

    /Henrik Olsson.

Similar Threads

  1. Pulse Capture and byte building
    By boroko in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 21st July 2009, 01:59
  2. Single digit 7 Seg LED clock - PIC16F88
    By thirsty in forum Code Examples
    Replies: 4
    Last Post: - 17th July 2009, 08:42
  3. COUNT is not counting again
    By jellis00 in forum mel PIC BASIC Pro
    Replies: 33
    Last Post: - 19th June 2009, 04:52
  4. Replies: 3
    Last Post: - 13th September 2008, 17:40
  5. Pulse Frequency Multiplication
    By jamie_s in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 21st August 2005, 10:39

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