/**
*功能:点阵LED流动显示5,4,3,2,1后,会一直显示闪动的矩形
*作者:徐冉
*日期:2013-06-10
*备注:已经通过调试
**/
/*****************AT89C52-RC MCU****************/
/***********************51hei开发板**************/
#include
typedef unsigned int uint;//数据类型声明优于#define
typedef unsigned char uchar;
sbit max7219_din = P2^0;//MAX7219串行数据输入端
sbit max7219_cs = P2^1; //MAX7219片选端
sbit max7219_clk = P2^2;//MAX7219时钟端
//显示取模编码表
uchar code dis[11][8] = {
{0x3C,0x20,0x20,0x3C,0x4,0x4,0x3C,0x0},//5
{0x8,0x18,0x28,0x48,0xFE,0x8,0x8,0x8},//4
{0x3E,0x2,0x2,0x3E,0x2,0x2,0x3E,0x0},//3
{0x7E,0x2,0x2,0x7E,0x40,0x40,0x40,0x7E},//2
{0x10,0x18,0x14,0x10,0x10,0x10,0x10,0x10},//1
{0xff,0x81,0x81,0x81,0x81,0x81,0x81,0xff},
{0,0x7e,0x42,0x42,0x42,0x42,0x7e,0},
{0,0,0x3c,0x24,0x24,0x3c,0,0},
{0,0,0,0x18,0x18,0,0,0,},
{0,0,0,0,0,0,0,0}
};
//毫秒延时程序
void delay(uint xms)
{
uint x, y;
for(x = 0; x < xms; x++)
for(y = 0; y < 110; y++);
}
//写MAX7219字节数据
void write_byte_max7219(uchar dat)
{
uchar i;
max7219_cs = 0;//选通max7219
for(i = 0; i < 8; i++)
{
max7219_clk = 0;
max7219_din = dat & 0x80;
dat <<= 1;
max7219_clk = 1;
}
}
//写数据到MAX7219
void write_max7219(uchar address, uchar dat)
{
max7219_cs = 0;
write_byte_max7219(address);
write_byte_max7219(dat);
max7219_cs = 1;
}
//MAX7219初始化
void max7219_init()
{
//译码模式
write_max7219(0x09, 0x00);
//亮度模式
write_max7219(0x0a, 0x06);//亮度为13/32
//设置掉电模式
write_max7219(0x0c, 0x01);
//扫描模式0~7
write_max7219(0x0b, 0x07);
//显示检测
write_max7219(0x0f, 0x00);
}
//主程序
void main(void)
{
uchar i = 0, j = 0;
delay(50);
max7219_init();
while(1)
{
//流动显示数字
for(i = 0; i < 5; i++)
{
for(j = 0; j < 8; j++)
{ //此处的数据必须从0x01开始写,否则写不成功;
write_max7219(j+1, dis[j]);
}
delay(500);
}
//矩形闪动
while(1)
{
for(i = 5; i < 10; i++)
{
for(j = 0; j < 8; j++)
{ //此处的数据必须从0x01开始写,否则写不成功;
write_max7219(j+1, dis[j]);
}
delay(100);
}
}
}
}
上一篇:一种用AT89C2051制作的新颖汽车尾灯指示器
下一篇:玩转LED点阵之二:16X64单红点阵左移
推荐阅读最新更新时间:2024-03-16 14:23