di2726.txt ;************************************************************************************************ ; LISTING 1 - TURBO C++ LISTING FOR PULSE GENERATION ; ; "Centronics port generates narrow pulse widths," EDN, Aug 2, 2001, pg 97 ; ;************************************************************************************************ /**** Program to Generate Pulse Width less then 1mSecond ****/ /**** Written By: Darvinder Singh Oberoi ****/ /**** CEDTI, JAMMU, INDIA ****/ #include #include #include #pragma inline /*** All values are in milli Second ***/ #define Max_Time 0.750 /* Maximum Pulse Width Time(mS) */ #define Neutral_Time 0.500 /* Neutral Width Time(mS) */ #define Min_Time 0.250 /* Minimum Pulse Width Time(mS) */ #define Time_Step 0.050 /* Time(mS) Steps for Pulse Change */ #define Counter_Count_Time 0.0008380958 /* Counter2's single count period(mS) */ #define Off_Time 10 /* Pulse OFF Duration (mS) */ /* Address of Printer Port */ #define Printer_Port 0x378 void screen_display(); /* Routine for displaying Instruction */ /***** Main Program Starts Here *****/ main() { char ch; unsigned counter_data, count_elapsed,required_count; float desired_time,counter_time; desired_time=Neutral_Time; /* Initial time */ clrscr(); screen_display(); /* Counter2 is set in Mode 2, control word data is b4 */ asm mov al, 0b4h /* Set the counter2 operation */ asm out 043h,al for (;ch != 'q';) /* Loop Till q/Q key is pressed */ { if (kbhit()) /* Check, if any key has been pressed or not */ ch = getch(); switch (ch) /* And take action depending upon the key pressed */ { case 'Q': ch = 'q';break; /* If Q key is pressed */ case 52: desired_time -=Time_Step; /* If 6 NUM Key is pressed */ ch=' ';break; case 54: desired_time += Time_Step;/* If 4 NUM Key is pressed */ ch=' ';break; case 53: desired_time=Neutral_Time; /* If 5 NUM Key is pressed */ ch=' ';break; case 56: desired_time =Max_Time; /* If 8 NUM Key is pressed */ ch=' ';break; case 50: desired_time =Min_Time; /* If 2 NUM Key is pressed */ ch=' ';break; } /* Check the extreme positions and reset them in case of violation */ if (desired_time > Max_Time) desired_time = Max_Time; if (desired_time < Min_Time) desired_time = Min_Time; /* Finds how many counter2 counts are required for the desired time interval */ required_count = desired_time/Counter_Count_Time; count_elapsed = 0; /* Counter2 is loaded with initial with count of ffff */ asm mov al, 0ffh /* Load the initial count as ffff */ asm out 042h, al /* Done in two cycles */ asm out 042h,al asm mov al, 01h /* Enable the counter to Start the ON period */ asm out 061h, al /* Desired Pulse is available at printer port's pin no.-2 */ /* The signal is inverted by the buffer stage */ asm mov al, 0 asm mov dx, Printer_Port asm out dx, al /* The read the data in this loop, till desired delay is 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 */ } /* Make pulse signal low here, after the desired counts have elapsed */ asm mov al, 1 /* Signal is inverted */ asm mov dx, Printer_Port asm out dx, al /* Disable the counter2 here */ asm mov al, 00h asm out 061h, al /* Print the information on the screen */ counter_time=count_elapsed*Counter_Count_Time; /* Calculate counter time */ gotoxy(8,12); cprintf("%f",desired_time); gotoxy(26,12); cprintf("%f",counter_time); gotoxy(45,12); cprintf(" "); gotoxy(60,12); cprintf(" " ); gotoxy(45,12); cprintf("%u", count_elapsed); gotoxy(60,12); cprintf("%u",required_count ); /* OFF period taken as 20ms*/ delay (Off_Time); } /* End of FOR Loop */ textcolor(LIGHTGRAY); /* Set the text color and clear the screen */ clrscr(); } /* END of the MAIN program here */ /**** Routine Which display the instructions & Other Information ****/ void screen_display() { textcolor(YELLOW); gotoxy(26,2); cprintf("Make Sure Num Lock is ON"); gotoxy(5,11); cprintf("Desired Time(mS) Counter Time(mS) Count Elapsed Required Count"); textcolor(YELLOW); gotoxy(25,19); cprintf("< Press Q/q key to Quit the Program > "); gotoxy(15,21); cprintf("Press 4 NUM Key to Decrease Time (By %f ms)",Time_Step); gotoxy(15,22); cprintf("Press 6 NUM Key to Increase Time (By %f ms)",Time_Step); gotoxy(15,23); cprintf("Press 8 NUM Key For Maximum Pulse Width ( %f ms )",Max_Time); gotoxy(15,24); cprintf("Press 2 NUM Key For Minimum Pulse Width ( %f ms )",Min_Time); gotoxy(15,25); cprintf("Press 5 NUM Key For Neutral Position ( %f ms )",Neutral_Time); textcolor(WHITE); } /*pulse.c*/