di2954.txt ;*************************************************************************************** ; ; LISTING 1 - DELAY-LOOP ROUTINE ; ; "Maintain precise timing with PC's speaker logic," EDN, July 11, 2002, pg 106 ; ;*************************************************************************************** /**** Program Generates Precise Delays ****/ /**** Written By: D.S.Oberoi & Harinder Dhingra ****/ #include #include #define counter_count_time 0.0008380958 /* Counter2's single count period(mS) */ #define read_port 0x378 /* Address of Read Port */ unsigned counter_data, count_elapsed, required_count; float desired_delay_time; void delay_loop(unsigned delay_count); main() { desired_delay_time=0.9; /* Delay Time in milli second */ /* Find how many counts are required for the desired delay */ required_count = desired_delay_time/counter_count_time; for (;;) { ; /* Place the desired statements here, before delay */ ; delay_loop(required_count); /* Call The Delay Routine */ ; /* Place the desired processing statements here, after delay */ ; } } /* Delay Routine starts here */ void delay_loop(unsigned delay_count) { count_elapsed = 0; asm mov al, 0b4h /* Set the counter2 operation */ asm out 043h,al asm mov al, 0ffh /* Load the intial count as ffffh */ asm out 042h, al /* completed in two cycles */ asm out 042h,al asm mov al, 01h /* Enable the counter and Start the ON period */ asm out 061h, al /* Counter data is read till, desired counts are not obtained */ while (count_elapsed < required_count) { asm in al, 042h /* Get LSB */ asm mov cl, al asm in al, 042h /* Get the MSB */ asm mov ah, al asm mov al, cl /* Two bytes, combined to get 16 bit data*/ asm mov counter_data , ax count_elapsed = (0xffff- counter_data); /* Calculate the elapsed counts */ } /* Disable the counter2 */ asm mov al, 00h asm out 061h, al }