Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <8051.h>
- /*
- * Device Layout and Pin Connections
- * ================================
- *
- * LED Layout (from top to bottom):
- * ------------------------------
- * D1 > P0.0
- * D2 > P0.1
- * D3 > P0.2
- * D4 > P0.3
- * D5 > P0.4 (Upper middle LED)
- * D6 > P0.5 (Upper middle LED)
- * D7 > P0.6 (Upper middle LED)
- * D8 > P0.7 (Upper middle LED)
- * D16 > P2.7 (Lower middle LED)
- * D15 > P2.6 (Lower middle LED)
- * D14 > P2.5 (Lower middle LED)
- * D13 > P2.4 (Lower middle LED)
- * D12 > P2.3
- * D11 > P2.2
- * D10 > P2.1
- * D9 > P2.0
- *
- * Switch Connections:
- * ------------------
- * Power Switch > VCC
- * Button > P3.7
- * Vibration > P3.2
- *
- * Note: The middle LEDs (D5-D8, D16-D13) are used for the main display pattern
- */
- // Define LED ports
- #define LEDS_LOW P0 // D1-D8 connected to P0.0-P0.7
- #define LEDS_HIGH P2 // D9-D16 connected to P2.0-P2.7
- // Button and motion sensor
- __sbit __at (0xB7) BUTTON; // P3.7 Button for changing display
- __sbit __at (0xB2) MOTION; // P3.2 Vibration/motion sensor
- // Current mode index
- unsigned char currentMode = 0;
- #define NUM_MODES 4
- // Button debounce variable
- __bit lastButtonState = 1;
- // IMPORTANT: For active LOW configuration, 0 = LED ON, 1 = LED OFF
- // LED mapping:
- // P0 (LEDS_LOW): Bits 4-7 control D5-D8 (upper middle)
- // P2 (LEDS_HIGH): Bits 4-7 control D13-D16 (lower middle)
- // X pattern (active LOW - 0 = ON)
- const __code unsigned char bar_pattern_low[] = {
- 0b11111111, // All OFF (space)
- 0b11111111, // All OFF (space)
- 0b11111111, // All OFF (space)
- 0b11111111, // All OFF (space)
- 0b10001111, // D5,D8 ON (X)
- 0b01001111, // D6,D7 ON (X)
- 0b01001111, // D6,D7 ON (X)
- 0b10001111, // D5,D8 ON (X)
- 0b11111111, // All OFF (space)
- 0b11111111, // All OFF (space)
- 0b11111111, // All OFF (space)
- 0b11111111 // All OFF (space)
- };
- const __code unsigned char bar_pattern_high[] = {
- 0b11111111, // All OFF (space)
- 0b11111111, // All OFF (space)
- 0b11111111, // All OFF (space)
- 0b11111111, // All OFF (space)
- 0b11110001, // D16,D13 ON (X)
- 0b11110010, // D15,D14 ON (X)
- 0b11110010, // D15,D14 ON (X)
- 0b11110001, // D16,D13 ON (X)
- 0b11111111, // All OFF (space)
- 0b11111111, // All OFF (space)
- 0b11111111, // All OFF (space)
- 0b11111111 // All OFF (space)
- };
- // Function declarations
- void delay_ms(unsigned int ms);
- void display_column(unsigned char pattern_low, unsigned char pattern_high);
- void check_button(void);
- void display_pattern(void);
- // Speed control - using user's values that worked well
- #define COLUMN_DISPLAY_TIME 3 // Time each column is displayed (milliseconds)
- #define COLUMN_SPACE_TIME 1 // Time between columns (milliseconds)
- // Main function
- void main(void) {
- // Initialize ports - all LEDs OFF
- LEDS_LOW = 0xFF;
- LEDS_HIGH = 0xFF;
- // Main loop
- while(1) {
- // Check for motion
- if (MOTION == 0) { // Motion detected (active LOW)
- // Display the X pattern
- for(unsigned char i = 0; i < sizeof(bar_pattern_low); i++) {
- display_column(bar_pattern_low[i], bar_pattern_high[i]);
- }
- }
- }
- }
- // Function to display a single column pattern
- void display_column(unsigned char pattern_low, unsigned char pattern_high) {
- // Apply the pattern to both LED groups
- LEDS_LOW = pattern_low;
- LEDS_HIGH = pattern_high;
- // Delay for persistence of vision effect
- delay_ms(COLUMN_DISPLAY_TIME);
- // Turn off all LEDs between columns
- LEDS_LOW = 0xFF;
- LEDS_HIGH = 0xFF;
- // Small delay between columns
- delay_ms(COLUMN_SPACE_TIME);
- }
- // Function to provide millisecond delay
- void delay_ms(unsigned int ms) {
- unsigned int i, j;
- for(i = 0; i < ms; i++) {
- for(j = 0; j < 120; j++); // Approximately 1ms delay at 12MHz
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement