Go to Post If you can't be bothered to read the Q&A, why are you spending thousands of dollars competing? - Ian Curtis [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
  #16   Spotlight this post!  
Unread 24-03-2007, 00:24
dragoonex's Avatar
dragoonex dragoonex is offline
Registered User
FRC #1325 (Inverse Paradox)
Team Role: Programmer
 
Join Date: Jan 2007
Rookie Year: 2006
Location: Canada
Posts: 9
dragoonex is on a distinguished road
Re: Anyone actually using hardware timers?

We implemented Timers into our code this year, and they are great. We set ours to fire off every millisecond. Unfortunately, we didn't have the time, but we plan to write a wrapper around one timer so we can schedule "events" to happen at certain time intervals. But so far, the timers have been proven to be extremely useful and you should definitely implement them.

Some tips, your interrupt routine should be fast. In fact, you should really only be really incrementing a number in it. Remember that since your timer is firing every X milliseconds, your number is being increased very rapidly. So use a big type like a long, or a long short. These will last your for days.

Also, always, always, always, disable your timer interrupts before trying to access your "tick" variable. Bad things will happen if you don't.
  #17   Spotlight this post!  
Unread 26-03-2007, 03:11
ericand's Avatar
ericand ericand is offline
Registered User
AKA: Eric Anderson
FRC #3765 (Terrabots)
Team Role: Mentor
 
Join Date: Jan 2005
Rookie Year: 2004
Location: St. Paul, MN
Posts: 148
ericand is a jewel in the roughericand is a jewel in the roughericand is a jewel in the rough
Re: Anyone actually using hardware timers?

We have a scheduler based on the hardware timers. It allows scheduling of events at regular intervals, or single shot events at some time in the future.

It uses function pointers to allow the scheduler code to avoid needning to know what functions it may be calling.
  #18   Spotlight this post!  
Unread 26-03-2007, 08:16
bear24rw's Avatar
bear24rw bear24rw is offline
Team 11 Programming Captain
AKA: Max T
FRC #0011 (MORT)
Team Role: Programmer
 
Join Date: Sep 2005
Rookie Year: 2005
Location: Flanders, NJ
Posts: 385
bear24rw is a splendid one to beholdbear24rw is a splendid one to beholdbear24rw is a splendid one to beholdbear24rw is a splendid one to beholdbear24rw is a splendid one to beholdbear24rw is a splendid one to beholdbear24rw is a splendid one to behold
Send a message via AIM to bear24rw
Re: Anyone actually using hardware timers?

Quote:
Originally Posted by ericand View Post
We have a scheduler based on the hardware timers. It allows scheduling of events at regular intervals, or single shot events at some time in the future.

It uses function pointers to allow the scheduler code to avoid needning to know what functions it may be calling.
What kind of things are you scheduling?
  #19   Spotlight this post!  
Unread 27-03-2007, 03:21
ericand's Avatar
ericand ericand is offline
Registered User
AKA: Eric Anderson
FRC #3765 (Terrabots)
Team Role: Mentor
 
Join Date: Jan 2005
Rookie Year: 2004
Location: St. Paul, MN
Posts: 148
ericand is a jewel in the roughericand is a jewel in the roughericand is a jewel in the rough
Re: Anyone actually using hardware timers?

Anything that needs to be called on a regular basis. For example, a regular sensor reading that is faster than the 26ms loop, but not "as fast as possible".
  #20   Spotlight this post!  
Unread 27-03-2007, 09:43
yoyodyne yoyodyne is offline
Registered User
AKA: Greg Smith
FRC #0116 (Epsilon Delta)
Team Role: Engineer
 
Join Date: Jan 2006
Rookie Year: 2004
Location: Reston, VA
Posts: 61
yoyodyne is a splendid one to beholdyoyodyne is a splendid one to beholdyoyodyne is a splendid one to beholdyoyodyne is a splendid one to beholdyoyodyne is a splendid one to beholdyoyodyne is a splendid one to beholdyoyodyne is a splendid one to behold
Re: Anyone actually using hardware timers?

Spencer,

I don't know exactly what you need in terms of timer resolution but team 116 has been using a real time clock that runs off of timer 4 with 5ms ticks. Code segments to setup the timer, handle the interrupt and do something with the timer are below. We choose timer4 because we could put it in a period mode so it automatically interrupt every Nms and we didn't know of any other code that used that timer. As you will see the ISR just increments a variable and that veriable is checked in user_routines_fast where ClockTick is called. Currently we have 10 software elapsed timers and up to 10 concurrent "Event Timers". When an event timer times out a routine is automatically called. So an event is scheduled to occur some time in the future. Lots of times we schedule events for error conditions and remove the event if something good happens like the camera actually returns a T-packet or acks a virtual window command

The h file and the timer library for 8520 and 8722 are attached. Rename the lib files to .lib instead of .txt

Here is an example of how you would print out your average processor loading once per second:

In the initialization code after you have made the RTC initialize call, use

ScheduleEventTimer(ProcessorUtilization,"some time",unused parameter);


This routine will run after "some time" in ms

void ProcessorUtilization(void)
{
unsigned int percent_utilized;
percent_utilized = (25600 - idle_count) >> 8; /*an idle count of 25,600 means none of the processor is being used */

printf("PU = %d%%\r",percent_utilized);
idle_count = 0;
ScheduleEventTimer(ProcessorUtilization,1000,0); /* Run again in 1 second */
}


the variable idle_count needs to be incremented in process_data_from local IO fast like this:


Delay10TCYx(39);
idle_count += 1; /* Every time this variable is incremented, 39us? of time was spent idle */

The way this works is we waist 39us every time through the fast loop and count how often that happens each second. If you hit 80% or more you need to make code changes for sure. 60% is our peak target.


So, if you want to try the 116 RTC - that would be great - let me know how it worked, if not the following code segments should help you set up a timer.

Greg












This code goes in the timer file - Initialize RTC gets called in the user_routines initialization section



/************************************************** *****************************
*
* FUNCTION: InitializeRTC()
*
* PURPOSE: Initializes the real time clock timers
*
* CALLED FROM:
*
* PARAMETERS: None
*
* RETURNS: Nothing
*
* COMMENTS:
*
*
************************************************** *****************************/
void InitializeRTC(void)
{
Initialize_Timer_4();
InitializeEventTimers();
InitializeElapsedTimers();
}


/************************************************** *****************************
*
* FUNCTION: Initialize_Timer_4()
*
* PURPOSE: Initializes the timer 1 hardware, which is responsible for
* producing real time clock ticks
*
* CALLED FROM: 116_timers.c/Initialize_RTC()
*
* PARAMETERS: Unsigned integer containing the sample rate expressed in Hz
*
* RETURNS: Nothing
*
* COMMENTS: The default tick rate is 200Hz (5ms)
*
*
************************************************** *****************************/
void Initialize_Timer_4(void)
{

// use these parameters for a 200Hz Timer Rate

// use a 1:16 prescaler and 1:14 postscaler
T4CON = 0b01101010;

// Count to 221 before rolling over and generating
// an interrupt (223.21 - 2 is ideal)
PR4 = 221;

// make sure the timer 1 register starts at zero
TMR4 = 0x00;

// timer 2 interrupt is low priority
IPR3bits.TMR4IP = 0;

// to prevent a spurious interrupt, make sure the interrupt flag is reset
PIR3bits.TMR4IF = 0;

// enable the timer 4 interrupt
PIE3bits.TMR4IE = 1;

// enable timer 4
T4CONbits.TMR4ON = 1;
}

/************************************************** *****************************
*
* FUNCTION: Timer_4_Int_Handler()
*
* PURPOSE: Timer 4 interrupt service routine
*
* CALLED FROM: user_routines_fast.c/InterruptHandlerLow()
*
* PARAMETERS: None
*
* RETURNS: Nothing
*
* COMMENTS:
*
************************************************** *****************************/
void Timer_4_Int_Handler(void)
{
rtc_tick += 1;
// rc_dig_out18 ^= 1;
}


This code gets put in Interrupt_Handler_low

if(PIR3bits.TMR4IF && PIE3bits.TMR4IE) // timer 4 interrupt?
{
PIR3bits.TMR4IF = 0; // clear the timer 4 interrupt flag [92]
Timer_4_Int_Handler(); // call the timer 4 interrupt handler (in 116_timers.c)
}


This code actually deals with the clock ticks - ClockTick does whatever you need to do with the time information. Don't forgot to disable the interrupt if you change a variable it can change.

void Process_Data_From_Local_IO(void)
{
/* Add code here that you want to be executed every program loop. */
unsigned char ticks;
unsigned char i;

/* Actually execute the real time clock code */

ticks = rtc_tick;
if(ticks > 0){ /* The RTC has ticked */
// if(ticks > 1)
// printf("THE CODE IS GETTING BOGGED DOWN - %3d TICKS\r",ticks);
for(i = 0; i < ticks; i++)
{
ClockTick();
//printf("HOW LONG DOES IT TAKE TO PUT OUT THIS STRING EVERY 5 ms?\r");
}
PIE3bits.TMR4IE = 0; /*Disable the timer interrupt */
rtc_tick = 0;
PIE3bits.TMR4IE = 1; /*Enable the timer interrupt */
}
Attached Files
File Type: h 116_timers.h (6.4 KB, 25 views)
File Type: txt 116_timers_8520_lib.txt (43.2 KB, 21 views)
File Type: txt 116_timers_lib.txt (52.3 KB, 22 views)
  #21   Spotlight this post!  
Unread 27-03-2007, 12:43
yoyodyne yoyodyne is offline
Registered User
AKA: Greg Smith
FRC #0116 (Epsilon Delta)
Team Role: Engineer
 
Join Date: Jan 2006
Rookie Year: 2004
Location: Reston, VA
Posts: 61
yoyodyne is a splendid one to beholdyoyodyne is a splendid one to beholdyoyodyne is a splendid one to beholdyoyodyne is a splendid one to beholdyoyodyne is a splendid one to beholdyoyodyne is a splendid one to beholdyoyodyne is a splendid one to behold
Re: Anyone actually using hardware timers?

It seems like timers are a problem for a lot of teams so I posted ours this morning on this thread in the wrong place... I didn't see the rest of the thread or that the issue was already solved.

http://www.chiefdelphi.com/forums/sh...d.php?p=605834

It sounds like you have taken a similar approach with event timers that call routines when they time out. After all when all your other sensors have failed or you should still at least have timers! The example shows one way to estimate processor loading which also seems to come up a lot.
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
Are you actually using bumpers? Chriszuma Technical Discussion 28 16-02-2006 18:41
Using Timers psquared Programming 9 30-01-2006 10:29
IS ANYONE USING PNUEMATICS archiver 2001 14 24-06-2002 00:18
Does anyone know what actually happened...? Adam Y. Regional Competitions 7 30-03-2002 01:48
Does anyone actually plan on playing rough? Joe Menassa General Forum 10 10-02-2002 05:09


All times are GMT -5. The time now is 20:01.

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