#include <stc12c5a60s2.h> // Include the specific header for the microcontroller
// Define data types for clarity and portability
typedef unsigned char uchar;
typedef unsigned int uint;
// --- Delay Function ---
// Provides approximately 1ms delay at 11.0592MHz system clock.
// Adjusted loop counts for better accuracy.
void delay_ms(uint ms)
{
uint i, j;
// Calculation: 1ms requires roughly 1085 machine cycles (11.0592MHz / 12 * 1ms).
// The loop structure aims for ~1080 cycles per millisecond.
while (ms--) {
// Outer loop iterates 'ms' times.
// Inner loops need to execute roughly 1080 cycles.
// Let's use i=110, j=10. Total cycles ~ 110 * 10 = 1100.
// Adding _nop_() inside the innermost loop helps ensure timing.
for (i = 0; i < 110; i++) {
for (j = 0; j < 10; j++) {
_nop_(); // Waste 1 cycle per iteration
}
}
}
}
// --- Pin Definitions ---
// Layer selection pins (Active LOW) - 7 layers defined
// Based on the original code's assignments:
sbit L1 = P3^0; // Layer 1 connected to P3.0
sbit L2 = P3^1; // Layer 2 connected to P3.1
sbit L3 = P3^3; // Layer 3 connected to P3.3 (Note: P3^2 is skipped)
sbit L4 = P3^4; // Layer 4 connected to P3.4
sbit L5 = P3^5; // Layer 5 connected to P3.5
sbit L6 = P3^6; // Layer 6 connected to P3.6
sbit L7 = P3^7; // Layer 7 connected to P3.7
// L8 (P2^0) is defined in the original code but not used here as it's a 7-layer display.
// Column Latch Control Pin
sbit COL_LATCH = P4^6; // Latch control signal for column drivers
// --- Pattern Definitions ---
// Define two sets of patterns for a dynamic, "beautiful" effect.
// Each array holds 7 patterns, one for each layer (index 0 = L1, index 6 = L7).
// Set 1: Visually distinct patterns
uchar patterns_set1[7] = {
0xAA, // Layer 1: Cols 0, 2, 4, 6 ON (10101010)
0x55, // Layer 2: Cols 1, 3, 5, 7 ON (01010101)
0x0F, // Layer 3: Cols 0, 1, 2, 3 ON (00001111)
0xF0, // Layer 4: Cols 4, 5, 6, 7 ON (11110000)
0x3C, // Layer 5: Cols 2, 3, 4, 5 ON (00111100)
0xC3, // Layer 6: Cols 0, 1, 6, 7 ON (11000011)
0x81 // Layer 7: Cols 0, 7 ON (10000001)
};
// Set 2: Different patterns for variation over time
uchar patterns_set2[7] = {
0x88, // Layer 1: Cols 3, 7 ON
0x11, // Layer 2: Cols 0, 4 ON
0x42, // Layer 3: Cols 1, 6 ON
0x96, // Layer 4: Cols 1, 2, 5, 6 ON
0x69, // Layer 5: Cols 0, 3, 4, 7 ON
0x00, // Layer 6: All columns OFF (to provide contrast)
0xFF // Layer 7: All columns ON
};
// Global variable to control pattern switching and dynamic effects
uchar pattern_cycle_counter = 0;
// --- Main Function ---
void main(void)
{
uchar i; // Loop variable for layers
uchar current_layer_pattern; // Stores the pattern to be displayed
uchar* active_pattern_set; // Pointer to the currently active pattern set
// 1. Configure Port Modes
// Set all relevant pins to Push-Pull Output mode for driving LEDs and control signals.
P0M0 = 0xFF; P0M1 = 0x00; // P0: Columns 0-7 -> Push-Pull Output
// P1 pins are not used in this example, default state (input) is usually fine.
P2M0 = 0x00; P2M1 = 0x00; // P2: Ensure P2.0 (L8) and others are default (input unless needed)
P3M0 = 0xFF; P3M1 = 0x00; // P3: Layer pins P3.0, P3.1, P3.3-P3.7 -> Push-Pull Output
P4M0 = 0x02; P4M1 = 0x00; // P4: Set P4.6 (COL_LATCH) -> Push-Pull Output (0x02 is 00000010b, sets bit 6)
// 2. Initialize State
// Turn OFF all layers initially (set layer pins HIGH)
L1 = L2 = L3 = L4 = L5 = L6 = L7 = 1;
// COL_LATCH should be HIGH initially (or pulse low then high to ensure reset state if needed)
COL_LATCH = 1;
// Clear column output initially
P0 = 0x00;
// 3. Main Multiplexing Loop
while (1)
{
// Select which pattern set to use based on the counter. Switches every 100 cycles.
if (pattern_cycle_counter < 100) {
active_pattern_set = patterns_set1;
} else {
active_pattern_set = patterns_set2;
}
// --- Cycle through each layer ---
// The loop runs 7 times, once for each layer.
for (i = 0; i < 7; i++)
{
// --- Turn OFF all layers ---
// Ensure only the current layer is active by setting all layer pins HIGH first.
L1 = 1; L2 = 1; L3 = 1; L4 = 1; L5 = 1; L6 = 1; L7 = 1;
// --- Activate the CURRENT layer ---
// Set the specific layer pin LOW based on the loop index 'i'.
switch (i) {
case 0: L1 = 0; break; // Activate Layer 1 (P3^0)
case 1: L2 = 0; break; // Activate Layer 2 (P3^1)
case 2: L3 = 0; break; // Activate Layer 3 (P3^3)
case 3: L4 = 0; break; // Activate Layer 4 (P3^4)
case 4: L5 = 0; break; // Activate Layer 5 (P3^5)
case 5: L6 = 0; break; // Activate Layer 6 (P3^6)
case 6: L7 = 0; break; // Activate Layer 7 (P3^7)
}
// --- Select and Modify Pattern for "Beautiful Effect" ---
// Get the base pattern for the current layer from the active set.
current_layer_pattern = active_pattern_set[i];
// Add a dynamic "flicker" or change:
// Let one layer (determined by the counter) have a slightly different pattern.
// This XORs bits 0, 2, 4, 6 (pattern 0xAA) with the layer pattern if the condition matches.
if (i == (pattern_cycle_counter % 7)) {
current_layer_pattern ^= 0xAA; // Toggle specific bits for visual flair
}
// --- Output Column Data and Latch ---
P0 = current_layer_pattern; // Send the calculated pattern to the columns (P0.0 to P0.7)
COL_LATCH = 0; // Start the latch pulse
// Add a small delay for the latch pulse width if required by the driver IC,
// otherwise, just the transition is often enough.
// _nop_(); _nop_();
COL_LATCH = 1; // End the latch pulse (rising edge often latches data)
// --- Delay for Visibility ---
// Keep the current layer pattern displayed for ~1ms.
// This controls the refresh rate. 7 layers * 1ms/layer = 7ms per full cycle.
// This gives a refresh rate of ~140Hz, which is smooth.
delay_ms(1);
} // End of layer loop
// --- Update Counter for Pattern Switching ---
pattern_cycle_counter++;
// Reset the counter after 200 cycles to switch between pattern sets.
if (pattern_cycle_counter >= 200) {
pattern_cycle_counter = 0; // Reset to switch back to pattern_set1
}
// Optional: A small delay between full pattern cycles might be added here if needed,
// but the delay inside the layer loop determines the primary refresh rate.
}
}