單片機控制點陣LED一秒刷新顯示I心形U
下面是程序源碼:
/******點陣LED刷新顯示I LVOE U**************/
/**
*功能:點陣LED一秒刷新顯示I 心形 U(I LOVE YOU)
*作者:徐冉
*日期:2013-06-14-22:44
*備注:程序已調試成功
**/
/*************AT89C52-RC MCU*******************/
/**************51hei開發(fā)板********************/
#include
typedef unsigned int uint;
typedef unsigned char uchar;
sbit din = P2^0;
sbit cs = P2^1;
sbit clk = P2^2;
uchar code table[5][8] = {
{0x00,0x3C,0x18,0x18,0x18,0x18,0x3C,0x00}, //I
{0x00,0x66,0xFF,0xFF,0xFF,0x7E,0x3C,0x18}, //心
{0x00,0x66,0x66,0x66,0x66,0x7E,0x3C,0x00}, //U
};
uchar i, j;
uint counter;
//寫字節(jié)
void write_byte(uchar date)
{
uchar i;
cs = 0;
for(i = 0; i < 8; i++)
{
clk = 0;
din = date & 0x80;
date <<= 1;
clk = 1;
}
}
//寫MAX7219
void write_max7219(uchar add, uchar date)
{
cs = 0;
write_byte(add);
write_byte(date);
cs = 1;
}
//MAX7219初始化
void max7219_init()
{
write_max7219(0x0c, 0x01);//正常模式
write_max7219(0x09, 0x00);//譯碼模式
write_max7219(0x0a, 0x08);//亮度控制
write_max7219(0x0b, 0x07);//掃描模式
write_max7219(0x0f, 0x00);//顯示檢測
}
//定時器T0初始化
void timer0_init()
{
TMOD = 0x01;
TH0 = 0x94; //定時30ms
TL0 = 0x00;
TR0 = 1;
EA = 1;
ET0 = 1;
}
//主程序
void main(void)
{
max7219_init();
timer0_init();
while(1)
{
if(i >= 8)
{
i = 0;
}
if(counter == 40)
{
counter = 0;
j++;
if(j >= 3)
{
j = 0;
}
}
write_max7219(i+1, table[j][i]);
}
}
//中斷服務程序
void timer0_int() interrupt 1
{
TH0 = 0x94;
TL0 = 0x00;
i++;
counter++;
}
評論