Advertisement
Guest User

Untitled

a guest
Mar 30th, 2025
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.95 KB | Gaming | 0 0
  1. #include <8051.h>
  2.  
  3. /*
  4.  * Device Layout and Pin Connections
  5.  * ================================
  6.  *
  7.  * LED Layout (from top to bottom):
  8.  * ------------------------------
  9.  * D1  > P0.0
  10.  * D2  > P0.1
  11.  * D3  > P0.2
  12.  * D4  > P0.3
  13.  * D5  > P0.4  (Upper middle LED)
  14.  * D6  > P0.5  (Upper middle LED)
  15.  * D7  > P0.6  (Upper middle LED)
  16.  * D8  > P0.7  (Upper middle LED)
  17.  * D16 > P2.7  (Lower middle LED)
  18.  * D15 > P2.6  (Lower middle LED)
  19.  * D14 > P2.5  (Lower middle LED)
  20.  * D13 > P2.4  (Lower middle LED)
  21.  * D12 > P2.3
  22.  * D11 > P2.2
  23.  * D10 > P2.1
  24.  * D9  > P2.0
  25.  *
  26.  * Switch Connections:
  27.  * ------------------
  28.  * Power Switch > VCC
  29.  * Button      > P3.7
  30.  * Vibration   > P3.2
  31.  *
  32.  * Note: The middle LEDs (D5-D8, D16-D13) are used for the main display pattern
  33.  */
  34.  
  35. // Define LED ports
  36. #define LEDS_LOW P0    // D1-D8 connected to P0.0-P0.7
  37. #define LEDS_HIGH P2   // D9-D16 connected to P2.0-P2.7
  38.  
  39. // Button and motion sensor
  40. __sbit __at (0xB7) BUTTON;    // P3.7 Button for changing display
  41. __sbit __at (0xB2) MOTION;    // P3.2 Vibration/motion sensor
  42.  
  43. // Current mode index
  44. unsigned char currentMode = 0;
  45. #define NUM_MODES 4
  46.  
  47. // Button debounce variable
  48. __bit lastButtonState = 1;
  49.  
  50. // IMPORTANT: For active LOW configuration, 0 = LED ON, 1 = LED OFF
  51.  
  52. // LED mapping:
  53. // P0 (LEDS_LOW): Bits 4-7 control D5-D8 (upper middle)
  54. // P2 (LEDS_HIGH): Bits 4-7 control D13-D16 (lower middle)
  55.  
  56. // X pattern (active LOW - 0 = ON)
  57. const __code unsigned char bar_pattern_low[] = {
  58.     0b11111111,  // All OFF (space)
  59.     0b11111111,  // All OFF (space)
  60.     0b11111111,  // All OFF (space)
  61.     0b11111111,  // All OFF (space)
  62.     0b10001111,  // D5,D8 ON (X)
  63.     0b01001111,  // D6,D7 ON (X)
  64.     0b01001111,  // D6,D7 ON (X)
  65.     0b10001111,  // D5,D8 ON (X)
  66.     0b11111111,  // All OFF (space)
  67.     0b11111111,  // All OFF (space)
  68.     0b11111111,  // All OFF (space)
  69.     0b11111111   // All OFF (space)
  70. };
  71.  
  72. const __code unsigned char bar_pattern_high[] = {
  73.     0b11111111,  // All OFF (space)
  74.     0b11111111,  // All OFF (space)
  75.     0b11111111,  // All OFF (space)
  76.     0b11111111,  // All OFF (space)
  77.     0b11110001,  // D16,D13 ON (X)
  78.     0b11110010,  // D15,D14 ON (X)
  79.     0b11110010,  // D15,D14 ON (X)
  80.     0b11110001,  // D16,D13 ON (X)
  81.     0b11111111,  // All OFF (space)
  82.     0b11111111,  // All OFF (space)
  83.     0b11111111,  // All OFF (space)
  84.     0b11111111   // All OFF (space)
  85. };
  86.  
  87. // Function declarations
  88. void delay_ms(unsigned int ms);
  89. void display_column(unsigned char pattern_low, unsigned char pattern_high);
  90. void check_button(void);
  91. void display_pattern(void);
  92.  
  93. // Speed control - using user's values that worked well
  94. #define COLUMN_DISPLAY_TIME 3    // Time each column is displayed (milliseconds)
  95. #define COLUMN_SPACE_TIME 1      // Time between columns (milliseconds)
  96.  
  97. // Main function
  98. void main(void) {
  99.     // Initialize ports - all LEDs OFF
  100.     LEDS_LOW = 0xFF;
  101.     LEDS_HIGH = 0xFF;
  102.    
  103.     // Main loop
  104.     while(1) {
  105.         // Check for motion
  106.         if (MOTION == 0) {  // Motion detected (active LOW)
  107.             // Display the X pattern
  108.             for(unsigned char i = 0; i < sizeof(bar_pattern_low); i++) {
  109.                 display_column(bar_pattern_low[i], bar_pattern_high[i]);
  110.             }
  111.         }
  112.     }
  113. }
  114.  
  115. // Function to display a single column pattern
  116. void display_column(unsigned char pattern_low, unsigned char pattern_high) {
  117.     // Apply the pattern to both LED groups
  118.     LEDS_LOW = pattern_low;
  119.     LEDS_HIGH = pattern_high;
  120.    
  121.     // Delay for persistence of vision effect
  122.     delay_ms(COLUMN_DISPLAY_TIME);
  123.    
  124.     // Turn off all LEDs between columns
  125.     LEDS_LOW = 0xFF;
  126.     LEDS_HIGH = 0xFF;
  127.    
  128.     // Small delay between columns
  129.     delay_ms(COLUMN_SPACE_TIME);
  130. }
  131.  
  132. // Function to provide millisecond delay
  133. void delay_ms(unsigned int ms) {
  134.     unsigned int i, j;
  135.     for(i = 0; i < ms; i++) {
  136.         for(j = 0; j < 120; j++);  // Approximately 1ms delay at 12MHz
  137.     }
  138. }
Tags: POVwand
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement