使用hc595驱动双色点阵

来源:互联网 发布:网络社区模式案例分析 编辑:程序博客网 时间:2024/04/30 17:04

#include<reg52.h>   
#include <intrins.h> 




unsigned char  segout[8]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80}; //8列
unsigned char code tab[]={
                        0x00,0x38,0x04,0x38,0x04,0x38,0x00,0x00, // w
0x00,0x38,0x04,0x38,0x04,0x38,0x00,0x00, // w
0x00,0x38,0x04,0x38,0x04,0x38,0x00,0x00, // w
0x00,0x06,0x06,0x00,0x00,0x00,0x00,0x00, // .
0x00,0x00,0x0C,0x12,0x12,0x7E,0x00,0x00, // d
0x00,0x00,0x1C,0x22,0x22,0x1C,0x00,0x00, // o
0x00,0x10,0x3E,0x50,0x40,0x00,0x00,0x00, // f
0x00,0x00,0x00,0x7E,0x02,0x04,0x00,0x00, // l
0x00,0x00,0x71,0x09,0x09,0x7E,0x00,0x00, // y
0x00,0x00,0x3C,0x52,0x52,0x32,0x00,0x00, // e
                        0x00,0x06,0x06,0x00,0x00,0x00,0x00,0x00, // .
0x00,0x00,0x1C,0x22,0x22,0x22,0x00,0x00, // c
  0x00,0x00,0x1C,0x22,0x22,0x1C,0x00,0x00, // o
0x00,0x00,0x1E,0x20,0x3E,0x20,0x1E,0x00, // m
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00  // 空格
                        };
/*------------------------------------------------
                硬件端口定义
------------------------------------------------*/
sbit LATCH = P1^0; 
sbit SRCLK= P1^1;
sbit SER  = P1^2;




sbit LATCH_B = P2^2;
sbit SRCLK_B= P2^1;
sbit SER_B= P2^0;




/*------------------------------------------------
 uS延时函数,含有输入参数 unsigned char t,无返回值
 unsigned char 是定义无符号字符变量,其值的范围是
 0~255 这里使用晶振12M,精确延时请使用汇编,大致延时
 长度如下 T=tx2+5 uS 
------------------------------------------------*/
void DelayUs2x(unsigned char t)
{   
 while(--t);
}
/*------------------------------------------------
 mS延时函数,含有输入参数 unsigned char t,无返回值
 unsigned char 是定义无符号字符变量,其值的范围是
 0~255 这里使用晶振12M,精确延时请使用汇编
------------------------------------------------*/
void DelayMs(unsigned char t)
{
     
 while(t--)
 {
     //大致延时1mS
     DelayUs2x(245);
DelayUs2x(245);
 }
}
/*------------------------------------------------
                发送字节程序
  带有方向参数,可以选择从高位节写入或者低位写入
------------------------------------------------*/
void SendByte(unsigned char dat,bit direction)
{    
  unsigned char i,temp; 
   if(direction==0)
     temp=0x80;
   else
     temp=0x01;


   for(i=0;i<8;i++)
        {
         SRCLK=0;
         SER=dat&temp;
if(direction==0)
         dat<<=1;
else
    dat>>=1;
         SRCLK=1;
         }
         
}
/*------------------------------------------------
                发送双字节程序
------------------------------------------------*/
void Send2Byte(unsigned char dat1,unsigned char dat2,bit direction)
{    
   SendByte(dat1,direction);
   SendByte(dat2,direction);      
}
/*------------------------------------------------
                   595锁存程序
------------------------------------------------*/
void Out595(void)
{
        LATCH=1;
        _nop_();
        LATCH=0;
}


/*------------------------------------------------
                发送位码字节程序
               使用另外一片单独595
------------------------------------------------*/
void SendSeg(unsigned char dat)
{    
  unsigned char i; 
        
   for(i=0;i<8;i++)  //发送字节
        {
         SRCLK_B=0;
         SER_B=dat&0x80;
         dat<<=1;
         SRCLK_B=1;
         }
      LATCH_B=1;    //锁存
      _nop_();
      LATCH_B=0;
         
}
/*------------------------------------------------
                   主程序
------------------------------------------------*/
void main()
{
unsigned char i,k,l;
while(1)
{




 for(k=0;k<=112;k++)     //所有的字符个数
         for(l=20;l>0;l--)    //延时长度,改变此值可以改变流动速度
            for(i=0;i<=7;i++) //8列显示
                  {
                     SendSeg(segout[7-i]);
Send2Byte(~(*(tab+i+k)),0xff,1); //写入字节方向改变
Out595();
                     DelayMs(1);
Send2Byte(0xff,0xff,0);//delay(10); //防止重影
Out595();
                  }
}
}
0 0
原创粉丝点击