Yulou Yang
/
mbed_max2719
maze game
Fork of mbed_max2719 by
main.cpp@0:6be425a138b2, 2016-04-10 (annotated)
- Committer:
- alexwynn41
- Date:
- Sun Apr 10 19:02:41 2016 +0000
- Revision:
- 0:6be425a138b2
- Child:
- 1:ac65e35a9196
initial commit;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
alexwynn41 | 0:6be425a138b2 | 1 | /* 06_spi_max7219_led8x8 |
alexwynn41 | 0:6be425a138b2 | 2 | * |
alexwynn41 | 0:6be425a138b2 | 3 | * Simple demo to drive a 8x8-as LED matrix by a MAX7219 LED driver IC |
alexwynn41 | 0:6be425a138b2 | 4 | * After initialisation two characters (H and W) are displayed alternatively. |
alexwynn41 | 0:6be425a138b2 | 5 | * The MAX7219 IC is driven by hardware SPI: SPI0 module at PTD1, PTD2, PTD3. |
alexwynn41 | 0:6be425a138b2 | 6 | */ |
alexwynn41 | 0:6be425a138b2 | 7 | |
alexwynn41 | 0:6be425a138b2 | 8 | #include "mbed.h" |
alexwynn41 | 0:6be425a138b2 | 9 | #include "MMA8451Q.h" |
alexwynn41 | 0:6be425a138b2 | 10 | |
alexwynn41 | 0:6be425a138b2 | 11 | PinName const SDA = PTE25; |
alexwynn41 | 0:6be425a138b2 | 12 | PinName const SCL = PTE24; |
alexwynn41 | 0:6be425a138b2 | 13 | |
alexwynn41 | 0:6be425a138b2 | 14 | SPI spi(PTD2, PTD3, PTD1); // Arduino compatible MOSI, MISO, SCLK |
alexwynn41 | 0:6be425a138b2 | 15 | DigitalOut cs(PTD0); // Chip select |
alexwynn41 | 0:6be425a138b2 | 16 | |
alexwynn41 | 0:6be425a138b2 | 17 | #define MMA8451_I2C_ADDRESS (0x1d<<1) |
alexwynn41 | 0:6be425a138b2 | 18 | |
alexwynn41 | 0:6be425a138b2 | 19 | const unsigned char led1[]= { |
alexwynn41 | 0:6be425a138b2 | 20 | 0xFF,0x18,0x18,0x18,0x18,0x18,0x18,0xFF |
alexwynn41 | 0:6be425a138b2 | 21 | }; //H |
alexwynn41 | 0:6be425a138b2 | 22 | const unsigned char led2[]= { |
alexwynn41 | 0:6be425a138b2 | 23 | 0x1F,0x60,0x80,0x40,0x40,0x80,0x60,0x1F |
alexwynn41 | 0:6be425a138b2 | 24 | }; //W |
alexwynn41 | 0:6be425a138b2 | 25 | |
alexwynn41 | 0:6be425a138b2 | 26 | /// Send two bytes to SPI bus |
alexwynn41 | 0:6be425a138b2 | 27 | void SPI_Write2(unsigned char MSB, unsigned char LSB) |
alexwynn41 | 0:6be425a138b2 | 28 | { |
alexwynn41 | 0:6be425a138b2 | 29 | cs = 0; // Set CS Low |
alexwynn41 | 0:6be425a138b2 | 30 | spi.write(MSB); // Send two bytes |
alexwynn41 | 0:6be425a138b2 | 31 | spi.write(LSB); |
alexwynn41 | 0:6be425a138b2 | 32 | cs = 1; // Set CS High |
alexwynn41 | 0:6be425a138b2 | 33 | } |
alexwynn41 | 0:6be425a138b2 | 34 | |
alexwynn41 | 0:6be425a138b2 | 35 | void direction(float x, float y){ |
alexwynn41 | 0:6be425a138b2 | 36 | if(x > 0.5){ |
alexwynn41 | 0:6be425a138b2 | 37 | printf("Left \n"); |
alexwynn41 | 0:6be425a138b2 | 38 | }else if(x < -0.5){ |
alexwynn41 | 0:6be425a138b2 | 39 | printf("Right \n"); |
alexwynn41 | 0:6be425a138b2 | 40 | }else if(y > 0.5){ |
alexwynn41 | 0:6be425a138b2 | 41 | printf("Up \n"); |
alexwynn41 | 0:6be425a138b2 | 42 | }else if(y < -0.5){ |
alexwynn41 | 0:6be425a138b2 | 43 | printf("Down \n"); |
alexwynn41 | 0:6be425a138b2 | 44 | }else{ |
alexwynn41 | 0:6be425a138b2 | 45 | printf("Neutral Position \n"); |
alexwynn41 | 0:6be425a138b2 | 46 | } |
alexwynn41 | 0:6be425a138b2 | 47 | } |
alexwynn41 | 0:6be425a138b2 | 48 | |
alexwynn41 | 0:6be425a138b2 | 49 | /// MAX7219 initialisation |
alexwynn41 | 0:6be425a138b2 | 50 | void Init_MAX7219(void) |
alexwynn41 | 0:6be425a138b2 | 51 | { |
alexwynn41 | 0:6be425a138b2 | 52 | SPI_Write2(0x09, 0x00); // Decoding off |
alexwynn41 | 0:6be425a138b2 | 53 | SPI_Write2(0x0A, 0x08); // Brightness to intermediate |
alexwynn41 | 0:6be425a138b2 | 54 | SPI_Write2(0x0B, 0x07); // Scan limit = 7 |
alexwynn41 | 0:6be425a138b2 | 55 | SPI_Write2(0x0C, 0x01); // Normal operation mode |
alexwynn41 | 0:6be425a138b2 | 56 | SPI_Write2(0x0F, 0x0F); // Enable display test |
alexwynn41 | 0:6be425a138b2 | 57 | wait_ms(500); // 500 ms delay |
alexwynn41 | 0:6be425a138b2 | 58 | SPI_Write2(0x01, 0x00); // Clear row 0. |
alexwynn41 | 0:6be425a138b2 | 59 | SPI_Write2(0x02, 0x00); // Clear row 1. |
alexwynn41 | 0:6be425a138b2 | 60 | SPI_Write2(0x03, 0x00); // Clear row 2. |
alexwynn41 | 0:6be425a138b2 | 61 | SPI_Write2(0x04, 0x00); // Clear row 3. |
alexwynn41 | 0:6be425a138b2 | 62 | SPI_Write2(0x05, 0x00); // Clear row 4. |
alexwynn41 | 0:6be425a138b2 | 63 | SPI_Write2(0x06, 0x00); // Clear row 5. |
alexwynn41 | 0:6be425a138b2 | 64 | SPI_Write2(0x07, 0x00); // Clear row 6. |
alexwynn41 | 0:6be425a138b2 | 65 | SPI_Write2(0x08, 0x00); // Clear row 7. |
alexwynn41 | 0:6be425a138b2 | 66 | SPI_Write2(0x0F, 0x00); // Disable display test |
alexwynn41 | 0:6be425a138b2 | 67 | wait_ms(500); // 500 ms delay |
alexwynn41 | 0:6be425a138b2 | 68 | } |
alexwynn41 | 0:6be425a138b2 | 69 | |
alexwynn41 | 0:6be425a138b2 | 70 | int main() |
alexwynn41 | 0:6be425a138b2 | 71 | { |
alexwynn41 | 0:6be425a138b2 | 72 | |
alexwynn41 | 0:6be425a138b2 | 73 | cs = 1; // CS initially High |
alexwynn41 | 0:6be425a138b2 | 74 | spi.format(8,0); // 8-bit format, mode 0,0 |
alexwynn41 | 0:6be425a138b2 | 75 | spi.frequency(1000000); // SCLK = 1 MHz |
alexwynn41 | 0:6be425a138b2 | 76 | Init_MAX7219(); // Initialize the LED controller |
alexwynn41 | 0:6be425a138b2 | 77 | MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS); |
alexwynn41 | 0:6be425a138b2 | 78 | PwmOut rled(LED1); |
alexwynn41 | 0:6be425a138b2 | 79 | PwmOut gled(LED2); |
alexwynn41 | 0:6be425a138b2 | 80 | PwmOut bled(LED3); |
alexwynn41 | 0:6be425a138b2 | 81 | |
alexwynn41 | 0:6be425a138b2 | 82 | |
alexwynn41 | 0:6be425a138b2 | 83 | while (1) { |
alexwynn41 | 0:6be425a138b2 | 84 | |
alexwynn41 | 0:6be425a138b2 | 85 | float x, y, z; |
alexwynn41 | 0:6be425a138b2 | 86 | x = acc.getAccX(); |
alexwynn41 | 0:6be425a138b2 | 87 | y = acc.getAccY(); |
alexwynn41 | 0:6be425a138b2 | 88 | z = acc.getAccZ(); |
alexwynn41 | 0:6be425a138b2 | 89 | rled = 1.0f - abs(x); |
alexwynn41 | 0:6be425a138b2 | 90 | gled = 1.0f - abs(y); |
alexwynn41 | 0:6be425a138b2 | 91 | bled = 1.0f - abs(z); |
alexwynn41 | 0:6be425a138b2 | 92 | direction(x,y); |
alexwynn41 | 0:6be425a138b2 | 93 | wait(0.5f); |
alexwynn41 | 0:6be425a138b2 | 94 | |
alexwynn41 | 0:6be425a138b2 | 95 | for(int i=1; i<9; i++) // Write first character (8 rows) |
alexwynn41 | 0:6be425a138b2 | 96 | SPI_Write2(i,led1[i-1]); |
alexwynn41 | 0:6be425a138b2 | 97 | wait(1); // 1 sec delay |
alexwynn41 | 0:6be425a138b2 | 98 | for(int i=1; i<9; i++) // Write second character |
alexwynn41 | 0:6be425a138b2 | 99 | SPI_Write2(i,led2[i-1]); |
alexwynn41 | 0:6be425a138b2 | 100 | wait(1); // 1 sec delay */ |
alexwynn41 | 0:6be425a138b2 | 101 | } |
alexwynn41 | 0:6be425a138b2 | 102 | |
alexwynn41 | 0:6be425a138b2 | 103 | |
alexwynn41 | 0:6be425a138b2 | 104 | } |