Menu

timer gate pin

Help
2018-09-11
2019-02-17
  • David Stephenson

    I am using a 16F18326 and I want to gate one of the 16-bit timers using an external input.
    The datasheet is not informative, but could I make a big assumption that setting TxGPPS (x=1,3,5) to one of the pins is the correct way to input a gate signal.

     
  • stan cartwright

    stan cartwright - 2018-09-12

    Would interrupt on pin change and the interrupt start the timer work?

     
  • Anobium

    Anobium - 2018-09-12

    A tough one to answer. Assumming Timer1 on PortA.0 as below. Change to timer 3 or 5 as appropiate and checkout the T1GPPS

    Dir porta.5 out
    Dir porta.0 in
    
    T1CKIPPS = 0x05          '   RA5->TMR1:T1CKI
    T1GPPS = 0x00            '   RA0->TMR1:T1G   
    
    
    Inittimer1  Ext, PS_1   'you need to set PS_1 to the correct value
    T1GCON = 0x80           'handcrank th T1GON value.   T1GSS T1G_pin; TMR1GE enabled; T1GTM disabled; T1GPOL low; T1GGO_nDONE done; T1GSPM disabled  
    SetTimer1 ( 0x1234)     'somevalue you need
    T1CON = 0x81           'handcrank T1Con value.  T1CKPS 1:1; T1SOSC T1CKI_enabled; T1SYNC synchronize; TMR1CS External; TMR1ON enabled;
    
     
  • David Stephenson

    Thanks. Looks like I will have to try it and see what happens.
    I am going to use the internal LF oscillator and have two timers running off it one of them will be gated by a dust/particle detector. The detector normally produces a high signal, but when a atmospheric particle is detected it goes low. The number and lenght of the low pulses give the number and size of the particle (at least I think so again the datasheet is a bit vague - it's a stbm271).
    I have tried using a resistor capacitor arrangement to sort of integrate the output, but it seems the high and low output drift to some extent so it would not be very accurate.

     
  • David Stephenson

    It turned out to be fairly straightforward. The problem was the lack of detail on the gate pin in the datasheet.
    So I've got timer1 and timer3 running off the LFINTOSC and timer3 only counts when the gate pin is low. Seems to still work in SLEEP so that will save some power.

    #chip 16F18326,4
    #config RSTOSC=hfint1, MCLRE=Off, WDTE=off, boren=off
    UNLOCKPPS
    
                'Module: TMR3
                T3GPPS = 0x0013    'RC3 > T3G
    LOCKPPS
    
    DIM STBM271_READING AS WORD
    
    t1con=b'11001101' 'timer1 1:1, lfintosc
    TMR1H=b'00000000' '2 sec roll-over
    TMR1L=b'00000000'
    pir1.0=0 'set interrupt flag off
    intcon.6=1 'enable peripheral interrupts
    pie1.0=1 'enable timer1 overflow interrupt
    t1gcon.7=0 'timer1 continuous counting
    
    t3con=b'11001101' 'timer3 1:1, LFINTOSC
    TMR3H=b'00000000' '2 sec roll-over
    TMR3L=b'00000000'
    t3gcon.7=1 'timer3 gated on pin rc3
    
    do
    vregpm=1 'low power sleep
    sleep
    nop
    vregpm=0 'back to full power
    pir1.0=0 'reset timer1 interrupt
    
    stbm271_reading=TMR3L
    stbm271_reading_h=TMR3H
    TMR3H=b'00000000' '2 sec roll-over
    TMR3L=b'00000000'
    loop
    
     
  • David Stephenson

    It turned out to be fairly straightforward. The problem was the lack of detail on the gate pin in the datasheet.
    So I've got timer1 and timer3 running off the LFINTOSC and timer3 only counts when the gate pin is low. Seems to still work in SLEEP so that will save some power.

    #chip 16F18326,4
    #config RSTOSC=hfint1, MCLRE=Off, WDTE=off, boren=off
    UNLOCKPPS
    
                'Module: TMR3
                T3GPPS = 0x0013    'RC3 > T3G
    LOCKPPS
    
    DIM STBM271_READING AS WORD
    
    t1con=b'11001101' 'timer1 1:1, lfintosc
    TMR1H=b'00000000' '2 sec roll-over
    TMR1L=b'00000000'
    pir1.0=0 'set interrupt flag off
    intcon.6=1 'enable peripheral interrupts
    pie1.0=1 'enable timer1 overflow interrupt
    t1gcon.7=0 'timer1 continuous counting
    
    t3con=b'11001101' 'timer3 1:1, LFINTOSC
    TMR3H=b'00000000' '2 sec roll-over
    TMR3L=b'00000000'
    t3gcon.7=1 'timer3 gated on pin rc3
    
    do
    vregpm=1 'low power sleep
    sleep
    nop
    vregpm=0 'back to full power
    pir1.0=0 'reset timer1 interrupt
    
    stbm271_reading=TMR3L
    stbm271_reading_h=TMR3H
    TMR3H=b'00000000' '2 sec roll-over
    TMR3L=b'00000000'
    loop
    
     

Log in to post a comment.

Auth0 Logo