Electronic and Computer Resources - Parallel Port Interfacing and Programming using Turbo C

 

BACK           HOME          NEXT

 

Interfacing with Push Button

 

To interface a push button or switch to the PC, all we need is just the push button or the switch itself and an accompanying pull-up resistor as shown below.

The typical value of resistor is 10k

There is no need to debounce the circuit since this is already handled by the PC.

In this configuration, the bit that will enter to the computer port is HIGH (1) if the push button is not pressed. If the push button is pressed, the bit that goes inside the computer is LOW (0). This is so since the input port will be connected directly to ground if the push button is pressed.

 Below is an example of interfacing problem that includes a push to control LED�s.

 

Alternate LED Blinking Program

Problem:

The circuit in the figure below is interfaced to the parallel port of the PC. Write a program such that when the push button (Pb) is pressed, the 2 LED�s will blink alternately. The LED�s should turn off if the Pb is released. Pressing any key in the keyboard should end the program.

R1 = R2 = 330 ohms                      R8 = 10k

Interfacing Circuit Diagram

 

Here�s the program. The numbers in the left are for the line explanations that follows.  

 

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

 

#include <dos.h>

#include <conio.h>

 

void main()

{

int in;

 

while (!kbhit())

{

in = inportb(0x379);

in = in & 0x10;

 

if (in==0)

{

outportb(0x378, 0x01);

delay(200);

outportb(0x378, 0x02);

delay(200);

}

else 

outportb(0x378, 0x03);

}

}

 

  Line explanations

  1. header file for inportb, outportb and delay functions

  2. header file for kbhit function

  3. line space to make the program more readable

  4. main function

  5. open brace for main function

  6. variable declaration of integer in

  7. line space to make the program more readable

  8. while loop with true condition if no key is pressed in the keyboard

  9. open brace for while

  10. read the status port and assigned the value to variable in

  11. in variable masked with 0001 00002 so that variable in will only have two possible values, 0000 00002 (016)if the push button is pressed and 0001 00002 (1016)if the push button is not pressed

  12. space to make the program more readable

  13. if statement which will be true if value of in is 0(meaning, the push button is pressed)

  14. open brace of if

  15. output the bits 0000 00012 to the data port which will cause LED2 to light and LED1 no light

  16. delay function to be able to observe the lighting of the LED

  17.  output the bits 0000 00102 to the data port which will cause LED1 to light and LED2 no light

  18. delay function to be able to observe the lighting of the LED

  19. close brace of if statement

  20. else statement that will be executed if the push button is not pressed

  21. output the bits 0000 00112 to the data port which will cause LED1 and LED2 to turn off

  22. close brace of while loop

  23. close brace of main function, the end of program.

 BACK         HOME        NEXT

 

 

    Contents

    Parallel Printer Port  

    Port Functions

    Sending a Byte

    Reading a Byte

    Interfacing LED

    Interfacing Push Button

    Interfacing 7-Segment

    Interfacing Coil

    Interfacing Relay

    Interfacing  S. Motor

    Interfacing AC Load

    Interfacing Sensor

 

 

 

Electronic and Computer Resources - Parallel Port Interfacing and Programming using Turbo C

 

 

 

 

Google

 

 

 

1.  The Parallel Printer Port     2.  Port Functions in Turbo C    3.  Sending a Byte to the Port    4.  Reading a Byte     5.  Interfacing with LED

6.   Interfacing with Push Button     7.  Interfacing with 7-Segment    8.  Interfacing with Coil    9.  Interfacing with Relay    

10.  Interfacing with Stepper Motor    11.  Interfacing with AC Load    12.  Interfacing with Sensor

For questions, corrections and suggestions, 

email to: [email protected]

Hosted by www.Geocities.ws

1