LED8X8點(diǎn)陣,使用74HC595芯片作為數(shù)據(jù)輸入,以P0口作為陰極循環(huán)掃描每一列。采用STC89C52RC單片機(jī),感興趣的可以復(fù)制來玩。
程序如下:
#include "reg52.h" //此文件中定義了單片機(jī)的一些特殊功能寄存器
#include<intrins.h>
typedef unsigned int u16; //對(duì)數(shù)據(jù)類型進(jìn)行聲明定義
typedef unsigned char u8;
sbit SRCLK=P3^6;//數(shù)據(jù)輸入時(shí)鐘,上升沿時(shí)SER的最高位被移入數(shù)據(jù)寄存器中
sbit RCLK=P3^5;//移位寄存器,上升沿時(shí)數(shù)據(jù)寄存器里的數(shù)據(jù)被移到輸出端
sbit SER=P3^4;//數(shù)據(jù)輸入
u16 timecount;
u8 cnta,cntb;
u8 code ledwei[]={0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe};//(0111 1111,1011 1111,1101 1111.....)
u8 code digittab[88]={ //設(shè)置0-9的顯示數(shù)組,也可以根據(jù)不同的接法寫出不同的結(jié)合,本程序采用共陰掃描
//當(dāng)輸入點(diǎn)為高,則點(diǎn)亮對(duì)應(yīng)的led
0x00,0x00,0x3e,0x41,0x41,0x41,0x3e,0x00, //0
0x00,0x00,0x00,0x00,0x21,0x7f,0x01,0x00, //1
0x00,0x00,0x27,0x45,0x45,0x45,0x39,0x00, //2
0x00,0x00,0x22,0x49,0x49,0x49,0x36,0x00, //3
0x00,0x00,0x0c,0x14,0x24,0x7f,0x04,0x00, //4
0x00,0x00,0x72,0x51,0x51,0x51,0x4e,0x00, //5
0x00,0x00,0x3e,0x49,0x49,0x49,0x26,0x00, //6
0x00,0x00,0x40,0x40,0x40,0x4f,0x70,0x00, //7
0x00,0x00,0x36,0x49,0x49,0x49,0x36,0x00, //8
0x00,0x00,0x32,0x49,0x49,0x49,0x3e,0x00, //9
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
};
void Hc595SendByte(u8 dat)//
{
u8 a;
SRCLK=0;
RCLK=0; //SRCLK,RCLK為0,以便產(chǎn)生上升沿
for(a=0;a<8;a++)
{
SER=dat>>7; //輸入的數(shù)據(jù)高位首先被移入數(shù)據(jù)寄存器中
dat<<=1; //次高位
SRCLK=1; // 數(shù)據(jù)時(shí)鐘產(chǎn)生上升沿,數(shù)據(jù)被移入
_nop_();
_nop_();
SRCLK=0; // 數(shù)據(jù)時(shí)鐘產(chǎn)生下降沿,數(shù)據(jù)寄存器里的數(shù)據(jù)不變,等待下一個(gè)數(shù)據(jù)(次高位)移入
}
RCLK=1; //移位寄存器上升沿,數(shù)據(jù)寄存器里的數(shù)據(jù)被移到輸出端
_nop_();
_nop_();
RCLK=0; // 輸出端的數(shù)據(jù)保持不變
}
void Initi_t0() //T0定時(shí)器的設(shè)置3000us中斷一次
{
TMOD=0x01;
TH0=(65536-3000)/256;
TL0=(65536-3000)%256;
TR0=1;
ET0=1;
EA=1;
}
void main()
{
Initi_t0();
while(1);
}
void t0(void) interrupt 1 using 0
{
TH0=(65536-1000)/256; //重裝,每隔3ms產(chǎn)生一次
TL0=(65536-1000)%256;
P0=0xff; //清除LED數(shù)據(jù),也可以在后面寫成Hc595SendByte(0x00),但顯示數(shù)據(jù)變暗
Hc595SendByte(digittab[cnta+cntb]);
P0=ledwei[cnta];
cnta++;
if(cnta==8) //當(dāng)cnta=8時(shí),8x8點(diǎn)整顯示digittab[]數(shù)組中的前8個(gè),及digittab[0]~digittab[7]
{
cnta=0;
}
timecount++; //為延時(shí),當(dāng)不滿100,LED8X8一直顯示digittab[0]~digittab[7]
if(timecount==100)
{
timecount=0;
cntb++; //初始為0,當(dāng)cntb+1時(shí),Hc595SendByte(digittab[cnta+cntb])就從digittab[1]~digittab[8]移數(shù),
if(cntb==80) //當(dāng)cntb+2時(shí),Hc595SendByte(digittab[cnta+cntb])就從digittab[2]~digittab[9]開始移數(shù).....當(dāng)cntb+3時(shí), //Hc595SendByte(digittab[cnta+cntb])就從digittab[3]~digittab[10]移數(shù)......一直到digittab[80]~digittab[87]
cntb=0; // 每次顯示digittab[]8位,從整體來看從0-9不斷的循環(huán)移動(dòng)
}
}
}
|