Facebook
From fhbhbfd, 1 Month ago, written in Plain Text.
This paste is a reply to Re: Re: Re: Codesadsad from gfydhm - view diff
Embed
Download Paste or View Raw
Hits: 152
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4.  
  5. #define DATA_PORT 0x378 /* parallel port base address */
  6. #define STATUS_PORT (DATA_PORT + 1)
  7. #define CONTROL_PORT (DATA_PORT + 2)
  8.  
  9. unsigned char status, data;
  10.  
  11. // Array to hold bit patterns for each digit
  12. unsigned char BCD_values[10] = {
  13.     0b00000000, // digit 0
  14.     0b00000001, // digit 1
  15.     0b00000010, // digit 2
  16.     0b00000011, // digit 3
  17.     0b00000100, // digit 4
  18.     0b00000101, // digit 5
  19.     0b00000110, // digit 6
  20.     0b00000111, // digit 7
  21.     0b00001000, // digit 8
  22.     0b00001001  // digit 9
  23. };
  24.  
  25. int main()
  26. {
  27.     if (ioperm(DATA_PORT, 3, 1))
  28.     {
  29.         fprintf(stderr, "Access denied to ports\n");
  30.         exit(1);
  31.     }
  32.  
  33.     int button_presses = 0;
  34.     // int previous_status = 0;
  35.  
  36.     while (1)
  37.     {
  38.         status = inb(STATUS_PORT); // Read from status port
  39.  
  40.         // Check if the button is pressed (bit 2)
  41.         if ((status & 0b00010000))
  42.         {
  43.             button_presses++; // Increment button press counter
  44.             printf("Button pressed! Total presses: %d\n", button_presses);
  45.             data = BCD_values[button_presses % 10]; // Get BCD value for current digit
  46.             outb(data, DATA_PORT);                  // Display the digit
  47.             while ((status & 0b00010000))
  48.             {
  49.                 usleep(100000);
  50.             }
  51.         }
  52.     }
  53.  
  54.     ioperm(DATA_PORT, 3, 0); // Release the ports
  55.     return 0;
  56. }
  57.