![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Demo for Glasgow Students that writes to dot matrix display and sends the display data out of the serial port
main.cpp@0:bc2c0a19b0e5, 2015-02-19 (annotated)
- Committer:
- jont
- Date:
- Thu Feb 19 08:38:28 2015 +0000
- Revision:
- 0:bc2c0a19b0e5
First release
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jont | 0:bc2c0a19b0e5 | 1 | #include "mbed.h" |
jont | 0:bc2c0a19b0e5 | 2 | /* |
jont | 0:bc2c0a19b0e5 | 3 | example of driving maxim chip for Glasgow Uni Projects. |
jont | 0:bc2c0a19b0e5 | 4 | |
jont | 0:bc2c0a19b0e5 | 5 | Dr J.J.Trinder 2013,14,15 |
jont | 0:bc2c0a19b0e5 | 6 | |
jont | 0:bc2c0a19b0e5 | 7 | This one modified to also send display data out of serial port for a remote simulated display |
jont | 0:bc2c0a19b0e5 | 8 | for more info email me |
jont | 0:bc2c0a19b0e5 | 9 | jon.trinder@glasgow.ac.uk |
jont | 0:bc2c0a19b0e5 | 10 | jont@ninelocks.com |
jont | 0:bc2c0a19b0e5 | 11 | |
jont | 0:bc2c0a19b0e5 | 12 | */ |
jont | 0:bc2c0a19b0e5 | 13 | |
jont | 0:bc2c0a19b0e5 | 14 | #define max7219_reg_noop 0x00 |
jont | 0:bc2c0a19b0e5 | 15 | #define max7219_reg_digit0 0x01 |
jont | 0:bc2c0a19b0e5 | 16 | #define max7219_reg_digit1 0x02 |
jont | 0:bc2c0a19b0e5 | 17 | #define max7219_reg_digit2 0x03 |
jont | 0:bc2c0a19b0e5 | 18 | #define max7219_reg_digit3 0x04 |
jont | 0:bc2c0a19b0e5 | 19 | #define max7219_reg_digit4 0x05 |
jont | 0:bc2c0a19b0e5 | 20 | #define max7219_reg_digit5 0x06 |
jont | 0:bc2c0a19b0e5 | 21 | #define max7219_reg_digit6 0x07 |
jont | 0:bc2c0a19b0e5 | 22 | #define max7219_reg_digit7 0x08 |
jont | 0:bc2c0a19b0e5 | 23 | #define max7219_reg_decodeMode 0x09 |
jont | 0:bc2c0a19b0e5 | 24 | #define max7219_reg_intensity 0x0a |
jont | 0:bc2c0a19b0e5 | 25 | #define max7219_reg_scanLimit 0x0b |
jont | 0:bc2c0a19b0e5 | 26 | #define max7219_reg_shutdown 0x0c |
jont | 0:bc2c0a19b0e5 | 27 | #define max7219_reg_displayTest 0x0f |
jont | 0:bc2c0a19b0e5 | 28 | |
jont | 0:bc2c0a19b0e5 | 29 | #define LOW 0 |
jont | 0:bc2c0a19b0e5 | 30 | #define HIGH 1 |
jont | 0:bc2c0a19b0e5 | 31 | |
jont | 0:bc2c0a19b0e5 | 32 | //serial port |
jont | 0:bc2c0a19b0e5 | 33 | |
jont | 0:bc2c0a19b0e5 | 34 | Serial pc(USBTX, USBRX); // tx, rx |
jont | 0:bc2c0a19b0e5 | 35 | |
jont | 0:bc2c0a19b0e5 | 36 | SPI max72_spi(PTD2, NC, PTD1); |
jont | 0:bc2c0a19b0e5 | 37 | DigitalOut load(PTD0); //will provide the load signal |
jont | 0:bc2c0a19b0e5 | 38 | |
jont | 0:bc2c0a19b0e5 | 39 | |
jont | 0:bc2c0a19b0e5 | 40 | char pattern_diagonal[8] = { 0x01, 0x2,0x4,0x08,0x10,0x20,0x40,0x80}; |
jont | 0:bc2c0a19b0e5 | 41 | char pattern_square[8] = { 0xff, 0x81,0x81,0x81,0x81,0x81,0x81,0xff}; |
jont | 0:bc2c0a19b0e5 | 42 | char pattern_star[8] = { 0x04, 0x15, 0x0e, 0x1f, 0x0e, 0x15, 0x04, 0x00}; |
jont | 0:bc2c0a19b0e5 | 43 | |
jont | 0:bc2c0a19b0e5 | 44 | |
jont | 0:bc2c0a19b0e5 | 45 | /* |
jont | 0:bc2c0a19b0e5 | 46 | Write to the maxim via SPI |
jont | 0:bc2c0a19b0e5 | 47 | args register and the column data |
jont | 0:bc2c0a19b0e5 | 48 | */ |
jont | 0:bc2c0a19b0e5 | 49 | void write_to_max( int reg, int col) |
jont | 0:bc2c0a19b0e5 | 50 | { |
jont | 0:bc2c0a19b0e5 | 51 | load = LOW; // begin |
jont | 0:bc2c0a19b0e5 | 52 | max72_spi.write(reg); // specify register |
jont | 0:bc2c0a19b0e5 | 53 | max72_spi.write(col); // put data |
jont | 0:bc2c0a19b0e5 | 54 | load = HIGH; // make sure data is loaded (on rising edge of LOAD/CS) |
jont | 0:bc2c0a19b0e5 | 55 | } |
jont | 0:bc2c0a19b0e5 | 56 | |
jont | 0:bc2c0a19b0e5 | 57 | //writes 8 bytes to the display |
jont | 0:bc2c0a19b0e5 | 58 | void pattern_to_display(char *testdata){ |
jont | 0:bc2c0a19b0e5 | 59 | int cdata; |
jont | 0:bc2c0a19b0e5 | 60 | for(int idx = 0; idx <= 7; idx++) { |
jont | 0:bc2c0a19b0e5 | 61 | cdata = testdata[idx]; |
jont | 0:bc2c0a19b0e5 | 62 | write_to_max(idx+1,cdata); |
jont | 0:bc2c0a19b0e5 | 63 | |
jont | 0:bc2c0a19b0e5 | 64 | //writet to the pretend display on the serial port |
jont | 0:bc2c0a19b0e5 | 65 | if(pc != NULL) { |
jont | 0:bc2c0a19b0e5 | 66 | pc.printf("%i;%02x\r\n", idx,cdata); |
jont | 0:bc2c0a19b0e5 | 67 | } |
jont | 0:bc2c0a19b0e5 | 68 | } |
jont | 0:bc2c0a19b0e5 | 69 | } |
jont | 0:bc2c0a19b0e5 | 70 | |
jont | 0:bc2c0a19b0e5 | 71 | |
jont | 0:bc2c0a19b0e5 | 72 | void setup_dot_matrix () |
jont | 0:bc2c0a19b0e5 | 73 | { |
jont | 0:bc2c0a19b0e5 | 74 | // initiation of the max 7219 |
jont | 0:bc2c0a19b0e5 | 75 | // SPI setup: 8 bits, mode 0 |
jont | 0:bc2c0a19b0e5 | 76 | max72_spi.format(8, 0); |
jont | 0:bc2c0a19b0e5 | 77 | |
jont | 0:bc2c0a19b0e5 | 78 | |
jont | 0:bc2c0a19b0e5 | 79 | |
jont | 0:bc2c0a19b0e5 | 80 | max72_spi.frequency(100000); //down to 100khx easier to scope ;-) |
jont | 0:bc2c0a19b0e5 | 81 | |
jont | 0:bc2c0a19b0e5 | 82 | |
jont | 0:bc2c0a19b0e5 | 83 | write_to_max(max7219_reg_scanLimit, 0x07); |
jont | 0:bc2c0a19b0e5 | 84 | write_to_max(max7219_reg_decodeMode, 0x00); // using an led matrix (not digits) |
jont | 0:bc2c0a19b0e5 | 85 | write_to_max(max7219_reg_shutdown, 0x01); // not in shutdown mode |
jont | 0:bc2c0a19b0e5 | 86 | write_to_max(max7219_reg_displayTest, 0x00); // no display test |
jont | 0:bc2c0a19b0e5 | 87 | for (int e=1; e<=8; e++) { // empty registers, turn all LEDs off |
jont | 0:bc2c0a19b0e5 | 88 | write_to_max(e,0); |
jont | 0:bc2c0a19b0e5 | 89 | } |
jont | 0:bc2c0a19b0e5 | 90 | // maxAll(max7219_reg_intensity, 0x0f & 0x0f); // the first 0x0f is the value you can set |
jont | 0:bc2c0a19b0e5 | 91 | write_to_max(max7219_reg_intensity, 0x08); |
jont | 0:bc2c0a19b0e5 | 92 | |
jont | 0:bc2c0a19b0e5 | 93 | } |
jont | 0:bc2c0a19b0e5 | 94 | |
jont | 0:bc2c0a19b0e5 | 95 | void clear(){ |
jont | 0:bc2c0a19b0e5 | 96 | for (int e=1; e<=8; e++) { // empty registers, turn all LEDs off |
jont | 0:bc2c0a19b0e5 | 97 | write_to_max(e,0); |
jont | 0:bc2c0a19b0e5 | 98 | } |
jont | 0:bc2c0a19b0e5 | 99 | } |
jont | 0:bc2c0a19b0e5 | 100 | |
jont | 0:bc2c0a19b0e5 | 101 | |
jont | 0:bc2c0a19b0e5 | 102 | int main() |
jont | 0:bc2c0a19b0e5 | 103 | { |
jont | 0:bc2c0a19b0e5 | 104 | pc.baud(9600); //setup serial port to 9600 |
jont | 0:bc2c0a19b0e5 | 105 | setup_dot_matrix (); /* setup matric */ |
jont | 0:bc2c0a19b0e5 | 106 | while(1){ |
jont | 0:bc2c0a19b0e5 | 107 | //da_star(); |
jont | 0:bc2c0a19b0e5 | 108 | pattern_to_display(pattern_diagonal); |
jont | 0:bc2c0a19b0e5 | 109 | wait_ms(1000); |
jont | 0:bc2c0a19b0e5 | 110 | pattern_to_display(pattern_square); |
jont | 0:bc2c0a19b0e5 | 111 | wait_ms(1000); |
jont | 0:bc2c0a19b0e5 | 112 | pattern_to_display(pattern_star); |
jont | 0:bc2c0a19b0e5 | 113 | wait_ms(1000); |
jont | 0:bc2c0a19b0e5 | 114 | clear(); |
jont | 0:bc2c0a19b0e5 | 115 | |
jont | 0:bc2c0a19b0e5 | 116 | } |
jont | 0:bc2c0a19b0e5 | 117 | } |