Go to Post What does code sound like going though a complier? - Hiteak [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 05-02-2006, 14:38
Ami Schwartz Ami Schwartz is offline
Registered User
FRC #1657
 
Join Date: Jan 2006
Location: Israel
Posts: 24
Ami Schwartz is an unknown quantity at this point
How to use a "Timer"

How to include the Timer to the program.

P.S how many 1/x bits per Sec.



Thx alot "Hamosad" team.
  #2   Spotlight this post!  
Unread 05-02-2006, 14:53
MacFlightSierra MacFlightSierra is offline
Registered User
FRC #0912 (Iron Lyons)
Team Role: Programmer
 
Join Date: Jan 2006
Rookie Year: 2004
Location: Toronto
Posts: 19
MacFlightSierra is an unknown quantity at this point
Re: How to use a "Timer"

one loop of the controller is 26.2 millis - approx 38 loops per second. just add a variable to count till 38 and then reset it to zero

good luck/behatzla'ha
  #3   Spotlight this post!  
Unread 05-02-2006, 15:46
Eldarion's Avatar
Eldarion Eldarion is offline
Electrical Engineer / Computer Geek
AKA: Eldarion Telcontar
no team (Teamless Orphan)
Team Role: Alumni
 
Join Date: Nov 2005
Rookie Year: 2005
Location: Númenor
Posts: 558
Eldarion has a reputation beyond reputeEldarion has a reputation beyond reputeEldarion has a reputation beyond reputeEldarion has a reputation beyond reputeEldarion has a reputation beyond reputeEldarion has a reputation beyond reputeEldarion has a reputation beyond reputeEldarion has a reputation beyond reputeEldarion has a reputation beyond reputeEldarion has a reputation beyond reputeEldarion has a reputation beyond repute
Send a message via AIM to Eldarion Send a message via Yahoo to Eldarion
Re: How to use a "Timer"

Quote:
Originally Posted by MacFlightSierra
one loop of the controller is 26.2 millis - approx 38 loops per second. just add a variable to count till 38 and then reset it to zero

good luck/behatzla'ha
A word of caution--I learned the hard way that this is not always true.

The loop executes at a minimum of 26.6ms, but it most certainly can take much longer than 26.6ms!
The BLROD doesn't trigger until the code takes a whopping 1/4 to 1/2 second per loop!

EDIT:
If you want an example of this behavior, try the 2005 default camera code.
The code slows dramatically as soon as the camera is hooked up, and as far as I can tell, takes more on the order of 1/4-second per loop.
__________________
CMUCam not working? Tracks sporadically? Try this instead: http://www.falconir.com!
PM me for more information if you are interested (it's open source!).

Want the FIRST Email blasts? See here: http://www.chiefdelphi.com/forums/sh...ad.php?t=50809

"The harder the conflict, the more glorious the triumph. What we obtain too cheaply, we esteem too lightly; it is dearness only that gives everything its value."
-- Thomas Paine

If it's falling apart it's a mechanical problem. If it's spewing smoke it's a electrical problem.
If it's rampaging around destroying things it's a programming problem.

"All technology is run on 'Magic Smoke' contained within the device. As everyone knows, whenever the magic smoke is released, the device ceases to function."
-- Anonymous

I currently speak: English, some German, Verilog, x86 and 8051 Assembler, C, C++, VB, VB.NET, ASP, PHP, HTML, UNIX and SQL

Last edited by Eldarion : 05-02-2006 at 15:52.
  #4   Spotlight this post!  
Unread 05-02-2006, 16:48
phrontist's Avatar
phrontist phrontist is offline
Proto-Engineer
AKA: Bjorn Westergard
FRC #1418 (Vae Victus)
Team Role: College Student
 
Join Date: Feb 2004
Rookie Year: 2004
Location: Falls Church, VA
Posts: 828
phrontist has a reputation beyond reputephrontist has a reputation beyond reputephrontist has a reputation beyond reputephrontist has a reputation beyond reputephrontist has a reputation beyond reputephrontist has a reputation beyond reputephrontist has a reputation beyond reputephrontist has a reputation beyond reputephrontist has a reputation beyond reputephrontist has a reputation beyond reputephrontist has a reputation beyond repute
Send a message via AIM to phrontist
Re: How to use a "Timer"

Do not depend on loop timings! They are highly variable!

Use timer interrupts. Kevin has code on his website.
__________________

University of Kentucky - Radio Free Lexington

"I would rather have a really big success or a really spectacular crash and failure then live out the warm eventual death of mediocrity" - Dean Kamen
  #5   Spotlight this post!  
Unread 05-02-2006, 16:58
Mike Shaul's Avatar
Mike Shaul Mike Shaul is offline
Registered User
FRC #0065 (Huskie Brigade)
Team Role: Engineer
 
Join Date: Jan 2006
Rookie Year: 2003
Location: Farmington Hills, MI
Posts: 53
Mike Shaul has a brilliant futureMike Shaul has a brilliant futureMike Shaul has a brilliant futureMike Shaul has a brilliant futureMike Shaul has a brilliant futureMike Shaul has a brilliant futureMike Shaul has a brilliant futureMike Shaul has a brilliant futureMike Shaul has a brilliant futureMike Shaul has a brilliant futureMike Shaul has a brilliant future
Re: How to use a "Timer"

I'm a little unclear of what the actual question is. Are you asking how to use the PIC's internal timers? How to use the timers would vary based on your application. The PIC's timer modules can be configured as either timers or counters based on a few different internal sources (at varying speeds) as well as external sources.

If you are interested in using the timer modules I would suggest you start by reading the data sheet on the PIC micro timer modules and then reviewing Kevin's interrupt/timer code from 2005 (he hasn't posted 2006 code yet).

Once you've reviewed the above items you should have a better idea of what you are looking for, or where you may need some assistance.
__________________
By far the best proof is experience. - Sir Francis Bacon
  #6   Spotlight this post!  
Unread 06-02-2006, 09:38
Ami Schwartz Ami Schwartz is offline
Registered User
FRC #1657
 
Join Date: Jan 2006
Location: Israel
Posts: 24
Ami Schwartz is an unknown quantity at this point
Re: How to use a "Timer"

We did read the PDF files and we dont know the specific code to operate the Timer.

( AKA. we dont know how to use the code thats in the "H" files ) .


Thx again : )
  #7   Spotlight this post!  
Unread 06-02-2006, 12:11
tribotec_ca88's Avatar
tribotec_ca88 tribotec_ca88 is offline
FIRST-a-holic
AKA: Camila Fitzgibbon
FRC #1382 (Tribotec Team)
Team Role: Student
 
Join Date: Jan 2004
Rookie Year: 2004
Location: São José dos Campos, Brazil
Posts: 194
tribotec_ca88 is just really nicetribotec_ca88 is just really nicetribotec_ca88 is just really nicetribotec_ca88 is just really nicetribotec_ca88 is just really nice
Send a message via ICQ to tribotec_ca88 Send a message via MSN to tribotec_ca88 Send a message via Yahoo to tribotec_ca88
Re: How to use a "Timer"

Quote:
Originally Posted by MacFlightSierra
one loop of the controller is 26.2 millis - approx 38 loops per second. just add a variable to count till 38 and then reset it to zero

good luck/behatzla'ha
Hmm...i wouldn't recommend using the "approx 38 loops per second" method.

If you're looking for greater precision i recommend using the interrupt-driven timers. Our team is currently using Timer1 of the five PIC timers. Perhaps i'm not the best person to explain you exactly how they work but let me see if i can help...

First off, we created a function known as Init_timers(); (inside a .c file of our choice), which is called from inside User_Initialization() under user_routines.c.

Inside Init_timers we wrote the following code (to initialize and configure Timer1):

void Init_timers(void)
{

T1CONbits.T1CKPS0 = 1; // 1:8 Prescale (clock=1.25MHz/each tick=800ns)
T1CONbits.T1CKPS1 = 1;

TMR1H = 0x85; // sets Timer1's most significant byte
TMRIL = 0xED; // sets Timer1's least significant byte


T1CONbits.TMR1CS = 0; // Uses Internal clock

T1CONbits.T1OSCEN = 0; // Internal oscillator off

T1CONbits.RD16 = 1; // Timer1 operations done in 16-bit accesses


IPR1bits.TMR1IP = 0; // Sets interrupt as low priority

PIR1bits.TMR1IF = 0; // Overflow flag

PIE1bits.TMR1IE = 1; // Timer1 interrupt OK

INTCONbits.GIEL = 1; // Low priority interrupts OK

T1CONbits.TMR1ON = 1; // Timer1 set as on

}

Next you'll need to add a few things inside InterruptHandlerLow() inside user_routines_fast. This is a system function; I.E. when an interrupt is set, this function is immediately called by IFI's interrupt handler. You do not need to call it from anywhere in your code, however, you will need to include the following lines of code inside it. You'll notice this function already has several "IF" loops so all you have to do is add one more...

void InterruptHandlerLow ()
{

if[...]

else if (PIR1bits.TMR1IF && PIE1bits.TMR1IE) // checks to see if overflow has occured
{
PIR1bits.TMR1IF = 0; // resets Timer1 overflow
TMR1L = 0xED;
TMR1H = 0x85;
timer1_count++; // Timer1 counter incremented every 25ms
}


[...]

} // end of InterruptHandlerLow() function

Since I'm assuming you'll be working with timers inside your Autonomous code (as are we), all you'd have to do next is use "IF"s to compare the timer1_count variable. As was explained, the timer increments every 25ms, so if for example you wanted to have your robot execute a specific part of your code for 1s (=1000 ms), simple!: check if timer1_count is equivalent to 40...! For 2 seconds, 80, for 10 seconds, 400, etc etc etc...

This may not perhaps be the most accurate method but from our experiences it suited our specific needs...

Hope this helps...
any questions whatsoever, pm me...



__________________
1382 Tribotec Team - Brazil
-----------------------------------------------
2005 NJ Regional GM Industrial Design Award
2005 NJ Regional Website Excellence Award
2004 NJ Regional Semifinalists
-----------------------------------------------
  #8   Spotlight this post!  
Unread 06-02-2006, 12:15
tribotec_ca88's Avatar
tribotec_ca88 tribotec_ca88 is offline
FIRST-a-holic
AKA: Camila Fitzgibbon
FRC #1382 (Tribotec Team)
Team Role: Student
 
Join Date: Jan 2004
Rookie Year: 2004
Location: São José dos Campos, Brazil
Posts: 194
tribotec_ca88 is just really nicetribotec_ca88 is just really nicetribotec_ca88 is just really nicetribotec_ca88 is just really nicetribotec_ca88 is just really nice
Send a message via ICQ to tribotec_ca88 Send a message via MSN to tribotec_ca88 Send a message via Yahoo to tribotec_ca88
Re: How to use a "Timer"

oops...rather small yet important detail....don't forget to include the "timers.h" header file inside any .c files that work with Timers...
__________________
1382 Tribotec Team - Brazil
-----------------------------------------------
2005 NJ Regional GM Industrial Design Award
2005 NJ Regional Website Excellence Award
2004 NJ Regional Semifinalists
-----------------------------------------------
  #9   Spotlight this post!  
Unread 06-02-2006, 14:49
Ami Schwartz Ami Schwartz is offline
Registered User
FRC #1657
 
Join Date: Jan 2006
Location: Israel
Posts: 24
Ami Schwartz is an unknown quantity at this point
Re: How to use a "Timer"

Thx all !!!! (for the help )

....

21' is comming soon
  #10   Spotlight this post!  
Unread 09-02-2006, 20:20
10scott10 10scott10 is offline
Registered User
None #1148
 
Join Date: Feb 2006
Location: los angeles
Posts: 8
10scott10 is an unknown quantity at this point
Re: How to use a "Timer"

thanks a ton. this is very useful. I think.
  #11   Spotlight this post!  
Unread 10-02-2006, 00:33
CronosPrime1 CronosPrime1 is offline
Registered User
FRC #0321
 
Join Date: Jan 2006
Location: Hyperspace
Posts: 53
CronosPrime1 is an unknown quantity at this point
Re: How to use a "Timer"

Do you think you could provide more details on interrupt driven timers? Does it use up one of the six interrupts your robot has? Oh, and the 25 ms timing loop, why is it 25 ms? It seems to me that the calculation is:

2 bytes*8 bits/byte = 16 bits

largest possible 2-byte integer = 2^16 - 1 = 65535

And 800 ns*65535 =52.428 ms

You seem to get half of that...

I'm not doubting the correctness of 25 ms, I just wonder as to why it is 25 ms...
__________________

Last edited by CronosPrime1 : 10-02-2006 at 00:48.
  #12   Spotlight this post!  
Unread 10-02-2006, 06:07
tribotec_ca88's Avatar
tribotec_ca88 tribotec_ca88 is offline
FIRST-a-holic
AKA: Camila Fitzgibbon
FRC #1382 (Tribotec Team)
Team Role: Student
 
Join Date: Jan 2004
Rookie Year: 2004
Location: São José dos Campos, Brazil
Posts: 194
tribotec_ca88 is just really nicetribotec_ca88 is just really nicetribotec_ca88 is just really nicetribotec_ca88 is just really nicetribotec_ca88 is just really nice
Send a message via ICQ to tribotec_ca88 Send a message via MSN to tribotec_ca88 Send a message via Yahoo to tribotec_ca88
Re: How to use a "Timer"

TMR1H = 0x85; // sets Timer1's most significant byte
TMRIL = 0xED; // sets Timer1's least significant byte

Rather small detail...on my posts above i made a typo mistake ... TMRIL should be TMR1L...so don't panic if you're getting any "symbol undefined" errors!! Sorry about that...

Quote:
Originally Posted by CronosPrime1
Do you think you could provide more details on interrupt driven timers? Does it use up one of the six interrupts your robot has? Oh, and the 25 ms timing loop, why is it 25 ms? It seems to me that the calculation is:

2 bytes*8 bits/byte = 16 bits

largest possible 2-byte integer = 2^16 - 1 = 65535

And 800 ns*65535 =52.428 ms

You seem to get half of that...

I'm not doubting the correctness of 25 ms, I just wonder as to why it is 25 ms...
Now a few comments on the 25 ms loop...:

OK up to that point that's correct... according to the Timers White Paper found herehttp://www.ifirobotics.com/docs/time...004-jan-14.pdf) the following formula should give you what you want:

# of ticks = (time elapsed) * 10^7s / (prescale factor)

As you said above, with a 16 bit timer, counting to 65535, we can measure 6.5535 ms (considering the fact that the internal clock is 10MHz). What happens is that this duration is far too small to use for real applications, so then we used a prescale factor. In this case, we're using one of 8. This means that the timer will only increment once every eight ticks, therefore it will overflow after 52.428 ms. So that is where and how you got this value.

OK up to that point i think we can agree on. Now, we use the formula above to find the # of ticks we need to get a nice even interval of 25 ms (= 0.025 s):

# of ticks = (time elapsed) * 10^7s / (prescale factor)
# of ticks = (0.025) * 10^7s /(8)



Using the last equation we find that we need 31250 ticks to do this. Since it is easier to look at an overflow condition in the timer, we will preload the timer with a value that will cause this overflow after 31250 ticks. To get this value we subtract 31250 from the 16-bit timer’s maximum value (65535) and get 34285.


Therefore we get 65535 (timer's max value of ticks) - 31250 (number of ticks we need) = 34285 (preload value).

This is the value we will preload our timer with. In the code written above you will see 34285 represented in hexadecimal as 0x85ED. Which pretty much explains why the following lines are written the way they are:


TMR1H = 0x85; // sets Timer1's most significant byte
TMR1L = 0xED; // sets Timer1's least significant byte


So, if for example you wanted to work a different time interval (50 ms, let's suppose) use the formula to find out the number of ticks you will need, subtract that value from 65535 and convert the result of that into hexadecimal. That will get you your preload value.

Hope this helps...
__________________
1382 Tribotec Team - Brazil
-----------------------------------------------
2005 NJ Regional GM Industrial Design Award
2005 NJ Regional Website Excellence Award
2004 NJ Regional Semifinalists
-----------------------------------------------

Last edited by tribotec_ca88 : 10-02-2006 at 06:42.
  #13   Spotlight this post!  
Unread 11-02-2006, 12:57
CronosPrime1 CronosPrime1 is offline
Registered User
FRC #0321
 
Join Date: Jan 2006
Location: Hyperspace
Posts: 53
CronosPrime1 is an unknown quantity at this point
Re: How to use a "Timer"

Oh alright. I somehow overlooked the preload values.

So, how do I set a sclaing factor? In your code there seems to be a line that sets it to one yet it's 8...
__________________
  #14   Spotlight this post!  
Unread 11-02-2006, 18:56
Joohoo's Avatar
Joohoo Joohoo is offline
Registered User
FRC #0340 (G.R.R.)
Team Role: Mentor
 
Join Date: Jun 2005
Rookie Year: 2005
Location: Rochester
Posts: 241
Joohoo is just really niceJoohoo is just really niceJoohoo is just really niceJoohoo is just really niceJoohoo is just really nice
Send a message via AIM to Joohoo Send a message via Yahoo to Joohoo
Re: How to use a "Timer"

There is one problem with this

the timer link you have is for the 8520 controller the new controller is the 8720 and I can't get the code that was given in this thread to work has any one else???
also does any one have the code/"white papers" for the 8720 timers???
or is it the same and we just messed with it too much???
  #15   Spotlight this post!  
Unread 11-02-2006, 20:39
Matt Krass's Avatar
Matt Krass Matt Krass is offline
"Old" and Cranky. Get off my lawn!
AKA: Dark Ages
FRC #0263 (Sachem Aftershock)
Team Role: Mentor
 
Join Date: Oct 2002
Rookie Year: 2002
Location: Long Island, NY
Posts: 1,187
Matt Krass has a reputation beyond reputeMatt Krass has a reputation beyond reputeMatt Krass has a reputation beyond reputeMatt Krass has a reputation beyond reputeMatt Krass has a reputation beyond reputeMatt Krass has a reputation beyond reputeMatt Krass has a reputation beyond reputeMatt Krass has a reputation beyond reputeMatt Krass has a reputation beyond reputeMatt Krass has a reputation beyond reputeMatt Krass has a reputation beyond repute
Send a message via AIM to Matt Krass
Re: How to use a "Timer"

Check out the OpenTimer#() functions in this PDF:
http://www.kevin.org/frc/C18_libraries.pdf

It's very useful.
__________________
Matt Krass
If I suggest something to try and fix a problem, and you don't understand what I mean, please PM me!

I'm a FIRST relic of sorts, I remember when we used PBASIC and we got CH Flightsticks in the KoP. In my day we didn't have motorized carts, we pushed our robots uphill, both ways! (Houston 2003!)
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


All times are GMT -5. The time now is 11:32.

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