Hot!TMR1 not working as counter

Author
naeem1234
Senior Member
  • Total Posts : 146
  • Reward points : 0
  • Joined: 2015/02/19 06:39:28
  • Location: 0
  • Status: offline
2017/10/07 20:52:33 (permalink)
0

TMR1 not working as counter

Hello everyone
My chip is PIC18F46K22, 40 Pins DIP Package.
I am trying to use TMR1 as 16-bit pulse counter. I am using RC0 pin as T1CKI input pin as external clock input for the counter.
My chip primary clock is external oscillator of 20MHz.
 
Here are my initializations and related code for TMR1:
PMD0bits.TMR1MD = 0;
T1CONbits.TMR1CS1 = 1; // T1 clock select external. RC0 pin is used as counter input.
T1CONbits.TMR1CS0 = 0;
TRISCbits.TRISC0 = 1;

T1CONbits.T1OSCEN = 0; // T1 external oscillator disable
T1CONbits.RD16 = 1; // T1 as 16 bit read write
T1CONbits.T1SYNC = 1; // T1 async mode
T1CONbits.T1CKPS1 = 1; // prescaler 1:8. As 8 pulses per revolution
T1CONbits.T1CKPS0 = 1; // and also it will increase the max pulse count from 65535 to 8x65535.
T1CONbits.TMR1ON = 1; // T1 ON
TMR1GE = 0; // T1 gate control disable
//T1CONbits.T1RUN = 0; // device clock is not from T1 clock
T1SOSCEN = 0;
T3SOSCEN = 1;
TMR1L = 0;
TMR1H = 0;
...
 

unsigned int TIMER1;

    TIMER1 = TMR1;
    if (TIMER1 > 65200) {
        TMR1 = 0;
        ...
    }
 
 
For alternate functions on RC0 pin, my configuration fuses are as following:
// CONFIG3H
#pragma config CCP2MX = PORTB3 // CCP2 MUX bit (CCP2 input/output is multiplexed with RB3)
#pragma config PBADEN = OFF // PORTB A/D Enable bit (PORTB<5:0> pins are configured as digital I/O on Reset)
#pragma config CCP3MX = PORTE0 // P3A/CCP3 Mux bit (P3A/CCP3 input/output is multiplexed with RB5)
#pragma config HFOFST = OFF // HFINTOSC Fast Start-up (HFINTOSC output and ready status are delayed by the oscillator stable status)
#pragma config T3CMX = PORTB5 // Timer3 Clock input mux bit (T3CKI is on RB5)
#pragma config P2BMX = PORTD2 // ECCP2 B output mux bit (P2B is on RD2)
#pragma config MCLRE = EXTMCLR // MCLR Pin Enable bit (MCLR pin enabled, RE3 input pin disabled)
 

 
 
There is nothing connected to pin RC0 externally, or it is grounded, but still the TMR1 keeps showing a count of approx. 300 counts per sec when i read its value after few minutes via serial port.
 
Any idea why the Timer1 is not working as counter?
 
Any help will be great.
#1

6 Replies Related Threads

    qhb
    Superb Member
    • Total Posts : 6257
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: offline
    Re: TMR1 not working as counter 2017/10/07 21:13:49 (permalink)
    +1 (1)
    naeem1234
    ... 
    There is nothing connected to pin RC0 externally, or it is grounded, but still the TMR1 keeps showing a count of approx. 300 counts per sec when i read its value after few minutes via serial port.
     
    Any idea why the Timer1 is not working as counter?

    So the pin is floating?
    In that case it IS working as a counter, and you are picking up mains hum on the pin.
    Why on earth have you left it floating?
    NEVER leave an input pin floating.
     
    #2
    naeem1234
    Senior Member
    • Total Posts : 146
    • Reward points : 0
    • Joined: 2015/02/19 06:39:28
    • Location: 0
    • Status: offline
    Re: TMR1 not working as counter 2017/10/07 21:19:07 (permalink)
    0
    Even if i connect the RC0 pin to ground i still get the same high count value.
    #3
    qhb
    Superb Member
    • Total Posts : 6257
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: offline
    Re: TMR1 not working as counter 2017/10/07 21:33:41 (permalink)
    +1 (1)
    Remove this line:
    T3SOSCEN = 1;
     
    It does not matter that you have have set TMR3 to take its clock input from RB5, setting the T3SOSCEN bit still starts the secondary oscillator, which makes pin RC0 the SOSCO pin.
    See: "FIGURE 2-4: SECONDARY OSCILLATOR AND EXTERNAL CLOCK INPUTS" in the datasheet.
    #4
    naeem1234
    Senior Member
    • Total Posts : 146
    • Reward points : 0
    • Joined: 2015/02/19 06:39:28
    • Location: 0
    • Status: offline
    Re: TMR1 not working as counter 2017/10/07 22:34:33 (permalink)
    0
    qhb
    Remove this line:
    T3SOSCEN = 1;
     
    It does not matter that you have have set TMR3 to take its clock input from RB5, setting the T3SOSCEN bit still starts the secondary oscillator, which makes pin RC0 the SOSCO pin.
    See: "FIGURE 2-4: SECONDARY OSCILLATOR AND EXTERNAL CLOCK INPUTS" in the datasheet.




    Thanks for your reply.
     
    I have removed this line now. The RC0 pin is connected to GND.
    Now the mysterious count has reduced a lot but still its not zero.
    Is it possible that the TMR1 is also getting its clock input from some internal source although i have initialized it for the external clock source?
    #5
    qhb
    Superb Member
    • Total Posts : 6257
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: offline
    Re: TMR1 not working as counter 2017/10/07 22:58:05 (permalink)
    +1 (1)
    Anything is possible, as you have not shown your entire program, so we can't see what else you might be doing.
     
    #6
    PStechPaul
    Super Member
    • Total Posts : 1412
    • Reward points : 0
    • Joined: 2006/06/27 16:11:32
    • Location: Cockeysville, MD, USA
    • Status: offline
    Re: TMR1 not working as counter 2017/10/08 13:26:38 (permalink)
    0
    Check for noise on the power supply and other pins. Does TMR1 just have a fixed value or does it count at some rate? Perhaps it needs to be set to zero? Providing all code would be helpful.

     
    #7
    Jump to:
    © 2017 APG vNext Commercial Version 4.5