Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

yet another PIC timer question

Status
Not open for further replies.

CodyG

Newbie level 2
Joined
Dec 1, 2011
Messages
2
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
Ontario, Canada
Activity points
1,299
Hello, I discovered these boards earlier this week and found it very useful, so thanks to everyone already!

I am having a hard time understanding timer1 for a PIC18F4520. I am running an 8MHz oscillator and I want to have it interrupt every 10 ms.

My understanding of this is that the timer would increment the counter (with a 1:8 prescaler) every 4uS:

Period = (1/(8MHz/4) x 8) =4uS

Using it in 16 Bit mode, the counter will count is 65,536 times before it reaches overflow, so I would be getting an interrupt every 262.144mS.

To get a 10 mS interrupt I only want the counter to count 10mS/4uS = 2500 times.

Do I just subtract 2500 from 65536 to get 63306 and place it in the TIMER1H and TIMER1L registers in order to get my required delay?

Thank you in advance.
 

Hi and Welcome,

Yes thats basically what you do, though have not checked your maths.

In your Main code you preload TMR1H, and L if you are particular, then start TMR1 and the Interrupts.

In the ISR, after testing if TMR1IF is now set , you preload TMR1H / L again then Clear TMR1 IF.

Dont forget that on entering the ISR, global interrupts are automatically turned off and turn on again with the Retfie, so don't spend too much time in your ISR or you might miss another TMR1 IF
 

Basically yes, thats what you do. The interrupt is generated as the timer counts from 65535 to 65536 (which is effectively zero because you run out of bits to count with). So in order to create a delay you use the number of counts it takes to reach that point You might have to take into account the time to execute code to initially load the counter and to respond to the interrupt to get exact timing.

By my calculation, excluding extra execution times:
with prescaler 1:8 use 63036 (not 63306 as you stated)
with prescaler 1:4 use 60536
with prescaler 1:2 use 55536
with no prescaler (1:1) use 45536

If your delay is repeating, it's a good idea to reload TMR1H first so there is less chance of another interrupt occuring while you are loading TMR1L although disabling the timer while you load it is safest.

Brian.
 
  • Like
Reactions: CodyG

    CodyG

    Points: 2
    Helpful Answer Positive Rating
great thank you!

I am unable to test the code right now, which is why I was asking. The timing doesn't need to be exact since this is just for toggling the clock for a stepper motor.

And thanks for catching my typo, luckily it didn't make it's way into my conversion to hex.

This is my resulting code so it seems all will be well.

Code:
 movlw	0xF6			;reload timer for 10ms
			movwf	TIMER1H,A		;8MHz, 1:8 Prescale, 8bit operation
			movlw	0x3C			        ;Period: (1/(8MHz/4) x 8) =4uS
			movwf	TIMER1L,A		        ;10mS/4uS = 2500
			btfsc	        PORTA,EnX,A	        ;65536-2500 = 63036 = F63C
			goto	        stepX			;If x is enabled, step X
LP_TestY:
			btfsc	        PORTA,EnY,A		;If Y is enabled, step Y
			goto	        stepY
LP_TestY_Done:			
			bcf		PIR1,TMR1IF,A	;Reset the timer interrupt flag
			return
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top