27 lines
663 B
Python
27 lines
663 B
Python
import asm
|
||
import t, c
|
||
|
||
PIT_CHANNEL0: t.CDefine = 0x40
|
||
PIT_CHANNEL1: t.CDefine = 0x41
|
||
PIT_CHANNEL2: t.CDefine = 0x42
|
||
PIT_COMMAND: t.CDefine = 0x43
|
||
|
||
def pit_init(frequency: t.CInt):
|
||
# 参数验证:确保频率为正数
|
||
if frequency <= 0: return
|
||
|
||
# 计算除数,使用16位无符号整数
|
||
divisor: t.CUInt32T = 1193180 / frequency
|
||
|
||
# 发送命令字:通道0,先低后高,模式3(方波),二进制计数
|
||
asm.outb(PIT_COMMAND, 0x36)
|
||
asm.io_wait()
|
||
|
||
# 写入低字节
|
||
asm.outb(PIT_CHANNEL0, divisor & 0xFF)
|
||
asm.io_wait()
|
||
|
||
# 写入高字节
|
||
asm.outb(PIT_CHANNEL0, (divisor >> 8) & 0xFF)
|
||
asm.io_wait()
|