Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Programming (http://www.chiefdelphi.com/forums/forumdisplay.php?f=51)
-   -   quick question: TIMERS (http://www.chiefdelphi.com/forums/showthread.php?t=26637)

Ryan M. 11-03-2004 18:36

Re: quick question: TIMERS
 
Yes, it does have timers (4), but I couldn't figure out how to use them, and didn't feel the need to try much. If someone knows how, those would also work.

Kevin Watson 11-03-2004 23:14

Re: quick question: TIMERS
 
Quote:

Originally Posted by Texan
Yes, it does have timers (4), but I couldn't figure out how to use them, and didn't feel the need to try much. If someone knows how, those would also work.

Have a look at the two clock programs that I wrote and posted here. Comments within the code will show you where to place your own test code. If you run into problems, just leave a note here and we'll help you out.

-Kevin

Astronouth7303 12-03-2004 07:35

Re: quick question: TIMERS
 
I have had problems with interupts.c and timers. I use Timer 2 (and/or 1) to increment a variable and if that variable overflows, it increments another one. The problem is that I can't ever seem to get either to change. Help?

Mark McLeod 12-03-2004 10:00

Re: quick question: TIMERS
 
Quote:

Originally Posted by Astronouth7303
I have had problems with interupts.c and timers. I use Timer 2 (and/or 1) to increment a variable and if that variable overflows, it increments another one. The problem is that I can't ever seem to get either to change. Help?

Sounds like the timers haven't been enabled in your initialization code.

Here's a sample initialization of Timer 4 to cause a 4ms interrupt.
Code:

OpenTimer4(TIMER_INT_ON &
          T4_PS_1_16 &
          T4_POST_1_10);
WriteTimer4(6); /* Preload timer to overflow after 4ms */

Here's the corrsponding user_routines_fast.c interrupt code.
Code:

unsigned long Clockms; // 1 millisecond clock
unsigned long Clockcs; // 100 millisecond
unsigned long Clocksec; // 1 second clock
 
static unsigned char t100ms=0;
static unsigned int t1sec=0;
 
...
 
else if (PIR3bits.TMR4IF) /* TIMER 4 INTERRUPT */
{
  /** This provides us with a clock for timing events **/
  PIR3bits.TMR4IF = 0; /* Clear Timer interrupt flag */
  WriteTimer4(6);        /* Reset Timer to overflow at 4ms */
  Clockms += 4; /* milliseconds */
  if (++t100ms >= 25)
  {
        t100ms = 0;
        Clockcs++; /* tenths of seconds */
        if (++t1sec >= 10)
        {
          t1sec = 0;
          Clocksec++; /* seconds */
        }
  }
}


10intheCrunch 12-03-2004 10:29

Re: quick question: TIMERS
 
Quote:

Originally Posted by Astronouth7303
I have had problems with interupts.c and timers. I use Timer 2 (and/or 1) to increment a variable and if that variable overflows, it increments another one. The problem is that I can't ever seem to get either to change. Help?

Why not just use a long? You won't overflow it (depending on the timer you use you'll have a couple thousand hours of timer before the long cycles).

Xufer 12-03-2004 11:20

Re: quick question: TIMERS
 
now if i was to use the timers that kevin has on his site inorder to do a dedreckoning autonomus what would it look like ? I begin to have a little more trouble when the interrupts are thrown in there i looked through the actuall clock.c file and the user_routines_fast.c file how would i specify time lengths for an action ? Im not too sure how the interupts work i read the first white paper on them but my understanding of them are still kinda wavy.

Mark McLeod 12-03-2004 11:42

Re: quick question: TIMERS
 
Quote:

Originally Posted by Xufer
now if i was to use the timers that kevin has on his site inorder to do a dedreckoning autonomus what would it look like ? I begin to have a little more trouble when the interrupts are thrown in there i looked through the actuall clock.c file and the user_routines_fast.c file how would i specify time lengths for an action ? Im not too sure how the interupts work i read the first white paper on them but my understanding of them are still kinda wavy.

In Kevin's examples you can use the "Clock" variable as your check.
e.g.,
Code:

if (Clock < 10) // Clock is in tenths of seconds
        // Do first thing
else if (Clock < 30) // @ 3 second
        // Do another thing
else if (Clock < 60) // @ 6 seconds
        // Keep adding stuff to do
.
.
.
else if (Clock < 150) // @ 15 seconds
        // Make sure you stop all engines at the end

There are lots of other autonomous examples spread through the forums.

[edit] Corrected the ">" , typed too freely. Thanks Jamie!

Astronouth7303 12-03-2004 13:49

Re: quick question: TIMERS
 
The timer (1-4, actually) is initialized, and I have checked the enabled bit. The overflow variable (a char) is to slow it down. I send the long both over the terminal and through the dashboard. Both always say 0. The code looks like it should work, but doesn't. This is my ISR:
Code:

extern char TICK;
extern long TIME;
extern long AUTOCLOCK;

void Timer_1_Int_Handler(void)
{
 TICK++;
 if (TICK == 255)
 {  TIME++; }
 AUTOCLOCK++;
}

Also, have "Clock > 150" first and "Clock > 0" last (Descending order), I found this out in some EDU trials. "Clock > 0" will be true if "Clock > 150", so if >0 is before >150, >150 never happens because >0 does.

jacob_dilles 12-03-2004 14:35

Re: quick question: TIMERS
 
make sure its not only enabled, but i think you have to start it too

Xufer 12-03-2004 15:22

Re: quick question: TIMERS
 
i got kevins interrupt code and i looked through it, how do i call one of the timers to start counting ? then how would i implement it into autonomus ?

10intheCrunch 12-03-2004 15:27

Re: quick question: TIMERS
 
Call the start method in the User_Initialization function (Timer2Start() or Timer4Start() or whatever it is). Then, use the state machines that we told you about in the first page of this thread. In the StateInit() function, set a new variable like StartTime or something to keep track of where the timer was when you moved into the next state. Then just subtract the start from the actual, and when that value is larger than the one you want (how long you want to stay in the state), move to the next state and call the Init function.

Astronouth7303 12-03-2004 18:02

Re: quick question: TIMERS
 
Initialize_Timer_X, And you have to modify the line that reads: TxCONbits.TMRxON = 0; to TxCONbits.TMRxON = 1;, else the timer won't go.

Mark McLeod 12-03-2004 19:28

Re: quick question: TIMERS
 
Quote:

Originally Posted by Astronouth7303
The timer (1-4, actually) is initialized, and I have checked the enabled bit. The overflow variable (a char) is to slow it down. I send the long both over the terminal and through the dashboard. Both always say 0. The code looks like it should work, but doesn't. This is my ISR:
Code:

extern char TICK;
extern long TIME;
extern long AUTOCLOCK;
 
void Timer_1_Int_Handler(void)
{
TICK++;
if (TICK == 255)
{ TIME++; }
AUTOCLOCK++;
}


Fixed my earlier post. Thanks for noticing my error.

Your code looks file of course.
Have you tried displaying TICKS on the dashboard or terminal to see if it is also not getting incremented?
I have to run now, but I'll be back later and try to duplicate what you did in a test program.

Astronouth7303 12-03-2004 20:00

Re: quick question: TIMERS
 
Quote:

Originally Posted by Mark McLeod
Your code looks file of course.
Have you tried displaying TICKS on the dashboard or terminal to see if it is also not getting incremented?
I have to run now, but I'll be back later and try to duplicate what you did in a test program.

Not incrementing. Here's the declarations (In Variables.h, which is included in Interupts.c)
Code:

extern long TIME;
extern long AUTOCLOCK;
extern char TICK;

They are actually defined in Variables.c.

The User_Routines_Fast.c Interuptor (Actually executed by proc) is this, just to check:
[code]#pragma code InterruptVectorLow = LOW_INT_VECTOR

void InterruptVectorLow (void)
{
_asm
goto InterruptHandlerLow // jump to InterruptHandlerLow()
_endasm
}

#pragma code

//...

#pragma interruptlow InterruptHandlerLow save=PROD,section("MATH_DATA"),section(".tmpdata")

void InterruptHandlerLow()
{
//...
}[/check]

I didn't edit this (to my knowledge :yikes:), so I can't imagine that would be it. The code is compiling fine, no errors or warnings.

jacob_dilles 12-03-2004 20:03

Re: quick question: TIMERS
 
Quote:

Originally Posted by Astronouth7303
Initialize_Timer_X, And you have to modify the line that reads: TxCONbits.TMRxON = 0; to TxCONbits.TMRxON = 1;, else the timer won't go.

this is what im talking about. just set TxCONbits.TMRxON = 1 when you want it to start, and TxCONbits.TMRxON = 0 when you want it to stop. if you put this line outside of the while(auton) loop (before and after respectivly), it will work perfectly


All times are GMT -5. The time now is 12:47.

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