# Newbie needs help with timer1 delay please

Status
Not open for further replies.

#### Schwuppes

##### New Member
Hi everyone!

Im very new to PIC programming and hope to get some help with timer1... I'm using MPLAB IDE 8.20

I am required to create a 75mS delay with timer1.
I am using a PIC 16F870 with a 4Mhz crystal
So the instruction cycle is 1 microsecond.

Sorry Im really totally new to this and just havent got my head around programming yet. I havent found much help on timer1 on the net either.

I have used the following formula to get the number i have to put into TMR1 to achieve a 75mS delay:

65536 - (75mS x (1Mhz / 8)
which gives me 56161 which is DB61 in hex
So I then load DB into TMR1H and 61 into TMR1L. Is this right so far?

And this is what Ive written so far:

BCF PIR1,0
CLRF TMR1L
MOVLW 0X0B
MOVWF TMR1H
MOVLW 0XDC
MOVWF TMR1L

TIME1

BSF T1CON,0
BTFSS PIR1,0
GOTO TIME1

EDIT:
For some reason this code is only giving me a 62mS delay in the stopwatch

To be honest Im a bit stuck right now as to how to continue from here... hope someone can point me in the right direction.

cheers!

Last edited:

#### Pommie

##### Well-Known Member
You are on the right track but I can't see where you setup T1CON. As the delay you need is 75,000 instructions and timer1 can only count to 65,536 then you need a prescaler. You appear to have chosen 8 when 2 would provide better accuracy. You can also use the assembler to do the calculation.

Code:
Period	equ	0x10000-(.75000/2)

movlw	0x10		;set timer 1 to prescaler 2
movwf	T1CON		;but don't start it yet

movlw	high(Period)
movwf	TMR1H
movlw	low(Period)
movwf	TMR1L

bcf	PIR1,TMR1IF	;clear interrupt flag
bsf	T1CON,TMR1ON	;start timer 1
wait	btfss	PIR1,TMR1IF	;wait for interrupt flag
goto	wait
Mike.

#### Schwuppes

##### New Member
Thanks very much Mike I greatly appreciate your help. It works now! I will visit this board more often I think.

Seems like I don't quite understand how the prescaler works, I thought using a prescaler of 8 would have made it more accurate and not the other way around

Greetings from Perth

Status
Not open for further replies.