Creating timer in PIC

Discussion in 'Embedded Systems and Microcontrollers' started by RG23, Mar 7, 2011.

  1. RG23

    Thread Starter Active Member

    Dec 6, 2010
    301
    2
    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
     
  2. someonesdad

    Senior Member

    Jul 7, 2009
    1,585
    141
    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.
     
  3. RG23

    Thread Starter Active Member

    Dec 6, 2010
    301
    2
    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
     
  4. RG23

    Thread Starter Active Member

    Dec 6, 2010
    301
    2
    I tried to increment the variable to reach upto 10 ms but it didn't work
     
  5. rmondeja

    New Member

    Mar 7, 2011
    1
    0
    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
     
  6. RG23

    Thread Starter Active Member

    Dec 6, 2010
    301
    2
    Which conditionals are you talking about?
     
  7. RG23

    Thread Starter Active Member

    Dec 6, 2010
    301
    2
    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
     
  8. someonesdad

    Senior Member

    Jul 7, 2009
    1,585
    141
    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.
     
  9. RG23

    Thread Starter Active Member

    Dec 6, 2010
    301
    2
    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
     
  10. RG23

    Thread Starter Active Member

    Dec 6, 2010
    301
    2
    If you have the link to that document, please paste here

    Thanks
     
  11. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
  12. MMcLaren

    Well-Known Member

    Feb 14, 2010
    759
    116
    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.
     
  13. RG23

    Thread Starter Active Member

    Dec 6, 2010
    301
    2
    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??????
     
  14. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    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
     
  15. RG23

    Thread Starter Active Member

    Dec 6, 2010
    301
    2
    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
     
  16. RG23

    Thread Starter Active Member

    Dec 6, 2010
    301
    2
    I would appreciate any help in solving this timer issue
     
  17. RG23

    Thread Starter Active Member

    Dec 6, 2010
    301
    2
    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: Mar 8, 2011
  18. blueroomelectronics

    AAC Fanatic!

    Jul 22, 2007
    1,758
    98
    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.
     
  19. RG23

    Thread Starter Active Member

    Dec 6, 2010
    301
    2
    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
     
  20. blueroomelectronics

    AAC Fanatic!

    Jul 22, 2007
    1,758
    98
    Single step through your code in the simulator. Putting your main code after the interrupt routine is traditional. There are several mistakes in your coding.
     
Loading...