Okay, let's modify your STC12C5A60S2 code to implement a matrix display concept, using P1 for 8 columns and P3 for 7 rows (assuming you have a 7x8 LED matrix connected this way).
The original code directly controls 7 LEDs on P1. This new code will control a matrix by scanning.
Here's the modified code. We'll reinterpret your two original patterns for a matrix:
Pattern 1 (All Bright Blink): Instead of setting P1 directly, we will rapidly scan through all columns, turning on the appropriate rows in each column to make the whole matrix appear bright. Then we scan with all LEDs off.
Pattern 2 (Individual LED Sequence): Instead of turning on individual bits of P1, we will turn on individual LEDs in the matrix by activating one column and one row at a time. We'll turn on the first 7 LEDs in the first row (Row 0, Columns 0-6) sequentially, similar to your original P1 blinking pattern.
We'll assume a common wiring setup where:
P1.0 to P1.7 control the 8 columns.
P3.0 to P3.6 control the 7 rows.
Columns are active low (e.g., connected via NPN transistors to ground). Setting a P1 bit to 0 activates that column. Setting P1 to 0xFF turns all columns off.
Rows are active high (e.g., connected via PNP transistors to VCC). Setting a P3 bit to 1 activates that row. Setting P3 to 0x00 turns all rows off.
An LED at (Row r, Column c) turns on when P3 bit r is high AND P1 bit c is low.
c
#include <stc12c5a60s2.h>
#define uchar unsigned char
#define uint unsigned int
// Global variables (t is now redefined inside main, n is global)
uint t;
uchar n;
// Adjust delay based on your microcontroller's clock frequency
// This delay function is blocking and simple. For matrix scanning,
// a timer interrupt is usually preferred for smoother display,
// but we'll stick to your style for this example.
void delay(uint q)
{
uint i,j;
for(i=0;i<q;i++)
for(j=0;j<110;j++); // Calibration might be needed for ~ms per q
}
// Function to scan and display data for a single column
// col: 0-7 (column index)
// row_data: 8-bit value where bit r (0-6) is 1 if LED at (r, col) should be on
void scan_column(uchar col, uchar row_data)
{
// Activate the specified column (assuming active low)
P1 = ~(1 << col); // Example: col=0 -> P1 = ~0x01 = 0xFE (P1.0 low, others high)
// Set the row data for the activated column (assuming active high for rows)
// We only use P3.0-P3.6 for 7 rows, so mask the upper bit
P3 = (row_data & 0x7F); // Example: if row_data=0x7F, P3 becomes 0x7F (rows 0-6 high)
// Brief delay for persistence of vision
// This value is crucial. Too long, and you see flicker. Too short, and brightness is low.
// This delay(1) might be around 1ms depending on calibration. A full scan (8 columns)
// would take ~8ms. A refresh rate of 125Hz (1000/8) is usually acceptable.
delay(1); // Adjust this value as needed
// Deactivate the column and rows immediately after the brief display time
// This prevents ghosting (LEDs in the wrong column/row faintly lit).
P1 = 0xFF; // All columns off (active low)
P3 = 0x00; // All rows off (active high)
}
void main()
{
// Configure P1 and P3 as push-pull output
P1M0 = 0xff;
P1M1 = 0x00;
P3M0 = 0xff;
P3M1 = 0x00;
// Initialize ports to turn off all LEDs in the matrix
// Assuming P1 (columns) active low, P3 (rows) active high
P1 = 0xFF; // Set all column bits high (inactive)
P3 = 0x00; // Set all row bits low (inactive)
while(1)
{
// Pattern 1: All Bright Blink (simulated by fast scanning)
t = 800; // Duration for each phase (on/off)
// Number of full 8-column scans needed within the 't' duration
// Adjust '10' based on the actual time taken by one full scan cycle (8 * delay(1) + overhead)
// If delay(1) is ~1ms, one scan cycle is ~8ms. 800ms / 8ms = 100 scans. Let's use a factor like 10 or 20.
// The goal is to repeat the scanning many times within the total ON/OFF duration 't'.
uint num_scans_per_phase = t / 20; // Rough estimate, needs tuning
if (num_scans_per_phase == 0) num_scans_per_phase = 1; // Ensure at least one scan cycle
for(n=0; n<4; n++) // Blink cycle (4 times)
{
// Display "All On" by scanning all columns with all 7 rows active
uchar all_on_rows = 0x7F; // Bits 0-6 high for 7 rows
for (uint k = 0; k < num_scans_per_phase; k++) // Repeat scanning for duration 't'
{
for (uchar col = 0; col < 8; col++) // Scan all 8 columns
{
scan_column(col, all_on_rows);
}
}
// Display "All Off" by scanning all columns with all rows inactive
uchar all_off_rows = 0x00; // All row bits low
for (uint k = 0; k < num_scans_per_phase; k++) // Repeat scanning for duration 't'
{
for (uchar col = 0; col < 8; col++) // Scan all 8 columns
{
scan_column(col, all_off_rows);
}
}
}
// Pattern 2: Individual LED Sequence (using blocking delay)
t = 400; // Duration for each individual LED
uchar row_to_blink = 0; // Let's use the first row (Row 0)
uchar row_pattern = (1 << row_to_blink); // Bit 0 high for Row 0
for(n=0; n<4; n++) // Repeat the sequence (4 times)
{
// Sequence through the first 7 columns in the selected row
// This simulates the original P1 blinking through bits 0-6
for (uchar i = 0; i < 7; i++) // Loop through columns 0 to 6
{
// To display a single LED at (row_to_blink, i) for time 't'
// we simply activate that specific column and row combination
// and keep it active during the blocking delay(t).
P1 = ~(1 << i); // Activate column i (active low)
P3 = row_pattern; // Activate the specified row (active high)
delay(t); // Keep this single LED ON for duration 't'
// Turn off all LEDs before moving to the next one
P1 = 0xFF; // All columns off
P3 = 0x00; // All rows off
// No delay needed here, the delay(t) of the *next* LED provides the gap.
}
}
}
}
Explanation of Changes:
Matrix Control: Instead of setting P1 directly to represent the LEDs, we now coordinate P1 (for columns) and P3 (for rows) to control individual LEDs in the matrix.
scan_column Function: A helper function is introduced to activate one column (P1) and set the desired state for the rows (P3) in that column, followed by a very short delay for persistence, and then turning off the column/rows to prevent ghosting.
Initialization:P1 is initialized to 0xFF (all columns off, assuming active low) and P3 to 0x00 (all rows off, assuming active high). This ensures the matrix is blank initially.
Pattern 1 (All Bright Blink):
We no longer just set P1=0x7F.
To make the matrix appear "all on", we must rapidly scan through all 8 columns (for (uchar col = 0; col < 8; col++)).
For each column, we call scan_column with row_data = 0x7F (all 7 rows active) to turn on all LEDs in that column.
This full 8-column scan cycle is repeated many times (num_scans_per_phase times) within the total 'on' duration (t).
The "off" phase is implemented similarly, but with row_data = 0x00 (all rows off).
Pattern 2 (Individual LED Sequence):
We no longer set P1 to 0x01, 0x02, etc.
To turn on a single LED at (Row 0, Column i), we directly set P1 to activate column i (~(1 << i)) and P3 to activate row 0 ((1 << 0)).
The delay(t) keeps this specific LED combination active for the desired duration.
After the delay, we turn off all columns (P1 = 0xFF;) and all rows (P3 = 0x00;) to ensure the LED is off before the next one in the sequence is turned on.
The loop iterates through columns 0 to 6 in the first row, replicating the number of LEDs controlled by your original code's second pattern.
This modified code changes the logic from simple direct port control to matrix scanning, effectively converting the concept from controlling a line of LEDs to controlling LEDs arranged in a 7x8 grid using column-based scanning. Remember to adjust the delay function and the short_scan_delay_val and num_scans_per_phase calculations based on your actual CPU clock and the desired brightness/flicker rate.