Timer1 & array confusion; very new to PBP programming


Closed Thread
Results 1 to 9 of 9

Hybrid View

  1. #1
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,624


    Did you find this post helpful? Yes | No

    Default

    Hi,
    By default the TMR1 "counts" at at fOSC/4 which means that every "tick" is 0.5us when running at 8Mhz. TMR1 is 16bits wide so it will overflow every 2^16*0.5us = 3.2768ms - not very good if you want to measure 1.2s.

    There are several ways of doing what you want:
    * Use the TMR1 prescaler to scale down the rate at which TMR1 is incremented. Unfortunately the highest ratio is 1:8 which means that one tick now equals 4us instead of 0.5 and that the timer will overflow in ~26ms - still not good....

    * Use an external clock source for the TMR1 osc. A 32.768Hz clock x-tal will give you a "tick-rate" of 1mS meaning that the value you get from the TMR1 registers are in ms and that you can measure times up to ~32 seconds.

    * Use the TMR1 interrupt and count the number of interrupts it generates. If you let it free-run it will cause an interrupt every 3.2768ms but you can stop and reload it in the interrupt service routine so it that it overflows and generates the interrupt every ms if you want to.

    To 'get the job done' I'd go with the second alternative. To get more used to the chip and PBP I'd go for the third alternative.

    The CLEARWD is not needed, PBP adds that automatically as long as you don't tell it no to.

    HTH.
    /Henrik.

    EDIT: Also, you do not use RESUME to return from a subroutine to which you jumped with a GOSUB as is the case with your SetTimer sub. You use RETURN. RESUME is for returning from an interrupt routine. I see that you also have a RESUME at the end of the EndTime sub to which you jump with a Goto - that won't work either....
    Last edited by HenrikOlsson; - 5th April 2010 at 12:07.

  2. #2
    Join Date
    May 2007
    Posts
    604


    Did you find this post helpful? Yes | No

    Default

    With an 8MHz clock, Timer1 overflows in 2^16*0.5us = 32.768ms - with the 1:8 prescaler it overflows in 262mS.

  3. #3
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,624


    Did you find this post helpful? Yes | No

    Default

    Oops, absolutly correct. I messed that one up by a factor of 10, sorry for the confusion.

  4. #4
    Join Date
    May 2007
    Posts
    604


    Did you find this post helpful? Yes | No

    Default

    Don't worry about it - I made a similar error on the MC forums this morning. Need to remember to get my morning cup of coffee first.

  5. #5
    Join Date
    Apr 2010
    Posts
    5


    Did you find this post helpful? Yes | No

    Default

    Thank you guys for such a quick response. And thanks for pointing out that error with the resume instead of a return, must have been out of my mind!

    Also, I would like to ask, if what i posted there is the right way of storing in an array?

    is it like:

    "arraystore[i] = newdata?"

    as for the timer base, i will still have to dig deeper into using interrupts and timer1 function..

    Any extra advices or pointing out of co-existing codes that I could look up to will be appreciated!

  6. #6
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,624


    Did you find this post helpful? Yes | No

    Default

    Hi,
    Your array code seems OK to me, like:
    Code:
    myArray VAR Word[3]  'Create an array of 3 words.
    i VAR byte
    
    For i = 0 to 2
      myArray[i] = 12345  'Store the value 12345 in array locations 0, 1, and 2
    NEXT
    The absolute easiest way to get started with interrupts is to use Darrels Instant Interrupt routines, they're available on his website. Have a look at the "timer template" - that might be a good starting point for your application.

    /Henrik.

  7. #7
    Join Date
    Apr 2010
    Posts
    5


    Did you find this post helpful? Yes | No

    Default

    thanks for your reply! Now, I'm actualling trying to use the SPI capabilities of the 16F88 that will be connected to an arduino. To my understanding, when data is transfered, it is in <binary>? but my questions is, in my programming compiler, do i use the same keywords with the values needed so that the master/slave can communicate?

    I am trying to understand, how is it that I can tell pic A to send this array of information (say a Keypad Code Input of 1,2,3,4) and send it to pic B who is the slave, which then will do something else, and returns a high value or confirmation to pic A that code is verified -> please do this, this and this...

    My professor just tells me about it being sent across as data on the same baud rate/frequency but i dont understand how this 2 diff pics can understand diff languages..

    Any explanation at all, would be greatly appreciated! this forum definitely has really talented and helpful forumers!

Members who have read this thread : 0

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