Page 1 of 1

setting timer speed

Posted: Sun Sep 09, 2007 4:21 pm
by AndrewAPrice
I've been working on porting newlib lately that I've forgotten one important multitasking question. How do I set the rate at which the internal timer fires? I think 8ms/(12.5 times a second) is a good rate.

Posted: Sun Sep 09, 2007 5:31 pm
by lode
Umm, 12.5 times a second would be 80ms, not 8ms.

Anyway.

The basic frequency is 1 193 182 Hz, you calculate the divider from this:
divider = 1193182 / desired_frequency_in_hz;

Then output 0x36 to PIT control port 0x43 to set the divider.
Output the least significant 8 bits to port 0x40, and
then the next 8 bits to the same port.

HTH

Posted: Sun Sep 09, 2007 5:57 pm
by pcmattman
Try this:

Code: Select all

/// Sets the clock rate, in hertz
void SetClockRate( short hz )
{
	short divisor = 1193180 / hz;       /* Calculate our divisor */
	outportb(0x43, 0x36);             /* Set our command byte 0x36 */
	outportb(0x40, divisor & 0xFF);   /* Set low byte of divisor */
	outportb(0x40, divisor >> 8);     /* Set high byte of divisor */
}
In my OS I setup the timer to tick at 1000 Hz.

Posted: Mon Sep 10, 2007 4:51 am
by AndrewAPrice
Lode wrote:Umm, 12.5 times a second would be 80ms, not 8ms.
Sorry. 125 times a second.

Posted: Mon Sep 10, 2007 5:46 am
by JamesM
Note that the divisor has a max length of 16 bits, which makes the minimum frequency 18.2Hz. See

<shameless plug>
http://www.jamesmolloy.co.uk/tutorial_h ... 20PIT.html
</shameless plug>

If you want a little more info / practical implementation.

JamesM

Posted: Mon Sep 10, 2007 4:36 pm
by pcmattman
JamesM wrote:Note that the divisor has a max length of 16 bits, which makes the minimum frequency 18.2Hz.
I just noticed that (only two bytes are sent)... I'll have to update my code to only use a short instead of an int - thanks :D.