Continue to Site

# 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

#### birdman0_o

##### Active Member
What chip and what language, I have code for the 16f628a in asm using four 7 segment displays.

#### PIC2PIC

##### New Member
This is the fastest answer I ever saw.

I hope I will know how to use this. It looks complicate. Thank you anyway.

#### PIC2PIC

##### New Member
@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

##### Active Member
Would you like to have my code to look at?
I don't want it to make this a confusing experience

#### PIC2PIC

##### New Member
@birdman0_o

Thank you man. You are very kind. But is there some simple explanation what registers I have to set.

#### birdman0_o

##### Active Member
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?

#### blueroomelectronics

##### Well-Known Member
What clock / oscillator are you using?

#### PIC2PIC

##### New Member
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.

#### blueroomelectronics

##### Well-Known Member
Run your code on the MPLAB simulator. It has a stopwatch.

#### PIC2PIC

##### New Member
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
41.1 KB · Views: 159

#### Pommie

##### Well-Known Member
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:

#### PIC2PIC

##### New Member
@Pommie
Thank you Pommie. Could you explain this code please. I never used CAPTURE/COMPARE/PWM MODULES before.

#### Pommie

##### Well-Known Member
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.

#### PIC2PIC

##### New Member
Thank you Pommie.

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.

#### Pommie

##### Well-Known Member
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.

Replies
6
Views
2K
Replies
9
Views
1K
Replies
6
Views
863
Replies
25
Views
2K
Replies
5
Views
554