David Wynn
/
mbed_max2719
ECE Project
Embed:
(wiki syntax)
Show/hide line numbers
main.cpp
00001 /* 06_spi_max7219_led8x8 00002 * 00003 * Simple demo to drive a 8x8-as LED matrix by a MAX7219 LED driver IC 00004 * After initialisation two characters (H and W) are displayed alternatively. 00005 * The MAX7219 IC is driven by hardware SPI: SPI0 module at PTD1, PTD2, PTD3. 00006 */ 00007 00008 #include "mbed.h" 00009 #include "MMA8451Q.h" 00010 00011 PinName const SDA = PTE25; 00012 PinName const SCL = PTE24; 00013 00014 SPI spi(PTD2, PTD3, PTD1); // Arduino compatible MOSI, MISO, SCLK 00015 DigitalOut cs(PTD0); // Chip select 00016 00017 #define MMA8451_I2C_ADDRESS (0x1d<<1) 00018 00019 const unsigned char led1[]= { 00020 0xFF,0xB4,0x15,0xD1,0x85,0xB5,0x95,0xFF 00021 }; //H 00022 const unsigned char led2[]= { 00023 0x1F,0x60,0x80,0x40,0x40,0x80,0x60,0x1F 00024 }; //W 00025 00026 /// Send two bytes to SPI bus 00027 void SPI_Write2(unsigned char MSB, unsigned char LSB) 00028 { 00029 cs = 0; // Set CS Low 00030 spi.write(MSB); // Send two bytes 00031 spi.write(LSB); 00032 cs = 1; // Set CS High 00033 } 00034 00035 void direction(float x, float y){ 00036 if(x > 0.5){ 00037 printf("Left \n"); 00038 }else if(x < -0.5){ 00039 printf("Right \n"); 00040 }else if(y > 0.5){ 00041 printf("Up \n"); 00042 }else if(y < -0.5){ 00043 printf("Down \n"); 00044 }else{ 00045 printf("Neutral Position \n"); 00046 } 00047 } 00048 00049 /// MAX7219 initialisation 00050 void Init_MAX7219(void) 00051 { 00052 SPI_Write2(0x09, 0x00); // Decoding off 00053 SPI_Write2(0x0A, 0x08); // Brightness to intermediate 00054 SPI_Write2(0x0B, 0x07); // Scan limit = 7 00055 SPI_Write2(0x0C, 0x01); // Normal operation mode 00056 SPI_Write2(0x0F, 0x0F); // Enable display test 00057 wait_ms(500); // 500 ms delay 00058 SPI_Write2(0x01, 0x00); // Clear row 0. 00059 SPI_Write2(0x02, 0x00); // Clear row 1. 00060 SPI_Write2(0x03, 0x00); // Clear row 2. 00061 SPI_Write2(0x04, 0x00); // Clear row 3. 00062 SPI_Write2(0x05, 0x00); // Clear row 4. 00063 SPI_Write2(0x06, 0x00); // Clear row 5. 00064 SPI_Write2(0x07, 0x00); // Clear row 6. 00065 SPI_Write2(0x08, 0x00); // Clear row 7. 00066 SPI_Write2(0x0F, 0x00); // Disable display test 00067 wait_ms(500); // 500 ms delay 00068 } 00069 00070 int main() 00071 { 00072 00073 cs = 1; // CS initially High 00074 spi.format(8,0); // 8-bit format, mode 0,0 00075 spi.frequency(1000000); // SCLK = 1 MHz 00076 Init_MAX7219(); // Initialize the LED controller 00077 MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS); 00078 unsigned char row = 0x03; 00079 00080 unsigned char position[] = { 00081 0x00,0x00,0x80,0x00,0x00,0x00,0x00,0x00 00082 }; 00083 00084 for(int i=1; i<9; i++) // Write first character (8 rows) 00085 SPI_Write2(i,led1[i-1]); 00086 while (1) { 00087 00088 float x, y; 00089 x = acc.getAccX(); 00090 y = acc.getAccY(); 00091 00092 if(x > 0.5){ 00093 printf("Left \n"); 00094 position[1] = position[1]*2; 00095 }else if(x < -0.5){ 00096 printf("Right \n"); 00097 position[1] = position[1]/2; 00098 }else if(y > 0.5){ 00099 printf("Up \n"); 00100 position[0] = position[0]-1; 00101 row--; 00102 }else if(y < -0.5){ 00103 printf("Down \n"); 00104 position[0] = position[0]+1; 00105 row++; 00106 } 00107 00108 00109 00110 wait(1); // 1 sec delay 00111 SPI_Write2(position[0], position[1]); 00112 for(int i=1; i<9; i++){ // Write first character (8 rows) 00113 if(i != row){ 00114 SPI_Write2(i,led1[i-1]); 00115 } 00116 } 00117 } 00118 00119 00120 }
Generated on Tue Jul 26 2022 10:50:20 by 1.7.2