next up previous
Next: 4.3.5 サンプルプログラム3の解説 Up: 4.3 RT-Linuxのサンプルプログラム Previous: 4.3.3 サンプルプログラム1の解説

4.3.4 サンプルプログラム2の解説

サンプル1におけるusleep を gettimeofdayを使って置き換えた.
int play_sound (int hz, int time) { // hz(Hz), time(msec)
    int tmp, i;
    long int d;
    struct timeval tv[2];
    struct timezone tz;

    for (i = 0; i < hz * 2 * time / 1000; ++i) {
        gettimeofday(&tv[0], &tz);
        do {
            gettimeofday(&tv[1], &tz);
            d = (tv[1].tv_sec - tv[0].tv_sec)*(1000000) +
                (tv[1].tv_usec - tv[0].tv_usec);
        } while (d < 500*1000/hz);
        tmp = inb(0x61);
        tmp = (i & 1) ? tmp | 0x02 : tmp & ~0x02;
        outb(tmp, 0x61);
    }
    outb((inb(0x61) & ~0x02), 0x61);
}


generated through LaTeX2HTML. M.Inaba 平成18年5月6日