1. 程式人生 > >基於51微控制器的8x8 LED點陣

基於51微控制器的8x8 LED點陣

最近學了LED點陣,並用了三種方式顯示字母組,分別為按鍵控制顯示,自動變換顯示,和滾動顯示

第一種

 #include"reg51.h"
#include<intrins.h>
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,0x
18,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<intrins.h>
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<intrins.h>
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; 







 }



   }
}