#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);
    }
}    


#include <stc12c5a60s2.h>
#include <intrins.h>

#define uchar unsigned char
#define uint  unsigned int

// ============ 端口定义 ============
sbit P4_6 = P4^6;
sbit P4_4 = P4^4;

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 COL_LATCH = P4^6;

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

// ============ 模式1：基础LED效果（主模式）============
void mode1_init()
{
    P1M0 = 0xff; 
    P1M1 = 0x00;
    P1 = 0x00;
}

void mode1_full_flash(uchar times, uint speed)
{
    uchar i;
    for(i = 0; i < times; i++)
    {
        P1 = 0x7f;    
        delay_ms(speed);
        P1 = 0x00;    
        delay_ms(speed);
    }
}

void mode1_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 mode1_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 mode1_cross_flash(uchar times, uint speed)
{
    uchar i;
    for(i = 0; i < times; i++)
    {
        P1 = 0x55;    
        delay_ms(speed);
        P1 = 0xAA;    
        delay_ms(speed);
    }
}

void run_main_mode()
{
    mode1_init();
    
    mode1_full_flash(3, 600);       // 全闪3次
    mode1_sequential_shift(2, 200); // 流水两次
    mode1_breathing_effect(1, 8);   // 呼吸一次
    mode1_cross_flash(4, 250);      // 交叉闪烁4次
}

// ============ 模式2：LED立方体效果 ============
void mode2_init()
{
    P0M0 = 0xff; P0M1 = 0x00;
    P1M0 = 0xff; P1M1 = 0x00;
    P2M0 = 0xff; P2M1 = 0x00;
    P3M0 = 0xff; P3M1 = 0x00;
    P4M0 |= (1 << 4) | (1 << 6);
    P4M1 &= ~((1 << 4) | (1 << 6));
}

void mode2_all_leds_on()
{
    P0 = 0xFF;
    P1 = 0xFF;
    P4_6 = 1;
    P2 = 0x00;
    P3 = 0x00;
    P4_4 = 0;
}

void mode2_all_leds_off()
{
    P0 = 0x00;
    P1 = 0x00;
    P4_6 = 0;
    P2 = 0xFF;
    P3 = 0xFF;
    P4_4 = 1;
}

void run_cube_mode()
{
    uchar i;
    mode2_init();
    
    for(i = 0; i < 3; i++)
    {
        mode2_all_leds_on();
        delay_ms(600);
        mode2_all_leds_off();
        delay_ms(600);
    }
}

// ============ 模式3：7层LED流水灯 ============
void mode3_init()
{
    P0M0 = 0xFF; P0M1 = 0x00;   
    P2M0 = 0x00; P2M1 = 0x00;   
    P3M0 = 0x00; P3M1 = 0x00;   
    P4M0 = 0xFF; P4M1 = 0x00;   
    L1 = L2 = L3 = L4 = L5 = L6 = L7 = 1;
    COL_LATCH = 1;
}

void run_layer_mode()
{
    uchar i, pattern;
    mode3_init();
    
    for(i = 0; i < 3; i++)
    {
        L1 = L2 = L3 = L4 = L5 = L6 = L7 = 0;
        
        pattern = 0x01;
        for(i = 0; i < 8; i++)
        {
            P0 = pattern;
            COL_LATCH = 0;
            COL_LATCH = 1;
            delay_ms(150);
            pattern <<= 1;
        }
        
        P0 = 0x00;
        delay_ms(600);
    }
}

// ============ 主程序 ============
void main(void)
{
    while(1)
    {
        // 主模式（最漂亮的花样）
        run_main_mode();
        delay_ms(6000);  // 暂停3秒
        
        // 过渡1：立方体闪烁
        run_cube_mode();
        delay_ms(6000);  // 暂停3秒
        
        // 过渡2：7层流水灯
        run_layer_mode();

    }
}    
将两个代码循环封装成函数套进while函数中
并且定义一个函数用来控制两个函数循环执行时间

要求第一个代码执行周期长一点