这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » 嵌入式开发 » STM32 » ai8051u花样流水灯《梵高的星空》

共1条 1/1 1 跳转至

ai8051u花样流水灯《梵高的星空》

工程师
2025-07-08 18:19:08     打赏

#include "ai8051u.h"    // 调用头文件


#include <stdio.h>

#include <stdlib.h>

#include <time.h>


void Timer0_Init(void) // 1毫秒@24MHz

{

    AUXR |= 0x80; // 定时器时钟1T模式

    TMOD &= 0xF0; // 设置定时器模式(高4位保留)

    TL0 = 0x40; // 设置定时初始值(低字节)

    TH0 = 0xA2; // 设置定时初始值(高字节)

    TF0 = 0; // 清除TF0标志

    TR0 = 1; // 启动定时器0

}


// 简单的延时函数(假设系统时钟为24MHz)

void delay(unsigned int ms)

{

    unsigned int i, j;

    for(i = ms; i > 0; i--)

        for(j = 110; j > 0; j--); // 大约1ms延时

}


//void  delay_ms(u8 ms)

//{

//     u16 i;

//     do{

//          i = MAIN_Fosc / 6000;

//          while(--i);

//     }while(--ms);

//}



// 主函数

int main(void)

{


    // 初始化数组x和y

    int x[] = {0,1,2,3,4,5,6,7};

    int y[] = {0,1};

    int size_x = sizeof(x)/sizeof(x[0]);

    int size_y = sizeof(y)/sizeof(y[0]);



    // 生成所有可能的(x, y)组合

    int total_combinations = size_x * size_y;

    int combinations[total_combinations][2];

    int index = 0;

    for(int i = 0; i < size_x; i++) {

        for(int j = 0; j < size_y; j++) {

            combinations[index][0] = x[i];

            combinations[index][1] = y[j];

            index++;

        }

    }


    // 初始化随机数种子

    srand(time(NULL));

    // 随机选择一个组合

    int random_index = rand() % total_combinations;

    printf("随机选择的组合是: (%d, %d)", combinations[random_index][0], combinations[random_index][1]);


    return 0;

    // 配置P2端口为准双向口

    P2M0 = 0;

    P2M1 = 0;


    // 初始化定时器0

    Timer0_Init();


    while(1)

    {

    x=[0,1,2,3,4,5,6,7]

y=[0,1]

        P2x = 0;   // P27端口输出低电平

P20 = 0;   // P20端口输出低电平

        delay(5000); // 延时100ms

P27 = 1;

        P20 = 1;

 

        P21 = 0;   // P21端口输出低电平

P26 = 0;   // P26端口输出低电平

        delay(5000); // 延时100ms

        P21 = 1;

P26 = 1;


        P22 = 0;   // P22端口输出低电平

        P25 = 0;   // P25端口输出低电平

        delay(5000); // 延时100ms

        P22 = 1;

P25 = 1;


        P23 = 0;   // P23端口输出低电平

P24 = 0;   // P24端口输出低电平

        delay(5000); // 延时100ms

P23 = 1;

P24 = 1;

 

        delay(5000); // 延时100ms

    }


    return 0; // 返回0表示程序正常结束

}



共1条 1/1 1 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册 ]