有没有大神能给解释一下void main 里边的意思和具体的实现流程
#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:点阵右沿对应的val数组元素编号. 因为要无缝滚屏, 用右沿做求余不会产生跳变
// lpos: 点阵左沿对应的val数组元素编号, 每次根据点阵右沿和点阵宽度计算得到
uint pos = 0, lpos = 0;
// cpos: 点阵左沿对应的文字编号
// bpos: 在这个文字中, 当前移动到第几个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; // 第几个字
bpos = lpos % 8; // 字的第几个bit
for (i = 0; i < 8; i++) // 对每一行, 对val的每个元素赋值
{
// 从第cpos个字的bpos位开始, 填满每个点阵对应的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);
}
}
|