Connect with us

PIC Delay

Discussion in 'Microcontrollers, Programming and IoT' started by sid2286, Sep 7, 2012.

Scroll to continue with content
  1. sid2286

    sid2286

    102
    1
    Aug 24, 2011
    I 'm using a PIC microcontroller 18f2520 with CCS compiler,

    I want to create a delay of 1 sec using Timer1. I have a crystal of 4Mhz and controller itself divides internally with 4,(Given in datasheet).

    Now i use a prescaler of 8, and since it is a 16bit timer, i get a maximum delay of approx. 524ms.

    I want to use a timer interrupt every 1 sec , How do i do that??

    Regards,
    Sid
     
  2. Harald Kapp

    Harald Kapp Moderator Moderator

    10,810
    2,438
    Nov 17, 2011
    Set timer1 such that the delay is 500ms. Have the timer call an ISR (interrupt service routine) that counts the number of interrupts and only after every second interrupt calls the intended function. This will result in a 1 second (2*500ms) call intervall for the function.

    In fact, you don't have to "count" within the ISR. just set a flag at the fisrt interrupt. Clear the same flag at the second interrupt. Call the function only when clearing the flag.
     
  3. sid2286

    sid2286

    102
    1
    Aug 24, 2011
    Ahh yeah..tat was easy...thanx! myself should have thought about it!

    My bad :(

    thanks man once again!

    Regards,
    SId
     
  4. foTONICS

    foTONICS

    332
    9
    Sep 30, 2011
    Why dont you just force a value into the timer and compensate for the missing instruction times?
     
  5. sid2286

    sid2286

    102
    1
    Aug 24, 2011
    oh I tired that, but as per my spec I'm using a pic18f2520 with 4Mhz crystal and max perscalar that is available is 8, on timer1 which is 16bit.(so max count is 65535)

    So maximum delay I can provide is 524ms.

    I cannot load anyother value.

    :(

    Rgrds,
    Sid
     
  6. foTONICS

    foTONICS

    332
    9
    Sep 30, 2011
    Then just nest a timer that is a factor of the total time you need inside another loop which counts the number of times the inner loop runs
     
  7. foTONICS

    foTONICS

    332
    9
    Sep 30, 2011
    Edit: just realized harold just said that
     
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Electronics Point Logo
Continue to site
Quote of the day

-