Help with timer

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
Tjaalie
Member
Member
Posts: 25
Joined: Mon Mar 21, 2005 12:00 am

Help with timer

Post by Tjaalie »

Hello,

For my OS i made i timer with a wait function but its hangs.
The code for the function is like this:

Code: Select all

void TimerWait(int ticks)
{
     //Vars
     int WaitTime = Timer + ticks;
     
     //Kijk of we niet te ver gaan
     if (WaitTime >= 1000)
     {
        WaitTime -= 1000;
     }
     
     //Wait
     while (Timer < WaitTime);
}
I dont know why it hangs its just so n00b but can anybody tell me why?[/code]
rexlunae
Member
Member
Posts: 134
Joined: Sun Oct 24, 2004 11:00 pm
Location: North Dakota, where the buffalo roam

Re: Help with timer

Post by rexlunae »

Tjaalie wrote:I dont know why it hangs its just so n00b but can anybody tell me why?
It probably hangs because of that loop at the end. The code you gave us does not contain anything that causes that loop to end. What is Time? Is it a global variable updated on an interrupt?
Last edited by rexlunae on Mon Mar 21, 2005 12:00 am, edited 1 time in total.
Tjaalie
Member
Member
Posts: 25
Joined: Mon Mar 21, 2005 12:00 am

Re: Help with timer

Post by Tjaalie »

The timer variable works like:

Code: Select all

void HandleTimer(struct regs* r)
{
     Timer++;

     if (Timer >= 1000)
     {
          Timer = 0;
     } 
}
So can you tell me whats wrong with the loop?
The i know the HanleTimer function get called cause when i put an print function in it it prints the text many times so its works. What do i do wrong?
Anton
Member
Member
Posts: 30
Joined: Thu Oct 21, 2004 11:00 pm
Location: Moscow, Russian Federation

Re: Help with timer

Post by Anton »

Tjaalie wrote: So can you tell me whats wrong with the loop?
Do you have optimization turned on? If you do turn it off.
Also make the variable Timer volatile
Tjaalie wrote: The i know the HanleTimer function get called cause when i put an print function in it it prints the text many times so its works.
This is wrong, since if you remove an print function, the compiler might optimize out the code.
Anton.
rexlunae
Member
Member
Posts: 134
Joined: Sun Oct 24, 2004 11:00 pm
Location: North Dakota, where the buffalo roam

Re: Help with timer

Post by rexlunae »

Anton wrote:Do you have optimization turned on? If you do turn it off.
Also make the variable Timer volatile
All that should be necessary is declaring it volatile. Turning off optimizations should not be necessary. I wonder what the interrupt flag is set to when he enters the loop though. If it is not allowing interrupts, the Timer variable won't get changed.
Tjaalie
Member
Member
Posts: 25
Joined: Mon Mar 21, 2005 12:00 am

Re: Help with timer

Post by Tjaalie »

thanx i only had to volatile it but what does
volatile do. can any body explain that why its working
now and not without it?
Last edited by Tjaalie on Tue Mar 22, 2005 12:00 am, edited 1 time in total.
bregma
Member
Member
Posts: 25
Joined: Tue Oct 26, 2004 11:00 pm
Location: the back woods
Contact:

Re: Help with timer

Post by bregma »

Tjaalie wrote:thanx i only had to volatile it but what does
volatile do. can any body explain that why its working
now and not without it?
"volatile" effectively tells the compiler that the memory location may be changed without the compiler's knowledge, so it won't try to use a cached value in a register but instead always retrieve it from memory.

--
smw
Legend
Member
Member
Posts: 195
Joined: Tue Nov 02, 2004 12:00 am
Contact:

Re: Help with timer

Post by Legend »

Or more general perhaps, it protectes the variable from optimizations.
*post*
jcmatias
Posts: 11
Joined: Mon Nov 08, 2004 12:00 am
Location: Ribeirao Preto SP Brasil

Re: Help with timer

Post by jcmatias »

How do you use this code?
Is this a ISR ?
If is the case , remember that the ISR return is IRET.
DruG5t0r3
Member
Member
Posts: 29
Joined: Thu Mar 24, 2005 12:00 am
Contact:

Re: Help with timer

Post by DruG5t0r3 »

That Timer function is far from being reliable. Lets say you have a 1mhz CPU and another one with 2 billion teraflops....(OK i'm exagerating)... Which one will go out of the loop first?...

I recommend to rely on IRQ 0 , I believe it's the only reliable timer you have on a x86 system.
Tjaalie
Member
Member
Posts: 25
Joined: Mon Mar 21, 2005 12:00 am

Re: Help with timer

Post by Tjaalie »

this timer uses the irq 0
Post Reply