EmbeddedArtists AB
/
lpc812_exp_solution_spi-7-segment
Solutions for the SPI 7-Segment experiments for LPC812 MAX
main.cpp@0:933fe0778bc8, 2013-11-24 (annotated)
- Committer:
- embeddedartists
- Date:
- Sun Nov 24 11:17:53 2013 +0000
- Revision:
- 0:933fe0778bc8
First version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
embeddedartists | 0:933fe0778bc8 | 1 | #include "mbed.h" |
embeddedartists | 0:933fe0778bc8 | 2 | |
embeddedartists | 0:933fe0778bc8 | 3 | DigitalOut ssel(D10); |
embeddedartists | 0:933fe0778bc8 | 4 | SPI spi(D11, D12, D13); // mosi, miso, sclk |
embeddedartists | 0:933fe0778bc8 | 5 | |
embeddedartists | 0:933fe0778bc8 | 6 | Serial pc(USBTX, USBRX); |
embeddedartists | 0:933fe0778bc8 | 7 | |
embeddedartists | 0:933fe0778bc8 | 8 | #define SEG_A 0x80 |
embeddedartists | 0:933fe0778bc8 | 9 | #define SEG_B 0x40 |
embeddedartists | 0:933fe0778bc8 | 10 | #define SEG_C 0x20 |
embeddedartists | 0:933fe0778bc8 | 11 | #define SEG_D 0x10 |
embeddedartists | 0:933fe0778bc8 | 12 | #define SEG_E 0x08 |
embeddedartists | 0:933fe0778bc8 | 13 | #define SEG_F 0x04 |
embeddedartists | 0:933fe0778bc8 | 14 | #define SEG_G 0x02 |
embeddedartists | 0:933fe0778bc8 | 15 | #define RDP 0x01 |
embeddedartists | 0:933fe0778bc8 | 16 | |
embeddedartists | 0:933fe0778bc8 | 17 | const uint8_t segments[16] = { |
embeddedartists | 0:933fe0778bc8 | 18 | SEG_A | SEG_B | SEG_C | SEG_D | SEG_E | SEG_F , //0 = A,B,C,D,E,F |
embeddedartists | 0:933fe0778bc8 | 19 | SEG_B | SEG_C , //1 = B,C |
embeddedartists | 0:933fe0778bc8 | 20 | SEG_A | SEG_B | SEG_D | SEG_E | SEG_G, //2 = A,B,D,E,G |
embeddedartists | 0:933fe0778bc8 | 21 | SEG_A | SEG_B | SEG_C | SEG_D | SEG_G, //3 = A,B,C,D,G |
embeddedartists | 0:933fe0778bc8 | 22 | SEG_B | SEG_C | SEG_F | SEG_G, //4 = B,C,F,G |
embeddedartists | 0:933fe0778bc8 | 23 | SEG_A | SEG_C | SEG_D | SEG_F | SEG_G, //5 = A,C,D,F,G |
embeddedartists | 0:933fe0778bc8 | 24 | SEG_A | SEG_C | SEG_D | SEG_E | SEG_F | SEG_G, //6 = A,C,D,E,F,G |
embeddedartists | 0:933fe0778bc8 | 25 | SEG_A | SEG_B | SEG_C , //7 = A,B,C |
embeddedartists | 0:933fe0778bc8 | 26 | SEG_A | SEG_B | SEG_C | SEG_D | SEG_E | SEG_F | SEG_G, //8 = A,B,C,D,E,F,G |
embeddedartists | 0:933fe0778bc8 | 27 | SEG_A | SEG_B | SEG_C | SEG_D | SEG_F | SEG_G, //9 = A,B,C,D,F,G |
embeddedartists | 0:933fe0778bc8 | 28 | SEG_A | SEG_B | SEG_C | SEG_E | SEG_F | SEG_G, //A = A,B,C,E,F,G |
embeddedartists | 0:933fe0778bc8 | 29 | SEG_C | SEG_D | SEG_E | SEG_F | SEG_G, //B = C,D,E,F,G |
embeddedartists | 0:933fe0778bc8 | 30 | SEG_D | SEG_E | SEG_G, //C = D,E,G |
embeddedartists | 0:933fe0778bc8 | 31 | SEG_B | SEG_C | SEG_D | SEG_E | SEG_G, //D = B,C,D,E,G |
embeddedartists | 0:933fe0778bc8 | 32 | SEG_A | SEG_D | SEG_E | SEG_F | SEG_G, //E = A,D,E,F,G |
embeddedartists | 0:933fe0778bc8 | 33 | SEG_A | SEG_E | SEG_F | SEG_G}; //F = A,E,F,G |
embeddedartists | 0:933fe0778bc8 | 34 | |
embeddedartists | 0:933fe0778bc8 | 35 | static void send(int val) |
embeddedartists | 0:933fe0778bc8 | 36 | { |
embeddedartists | 0:933fe0778bc8 | 37 | // control SSEL |
embeddedartists | 0:933fe0778bc8 | 38 | ssel = 0; |
embeddedartists | 0:933fe0778bc8 | 39 | |
embeddedartists | 0:933fe0778bc8 | 40 | spi.write(val); |
embeddedartists | 0:933fe0778bc8 | 41 | |
embeddedartists | 0:933fe0778bc8 | 42 | // control SSEL |
embeddedartists | 0:933fe0778bc8 | 43 | ssel = 1; |
embeddedartists | 0:933fe0778bc8 | 44 | } |
embeddedartists | 0:933fe0778bc8 | 45 | |
embeddedartists | 0:933fe0778bc8 | 46 | static bool readback(int val) |
embeddedartists | 0:933fe0778bc8 | 47 | { |
embeddedartists | 0:933fe0778bc8 | 48 | int existing = spi.write(val); |
embeddedartists | 0:933fe0778bc8 | 49 | int expected = spi.write(0x00); |
embeddedartists | 0:933fe0778bc8 | 50 | |
embeddedartists | 0:933fe0778bc8 | 51 | if ((val & 0xff) == (expected & 0xff)) { |
embeddedartists | 0:933fe0778bc8 | 52 | return true; |
embeddedartists | 0:933fe0778bc8 | 53 | } |
embeddedartists | 0:933fe0778bc8 | 54 | return false; |
embeddedartists | 0:933fe0778bc8 | 55 | } |
embeddedartists | 0:933fe0778bc8 | 56 | |
embeddedartists | 0:933fe0778bc8 | 57 | static void experiment1_alt1() |
embeddedartists | 0:933fe0778bc8 | 58 | { |
embeddedartists | 0:933fe0778bc8 | 59 | pc.printf("Starting...\n"); |
embeddedartists | 0:933fe0778bc8 | 60 | |
embeddedartists | 0:933fe0778bc8 | 61 | do { |
embeddedartists | 0:933fe0778bc8 | 62 | if (readback(~segments[0xe])) { |
embeddedartists | 0:933fe0778bc8 | 63 | wait(0.5); |
embeddedartists | 0:933fe0778bc8 | 64 | if (readback(~segments[0xa])) { |
embeddedartists | 0:933fe0778bc8 | 65 | printf("readback successfull\n"); |
embeddedartists | 0:933fe0778bc8 | 66 | break; |
embeddedartists | 0:933fe0778bc8 | 67 | } |
embeddedartists | 0:933fe0778bc8 | 68 | } |
embeddedartists | 0:933fe0778bc8 | 69 | printf("readback failed\n"); |
embeddedartists | 0:933fe0778bc8 | 70 | } while(0); |
embeddedartists | 0:933fe0778bc8 | 71 | |
embeddedartists | 0:933fe0778bc8 | 72 | while(1) |
embeddedartists | 0:933fe0778bc8 | 73 | ; |
embeddedartists | 0:933fe0778bc8 | 74 | } |
embeddedartists | 0:933fe0778bc8 | 75 | |
embeddedartists | 0:933fe0778bc8 | 76 | static void experiment1_alt2() |
embeddedartists | 0:933fe0778bc8 | 77 | { |
embeddedartists | 0:933fe0778bc8 | 78 | while(1) { |
embeddedartists | 0:933fe0778bc8 | 79 | for (int i = 0; i < 16; i++) { |
embeddedartists | 0:933fe0778bc8 | 80 | send(~segments[i]); |
embeddedartists | 0:933fe0778bc8 | 81 | wait(0.4); |
embeddedartists | 0:933fe0778bc8 | 82 | } |
embeddedartists | 0:933fe0778bc8 | 83 | } |
embeddedartists | 0:933fe0778bc8 | 84 | } |
embeddedartists | 0:933fe0778bc8 | 85 | |
embeddedartists | 0:933fe0778bc8 | 86 | static void experiment1_alt3() |
embeddedartists | 0:933fe0778bc8 | 87 | { |
embeddedartists | 0:933fe0778bc8 | 88 | while(1) { |
embeddedartists | 0:933fe0778bc8 | 89 | send(~SEG_A);wait(1); |
embeddedartists | 0:933fe0778bc8 | 90 | send(~SEG_B);wait(1); |
embeddedartists | 0:933fe0778bc8 | 91 | send(~SEG_C);wait(1); |
embeddedartists | 0:933fe0778bc8 | 92 | send(~SEG_D);wait(1); |
embeddedartists | 0:933fe0778bc8 | 93 | send(~SEG_E);wait(1); |
embeddedartists | 0:933fe0778bc8 | 94 | send(~SEG_F);wait(1); |
embeddedartists | 0:933fe0778bc8 | 95 | send(~SEG_G);wait(1); |
embeddedartists | 0:933fe0778bc8 | 96 | send(~RDP);wait(1); |
embeddedartists | 0:933fe0778bc8 | 97 | } |
embeddedartists | 0:933fe0778bc8 | 98 | } |
embeddedartists | 0:933fe0778bc8 | 99 | |
embeddedartists | 0:933fe0778bc8 | 100 | |
embeddedartists | 0:933fe0778bc8 | 101 | int main() |
embeddedartists | 0:933fe0778bc8 | 102 | { |
embeddedartists | 0:933fe0778bc8 | 103 | //experiment1_alt1(); // readback test - shift register verification |
embeddedartists | 0:933fe0778bc8 | 104 | //experiment1_alt2(); // automatic counter |
embeddedartists | 0:933fe0778bc8 | 105 | experiment1_alt3(); // running one |
embeddedartists | 0:933fe0778bc8 | 106 | } |