單片機程序-點陣LED
/**
*功能:點陣LED流動顯示5,4,3,2,1后,會一直顯示閃動的矩形
*作者:徐冉
*日期:2013-06-10
*備注:已經(jīng)通過調(diào)試
**/
/*****************AT89C52-RC MCU****************/
/***********************51hei開發(fā)板**************/
#include
typedef unsigned int uint;//數(shù)據(jù)類型聲明優(yōu)于#define
typedef unsigned char uchar;
sbit max7219_din = P2^0;//MAX7219串行數(shù)據(jù)輸入端
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字節(jié)數(shù)據(jù)
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;
}
}
//寫數(shù)據(jù)到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)
{
//流動顯示數(shù)字
for(i = 0; i < 5; i++)
{
for(j = 0; j < 8; j++)
{ //此處的數(shù)據(jù)必須從0x01開始寫,否則寫不成功;
write_max7219(j+1, dis[j]);
}
delay(500);
}
//矩形閃動
while(1)
{
for(i = 5; i < 10; i++)
{
for(j = 0; j < 8; j++)
{ //此處的數(shù)據(jù)必須從0x01開始寫,否則寫不成功;
write_max7219(j+1, dis[j]);
}
delay(100);
}
}
}
}
評論