How to set TMRO & TMR3 as Counter?.


Closed Thread
Results 1 to 22 of 22

Hybrid View

  1. #1
    Join Date
    Jun 2005
    Location
    Penang
    Posts
    40

    Default How to set TMRO & TMR3 as Counter?.

    Hi all,

    I am a bit confused on how to set TMR0 and TMR3 as a Counter in PIC18F4550?.I plan to use these 2 Counter as a pulse counter from my robots encoder.Thanks for your guidance guys.I am kinda new in this.

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


    Did you find this post helpful? Yes | No

    Default

    Hi,
    TMR3 seems to share it's clock pin with TMR1 and I haven't really looked into what effects that has but here's what I'd try:
    Code:
    TRISA.4 = 1		'TMR0 CKI Pin as input
    TRISC.0 = 1		'TMR3 (and TMR1) CKI Pin as input
    
    'From Datasheet Section 11
    T0CON.7 = 1		'TMR0 is ON
    T0CON.6 = 0		'TMR0 is 16 bits
    T0CON.5 = 1		'TMR0 clock source is EXTERNAL...
    T0CON.4 = 0		'...and counts on rising edge
    T0CON.3 = 1		'NO prescaler is assigned
    T0CON.2 = 0		'Prescaler 1:1
    T0CON.1 = 0		'Prescaler 1:1
    T0CON.0 = 0		'Prescaler 1:1
    
    
    'From Datasheet Section 14
    T3CON.7 = 0		'Disables 16bit read/write
    T3CON.6 = 0		'TMR1 is clock for both CCP's (together with BIT 3)
    T3CON.5 = 0		'Prescaler 1:1
    T3CON.4 = 0		'Prescaler 1:1
    T3CON.3 = 0		'TMR1 is clock for both CCP's (together with BIT 5)
    T3CON.2 = 1		'Don't sync with internal clock.
    T3CON.1 = 1		'TMR3 clock source is EXTERNAL
    T3CON.0 = 1		'TMR3 is ON
    I've set the bits one by one here so that it's easier to see what each one does. Note that I haven't tested this so it's perfectly normal if it doesn't work...

    /Henrik Olsson.

  3. #3
    Join Date
    Jun 2005
    Location
    Penang
    Posts
    40


    Did you find this post helpful? Yes | No

    Default So..

    Thats a great guide HenrikOlsson,

    Well,the truth is .. this is my first time in my life setting up Counter in PIC.Although i know whats it function would work like,i am still confused ( Well,not the pre-scaler part,rising edge or falling edge and so on ..) but how to set it,accumulate it and clear it?.That's what something new to me.I am able to read the datasheet but understand 50/50...

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


    Did you find this post helpful? Yes | No

    Default

    Hi,
    Ahh, well the TMR registers are like any other register in the PIC so you can read/write to/from them as you would with any other register. So to set the TMR0 registers to 0 (assuming a 16 bit TMR0 here):
    Code:
    MyCount var WORD		'This is my counter...
    
    TMR0L = 0			'Reset lower byte
    TMR0H = 0			'Reset hogher byte
    
    Start:
      MyCount.HighByte = TMR0H	'Get high byte of counter
      MyCount.LowByte = TMR0L	'Get low byte of counter
      LCDOUT $FE,1,#MyCount		'Display it
      Pause 100
    Goto Start
    To PRESET it you write whatever value you wish to the two registers:
    Code:
    TMR0H = 3        'Preset TMR0 to 878....
    TMR0L = 110     ' ( 3 * 256 + 110 )
    That's really all there is to it. Well, one more thing. When reading the two TMR registers in the way shown above there is a slight chance the lower byte rolls over in the time between reading TMR0H and TMR0L. To come around that problem you can either enable 16bits read/writes (on PICs that supports it) or read the high byte two times, like:
    Code:
    MyCount VAR WORD
    Temp var byte
    
    
    MyCount.HighByte = TMR0H	'Get high byte of counter
    MyCount.LowByte = TMR0L		'Get low byte of counter
    Temp = TMR0H			'Get high byte again
    
    IF Temp <> MyCount.HighByte then  'Do a fresh read
      MyCount.HighByte = TMR0H	'Get high byte of counter
      MyCount.LowByte = TMR0L	'Get low byte of counter
    EndIf
    HTH
    /Henrik Olsson.

  5. #5
    Join Date
    Jun 2005
    Location
    Penang
    Posts
    40


    Did you find this post helpful? Yes | No

    Default Great..

    Hi HenrikOlsson,

    Ok.I understand now.I gonna give a try and let you know the result.Thanks a bunch my "Sail on the same ship" friend.

  6. #6
    Join Date
    Jun 2005
    Location
    Penang
    Posts
    40


    Did you find this post helpful? Yes | No

    Default Mmm...share

    To HenrikOlsson,

    Hope you remember me.

    Is the TMR0 and TMR3 were shared?.So that means i could only use 1 counter at a time right?.

Similar Threads

  1. Pin won't stay high during Repeat...Until loop??
    By jellis00 in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 16th August 2009, 23:57
  2. Interruptus Frustratus
    By Byte_Butcher in forum General
    Replies: 16
    Last Post: - 17th April 2009, 20:36
  3. Counter not counting !!!
    By lerameur in forum mel PIC BASIC Pro
    Replies: 24
    Last Post: - 20th February 2009, 22:15
  4. 16F628A - Stops if release power switch.
    By dene12 in forum General
    Replies: 16
    Last Post: - 14th February 2009, 07:57
  5. error on compiling
    By parker in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 16th June 2005, 14:31

Members who have read this thread : 1

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