您现在的位置:首页 > >

基于51单片机的8x8 LED点阵

发布时间:

最*学了LED点阵,并用了三种方式显示字母组,分别为按键控制显示,自动变换显示,和滚动显示


第一种


#include"reg51.h"
#include
typedef unsigned char u8;
typedef unsigned int u16;

sbit RCLK=P3^5;
sbit SRCLK=P3^6;
sbit SER=P3^4;

#define GPIP_KEY P1
#define GPIO_DIG P0

u8 keyvalue;




u8 code duan[16][8]={{0x00,0x42,0x7e,0x42,0x42,0x3c,0x00,0x00},//对字母编码
{0x00,0x11,0x12,0x14,0xF8,0x14,0x12,0x11},
{0x00,0x00,0x60,0x10,0x0E,0x10,0x60,0x00},
{0x18,0x24,0x22,0x11,0x22,0x24,0x18,0x00},
{0x00,0x42,0x7E,0x42,0x02,0x06,0x00,0x00},
{0x00,0x06,0x61,0x91,0x89,0x86,0x60,0x00},
{0x81,0xFF,0x81,0x60,0x18,0x86,0xFF,0x81}};

u8 code ledwei[]={0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe};//扫描数组


void delay(u16 i)
{
while(i--);
}

void keydown() //按键函数
{
u8 a;
GPIP_KEY=0x0f;
if(GPIP_KEY!=0x0f)
{
delay(1000);
if(GPIP_KEY!=0x0f)
{
GPIP_KEY=0x0f;
switch(GPIP_KEY)
{
case(0x07): keyvalue=0; break;
case(0x0b): keyvalue=1; break;
case(0x0d): keyvalue=2; break;
case(0x0e): keyvalue=3; break;
}
GPIP_KEY=0xf0;
switch(GPIP_KEY)
{
case(0x70): keyvalue=keyvalue; break;
case(0xb0): keyvalue=keyvalue+4; break;
case(0xd0): keyvalue=keyvalue+8; break;
case(0xe0): keyvalue=keyvalue+12; break;
}
while((a<50)&&(GPIP_KEY!=0xf0))
{
delay(1000);
a++;
}
}

}
}




void hc595sendbyte(u8 dat) //hc595模块定义
{
u8 a;
SRCLK=0;
RCLK=0;
for(a=0;a<8;a++)
{
SER=dat>>7;
dat<<=1;
SRCLK=1;
_nop_();
_nop_();
SRCLK=0;
}
RCLK=1;
_nop_();
_nop_();
SRCLK=0;
}




void main()
{
u8 i;
while(1)
{
keydown();

for(i=0;i<8;i++)
{
P0=ledwei[i];
hc595sendbyte(duan[keyvalue][i]);
delay(100);
hc595sendbyte(0x00); //LED矩阵消影


}




}
}

第二种:


#include"reg51.h"
#include
typedef unsigned char u8;
typedef unsigned int u16;


sbit RCLK=P3^5;
sbit SRCLK=P3^6;
sbit SER=P3^4;


#define GPIP_KEY P1
#define GPIO_DIG P0


u8 keyvalue;








u8 code duan[7][8]={{0x00,0x42,0x7e,0x42,0x42,0x3c,0x00,0x00},//字母段设置
{0x00,0x11,0x12,0x14,0xF8,0x14,0x12,0x11},
{0x00,0x00,0x60,0x10,0x0E,0x10,0x60,0x00},
{0x18,0x24,0x22,0x11,0x22,0x24,0x18,0x00},
{0x00,0x42,0x7E,0x42,0x02,0x06,0x00,0x00},
{0x00,0x06,0x61,0x91,0x89,0x86,0x60,0x00},
{0x81,0xFF,0x81,0x60,0x18,0x86,0xFF,0x81}};


u8 code ledwei[]={0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe};//扫描段




void delay(u16 i)
{
while(i--);
}









void hc595sendbyte(u8 dat) //hc595模块设置
{
u8 a;
SRCLK=0;
RCLK=0;
for(a=0;a<8;a++)
{
SER=dat>>7;
dat<<=1;
SRCLK=1;
_nop_();
_nop_();
SRCLK=0;
}
RCLK=1;
_nop_();
_nop_();
SRCLK=0;
}








void main()
{

while(1)
{
u8 i,j,t=100;


for(i=0;i<7;i++)
{

while(t--)
{
for(j=0;j<8;j++)
{
P0=ledwei[j];
hc595sendbyte(duan[i][j]);
delay(100);
hc595sendbyte(0x00);//LED矩阵消影
}

}
t=100;










}
hc595sendbyte(0x00);


}

}

第三种


#include"reg51.h"
#include
typedef unsigned char u8;
typedef unsigned int u16;

sbit RCLK=P3^5;
sbit SRCLK=P3^6;
sbit SER=P3^4;

#define GPIP_KEY P1
#define GPIO_DIG P0

u8 keyvalue;




u8 code duan[]={0x00,0x00,0x00,0x00,0x42,0x7E,0x42,0x00, //I
0x41,0x7F,0x41,0x01,0x01,0x03,0x00, //L
0x3C,0x42,0x42,0x42,0x3C,0x00, //O
0x70,0x0C,0x02,0x0C,0x70,0x00, //V
0x7F,0x49,0x49,0x49,0x49,0x00, //E
0x60,0x10,0x0E,0x10,0x60,0x00, //Y
0x3C,0x42,0x42,0x42,0x3C,0x00, //O
0x40,0x7C,0x42,0x01,0x01,0x42,0x7C,0x40 //U
};

u8 code ledwei[]={0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe};//扫描编码段


void delay(u16 i)
{
while(i--);
}





void hc595sendbyte(u8 dat)//hc595模块初始化
{
u8 a;
SRCLK=0;
RCLK=0;
for(a=0;a<8;a++)
{
SER=dat>>7;
dat<<=1;
SRCLK=1;
_nop_();
_nop_();
SRCLK=0;
}
RCLK=1;
_nop_();
_nop_();
SRCLK=0;
}




void main()
{

while(1)
{
u8 i,j,t=30,q=0;

for(i=0;i<46;i++)
{

while(t--)
{
for(j=i;j<8+i;j++)
{
P0=ledwei[q++];
hc595sendbyte(duan[j]);
delay(100);
hc595sendbyte(0x00); //LED矩阵消影
}
q=0;
}
t=30;







}



}
}


热文推荐
猜你喜欢
友情链接: 团党工作范文 工作范文 表格模版 社科文档网 营销文档资料 工程文档大全