Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

How to achieve 0.5s delay using Timer1 for PIC16F877?

Status
Not open for further replies.

bingoz2002

Junior Member level 1
Joined
Jul 12, 2005
Messages
15
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
Singapore
Activity points
1,468
i'm using Assembly Language (MPLAB) to control a stepper motor and am using the timer module and interrupt. I need a delay of 0.5s. However, the timer will cause an overflow(to call interrupt) after an increment of 0000h to FFFFh. This will take 1.04s. How do i achieve a delay of 0.5s using this timer? it seems like the increment from 0000h to FFFFh is fixed and could not be stopped at any other value. the clock frequency is 2MHz. help anyone..
 

Re: Timer1 for PIC16F877

bingoz2002 said:
i'm using Assembly Language (MPLAB) to control a stepper motor and am using the timer module and interrupt. I need a delay of 0.5s. However, the timer will cause an overflow(to call interrupt) after an increment of 0000h to FFFFh. This will take 1.04s. How do i achieve a delay of 0.5s using this timer? it seems like the increment from 0000h to FFFFh is fixed and could not be stopped at any other value. the clock frequency is 2MHz. help anyone..
Just before leaving the ISR routine initialize Timer1 to 7FFF, it will then overflow after 0.5sec.

CD :)
 

Re: Timer1 for PIC16F877

May i know which register/bit of timer1 is responsible for that??
 

Re: Timer1 for PIC16F877

bingoz2002 said:
May i know which register/bit of timer1 is responsible for that??
Just write 7F to the TMR1H register, and FF to the TMR1L register.

CD :)
 

Re: Timer1 for PIC16F877

i've made the changes in the program but it still gives the same results. in my program, the LED should form a blinkingl ight. However, the time taken for the LED to blink is approx. 1s. this is too long and had to change it to 0.5s. i've tried putting other values in TMR1L and TMR1H but still the same results, 1s. below are the code. can help me look where the mistakes lies??

#INCLUDE<P16F877.INC>

INIT ORG H'00'
NOP
CLRF PCLATH ;CLEAR PAGE BITS
BSF STATUS,RP0
BCF STATUS,RP1 ;BANK1
MOVLW B'11110000'
MOVWF TRISA ;CONFIGURE PORTD AS OUTPUT
MOVLW H'07'
MOVWF ADCON1


TIMERINIT CLRF PIE1 ;DISABLE PERIPHERAL INTERRUPTS
BSF PIE1,TMR1IE
BCF STATUS,RP0 ;BANK0
CLRF T1CON ;STOP TIMER1,PRESCALER 1:1
CLRF TMR1H ;CLEAR TIMER HIGH BIT REGISTER
MOVLW H'EF'
MOVWF TMR1H
CLRF TMR1L ;CLEAR TIMER LOW BIT REGISTER
MOVLW H'FF'
MOVWF TMR1L
CLRF INTCON ;DISABLE INTERRUPTS
CLRF PIR1 ;CLEAR PERIPHERAL INTERRUPTS FLAGS
BSF PIR1,CCP1IF
MOVLW 0X30

MOVWF T1CON ;PRESCALER 1:8, TIMER1 STOPPED
;OSC DISABLED
BSF INTCON,GIE


ONTIMER BSF T1CON,TMR1ON ;TIMER STARTS INCREMENTING

WAITOVERFLOW BTFSS PIR1,TMR1IF ;CHECKING FOR OVERFLOW TO OCCUR
GOTO WAITOVERFLOW ;KEEP WAITING

INTERRUPT COMF PORTA
BCF PIR1,TMR1IF ;CLEAR INTERRUPT FLAG
GOTO WAITOVERFLOW

END
 

Re: Timer1 for PIC16F877

I just woke up, so my brain is not very clear but I can see this already:

MOVLW H'EF'
MOVWF TMR1H
MOVLW H'FF'
MOVWF TMR1L

So you write H'EFFF' (D'61439') to Timer1 which corresponds in your case in a little less than 1 sec.

You need to write H'7FFF' (D'32767') to Timer1 for approx. 0.5sec.

I will have a closer look to your code when I'm really awake...:)

CD :)
 

Re: Timer1 for PIC16F877

've changed it to 7FFF but still the same. Has it got to do with the prescaler? i'm trying to change the prescaler to 4 instead of 8. i'll keep the result posted.

Added after 45 minutes:

i managed to obtain 0.5s delay by changing the prescaler and putting in the value h'7FFF' in Timer1. However, i'm still curios. WHat effect does the prescaler have on the timer. i don really understand this. i did research on it and all it says was that the prescaler determines the granularity.. what does dat mean? i realised that when the prescaler is set to 1:2 and 1:1, the blinking is even faster..
 

Re: Timer1 for PIC16F877

bingoz2002 said:
've changed it to 7FFF but still the same. Has it got to do with the prescaler? i'm trying to change the prescaler to 4 instead of 8. i'll keep the result posted.

Seting the prescaler to 4 will result in an interrupt frequency of:
500000/65536/4 -> 1.907 ... or 0.524 ...

If you need exactly 0.5 seconds you could try this code with prescaler set to 4:
INTERRUPT MOVLW H'0B'
MOVWF TMR1H
MOVLW H'E4'
MOVWF TMR1L
COMF PORTA
BCF PIR1,TMR1IF ;CLEAR INTERRUPT FLAG
GOTO WAITOVERFLOW

hope this helps and best regards
 

    bingoz2002

    Points: 2
    Helpful Answer Positive Rating
Re: Timer1 for PIC16F877

Does TMR1H and TMR1L serves any purpose in controlling the time. i did change the values but no effects were seen. Only by changing the prescaler then i could see thr change in time.
From research, we found that we need to set CCP in order to let TMR1L/H to take effects. IS this really necessary??
 

Re: Timer1 for PIC16F877

Hi
TMR1H are TMR1L simply registers that contain the timer value.

you can read and write to these registers.

If You write something to these registers you can make the timer overflow to occur quicker and hence you can control timing.

But Presclar scales down the cycles so that the filling of the these two registers can be controlled from them.

But actually I didnt get your question.

regards
Gopi
 

Re: Timer1 for PIC16F877

bingoz2002 said:
Does TMR1H and TMR1L serves any purpose in controlling the time. i did change the values but no effects were seen. Only by changing the prescaler then i could see thr change in time.
From research, we found that we need to set CCP in order to let TMR1L/H to take effects. IS this really necessary??

As I wrote above you will have to set the values for TMR1L and TMR1L in every timer1 interrupt to change the overflowrate ...

best regards
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top