|
單片機(jī)C語(yǔ)言實(shí)例-131-雙色點(diǎn)陣交替圖形顯示
內(nèi)容:使用hc595驅(qū)動(dòng)雙色點(diǎn)陣
------------------------------------------------*/
#include<reg52.h> //包含頭文件,一般情況不需要改動(dòng),頭文件包含特殊功能寄存器的定義
#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[]={//沙漏流動(dòng)圖形表格
// 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延時(shí)函數(shù),含有輸入?yún)?shù) unsigned char t,無返回值
unsigned char 是定義無符號(hào)字符變量,其值的范圍是
0~255 這里使用晶振12M,精確延時(shí)請(qǐng)使用匯編,大致延時(shí)
長(zhǎng)度如下 T=tx2+5 uS
------------------------------------------------*/
void DelayUs2x(unsigned char t)
{
while(--t);
}
/*------------------------------------------------
mS延時(shí)函數(shù),含有輸入?yún)?shù) unsigned char t,無返回值
unsigned char 是定義無符號(hào)字符變量,其值的范圍是
0~255 這里使用晶振12M,精確延時(shí)請(qǐng)使用匯編
------------------------------------------------*/
void DelayMs(unsigned char t)
{
while(t--)
{
//大致延時(shí)1mS
DelayUs2x(245);
DelayUs2x(245);
}
}
/*------------------------------------------------
發(fā)送字節(jié)程序
------------------------------------------------*/
void SendByte(unsigned char dat)
{
unsigned char i;
for(i=0;i<8;i++)
{
SRCLK=0;
SER=dat&0x80;
dat<<=1;
SRCLK=1;
}
}
/*------------------------------------------------
發(fā)送雙字節(jié)程序
595級(jí)聯(lián),n個(gè)595,就需要發(fā)送n字節(jié)后鎖存
------------------------------------------------*/
void Send2Byte(unsigned char dat1,unsigned char dat2)
{
SendByte(dat1);
SendByte(dat2);
}
/*------------------------------------------------
595鎖存程序
595級(jí)聯(lián)發(fā)送數(shù)據(jù)后,鎖存有效
------------------------------------------------*/
void Out595(void)
{
LATCH=0;
_nop_();
LATCH=1;
}
/*------------------------------------------------
發(fā)送位碼字節(jié)程序
使用另外一片單獨(dú)595
------------------------------------------------*/
void SendSeg(unsigned char dat)
{
unsigned char i;
for(i=0;i<8;i++) //發(fā)送字節(jié)
{
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++)//這個(gè)主要用于延時(shí)顯示動(dòng)畫效果
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++)//這個(gè)主要用于延時(shí)顯示動(dòng)畫效果
for(i=0;i<8;i++) //8列顯示
{
SendSeg(segout);
Send2Byte(0xff,~tab[i+j*8]);
Out595();
DelayMs(1);
Send2Byte(0xff,0xff); //防止重影
Out595();
}
}
}
更多詳情參考附件文檔
+08:00C130聯(lián)盟網(wǎng)5714.png (21.65 KB, 下載次數(shù): 8)
下載附件
保存到相冊(cè)
2022-1-13 10:07 上傳
游客,如果您要查看本帖隱藏內(nèi)容請(qǐng) 回復(fù)
|
|