Go to Post All in all, you get what you celebrate. The more we celebrate the under-served parts of our team the more students step up and make that part great. - Alpha Beta [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Closed Thread
 
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 24-02-2007, 17:36
Stvn's Avatar
Stvn Stvn is offline
FIRST Competition Competer
AKA: Steven Rhodes
FRC #0100 (WHS/CHS - WildHats)
Team Role: Leadership
 
Join Date: Feb 2007
Rookie Year: 2004
Location: Woodside, CA
Posts: 90
Stvn is an unknown quantity at this point
Send a message via AIM to Stvn
Lightbulb Re: Real time clocks, out of the question?

Quote:
Originally Posted by SgtMillhouse648 View Post
Hi, We too have had the same problem, in easyC there are a total of 6 timers, however, we haven't been able to get any of them to work, i believe timer 1 is used for the camera, but we cant seem to get any of the other timers to work. If any other teams have been able to get them to work, it would be extremely helpful.
Thanks
Malhon
Remember to preset and start the timers before using them. Timers won't work if they aren't running.
__________________
  #2   Spotlight this post!  
Unread 25-02-2007, 10:32
kaszeta's Avatar
kaszeta kaszeta is offline
Registered User
FRC #0095 (Grasshoppers)
Team Role: Mentor
 
Join Date: Feb 2004
Rookie Year: 2002
Location: Lebanon, NH
Posts: 334
kaszeta is a glorious beacon of lightkaszeta is a glorious beacon of lightkaszeta is a glorious beacon of lightkaszeta is a glorious beacon of lightkaszeta is a glorious beacon of light
Re: Real time clocks, out of the question?

There's a nice set of interface functions for using Timer1 here: http://www.chiefdelphi.com/forums/sh...ht=wallclock.c
  #3   Spotlight this post!  
Unread 25-02-2007, 13:41
Dave K.'s Avatar
Dave K. Dave K. is offline
Engineer/Mentor
FRC #0930
Team Role: Mentor
 
Join Date: Jan 2007
Rookie Year: 2005
Location: WI
Posts: 91
Dave K. is a splendid one to beholdDave K. is a splendid one to beholdDave K. is a splendid one to beholdDave K. is a splendid one to beholdDave K. is a splendid one to beholdDave K. is a splendid one to beholdDave K. is a splendid one to behold
Re: Real time clocks, out of the question?

The way Kevin's code sets up the interrupt is the best. The other example code posted/referenced all appear to reload the Timer register each time the interrupt is serviced, and the interrupt service latency then becomes an additive value to the timer's period. To some extent, only reloading the upper byte will help avoid the problem, but its easy to avoid the problem all together by just configuring the timer differently. An added bonus is that a few instruction cycles are removed from the ISR by not having to reload the register each time.


Another thing to remember here is that this microcontroller does not have a divide instruction, so including divide operations in an interrupt routine is forcing the microcontroller to execute a lot of instructions on a frequent basis.


Some of the examples provided utilize variables wider than 8 bits, but I didn't notice any cautionary notes for routines that access these variables outside of the interrupt service routine. Because this micrcontroller operates on 8 bits at a time, if a routine were testing a 16 or 32 bit variable and an interrupt occurred in the middle of such a test, the ISR will end up invalidating the foreground operation that it interrupted. No amount of 'volatile' declarations will fix that problem. If the timer value absolutely MUST be greater than 8 bits, then the foreground operations that access this variable must temporarily disable the relevant timer interrupt prior to accessing or manipulating the variable, then re-enable the interrupt after the operation is complete.


I tend to use timers that count down to zero because most microcontrollers are able to test for a zero condition in a single instruction, whereas testing for a specific value tends to use two or more instructions... and this is something which is true for the PIC18.

In other words:

Code:
  if (a) {
     do_something();
  }
Will generate fewer instructions than:

Code:
  if (a>59) {
    do_something();
  }

As such I tend to write timer ISR's this way:

Code:
   // 1ms timer tick ISR

   if (Timer1ms) {
      --Timer1ms;
   }


   if (!(--Timer10)) {
      Timer10 = 10;

      if (Timer10ms) {
         --Timer10ms;
     }

     if (!(Timer100)) {
        Timer100 = 10;

        if (Timer100ms) {
           --Timer100ms;
        }

        if (!(Timer1000)) {
           Timer1000 = 10;

           if (Timer1s) {
             --Timer1s;
          }
       }// !Timer1000
     } // !Timer100
    } // !Timer10
}
In order to use these timers in the foreground code, it is as simple as:


Code:
    Timer10ms = 100;    // Start Timer, 100 * 10ms = 1 second


// elsewhere

    if (!(Timer10ms)) {   // if timer expired
       do_something();
    }
Likewise, if you want the expiration of a timer to set an event flag, the ISR could be modified like this:

Code:
    if  (Timer) {              // if timer is running
       if (!(--Timer)) {      // decrement, and if it hits zero
          Event_Flag = 1;   // set event flag
      }
    }
So the foreground code could do something like this:

Code:
    Event_Flag = 0;  // make sure event flag is cleared
    Timer = 100;      // start timer

// elsewhere

    if (Event_Flag) {
       do_something();
    }
Clearly individual timers can be declared as needed, at whatever the most appropriate resolution for a given function may be. In general, I have found that most timers can be designed to work within an 8 bit value... in other words, if I need a timer that normally needs to run for a 1 second period, that a 10ms resolution timer, loaded with 100 provides more than enough precision.
__________________
--Dave
  #4   Spotlight this post!  
Unread 25-02-2007, 14:19
AdamHeard's Avatar
AdamHeard AdamHeard is offline
Lead Mentor
FRC #0973 (Greybots)
Team Role: Mentor
 
Join Date: Oct 2004
Rookie Year: 2004
Location: Atascadero
Posts: 5,503
AdamHeard has a reputation beyond reputeAdamHeard has a reputation beyond reputeAdamHeard has a reputation beyond reputeAdamHeard has a reputation beyond reputeAdamHeard has a reputation beyond reputeAdamHeard has a reputation beyond reputeAdamHeard has a reputation beyond reputeAdamHeard has a reputation beyond reputeAdamHeard has a reputation beyond reputeAdamHeard has a reputation beyond reputeAdamHeard has a reputation beyond repute
Send a message via AIM to AdamHeard
Re: Real time clocks, out of the question?

I wrote some code that starts uses the timer mentioned above that turns on an LED 1:30 seconds into the operator controller part.

Our team has a "Heads up display" we used last year for shooting, we remade it this year but this was the only use we could come up with.
  #5   Spotlight this post!  
Unread 25-02-2007, 14:38
evan_wilson's Avatar
evan_wilson evan_wilson is offline
QUARK
AKA: Evan Wilson
FRC #2172 (Street Legal)
Team Role: Programmer
 
Join Date: Jan 2007
Rookie Year: 2007
Location: Elyria, OH
Posts: 15
evan_wilson is an unknown quantity at this point
Send a message via AIM to evan_wilson Send a message via Yahoo to evan_wilson
Smile Re: Real time clocks, out of the question?

Alright. I have another question about how the timer variables work in all of these. I've used storage classes/qualifiers occasionally but I don't understand the significance of using, say, extern and volatile to store the timer variables. Could anyone shed some light on this?
  #6   Spotlight this post!  
Unread 25-02-2007, 16:50
Dave K.'s Avatar
Dave K. Dave K. is offline
Engineer/Mentor
FRC #0930
Team Role: Mentor
 
Join Date: Jan 2007
Rookie Year: 2005
Location: WI
Posts: 91
Dave K. is a splendid one to beholdDave K. is a splendid one to beholdDave K. is a splendid one to beholdDave K. is a splendid one to beholdDave K. is a splendid one to beholdDave K. is a splendid one to beholdDave K. is a splendid one to behold
Re: Real time clocks, out of the question?

Quote:
Originally Posted by evan_wilson View Post
Alright. I have another question about how the timer variables work in all of these. I've used storage classes/qualifiers occasionally but I don't understand the significance of using, say, extern and volatile to store the timer variables. Could anyone shed some light on this?
'extern' is much like a function prototype, meaning that it is allowing you declare the variable within the local scope such that the compiler will know what the variable type is and can generate the appropriate code. When the linker pulls all of the object files together, it will resolve the external dependancies of each of the object files, and if you haven't declared the variable itself anywhere within the program, will generate an error and abort.

'volatile' tells the compiler that the variable may change on its own and to generate code as appropriate. For optimization purposes, a compiler may read a variable from memory into a temporary register, and subsequent read/write operations to that variable _may_ only occur to the register without updating the memory location. Telling the compiler that the variable is volatile causes the compiler to re-read the variable's memory location each time it is used, and to write it back to memory each time it is updated.

'volatile' would be used for things like hardware periphial registers, as well as things like variables that might change inside of an interrupt routine.

Since the compiler can't know why you are telling it that the variable is volatile, it wouldn't generate additional instructions to disable interrupts during critical regions to ensure that a multi-byte variable read/write operation is performed without the potential for another interrupt routine corrupting the operation. Pre-emptive multi-tasking operating systems generally pose the same potential for corruption that an interrupt routine does.

The 'static' qualifier allows variables to be declared within a local, rather than global, scope and simply tells the compiler to declare the variable in a memory location that is static, rather than allocating it on the stack frame (or emulated equivilent thereof) of the local function.

Any variable declared outside of a function is normally considered a global variable, but unless other source files provide an 'extern' reference, the variable's use will remain limited to that source file, and is sometimes referred to as a 'local global'. The caveat here is that if you were to declare another 'local global' in another source file, the linker will (usually) have a problem with the duplicate declarations, and generate an error.



Recognizing that Microchips documentation already presumes a certain familiarity with the "C" language and doesn't really get into all of the specifics, for those that already have a general grasp on the "C" language, two books that I would recommend are:

The C Programming Language by Brian Kernighan and Dennis Ritchie, who are the origional authors of "C".
A book on C by Al Kelly and Ira Pohl.

K&R's book is very terse with minimal examples, but is the bible. The 2nd one is also a bit terse, and goes into depth more than K&R's book.

I've included links to Amazon's website as a convienent reference, but retailers such as Barnes & Noble also generally carry these and other good reference books where you can browse before you buy.
__________________
--Dave
  #7   Spotlight this post!  
Unread 25-02-2007, 20:41
evan_wilson's Avatar
evan_wilson evan_wilson is offline
QUARK
AKA: Evan Wilson
FRC #2172 (Street Legal)
Team Role: Programmer
 
Join Date: Jan 2007
Rookie Year: 2007
Location: Elyria, OH
Posts: 15
evan_wilson is an unknown quantity at this point
Send a message via AIM to evan_wilson Send a message via Yahoo to evan_wilson
Red face Re: Real time clocks, out of the question?

Strangely enough, K&R is sitting right here on my bookshelf. Somehow I hadn't remembered "volatile". I'll go reread through the data types/variables stuff again.

Not to seem pestery... but I've once again confused myself in this code. I read through the PIC's datasheet (timer section) and studied up on the registers. On the good side, I now completely understand all the scalers, clocks, and flags, but I could not seem to understand how the high and low byte registers worked/were needed. I don't see how an integer could go past 65,535, so why does the high byte happen to be able to have a value over that?

I also could not find an equivalent for the PR2 register for Timer1. Is that simply not present and do I have to use conventional math to downscale further?

Thanks again for the help.
  #8   Spotlight this post!  
Unread 25-02-2007, 22:58
Dave K.'s Avatar
Dave K. Dave K. is offline
Engineer/Mentor
FRC #0930
Team Role: Mentor
 
Join Date: Jan 2007
Rookie Year: 2005
Location: WI
Posts: 91
Dave K. is a splendid one to beholdDave K. is a splendid one to beholdDave K. is a splendid one to beholdDave K. is a splendid one to beholdDave K. is a splendid one to beholdDave K. is a splendid one to beholdDave K. is a splendid one to behold
Re: Real time clocks, out of the question?

Quote:
Originally Posted by evan_wilson View Post
Strangely enough, K&R is sitting right here on my bookshelf. Somehow I hadn't remembered "volatile". I'll go reread through the data types/variables stuff again.

Not to seem pestery... but I've once again confused myself in this code. I read through the PIC's datasheet (timer section) and studied up on the registers. On the good side, I now completely understand all the scalers, clocks, and flags, but I could not seem to understand how the high and low byte registers worked/were needed. I don't see how an integer could go past 65,535, so why does the high byte happen to be able to have a value over that?

I also could not find an equivalent for the PR2 register for Timer1. Is that simply not present and do I have to use conventional math to downscale further?

Thanks again for the help.

Timer's 2 & 4 are 8 bit wide timers and have the "PR" registers, the other timers are 16 bit.

Timer 1 is a bit special and can be used for a low power, real time clock function.

Without a specific reference, I'm not sure what you mean about the high byte having a value greater than 65535. My only guess would be that by setting a value to zero, you'll get an effective divide rate of 65536, but I don't immediately see that in the documentation.

The reason that the 16 bit counter is split into two 8 bit registers is that the CPU only operates on data 8 bits at a time. So a 16 bit read/write operation requires two data bus operations to manipulate the 16 bit value.

Does that help or did I miss one or more of the questions?
__________________
--Dave
  #9   Spotlight this post!  
Unread 25-02-2007, 23:10
evan_wilson's Avatar
evan_wilson evan_wilson is offline
QUARK
AKA: Evan Wilson
FRC #2172 (Street Legal)
Team Role: Programmer
 
Join Date: Jan 2007
Rookie Year: 2007
Location: Elyria, OH
Posts: 15
evan_wilson is an unknown quantity at this point
Send a message via AIM to evan_wilson Send a message via Yahoo to evan_wilson
Question Re: Real time clocks, out of the question?

Ahhh, that explains things a bit. I should have thought about that. RD16 two bytes wide, duh.

Thanks for that tidbit.

And the high and low bytes. In the wallclock code (http://www.chiefdelphi.com/forums/sh...t=wallclock.c), the high byte value is.... NEVER MIND

Someone can't count their zeros.

I still don't exactly understand what the purpose of the high and low bytes are. Is it like an loop condition? Above the max value for a 16bit number counts as an overflow and it resets, setting the flag with it?

THANK YOU
  #10   Spotlight this post!  
Unread 26-02-2007, 00:17
Dave K.'s Avatar
Dave K. Dave K. is offline
Engineer/Mentor
FRC #0930
Team Role: Mentor
 
Join Date: Jan 2007
Rookie Year: 2005
Location: WI
Posts: 91
Dave K. is a splendid one to beholdDave K. is a splendid one to beholdDave K. is a splendid one to beholdDave K. is a splendid one to beholdDave K. is a splendid one to beholdDave K. is a splendid one to beholdDave K. is a splendid one to behold
Re: Real time clocks, out of the question?

Quote:
Originally Posted by evan_wilson View Post
Ahhh, that explains things a bit. I should have thought about that. RD16 two bytes wide, duh.

Thanks for that tidbit.

And the high and low bytes. In the wallclock code (http://www.chiefdelphi.com/forums/sh...t=wallclock.c), the high byte value is.... NEVER MIND

Someone can't count their zeros.

I still don't exactly understand what the purpose of the high and low bytes are. Is it like an loop condition? Above the max value for a 16bit number counts as an overflow and it resets, setting the flag with it?

THANK YOU

One of the interrupt options is to interrupt on timer overflow, and that is how some of the other examples posted work.

If you:

- stop the timer
- load a 16 bit value into the timer register
- reset the interrupt flag
- start the timer

You will get an overflow interrupt in however many timer ticks it takes for the pre-load value to reach the point of overflow (0xffff -> 0x0000).


Having the timer value available could also be useful for calculating the period between two events, such as a transition on an input pin that triggers an interrupt. The input interrupt could read the current timer value, and subtract the timer value of the last input transition to calculate the amount of time elapsed since the last input interrupt.

Ideally, one would use something like the CCP module to time stamp an input transition, but in the case of IFI's RC, those pins aren't available as for user Digital I/O pins.

As an example, for something like motor velocity control, at slow speeds, if a rotational speed slower than 1 pulse per sample period was desired, you could use period measurement to gain some additional resolution, and above a certain rotational speed, you could switch to counting the number of pulses per period method.

Each microcontroller manufactuer has their own ideas on the degree's of flexibility that they make available. Microchip's 8 bit PIC family is pretty average. Some parts have less flexibility, others have quite a bit more. In this case, you have to do the best you can with what they give you...
__________________
--Dave
  #11   Spotlight this post!  
Unread 26-02-2007, 15:00
Nathan's Avatar
Nathan Nathan is offline
Registered User
FRC #1501 (Team T.H.R.U.S.T.)
Team Role: Alumni
 
Join Date: Sep 2006
Rookie Year: 2007
Location: United States
Posts: 149
Nathan has a spectacular aura aboutNathan has a spectacular aura aboutNathan has a spectacular aura about
Re: Real time clocks, out of the question?

I thought the WPI library had built in timers? What's the difference between those and "real time" timers?

Thanks,
Nathan
Closed Thread


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Networkable real-time game simulation for 'Aim High'! test it out now Bongle General Forum 11 19-03-2006 23:10
Real time photos from the buckeye regional Greg Needel Regional Competitions 4 25-03-2004 12:46
Robot motion after the time has run out. Randy Ai Rules/Strategy 1 06-01-2003 17:17
Real Time Scoring archiver 2000 2 24-06-2002 00:09
real time chat SharkBite CD Forum Support 4 07-02-2002 21:22


All times are GMT -5. The time now is 18:00.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi