单片机C语言实例-131-双色点阵交替图形显示
内容:使用hc595驱动双色点阵
------------------------------------------------*/
#include<reg52.h> //包含头文件,一般情况不需要改动,头文件包含特殊功能寄存器的定义
#include <intrins.h>
//unsigned char segout[8]={0,1,2,3,4,5,6,7}; //8列
unsigned char segout[8]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80}; //8列
unsigned char code tab[]={//沙漏流动图形表格
// 0x81,0xC3,0xA5,0xD9,0xF9,0xE7,0xC3,0x81,
0xFF,0x7E,0x3C,0x18,0x18,0x24,0x42,0xFF,
0xFF,0x76,0x3C,0x18,0x18,0x24,0x4A,0xFF,
0xFF,0x66,0x3C,0x18,0x18,0x24,0x5A,0xFF,
0xFF,0x46,0x3C,0x18,0x18,0x24,0x7A,0xFF,
0xFF,0x42,0x3C,0x18,0x18,0x24,0x7E,0xFF,
0xFF,0x42,0x34,0x18,0x18,0x2C,0x7E,0xFF,
0xFF,0x42,0x24,0x18,0x18,0x3C,0x7E,0xFF,
0x81,0xC3,0xA5,0x9B,0x9F,0xE7,0xC3,0x81
};
/*------------------------------------------------
硬件端口定义
------------------------------------------------*/
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)
{
unsigned char i;
for(i=0;i<8;i++)
{
SRCLK=0;
SER=dat&0x80;
dat<<=1;
SRCLK=1;
}
}
/*------------------------------------------------
发送双字节程序
595级联,n个595,就需要发送n字节后锁存
------------------------------------------------*/
void Send2Byte(unsigned char dat1,unsigned char dat2)
{
SendByte(dat1);
SendByte(dat2);
}
/*------------------------------------------------
595锁存程序
595级联发送数据后,锁存有效
------------------------------------------------*/
void Out595(void)
{
LATCH=0;
_nop_();
LATCH=1;
}
/*------------------------------------------------
发送位码字节程序
使用另外一片单独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=0; //锁存
_nop_();
LATCH_B=1;
}
/*------------------------------------------------
主程序
------------------------------------------------*/
void main()
{
unsigned char i,j,k;
while(1)
{
for(j=0;j<8;j++)
for(k=0;k<40;k++)//这个主要用于延时显示动画效果
for(i=0;i<8;i++) //8列显示
{
SendSeg(segout);
Send2Byte(~tab[i+j*8],0xff);
Out595();
DelayMs(1);
Send2Byte(0xff,0xff); //防止重影
Out595();
}
//另外一种颜色
for(j=0;j<8;j++)
for(k=0;k<40;k++)//这个主要用于延时显示动画效果
for(i=0;i<8;i++) //8列显示
{
SendSeg(segout);
Send2Byte(0xff,~tab[i+j*8]);
Out595();
DelayMs(1);
Send2Byte(0xff,0xff); //防止重影
Out595();
}
}
}
更多详情参考附件文档
|