1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
| #include <SPI.h> #define DI 0 // D0, pin 5 Data In #define DO 1 // D1, pin 6 Data Out (this is *not* MOSI) #define USCK 2 // D2, pin 7 Universal Serial Interface clock #define SS 3 // D3, pin 2 Slave Select
/* * 16进制表示法,也可以写成二进制,例如B00111100(0x3c) * 下为0-9的16进制显示 */ byte numbers[11][8]= { {0x3c,0x42,0x42,0x42,0x42,0x42,0x42,0x3c}, {0x08,0x18,0x28,0x08,0x08,0x08,0x08,0x7e}, {0x7e,0x02,0x02,0x7e,0x40,0x40,0x40,0x7e}, {0x7e,0x02,0x02,0x7e,0x02,0x02,0x02,0x7e}, {0x08,0x18,0x28,0x48,0xfe,0x08,0x08,0x1c}, {0x7e,0x40,0x40,0x7e,0x02,0x02,0x02,0x7e}, {0x7e,0x40,0x40,0x7e,0x42,0x42,0x42,0x7e}, {0x7e,0x02,0x04,0x08,0x10,0x10,0x10,0x10}, {0x7e,0x42,0x42,0x7e,0x42,0x42,0x42,0x7e}, {0x7e,0x42,0x42,0x7e,0x02,0x02,0x02,0x7e}, {0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff}, };
/** * Matrix寄存器地址常量 */ const byte NOOP=0x0; //不运行 const byte DECODEMODE=0x9; //译码模式 const byte INTENSITY=0xA; //显示强度 const byte SCANLIMIT=0xB; //扫描限制 const byte SHUTDOWN=0xC; //停机 const byte DISPLAYTEST=0xF; //显示器检测
void max7219(byte reg,byte data){ digitalWrite(SS,LOW); SPI.transfer(reg); SPI.transfer(data); digitalWrite(SS,HIGH); delay(200); }
void setup(){ pinMode(SS,OUTPUT); digitalWrite(SS,HIGH);
SPI.begin(); initMax(); }
void initMax(){ ////清屏代码,暂不使用 /*for(byte i=0;i<sizeof(numbers[0]);i++){ max7219(i+1,0); }*/
max7219(SCANLIMIT,7); max7219(DECODEMODE,0); max7219(INTENSITY,1); max7219(DISPLAYTEST,0); max7219(SHUTDOWN,1); }
byte i,j; void loop(){ for(i=0;i<11;i++){ for(j=0;j<8;j++) max7219(j+1,numbers[i][j]);
delay(1200); } }
|