setting timer speed

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
User avatar
AndrewAPrice
Member
Member
Posts: 2309
Joined: Mon Jun 05, 2006 11:00 pm
Location: USA (and Australia)

setting timer speed

Post 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.
My OS is Perception.
User avatar
lode
Posts: 15
Joined: Tue Oct 17, 2006 6:28 pm
Location: Oulu, Finland

Post 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
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Post 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.
Last edited by pcmattman on Mon Sep 10, 2007 4:36 pm, edited 1 time in total.
User avatar
AndrewAPrice
Member
Member
Posts: 2309
Joined: Mon Jun 05, 2006 11:00 pm
Location: USA (and Australia)

Post by AndrewAPrice »

Lode wrote:Umm, 12.5 times a second would be 80ms, not 8ms.
Sorry. 125 times a second.
My OS is Perception.
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Post 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
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Post 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.
Post Reply