|
有沒有大神能給解釋一下void main 里邊的意思和具體的實(shí)現(xiàn)流程
#include <reg52.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define num1 5
sbit CLK = P2^0;
sbit CS = P2^1;
sbit DIN = P2^2;
uchar code bytes[] = {
0x08,0x14,0x22,0x3E,0x22,0x22,0x22,0x22,//A
0x3C,0x22,0x22,0x3E,0x22,0x22,0x3C,0x00,//B
0x3C,0x40,0x40,0x40,0x40,0x40,0x3C,0x00,//C
0x3C,0x42,0x42,0x42,0x42,0x42,0x42,0x3C,//0
0x08,0x18,0x28,0x08,0x08,0x08,0x08,0x08,//1
0x7E,0x02,0x02,0x7E,0x40,0x40,0x40,0x7E,//2
0x3E,0x02,0x02,0x3E,0x02,0x02,0x3E,0x00,//3
0x08,0x18,0x28,0x48,0xFE,0x08,0x08,0x08,//4
0x3C,0x20,0x20,0x3C,0x04,0x04,0x3C,0x00,//5
0x3C,0x20,0x20,0x3C,0x24,0x24,0x3C,0x00,//6
0x3E,0x22,0x04,0x08,0x08,0x08,0x08,0x08,//7
0x00,0x3E,0x22,0x22,0x3E,0x22,0x22,0x3E,//8
0x3E,0x22,0x22,0x3E,0x02,0x02,0x02,0x3E,//9
};
uchar val[num1];
uchar character_len = sizeof(bytes) / 8;
void delay(uint x)
{
uint i,j;
for(i = 0; i < x; i++)
for(j = 0;j < 112; j++);
}
void max7219_byte(uchar dat)
{
uchar i;
CS = 0;
for(i = 8; i >= 1; i--)
{
CLK = 0;
DIN = dat & 0x80; // &10000000, 取最高位
dat = dat << 1;
CLK = 1;
}
}
void max7219_shuju1(uchar index, uchar addr, uchar dat)
{
CS = 0;
max7219_byte(addr);
max7219_byte(dat);
while(index--)
{
max7219_byte(0x00);
max7219_byte(0x00);
}
CS = 1;
}
void max7219_shuju2(uchar addr, uchar len, uchar* dat)
{
CS = 0;
while(len--)
{
max7219_byte(addr);
max7219_byte(*dat++);
}
CS = 1;
}
void max7219_init()
{
uchar i;
for (i = 0; i < num1; i++)
{
max7219_shuju1(i,0x0c,0x01);
max7219_shuju1(i,0x09,0x00);
max7219_shuju1(i,0x0a,0x03);
max7219_shuju1(i,0x0b,0x07);
max7219_shuju1(i,0x0f,0x00);
}
}
void main(void)
{
// pos:點(diǎn)陣右沿對(duì)應(yīng)的val數(shù)組元素編號(hào). 因?yàn)橐獰o縫滾屏, 用右沿做求余不會(huì)產(chǎn)生跳變
// lpos: 點(diǎn)陣左沿對(duì)應(yīng)的val數(shù)組元素編號(hào), 每次根據(jù)點(diǎn)陣右沿和點(diǎn)陣寬度計(jì)算得到
uint pos = 0, lpos = 0;
// cpos: 點(diǎn)陣左沿對(duì)應(yīng)的文字編號(hào)
// bpos: 在這個(gè)文字中, 當(dāng)前移動(dòng)到第幾個(gè)bit, 值從0-7
uchar i, j, cpos = 0, bpos = 0, tcpos = 0;
max7219_init();
while(1)
{
lpos = pos + sizeof(bytes) - num1 * 8;
cpos = lpos / 8; // 第幾個(gè)字
bpos = lpos % 8; // 字的第幾個(gè)bit
for (i = 0; i < 8; i++) // 對(duì)每一行, 對(duì)val的每個(gè)元素賦值
{
// 從第cpos個(gè)字的bpos位開始, 填滿每個(gè)點(diǎn)陣對(duì)應(yīng)的byte
for (j = 0; j < num1; j++)
{
// 高位部分
tcpos = (cpos + j) % character_len;
val[j] = bytes[tcpos * 8 + i] << bpos;
// 低位部分
tcpos = (cpos + j + 1) % character_len;
val[j] |= bytes[tcpos * 8 + i] >> (8 - bpos);
}
max7219_shuju2(i+1, num1, val);
}
// 每處理完一屏, pos右移一位
pos = (pos + 1) % sizeof(bytes);
delay(150);
}
}
|
|