#include <stc12c5a60s2.h>

#define uchar unsigned char
#define uint unsigned int

// 定义延时基准值
uint base_delay = 500;

// 函数声明
void delay_ms(uint ms);
void full_flash(uchar times, uint speed);
void sequential_shift(uchar times, uint speed);
void breathing_effect(uchar times, uint speed);
void cross_flash(uchar times, uint speed);

void main()
{  
    // 设置P1和P3组为推挽输出
    P1M0 = 0xff; P1M1 = 0x00;
    P3M0 = 0xff; P3M1 = 0x00;

    // 初始化端口
    P1 = 0x00;
    P3 = 0x00;

    while(1)
    {
        // 效果1：全亮闪烁
        full_flash(4, 800);
        
        // 效果2：顺序移位
        sequential_shift(3, 400);
        
        // 效果3：呼吸灯效果
        breathing_effect(2, 10);
        
        // 效果4：交叉闪烁
        cross_flash(5, 300);
    }
}

// 毫秒级延时函数
void delay_ms(uint ms)
{
    uint i, j;
    for(i = 0; i < ms; i++)
        for(j = 0; j < 110; j++);
}

// 全亮闪烁效果
void full_flash(uchar times, uint speed)
{
    uchar i;
    for(i = 0; i < times; i++)
    {
        P1 = 0x7f;    // 点亮特定LED组合
        delay_ms(speed);
        P1 = 0x00;    // 熄灭所有LED
        delay_ms(speed);
    }
}

// 顺序移位效果
void sequential_shift(uchar times, uint speed)
{
    uchar i, pattern;
    for(i = 0; i < times; i++)
    {
        for(pattern = 0x01; pattern <= 0x40; pattern <<= 1)
        {
            P1 = pattern;
            delay_ms(speed);
        }
    }
}

// 呼吸灯效果
void breathing_effect(uchar times, uint speed)
{
    uchar i, brightness;
    uint delay_val;
    
    for(i = 0; i < times; i++)
    {
        // 渐亮
        for(brightness = 0; brightness < 100; brightness++)
        {
            delay_val = 100 - brightness;
            P1 = 0x7f;
            delay_ms(delay_val * speed / 100);
            P1 = 0x00;
            delay_ms(brightness * speed / 100);
        }
        
        // 渐灭
        for(brightness = 100; brightness > 0; brightness--)
        {
            delay_val = 100 - brightness;
            P1 = 0x7f;
            delay_ms(delay_val * speed / 100);
            P1 = 0x00;
            delay_ms(brightness * speed / 100);
        }
    }
}

// 交叉闪烁效果
void cross_flash(uchar times, uint speed)
{
    uchar i;
    for(i = 0; i < times; i++)
    {
        P1 = 0x55;    // 奇数位亮
        delay_ms(speed);
        P1 = 0xAA;    // 偶数位亮
        delay_ms(speed);
    }
}                 #include <stc12c5a60s2.h>

// Define standard data types
#define uchar unsigned char
#define uint unsigned int

// Note: The LongToBin and BIN macros are not used in this simplified code,
// but are left commented out just in case for reference.
// #define LongToBin(n) (((n>>21)&0x80)|((n>>18)&0x40)|((n>>15)&0x20)|((n>>12)&0x10)|((n>>9)&0x08)|((n>>6)&0x04)|((n>>3)&0x02)|((n)&0x01))
// #define BIN(n) LongToBin(0x##n##)

// sbit definitions are not strictly necessary if using whole port writes (P0, P1, P2, P3)
// but keeping the individual sbits for clarity on which pins were originally used.
// Layer/Anode drivers (based on dh1, dh7 behavior)
sbit P0_0=P0^0; // Layer 0
sbit P0_1=P0^1; // Layer 1
sbit P0_2=P0^2; // Layer 2
sbit P0_3=P0^3; // Layer 3
sbit P0_4=P0^4; // Layer 4
sbit P0_5=P0^5; // Layer 5
sbit P0_6=P0^6; // Layer 6
sbit P0_7=P0^7; // Layer 7
sbit P4_6=P4^6; // Layer 8 (Original comment: 推挽 - Push-pull, but used as layer control)
sbit P1_1=P1^1; // Layer 9?
sbit P1_2=P1^2; // Layer 10?
sbit P1_3=P1^3; // Layer 11?
sbit P1_4=P1^4; // Layer 12?
sbit P1_5=P1^5; // Layer 13?
sbit P1_6=P1^6; // Layer 14?
sbit P1_7=P1^7; // Layer 15?

// Column/Cathode drivers (based on sl, dh2, dh5, dh6 behavior)
sbit P3_0=P3^0; // Column 0
sbit P3_1=P3^1; // Column 1
sbit P3_2=P3^2; // Original comment: 红外线 - Infrared (likely not an LED column)
sbit P3_3=P3^3; // Column 2
sbit P3_4=P3^4; // Column 3
sbit P3_5=P3^5; // Column 4
sbit P3_6=P3^6; // Column 5
sbit P3_7=P3^7; // Column 6
sbit P2_0=P2^0; // Column 7
sbit P2_1=P2^1; // Column 8
sbit P2_2=P2^2; // Column 9
sbit P2_3=P2^3; // Column 10
sbit P2_4=P2^4; // Column 11
sbit P2_5=P2^5; // Column 12
sbit P2_6=P2^6; // Column 13
sbit P2_7=P2^7; // Column 14
sbit P4_4=P4^4; // Column 15 (Original comment: I/O)

// Simple busy-wait delay function (original code's implementation)
void delay_1ms(uint q)
{
	uint i,j;
	for(i=0; i<q; i++)
		for(j=0; j<110; j++); // Loop count might need adjustment for different crystal speeds
}

// Function to turn all LEDs ON
void all_leds_on()
{
    // Set all Layer/Anode drivers high
    P0 = 0xFF;      // P0.0 to P0.7
    P1 = 0xFF;      // P1.0 to P1.7 (includes P1.1 to P1.7 used as layers)
    P4_6 = 1;       // P4.6

    // Set all Column/Cathode drivers low (assuming common cathode)
    P2 = 0x00;      // P2.0 to P2.7
    P3 = 0x00;      // P3.0 to P3.7 (includes P3.2 which might be IR, but setting low is safe)
    P4_4 = 0;       // P4.4
}

// Function to turn all LEDs OFF
void all_leds_off()
{
    // Set all Layer/Anode drivers low
    P0 = 0x00;      // P0.0 to P0.7
    P1 = 0x00;      // P1.0 to P1.7
    P4_6 = 0;       // P4.6

    // Set all Column/Cathode drivers high (assuming common cathode)
    P2 = 0xFF;      // P2.0 to P2.7
    P3 = 0xFF;      // P3.0 to P3.7
    P4_4 = 1;       // P4.4
}


void main()
{
    // Initialize Port 4 configuration
    // P4SW = 0Xff; // Keep original setup
    P4M0 = 0xef; // Set P4.0-P4.3, P4.5, P4.7 to push-pull (bit 4 P4_4, bit 6 P4_6 are 0 in M0)
    P4M1 = 0x00; // Set P4.0-P4.3, P4.5, P4.7 to push-pull (bit 4 P4_4, bit 6 P4_6 are 0 in M1). Combined with M0=0, M1=0 this is push-pull. Bit 4 and 6 were kept as input/high-impedance by original 0xef/0x00? No, 0xef is 11101111. M0 & M1 bits for P4.4 and P4.6 are 0, so they are configured as input/high-impedance *initially*. We need them as output. Let's set all used pins to push-pull.
    // P4M0 should be 1 for bits 4 and 6 (output push-pull)
    // P4M1 should be 0 for bits 4 and 6 (output push-pull)
    // Other bits on P4 might be used. Original P4SW=0xff redirects P4.0-P4.3 to peripherals.
    // Let's configure only P4.4 and P4.6 as push-pull output, leave others as original.
    P4M0 |= (1 << 4) | (1 << 6); // Set M0 bits for P4.4 and P4.6
    P4M1 &= ~((1 << 4) | (1 << 6)); // Clear M1 bits for P4.4 and P4.6


    // Initialize Port 1 configuration
    P1M0 = 0xff; // Set all P1 pins to push-pull output
    P1M1 = 0x00;
    // P1ASF = 1; // Disable Analog input on P1 since ADC is removed
    // AUXR1 = BIN(100); // Disable ADC clock control

    // Initialize Port 0 configuration
    P0M0 = 0xff; // Set all P0 pins to push-pull output
    P0M1 = 0x00;

    // Initialize Port 2 configuration
    P2M0 = 0xff; // Set all P2 pins to push-pull output
    P2M1 = 0x00;

    // Initialize Port 3 configuration
    P3M0 = 0xff; // Set all P3 pins to push-pull output
    P3M1 = 0x00;

    // Disable all interrupts as they are not used in this simple flash mode
    EA = 0; // Disable global interrupts


    // Main loop for the flashing animation
	while(1)
	{
        // Turn all LEDs ON
        all_leds_on();
        // Delay for a duration (e.g., 500ms)
        delay_1ms(500);

        // Turn all LEDs OFF
        all_leds_off();
        // Delay for a duration (e.g., 500ms)
        delay_1ms(500);
	}
}

// Removed InitTimer0 interrupt handler
// void InitTimer0() interrupt 1
// {
//    ... (timer logic)
// }                   #include <stc12c5a60s2.h>

#define uchar unsigned char
#define uint  unsigned int

// 延时函数（大约 1 ms @11.0592MHz）
void delay_ms(uint ms)
{
    uint i, j;
    while (ms--)
        for (i = 0; i < 110; i++)
            for (j = 0; j < 1; j++);
}

// 7 层灯选通信号（低电平选中该层）
sbit L1 = P3^0;
sbit L2 = P3^1;
sbit L3 = P3^3;
sbit L4 = P3^4;
sbit L5 = P3^5;
sbit L6 = P3^6;
sbit L7 = P3^7;
sbit L8 = P2^0;   // 如果只有 7 层，可忽略此行

// 推挽/锁存控制（如果原来用 P4.6 做列锁存，则保留；否则可直接不用）
sbit COL_LATCH = P4^6;

// 主函数
void main(void)
{
    uchar i, pattern;

    // 1）端口模式全部推挽输出
    P0M0 = 0xFF; P0M1 = 0x00;   // P0 作为列输出
    P1M0 = 0xFF; P1M1 = 0x00;   // 如果 P1 有扩展列，可一并设
    P2M0 = 0x00; P2M1 = 0x00;   // P2.0(L8) 作为层选，剩余不动
    P3M0 = 0x00; P3M1 = 0x00;   // P3.0~P3.7 全作层选
    P4M0 = 0xFF; P4M1 = 0x00;   // P4.6 作为锁存

    // 初始：关闭所有层
    L1 = L2 = L3 = L4 = L5 = L6 = L7 = 1;
    COL_LATCH = 1;

    while (1)
    {
        // 让所有层都选通（低电平有效）
        L1 = L2 = L3 = L4 = L5 = L6 = L7 = 0;

        // 列数据做 1→2→4→…→128 的流水（共 8 列）
        pattern = 0x01;
        for (i = 0; i < 8; i++)
        {
            P0 = pattern;          // P0 输出当前列
            COL_LATCH = 0;         // 如果需要锁存
            COL_LATCH = 1;

            delay_ms(100);         // 刷新间隔，可调

            pattern <<= 1;         // 下一个列
        }

        // 全灭
        P0 = 0x00;
        delay_ms(200);
    }
}           将这三个代码功能封装成3个模式，然后循环调用