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.

:: Timer 2 Woes ::

Status
Not open for further replies.

suby786

New Member
Hey all

ive become quite proficient with PICs, the one im using currently is the PIC16F877A...very common i know and im using TIMER1 really well... Its been quite user friendly

However i need another timer, and if i can help it, id like to use another timer and not use multiples of timer 1.

I need timer 2 OR timer 0 to count up to 0.1ms (20MHz clock) so loading the PRESCALER to 1:4 will allow this.

But Timer0 doesnt indictae how to TURN ON or TURN OFF the timer, i need to be able to control it like TIMER1. Timer 2 has this functionality but gets me a bif baffled. The use of the PR2 register, if i load this with 125, then with the values stated, i should get an interrupt at 0.1ms, but i dont

I cant see where ive gone wrong? heres the timer code

Code:
         BSF		T1CON, T1CKPS1				;THESE 2 LINES
	BSF		T1CON, T1CKPS0			      ;SET PRESCALER TO 8
	BCF		T1CON, T1OSCEN
	BCF 	         T1CON, TMR1CS
	BCF		T1CON, TMR1ON				;STOPS TIMER

	CLRF	T2CON
	BSF		T2CON, T2CKPS0				;POSTSCALER TO 4
	
	MOVLW	h'FE'			;reset timer1 values 0.5MS
	MOVWF	TMR1H
	MOVLW	h'C7'			;reset timer1 values 0.5MS
	MOVWF	TMR1L
	


	
	BSF		INTCON, PEIE
	BCF 	        PIR1, TMR1IF
	BCF 	         PIR1, TMR2IF

	BSF		STATUS, 5			;Change to BANK 1
	BSF		PIE1, TMR1IE	
	BSF		PIE1, RCIE
	BSF		OPTION_REG,	INTEDG	
	BCF		STATUS, 5			;Change to BANK 0

	BSF		INTCON,	INTE	;Enable EXTERNAL INTERRUPT
	BSF 	INTCON, GIE		;GLOBAL INTERRUPT ENABLE

	BSF		T2CON, TMR2ON		;STARTS TIMER
	BSF		PORTC, 1
 
Timer2 is FAR easier, and much more useful than timer0 (which is really a bit naff!), but timer2 counts up until the value equals PR2, then starts counting from the beginning again. Check my seven segment LED tutorial at which uses a timer2 interrupt to multiplex the displays.
 
I've done up quick examples of all three timers, and converting it back to assembly from PIC Basic would not be too hard;

**broken link removed**

The best timer calculator I have found is here;

http://www.best-microcontroller-projects.com/pic-timer-0.html

TMR2 is definitely the best low maintenance timer, as there is no additional code required once it is running.

Just on TMR0, you can disable TMR0 interrupts via INTCON.5 and its just as handy for mid length type delays.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top