Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 #include "mbed.h" 00002 00003 // PC_3: DIN, PB_10: CLK, D2: LOAD/CS 00004 SPI max72_spi(PC_3, NC, PB_10); 00005 DigitalOut load(D2); 00006 00007 int maxInUse = 1; //change this variable to set how many MAX7219's you'll use 00008 00009 // define max7219 registers 00010 #define max7219_reg_noop 0x00 00011 #define max7219_reg_digit0 0x01 00012 #define max7219_reg_digit1 0x02 00013 #define max7219_reg_digit2 0x03 00014 #define max7219_reg_digit3 0x04 00015 #define max7219_reg_digit4 0x05 00016 #define max7219_reg_digit5 0x06 00017 #define max7219_reg_digit6 0x07 00018 #define max7219_reg_digit7 0x08 00019 #define max7219_reg_decodeMode 0x09 00020 #define max7219_reg_intensity 0x0a 00021 #define max7219_reg_scanLimit 0x0b 00022 #define max7219_reg_shutdown 0x0c 00023 #define max7219_reg_displayTest 0x0f 00024 00025 #define LOW 0 00026 #define HIGH 1 00027 #define MHZ 1000000 00028 00029 void maxSingle( int reg, int col) { 00030 //maxSingle is the "easy" function to use for a 00031 //single max7219 00032 load = LOW; // begin 00033 max72_spi.write(reg); // specify register 00034 max72_spi.write(col); // put data 00035 load = HIGH; // make sure data is loaded (on rising edge of LOAD/CS) 00036 } 00037 00038 void maxAll (int reg, int col) { // initialize all MAX7219's in the system 00039 load = LOW; // begin 00040 for ( int c=1; c<= maxInUse; c++) { 00041 max72_spi.write(reg); // specify register 00042 max72_spi.write(col); // put data 00043 } 00044 load = HIGH; 00045 } 00046 00047 void maxOne(int maxNr, int reg, int col) { 00048 //maxOne is for adressing different MAX7219's, 00049 //while having a couple of them cascaded 00050 int c = 0; 00051 load = LOW; 00052 00053 for ( c = maxInUse; c > maxNr; c--) { 00054 max72_spi.write(0); // no-op 00055 max72_spi.write(0); // no-op 00056 } 00057 00058 max72_spi.write(reg); // specify register 00059 max72_spi.write(col); // put data 00060 00061 for ( c=maxNr-1; c >= 1; c--) { 00062 max72_spi.write(0); // no-op 00063 max72_spi.write(0); // no-op 00064 } 00065 load = HIGH; 00066 } 00067 00068 00069 void setup () { 00070 // initiation of the max 7219 00071 // SPI setup: 8 bits, mode 0 00072 max72_spi.format(8, 0); 00073 00074 // going by the datasheet, min clk is 100ns so theoretically 10MHz should work... 00075 // max72_spi.frequency(10*MHZ); 00076 00077 maxAll(max7219_reg_scanLimit, 0x07); 00078 maxAll(max7219_reg_decodeMode, 0x00); // using an led matrix (not digits) 00079 maxAll(max7219_reg_shutdown, 0x01); // not in shutdown mode 00080 maxAll(max7219_reg_displayTest, 0x00); // no display test 00081 for (int e=1; e<=8; e++) { // empty registers, turn all LEDs off 00082 maxAll(e,0); 00083 } 00084 maxAll(max7219_reg_intensity, 0x0f & 0x0f); // the first 0x0f is the value you can set 00085 // range: 0x00 to 0x0f 00086 } 00087 00088 00089 /* maxSingle(1,1); // - - - - - - - + 00090 maxSingle(2,2); // - - - - - - + - 00091 maxSingle(3,4); // - - - - - + - - 00092 maxSingle(4,8); // - - - - + - - - 00093 maxSingle(5,16); // - - - + - - - - 00094 maxSingle(6,32); // - - + - - - - - 00095 maxSingle(7,64); // - + - - - - - - 00096 maxSingle(8,128); // + - - - - - - - 00097 wait_ms(2000); 00098 */ 00099 int main() { 00100 setup (); 00101 while(1){ 00102 for(int i=1;i<=8;i++){ 00103 for(int j=1;j<=128;j*=2){ 00104 maxSingle(i,j); 00105 wait(0.1); 00106 } 00107 } 00108 for(int i=8;i>=1;i--){ 00109 for(int j=128;j>=1;j*=0.5){ 00110 maxSingle(i,j); 00111 wait(0.1); 00112 } 00113 } 00114 } 00115 }
Generated on Fri Jul 22 2022 22:40:14 by
1.7.2