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.
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.
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:
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...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
/Henrik Olsson.
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...
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):
To PRESET it you write whatever value you wish to the two registers: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
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:TMR0H = 3 'Preset TMR0 to 878.... TMR0L = 110 ' ( 3 * 256 + 110 )
HTHCode: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
/Henrik Olsson.
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.
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?.
Bookmarks