标题: AT89C2051单片机和74HC138控制64位LED灯 程序电路图 [打印本页]

作者: 马上学习    时间: 2024-11-5 10:34
标题: AT89C2051单片机和74HC138控制64位LED灯 程序电路图
#include <reg2051.h>

void delay(unsigned int cnt)
{
  unsigned  int i;
        while(cnt--)
        {
                for(i = 0;i < 227;i++);
        }
}


void main()
{
                P1=0x01; //001
                P3=0x00;        
                delay(50);
                P3=0x01;
                delay(50);
                P3=0x02;        
                delay(50);
                P3=0x03;
                delay(50);
                P3=0x04;        
                delay(50);
                P3=0x05;
                delay(50);
                P3=0x06;        
                delay(50);
                P3=0x07;
                delay(50);

                P1=0x02; //10
                P3=0x00;        
                delay(50);
                P3=0x01;
                delay(50);
                P3=0x02;        
                delay(50);
                P3=0x03;
                delay(50);
                P3=0x04;        
                delay(50);
                P3=0x05;
                delay(50);
                P3=0x06;        
                delay(50);
                P3=0x07;
                delay(50);
               
                P1=0x04; //100
                P3=0x00;        
                delay(50);
                P3=0x01;
                delay(50);
                P3=0x02;        
                delay(50);
                P3=0x03;
                delay(50);
                P3=0x04;        
                delay(50);
                P3=0x05;
                delay(50);
                P3=0x06;        
                delay(50);
                P3=0x07;
                delay(50);

                P1=0x08; //1000
                P3=0x00;        
                delay(50);
                P3=0x01;
                delay(50);
                P3=0x02;        
                delay(50);
                P3=0x03;
                delay(50);
                P3=0x04;        
                delay(50);
                P3=0x05;
                delay(50);
                P3=0x06;        
                delay(50);
                P3=0x07;
                delay(50);
               
                P1=0x10; //1 0000
                P3=0x00;        
                delay(50);
                P3=0x01;
                delay(50);
                P3=0x02;        
                delay(50);
                P3=0x03;
                delay(50);
                P3=0x04;        
                delay(50);
                P3=0x05;
                delay(50);
                P3=0x06;        
                delay(50);
                P3=0x07;
                delay(50);

                P1=0x020; //10 0000
                P3=0x00;        
                delay(50);
                P3=0x01;
                delay(50);
                P3=0x02;        
                delay(50);
                P3=0x03;
                delay(50);
                P3=0x04;        
                delay(50);
                P3=0x05;
                delay(50);
                P3=0x06;        
                delay(50);
                P3=0x07;
                delay(50);
               
                P1=0x040; //100 0000
                P3=0x00;        
                delay(50);
                P3=0x01;
                delay(50);
                P3=0x02;        
                delay(50);
                P3=0x03;
                delay(50);
                P3=0x04;        
                delay(50);
                P3=0x05;
                delay(50);
                P3=0x06;        
                delay(50);
                P3=0x07;
                delay(50);
               
                P1=0x080; //1000 0000
                P3=0x00;        
                delay(50);
                P3=0x01;
                delay(50);
                P3=0x02;        
                delay(50);
                P3=0x03;
                delay(50);
                P3=0x04;        
                delay(50);
                P3=0x05;
                delay(50);
                P3=0x06;        
                delay(50);
                P3=0x07;
                delay(50);
}

AT89C2051和74HC138控制64位LED灯.png (76.38 KB, 下载次数: 0)

AT89C2051和74HC138控制64位LED灯.png

作者: rsx9583    时间: 2024-11-18 10:17
看到楼主的代码,让我陷入深深的回忆当中——当年写汇编代码应该就是这样的风格,你是不是还没扭转过来
作者: songxia8013    时间: 2024-12-27 07:28
这程序写的太长了,可以优化下。搞个数组,然后for循环调用,很短的程序应该就能搞定。
作者: 马上学习    时间: 2025-3-5 14:27
谢谢songxia8013提醒,现在修改如下:

#include <reg2051.h>

const unsigned char P1_arry[8]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
const unsigned char P3_arry[8]={0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07};

void delay(unsigned int cnt)
{
  unsigned  int i;
        while(cnt--)
        {
                for(i = 0;i < 227;i++);
        }
}


void main()
{
         
         unsigned char i, j;

        while(1)
        {
                for(i=0;i<8;i++)
                {
                        P1 = P1_arry[ i];
                        for(j=0;j<8;j++)
                        {
                                P3 = P3_arry[j];
                                delay(200);
                        }                       
                }
        }       
}




欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1