Go to Post Now I have yet another webpage that I will feel the need to constantly check - rachelholladay [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, 10:27
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
Unhappy Real time clocks, out of the question?

Hullo everyone. I'm Evan Wilson, lead programmer for Team 2172, St. Edward High School.

Our robot is all crated and shipped off... with horrible loop-counting timekeeping. In the past, I've heard of creating a real time clock with the Timer1 module in the PIC controller.

Is it humanly possible to create such a program? I DO know some PIC assembly, and certainly C. I assume this would be intensively assembly. Also, is there actually a crystal in the controller already? When reading the datasheet for the chip, it almost made it sound as if I'd need to do some whacked out solder job...

I would like to have gotten this done for this year's competition, but seeing as we have no adult mentors (and I'm a junior with a bit of a tight schedule) we couldn't cram in the work for that.

Anyways, thanks to anyone who could help, give links, etc.
  #2   Spotlight this post!  
Unread 24-02-2007, 10:36
SgtMillhouse648's Avatar
SgtMillhouse648 SgtMillhouse648 is offline
Registered User
AKA: Malhon Godwin
FRC #0648 (QC Elite)
Team Role: Engineer
 
Join Date: Apr 2006
Rookie Year: 2005
Location: Davenport, IA
Posts: 153
SgtMillhouse648 has a spectacular aura aboutSgtMillhouse648 has a spectacular aura about
Send a message via MSN to SgtMillhouse648
Re: Real time clocks, out of the question?

Hi, We too have had te 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 helpfull.
Thanks
Malhon
__________________
2007-
Midwest Regional-
Regional Finalists with 1000 and 447
Rockwell Automation Inspiration in Control Award
Regional Woodie Flowers - Ed Wegscheid



  #3   Spotlight this post!  
Unread 24-02-2007, 10:39
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
Post Re: Real time clocks, out of the question?

Well, in my case, I've used MPLAB the whole way. But does easyC actually have timers? I dug into the datasheet and found the Timer0 and Timer1 modules in the microcontroller and wasn't sure how to use them.

http://users.tkk.fi/~jalapaav/Electr...ock/index.html

That seems a little helpful.
  #4   Spotlight this post!  
Unread 24-02-2007, 10:46
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?

Yes, EasyC does have timers, six of them. I believe we have used up to three at once, and they all work fine.

What exactly isn't working for your? Could you post your code?

Thanks,
Nathan
  #5   Spotlight this post!  
Unread 24-02-2007, 10:51
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
Unhappy Re: Real time clocks, out of the question?

Well, the problem is that I don't have any. Without EasyC, I just have the arm calculating based on loop counting. Multiples of 26.2 ms. There's no way I can switch to EasyC this year. I guess that would be a logical solution for next year.

Is there anyone here who has ever written a timer and interrupt routine for a real time clock? That's really the only thing I could worry about anyways. Otherwise I'll just have to stick with loop counting.
  #6   Spotlight this post!  
Unread 24-02-2007, 11:21
Mark McLeod's Avatar
Mark McLeod Mark McLeod is online now
Just Itinerant
AKA: Hey dad...Father...MARK
FRC #0358 (Robotic Eagles)
Team Role: Engineer
 
Join Date: Mar 2003
Rookie Year: 2002
Location: Hauppauge, Long Island, NY
Posts: 8,782
Mark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond repute
Re: Real time clocks, out of the question?

Timers are simple to use, no soldering required.
IFI has a white paper on coding timers that goes into the bits and bytes of it all:
http://www.ifirobotics.com/docs/time...004-jan-14.pdf

For a description of the timer library calls see the MPLAB-C18-Libraries.pdf manual under C:\mcc18\doc.

Here's a sample using the timer libraries:
Code:
// In user_routines.c
#include <timers.h>
#define CLOCKOFFSET 60535  //Set timer for 4ms
  // Maximum for 16-bit timer 65535 - 5000 (4ms) = 60535
 
/* Use Timer3 (16-bits) to control our clock */
/* timer set to 4 ms */
 
// Call from User_Initialization()
  OpenTimer3(TIMER_INT_ON &
             T3_16BIT_RW &
             T3_SOURCE_INT &
             T3_PS_1_8
             );
 
  WriteTimer3(CLOCKOFFSET);  /* Preload timer to overflow after 4ms */
Code:
  // In user_routines_fast.c
  #include <timers.h>
  // globals declared as extern wherever else they are used
  volatile unsigned long Clockms=0; // 1 millisecond clock
  volatile unsigned int Clockds=0; // 1/10 second
  volatile unsigned int Clocksec=0; // 1 second clock
  //etc.
 
 
// In InterruptHandlerLow()
  if (PIR2bits.TMR3IF) /* TIMER 3 INTERRUPT */
  {
    /** This provides us with a clock for timing events **/
    PIR2bits.TMR3IF = 0; /* Clear Timer interrupt flag */
    WriteTimer3(CLOCKOFFSET);  /* Reset Timer to overflow in 4ms */
    Clockms += 4; /* milliseconds */
    Clockds = Clockms / 100;
    Clocksec = Clockms / 1000;
    // etc.
  }
__________________
"Rationality is our distinguishing characteristic - it's what sets us apart from the beasts." - Aristotle

Last edited by Mark McLeod : 24-02-2007 at 12:17.
  #7   Spotlight this post!  
Unread 24-02-2007, 11:29
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
Talking Re: Real time clocks, out of the question?

Thank you very much. I hope my assembly serves me well.
I think I can dig up the differences in the new controller.

Want to be a mentor...

just kidding.
  #8   Spotlight this post!  
Unread 26-02-2007, 21:12
Astronouth7303's Avatar
Astronouth7303 Astronouth7303 is offline
Why did I come back?
AKA: Jamie Bliss
FRC #4967 (That ONE Team)
Team Role: Mentor
 
Join Date: Jan 2004
Rookie Year: 2004
Location: Grand Rapids, MI
Posts: 2,071
Astronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud of
Re: Real time clocks, out of the question?

Setting up just a realtime clock isn't that hard. I was thinking of doing it myself. I just have to figure out how to configure which timer to achieve something reasonable.
  #9   Spotlight this post!  
Unread 26-02-2007, 22:27
Mike Bortfeldt Mike Bortfeldt is offline
Registered User
FRC #1126 (& 1511)
Team Role: Mentor
 
Join Date: Oct 2004
Rookie Year: 2004
Location: Rochester, NY
Posts: 119
Mike Bortfeldt has much to be proud ofMike Bortfeldt has much to be proud ofMike Bortfeldt has much to be proud ofMike Bortfeldt has much to be proud ofMike Bortfeldt has much to be proud ofMike Bortfeldt has much to be proud ofMike Bortfeldt has much to be proud ofMike Bortfeldt has much to be proud of
Re: Real time clocks, out of the question?

Here is the code we used for a "clock". It uses Timer 2 and has a number of options for flexibility. It is not a "real time clock" in the sense that it doesn't compute hour/minute/second but works off clock "ticks", which for a 2 minute match, or a 15 second autonomous, works fine for us. By default, it's configured to count milliseconds (1 tick = 1 millisecond), but has 2 other predefined options for increased accuracy.

Mike
Attached Files
File Type: zip Clock.zip (13.7 KB, 30 views)
  #10   Spotlight this post!  
Unread 27-02-2007, 09:29
Mark McLeod's Avatar
Mark McLeod Mark McLeod is online now
Just Itinerant
AKA: Hey dad...Father...MARK
FRC #0358 (Robotic Eagles)
Team Role: Engineer
 
Join Date: Mar 2003
Rookie Year: 2002
Location: Hauppauge, Long Island, NY
Posts: 8,782
Mark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond repute
Re: Real time clocks, out of the question?

In teaching timers I use a demonstration program to calculate all the possible timer prescaler/postscaler combinations for a given Hertz or time period. It's an easy beginner exercise to write such a program for the PIC (or VB), and also can be used to teach them about maintaining the Master processor communication leading to an easy elimination of the Master handshake.

Here's a sample output calculating 2400 Hz used with an LCD display off a digital output.
Attached Files
File Type: txt Timer2400Hz.TXT (4.4 KB, 30 views)
__________________
"Rationality is our distinguishing characteristic - it's what sets us apart from the beasts." - Aristotle

Last edited by Mark McLeod : 27-02-2007 at 13:11.
  #11   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.
__________________
  #12   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
  #13   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
  #14   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.
  #15   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?
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