Timer using Picbasic Pro


Closed Thread
Results 1 to 15 of 15
  1. #1
    Brutis's Avatar
    Brutis Guest

    Default Timer using Picbasic Pro

    Good Day,

    If anyone has code examples in Picbasic Pro using Timer1 Module for Pic16F876 & Pic16F877 and using a 4Mhz clock. That would be great. I basically need a 0.5 second timer so I can execute another function in the code.


    Thank You

    Brutis

  2. #2
    Join Date
    Feb 2003
    Location
    Salt Lake City, Utah USA
    Posts
    517


    Did you find this post helpful? Yes | No

    Default

    Brutis,

    Had you visioned using PBP interrupts, "Instant Interrupts," ASM interrupts, or polling for TMR1 overflows? This might help people help you.

    Paul Borgmeier
    Salt Lake City, Utah
    USA

  3. #3
    Brutis's Avatar
    Brutis Guest


    Did you find this post helpful? Yes | No

    Default Tmr1

    Hi Paul,

    Thanks for the reply, I'm using the timer1 module in PBP. But I cannot seem to get a 1 second timer from this. The most I can get is 5.24 ms before the interrupt flag gets set. Do you know how I can get a 1 second timer form TMR1?

    Regards,

    Brutis

  4. #4
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    Have a look at Olympic Timer (search the forum) to give you an idea of how TMR1 could be used to increment variables to count whatever time period you require.

  5. #5
    Brutis's Avatar
    Brutis Guest


    Did you find this post helpful? Yes | No

    Default Re-Olympic Timer.

    Hi Melanie,

    Thanks for the link. Did not fully understand all the code. Would you happen to know the simplist way to acheive a 1 second rate using the timer1. I'm using a 0.5 second rate now but I need more time to execute my code. 1 second would be great before the flag gets set. Any idea how to do this.

    Thanks

    Regards,

    Brutis.

  6. #6
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    Datasheet! *sigh*

    Timer1 ticks every 1uS @ 4MHz.

    It's 16 bits wide, therefore with a 4MHz clock will time 65.536mS between each Flag.

    Using the 1:8 prescaler your maximum time is 8*65.536mS=524.288mS

    So unless Microchip has shoved hardware into your PIC that mine doesn't have, the answer is NO, Timer1 will NEVER tick 1 second @ 4MHz.

    You can always use a Bit Variable as a Flag... If BitFlag=0 and Timer1Flag then half a second has elapsed - so Set the BitFlag. If BitFlag=1 and Timer1Flag then one second has elapsed, so reset the BitFlag and start over for a new Second.

    Homework: What has Timer2 got that Timer1 doesn't have?

  7. #7


    Did you find this post helpful? Yes | No

    Default homework

    Timer2 has a prescaler and a postscaler. Am I right Melanie, am I right??

  8. #8
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    Yup, you're right... Homework Part Deux: Which now means what's the maximum Time you can let Timer2 run for at 4MHz with all the prescalers and postscalers enabled?

  9. #9


    Did you find this post helpful? Yes | No

    Default homework

    Boy - you're putting me through the mill. I come up with 16.777 seconds. Am I right... am I right??

  10. #10
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    Close, but no cigar...(we're talking about Timer2 in a PIC16F876/877)...

  11. #11
    Join Date
    Mar 2006
    Location
    Gothenburgh, Sweden
    Posts
    18


    Did you find this post helpful? Yes | No

    Default

    Hi Melanie.

    Im working with a 18f4550 and using USB to communicate with a Visual basic program.
    I need to measure the time an input pin is activated.
    Normaly I use Pulsin but the USB commands must be polled every 1ms to keep contact with the computer. So how can I write a code that doesnt "halt" the program during the measuring?
    The performance must be the same as Pulsin, 2us reolution at 20Mhz and it would be great if I can measure up to 500ms instead of 130ms like Pulsin.

    Thanks in advance.

    Stefan.

  12. #12
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    I would use say CCP1 pin of the PIC and confiure the CCP Module in Capture Mode. In this mode, the module will capture the 16-bit value of the TMR1 Register when the CCP1 pin changes state. This will occur with minimal intervention from your program. The resolution at 20MHz will be in steps of 200nS to a maximum of 16 bits - ie 13.1mS. Use the integral Hardware of the PIC to help you with this one. Unfortunately you're limited to the size of the TMR1 register. Alternatively, you can use instant interrupts in conjunction with TMR1 to give you longer time periods if you enable the prescaler. Without knowing what's going on in your application it's difficult to recommend.

  13. #13


    Did you find this post helpful? Yes | No

    Default homework

    OK. Timer2 is only 8 bits wide (didn't see that before). 1uS prescaled by 16 = 16uS. Multiply by 8 bits (255) = 4080uS = 4.08mS. Multiply by 16 postscaler = 65.28mS. Am I right? Am I right? I don't want a cigar - we're playing for a pack of Pall Mall.

  14. #14
    Join Date
    Mar 2006
    Location
    Gothenburgh, Sweden
    Posts
    18


    Did you find this post helpful? Yes | No

    Default

    Thanks for your fast reply.

    In my application I have a wheel that rotates from 2rev/sec up to 110rev/sec. The wheel has a sensor wich is activated during 180degrees of a rotation. So during the first half of rot I measure the time it takes to turn 180deg and during the second half I will send the values to the computer.
    So the measured times will be in range from 4,5ms to 250ms or actually 4500us to 250000us to get a good accuracy.
    I must measure every rotation because I use the values to calculate the acceleration of the wheel.
    I found an interesting post here: http://www.picbasic.co.uk/forum/showthread.php?t=2803
    I will try to use some of this code but I need to extend the time to measure.
    Is it possible to detect when the timer rolls over and then save the current values in a word and then reset the timer and continue to measure? In my VB application I can add the words togeather to get the complete time. If the timer ticks in 2us interval @ 20Mhz then I belive that I can get up to 65000 something units of 2us. Then I need only two words to store the time in to be able to measure up to 250ms. Am I right about this? Is it possible to do? If this works i hope that I can use two timers to measure two wheels at the same time.

    Best Regards
    Stefan.

  15. #15
    Join Date
    Jan 2005
    Location
    Sweden
    Posts
    2


    Did you find this post helpful? Yes | No

    Default

    Hallå Stefan!

    Seems like it's a car/MC project.
    Laptimer with 0-100 (0-60 to our friends on secluded islands) "bragging rights meter"?

    Lycka till!
    Last edited by RRacer; - 10th April 2006 at 22:38.

Similar Threads

  1. can anyone here can convert this in picbasic pro?
    By reianjim in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 3rd December 2009, 11:43
  2. Replies: 3
    Last Post: - 1st July 2008, 21:07
  3. How to configure SPI in PICBASIC PRO?
    By moogle in forum mel PIC BASIC Pro
    Replies: 13
    Last Post: - 15th April 2007, 18:31
  4. Question for all that use MELABS PICBASIC PRO
    By oskuro in forum Off Topic
    Replies: 2
    Last Post: - 24th March 2005, 17:15
  5. PicBasic Pro & PicBasic syntax different
    By Billyc in forum General
    Replies: 5
    Last Post: - 16th April 2004, 21:19

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts