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.

Timer1

Status
Not open for further replies.

richb

New Member
Hi

I'm currently using timer1 on the PIC16F877a but getting very confused with the math. The chip is hooked up to a 20mhz crystal.

I need to throw an interrupt every 10MS so I need to put an initial value into the TMR1L, TMR1H registers but not sure how to calculate this. Any help would be appreciated.

Thanks

Rich

have looked at previous threads + data sheet but still confused btw.
 
Last edited:
using 20MHz and 4 cycle per one count- review datasheet for pic16f877a- so
5MHz = 5*2^20 = 5242880
(1/10us)=100KHz
=> 5242880/100K = 52.4288 ~ 34 h
=>FFFF-34 = FFCB
so you have each time to load TMR1L,TMR1H with this value FFCB .
 
Eng.Abbasi said:
using 20MHz and 4 cycle per one count- review datasheet for pic16f877a- so
5MHz = 5*2^20 = 5242880
(1/10us)=100KHz
=> 5242880/100K = 52.4288 ~ 34 h
=>FFFF-34 = FFCB
so you have each time to load TMR1L,TMR1H with this value FFCB .

I know a Megabyte is 2^20 because it's the closest think to 1,000,000 in base 2, but I always thought Megahertz was excactly 1,000,000 cycles since we all live in a base 10 world.
 
Eng.Abbasi said:
using 20MHz and 4 cycle per one count- review datasheet for pic16f877a- so
5MHz = 5*2^20 = 5242880
(1/10us)=100KHz
=> 5242880/100K = 52.4288 ~ 34 h
=>FFFF-34 = FFCB
so you have each time to load TMR1L,TMR1H with this value FFCB .


What??? 5MHz is not 5*2^20. It is 5,000,000! In addition, timer 1 increments and overflows after FFFF so your math gives a 10us interrupt rate

20Mhz crystal = 5MHz instruction rate
(frequency * period) 5,000,000 * 0.01 = 50,000 so you would load Timer 1 to:
0x0000-0xC350 = 0x3CB0 (overflows on the transition from 0xFFFF to 0x0)
and it will take exactly 10ms to overflow.

Nigel is correct that Timer 2 is a better option for periodic timing. It has a period register that triggers an interrupt and resets the timer every time a match is made. After the initial setup, there is no code overhead to get periodic interrupts. If you use timer 1, you will have to immediately reload the timer registers (minus a few clock cycles) in order to have continued interrupts.
 
Can I suggest using the Special events trigger of ccp1. It resets timer1 after a preset time.

This code will set it up to produce a 10mS interrupt.
Code:
		movlw	B'00000001'
		movwf	T1CON;		enable timer 1
		movlw	low(50000);	Lets interrupt every 1/100th 
		movwf	CCPR1L;		of a second using the special
		movlw	high(50000);	event trigger on timer 1
		movwf	CCPR1H
		movlw	B'00001011';	enable special event trigger on CCP1
		movwf	CCP1CON;	
		bsf	STATUS,RP0
		bsf	PIE1,CCP1IE;	enable CCP1 interrupt
		bcf	STATUS,RP0
		movlw	0C0h
		movwf	INTCON;		enable Peripheral interrupts

In your interrupt you have to reset the CCP1 interrupt bit with, bcf PIR1,CCP1IF.

HTH

Mike.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top