Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Resetting Timer1 in a PIC

Status
Not open for further replies.

richacm

New Member
Hi,

I am doing some time specific processing in my PIC16F690 and I keep track of it via timer 1. However I am suspicious that the timer is not being reset when I ask it to. Can someone check that what I am doing is correct for resetting Timer 1? Please excuse the rough syntax.

Setup timer - Do this just once at the start of the program
TMR1IE.PIE1 = 1 Enable timer 1 interrupt
INTCON = %11000000 Set the PEIE and GIE bits
PIR1.TMR1IF = 0 Clear the interrupt flag

Reset portion - this gets recalled whenever I want to reset the timer
T1CON = %00110000 Turn on 1:8 prescalar - turn timer off
TMR1H = $00 Set high register to 0
TMR1L = $00 Set low register to 0
PIR1.TMR1IF = 0 Clear any interrupt flags
T1CON.TMR1ON = 1 Finally turn the timer on

Interrupt option - this shouldn't get called but have it here to handle any errors
PIR1.TMR1IF = 0 Clear the interrupt
--- I set another non timer related flag here as well -----

Whenever I check what the timer values are I do:
TMR1H * 256 + TMR1L

I am not 100% sure but I think my timer is not starting at 0 again when I do a reset. Can anyone see a problem?

Many thanks,

Craig
 
Last edited:
i would recommend clearing the interrupt flag being the first thing you do in a interrupt.

Im not 100% nor 50% sure but i dont think the TMR1H/L is what you are supposed to count with. I think you need like a separate variable for that.
 
Last edited:
What I am trying to do is decode a 'Manchester coding' signal. The first 20 bits that are coming across are the header bits and I am going to use these to synchronise the frame size so that I can accurately decode the rest of the message. The sender can then alter the frequency and the decoder will compensate accordingly.

To do this I am timing the low and high pulses from the header and then setting variables indicating time limits...i.e. minimum pulse size = quarter a frame, maximum pulse size = 3/4 frame, maximum frame size = 1 1/4 frames. Based on these calculated variables I can then read the rest of the transmition.

To synchronise the header I am starting the Timer 1 and then when a transition occurs I read what the timer 1 value is and then reset the timer and wait for another transition. I allow 12 pulses to synchronise.

Cheers,

Craig
 
I have been doing some more testing and have come across this additional problem. I keep getting an interupt when the TMR1L gets to $FF and increments the TMR1H register. Where-as the documentation for the PIC16F690 says that the interupt will only occur when the timer rolls from $FFFF to $0000. Is this something expected?

Thanks,

Craig
 
I have been doing some more testing and have come across this additional problem. I keep getting an interupt when the TMR1L gets to $FF and increments the TMR1H register. Where-as the documentation for the PIC16F690 says that the interupt will only occur when the timer rolls from $FFFF to $0000. Is this something expected?
If you're getting an TMR1IF bit set when TMR1L rolls over, it can only be because TMR1H was already $ff. It's a 16-bit timer only. There can be no interrupt at 8-bits, unless you're preloading TMR1H/L in the ISR. TMR1L would already have rolled already rolled over 255 times as it incremented the TMR1H MSByte, but the one you see is that last one when TMR1H/L reach $ffff and roll over, triggering the interrupt (and probably hitting your breakpoint).
 
Last edited:
My code is as in the original post. I did a test where the timer reached 175...I caught the value and got the result (I read the TMR1H register as well). I then doubled this figure so the timer should have reached 350, instead I get an interrupt firing.

My only guess is that I have a faulty PIC. I have ordered another one.

Craig
 
Inside the interrupt I outputted the TMR1H and TMR1L to some LED's. When the interrupt occurs TMR1H = 0 and TMR1L = FF - weird! So TMR1H wasn't FF when it interrupted.

I must be doing something wrong....:mad:
 
Did not check the datasheet, but is it possible to run that timer as an 8bit counter?

You can also do a BTG on one of your output port each time the IRQ happens, then run MPLAB SIM and use the scope to measure how long the time interval between the BTG's are. From that you can work out if it is really giving the IRQ at FF. (Suppose the stopwatch could also be used to measure the time).
 
Craig,

Could your TMR1 registers be overflowing?

On a signal transition are you turning off Timer 1 before reading TMR1 registers?

Have you considered using CCP module "capture" mode which will copy the exact timer value into the CCPR1 registers on the transition?

Mike
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top