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.

Clock with Timer1

Status
Not open for further replies.

PIC2PIC

New Member
Hi!

I want to make a clock by using Timer1. I made all the other code exept that I don't know how to use Timer1. Is there some simple explanation how to use Timer1, i.e. what registers bits I have to set and how to count a second, and after that count of second how to cause interrupt.

Thank you
 
This is the fastest answer I ever saw.

I hope I will know how to use this. It looks complicate. Thank you anyway.
 
@birdman0_o :What chip and what language, I have code for the 16f628a in asm using four 7 segment displays

The PIC is 16f877 , I use MPLAB 8,40 and write code in assembly. I already done all hardware work and made code that use WDT but is terribly inaccurate. So I am suggested to use Timer1 whitch I newer used before. So I ask for advice.
 
@birdman0_o

Thank you man. You are very kind. But is there some simple explanation what registers I have to set.
 
Yes. You can look at the datasheet of the 877 and it has the timer1 module explination. I will give you a basic description now though:

The timer1 is 16 bits, at 4mhz it can get down to 0.5 hz, which if you count twice will give you one second, 120 times will give you one minute.

You will need to turn on the timer, set the prescaler at it's max, and then set the tmr1h and tmr1l register to get the exact number of cycles.

You will want to cause an interrupt once it has overflowed.

Good so far?
 
Thank you. I made it.
Now I have to make it tick accurately every second. It is now that ticks about 1.1 second or so. What do I do?
I have quartz 4MHz.
 
How do I use this STOPWACH.
I get some results but I dont know what they mean. There is in the attachment results.
 

Attachments

  • Rezul.JPG
    Rezul.JPG
    41.1 KB · Views: 178
Here you go,

Code:
main
		movlw	B'00000001'
		movwf	T1CON			;enable timer 1

		movlw	Low(.1000000/.100)	;interrupt every
		movwf	CCPR1L			;10mS
		movlw	high(.1000000/.100)
		movwf	CCPR1H
		movlw	B'00001011'		;enable special event 
		movwf	CCP1CON;		;trigger on CCP1	


Loop		btfss	PIR1,CCP1IF
		goto	Loop

		bcf	PIR1,CCP1IF    ;will execute this instruction every 10mS
		goto	Loop

The above code will set PIR1,CCP1IF every 10mS. If you enable interrupts it will generate an interrupt every 10mS.

Mike.
 
Last edited:
@Pommie
Thank you Pommie. Could you explain this code please. I never used CAPTURE/COMPARE/PWM MODULES before.
 
Have a read of the "special event trigger" in the datasheet. It explains how it works. If it's still not clear then ask a specific question. Or, you could just use it.

Mike.
 
Thank you Pommie.

Could you please explain me following lines in your code :

movlw Low(.1000000/.100) ;interrupt every
movwf CCPR1L ;10mS
movlw high(.1000000/.100)
movwf CCPR1H

i.e. what is this word "Low " and "High " mean, I didn't know this "Instructions" (if they are ? ) and what are these values in the parentheses.
 
They just return the low or high byte of the (16bit) expression. In this case the expression is 1 million (the instructions per second) divided by 100 to get a 10mS time period. You could do the maths yourself and get 10,000, then convert to hex, 0x2710 and split it into the two numbers, 0x27 and 0x10. I prefer to let the assembler do the maths for me.

Mike.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top