Creating timer in PIC

Thread Starter

RG23

Joined Dec 6, 2010
304
I want to create a timer of 25 us in PIC 16f887 and use the same timer for different functions.

Using that same timer I want to create different time intervals

If anyone has an idea please let me know
 

someonesdad

Joined Jul 7, 2009
1,583
For assembly code, look at the beginning lessons in the PICkit 2 Starter Kit user manual. A basic timer can be done in a few assembly language statements and you'd make it a subroutine. In C, you can call the _delay(us) function which delays the specified number of microseconds. Or, you can do it yourself with a busy-wait loop or use a timer and an interrupt.
 

Thread Starter

RG23

Joined Dec 6, 2010
304
Actually I have created a timer of 25 us but when I want to use that same timer to work as 10 ms timer it doesn't work
 

rmondeja

Joined Mar 7, 2011
1
It has been a while since I finish my last project with pics, but I'm pretty sure you can accomplish the 10 ms timer using two counters, from 25 us to 1 ms the ideal counter will be call 400 times and with 8 bits pics the maxim is 256, thats why it has to be done using two counters. The two counters products must give a result of 400, finally all this should be solved using conditionals
 

Thread Starter

RG23

Joined Dec 6, 2010
304
I have three tasks to perform say x1, x2, x3

I want to create only a single timer of 25 us which I have done.
Next
x1 has to be performed every 50 ms
x2 has to be performed every 500 ms
x3 has to be performed every 10 ms

Please let me know if anyone has an idea
 

someonesdad

Joined Jul 7, 2009
1,583
Did you bother finding the document I told you about? It gives you the solution you're looking for; it's only a few lines of code and easy to understand.
 

Thread Starter

RG23

Joined Dec 6, 2010
304
I could not find anything related to timer in PIckit2 Starter User manual.

I know how to define the timer.

But I want the same timer of 25 us to work as 3 timers of 10 ms, 500 ms , 50 ms at the same time

I haven't yet fixed that problem

Please let me know your opinion

Thanks
 

MMcLaren

Joined Feb 14, 2010
861
May I ask why you've chosen 25-usecs as your "base" timing interval? That's a very short interval. I would think a 1 or 10 msec "base" interval would be more appropriate for your 10-msec, 50-msec, and 500-msec intervals.
 

Thread Starter

RG23

Joined Dec 6, 2010
304
I am using that inteval timer because I also need the frequencies higher than 10 kHz

Where is there mention of Timer in Pickit2 manual??????
 

t06afre

Joined May 11, 2009
5,934
What kind of processing will you do at 10-msec, 50-msec, and 500-msec intervals. Will you just toggle som pins or run some code. That would be nice to know. I think we need more info about your project
 

Thread Starter

RG23

Joined Dec 6, 2010
304
yes I would be running some code during these time intervals

I also would like to know if it is indeed possible to run three different tasks at the same time using only one timer

Thanks
 

Thread Starter

RG23

Joined Dec 6, 2010
304
Following is the code I tried but not succeeded

I have to use timer of 25 us

Using that same timer I have to toggle bit 5 each of PORTA,B,C after every 10 ms, 50 ms, 500 ms respectively

#include <p16F887.inc>

org 0
Start:
goto SOC
org 4
goto ISR

SOC:
call Bank0
call Bank1
bcf INTCON,2
bsf INTCON,5

ISR:
btfsc INTCON,2
goto label1

label1:
movlw b'00100000'
xorwf PORTA
Bank0:
movlw 0xFB
movwf TMR0
return
Bank1:
BSF STATUS, RP0
movlw 0x61
movwf OSCCON
movlw 0x00
movwf TRISA
movlw 0x00
movwf TRISB
movlw 0x00
movwf TRISC
movlw 0x00
movwf OPTION_REG
BCF STATUS,RP0
return

End
 
Last edited:
Your code needs work, your main code just runs right into your interrupt routine, no retfie, no clearing the IF flag.

Try running with the MPLAB simulator. You could see what's happening while single stepping through your code.
 

Thread Starter

RG23

Joined Dec 6, 2010
304
I included retfie now
#include <p16F887.inc>

org 0
Start:
goto SOC
org 4
goto ISR

SOC:
call Bank0
call Bank1
bcf INTCON,2
bsf INTCON,5

ISR:
btfsc INTCON,2
goto label1
retfie
label1:
movlw b'00100000'
xorwf PORTA
retfie
Bank0:
movlw 0xFB
movwf TMR0
return
Bank1:
BSF STATUS, RP0
movlw 0x61
movwf OSCCON
movlw 0x00
movwf TRISA
movlw 0x00
movwf TRISB
movlw 0x00
movwf TRISC
movlw 0x00
movwf OPTION_REG
BCF STATUS,RP0
return

End
 
Top