malcolm lear
/
LabmbedV30
Labmbed device drivers
main.cpp@4:e2310d494d19, 2017-01-12 (annotated)
- Committer:
- malcolmlear
- Date:
- Thu Jan 12 16:41:19 2017 +0000
- Revision:
- 4:e2310d494d19
- Parent:
- 3:8eee79f59b30
- Child:
- 5:7eea83fb1cb4
Test program tidied
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
malcolmlear | 0:416329806e55 | 1 | // Demo Program for Labmbed Board |
malcolmlear | 0:416329806e55 | 2 | |
malcolmlear | 0:416329806e55 | 3 | #include "mbed.h" |
malcolmlear | 0:416329806e55 | 4 | #include "TextLCD.h" |
malcolmlear | 0:416329806e55 | 5 | |
malcolmlear | 2:caa0e6b2b436 | 6 | TextLCD lcd(p15, p16, p17, p18, p19, p20); // LCD: RS, E, D4-D7 |
malcolmlear | 2:caa0e6b2b436 | 7 | SPI spi(p5, p6, p7); // SPI: MOSI, MISO, SCLK (MISO not used with LCD) |
malcolmlear | 2:caa0e6b2b436 | 8 | DigitalOut lat(p8); // data latch for LED driver TLC59281 |
malcolmlear | 3:8eee79f59b30 | 9 | DigitalOut sel0(p26); // input select bits: |
malcolmlear | 3:8eee79f59b30 | 10 | DigitalOut sel1(p25); // " |
malcolmlear | 3:8eee79f59b30 | 11 | DigitalOut sel2(p24); // " |
malcolmlear | 3:8eee79f59b30 | 12 | DigitalIn in0(p14); // input from switches, keypad etc |
malcolmlear | 3:8eee79f59b30 | 13 | DigitalIn in1(p13); // " |
malcolmlear | 3:8eee79f59b30 | 14 | DigitalIn in2(p12); // " |
malcolmlear | 3:8eee79f59b30 | 15 | DigitalIn in3(p11); // " |
malcolmlear | 2:caa0e6b2b436 | 16 | I2C i2c(p9, p10); // I2C: SDA, SCL |
malcolmlear | 0:416329806e55 | 17 | |
malcolmlear | 2:caa0e6b2b436 | 18 | int led_bits = 0; // global LED status used for readback |
malcolmlear | 2:caa0e6b2b436 | 19 | const int tmp102addr = 0x92; // TMP102 temperature I2C address |
malcolmlear | 0:416329806e55 | 20 | |
malcolmlear | 0:416329806e55 | 21 | |
malcolmlear | 0:416329806e55 | 22 | void initleds() { |
malcolmlear | 2:caa0e6b2b436 | 23 | lat = 0; // latch must start low |
malcolmlear | 2:caa0e6b2b436 | 24 | spi.format(16,0); // SPI 16 bit data, low state, high going clock |
malcolmlear | 2:caa0e6b2b436 | 25 | spi.frequency(1000000); // 1MHz clock rate |
malcolmlear | 0:416329806e55 | 26 | } |
malcolmlear | 0:416329806e55 | 27 | |
malcolmlear | 0:416329806e55 | 28 | void setleds(int ledall) { |
malcolmlear | 2:caa0e6b2b436 | 29 | led_bits = ledall; // update global LED status |
malcolmlear | 0:416329806e55 | 30 | spi.write((led_bits & 0x03ff) | ((led_bits & 0xa800) >> 1) | ((led_bits & 0x5400) << 1)); |
malcolmlear | 2:caa0e6b2b436 | 31 | lat = 1; // latch pulse start |
malcolmlear | 2:caa0e6b2b436 | 32 | lat = 0; // latch pulse end |
malcolmlear | 0:416329806e55 | 33 | } |
malcolmlear | 0:416329806e55 | 34 | |
malcolmlear | 0:416329806e55 | 35 | void setled(int ledno, int ledstate) { |
malcolmlear | 2:caa0e6b2b436 | 36 | ledno = ((ledno - 1) & 0x0007) + 1; // limit led number |
malcolmlear | 2:caa0e6b2b436 | 37 | ledno = (8 - ledno) * 2; // offset of led state in 'led_bits' |
malcolmlear | 2:caa0e6b2b436 | 38 | ledstate = ledstate & 0x0003; // limit led state |
malcolmlear | 0:416329806e55 | 39 | ledstate = ledstate << ledno; |
malcolmlear | 2:caa0e6b2b436 | 40 | int statemask = ((0x0003 << ledno) ^ 0xffff); // mask used to clear led state |
malcolmlear | 1:04e1ee8faa04 | 41 | led_bits = ((led_bits & statemask) | ledstate); // clear and set led state |
malcolmlear | 0:416329806e55 | 42 | setleds(led_bits); |
malcolmlear | 0:416329806e55 | 43 | } |
malcolmlear | 0:416329806e55 | 44 | |
malcolmlear | 0:416329806e55 | 45 | int readled(int ledno) { |
malcolmlear | 2:caa0e6b2b436 | 46 | ledno = ((ledno - 1) & 0x0007) + 1; // limit led number |
malcolmlear | 2:caa0e6b2b436 | 47 | ledno = (8 - ledno) * 2; // offset of led state in 'led_bits' |
malcolmlear | 0:416329806e55 | 48 | int ledstate = led_bits; |
malcolmlear | 2:caa0e6b2b436 | 49 | ledstate = ledstate >> ledno; // shift selected led state into ls 2 bits |
malcolmlear | 2:caa0e6b2b436 | 50 | return (ledstate & 0x0003); // mask out and return led state |
malcolmlear | 0:416329806e55 | 51 | } |
malcolmlear | 0:416329806e55 | 52 | |
malcolmlear | 0:416329806e55 | 53 | int readleds() { |
malcolmlear | 2:caa0e6b2b436 | 54 | return led_bits; // return LED status |
malcolmlear | 0:416329806e55 | 55 | } |
malcolmlear | 0:416329806e55 | 56 | |
malcolmlear | 3:8eee79f59b30 | 57 | void selinput(int input) { |
malcolmlear | 3:8eee79f59b30 | 58 | sel0 = input & 0x0001; // set sel[0:2] pins |
malcolmlear | 3:8eee79f59b30 | 59 | sel1 = (input >> 1) & 0x0001; // |
malcolmlear | 3:8eee79f59b30 | 60 | sel2 = (input >> 2) & 0x0001; // |
malcolmlear | 3:8eee79f59b30 | 61 | } |
malcolmlear | 0:416329806e55 | 62 | |
malcolmlear | 3:8eee79f59b30 | 63 | int readswitches() { |
malcolmlear | 3:8eee79f59b30 | 64 | selinput(5); // select least significant 4 switches in[3:0] |
malcolmlear | 3:8eee79f59b30 | 65 | int switches = in0 + (in1 << 1) + (in2 << 2) + (in3 << 3); |
malcolmlear | 3:8eee79f59b30 | 66 | selinput(4); // select most significant 4 switches in[3:0] |
malcolmlear | 3:8eee79f59b30 | 67 | return (switches + (in0 << 4)+ (in1 << 5) + (in2 << 6) + (in3 << 7)); |
malcolmlear | 3:8eee79f59b30 | 68 | } |
malcolmlear | 0:416329806e55 | 69 | |
malcolmlear | 3:8eee79f59b30 | 70 | int readswitch(int switchno) { |
malcolmlear | 3:8eee79f59b30 | 71 | switchno = ((switchno - 1) & 0x0007) + 1; // limit switch number |
malcolmlear | 3:8eee79f59b30 | 72 | switchno = 8 - switchno; // offset of switch state in readswitches() |
malcolmlear | 3:8eee79f59b30 | 73 | int switchstate = readswitches(); |
malcolmlear | 3:8eee79f59b30 | 74 | switchstate = switchstate >> switchno; // shift selected switch state into ls bit |
malcolmlear | 3:8eee79f59b30 | 75 | return (switchstate & 0x0001); // mask out and return switch state |
malcolmlear | 3:8eee79f59b30 | 76 | } |
malcolmlear | 0:416329806e55 | 77 | |
malcolmlear | 3:8eee79f59b30 | 78 | int readkeys() { |
malcolmlear | 3:8eee79f59b30 | 79 | selinput(0); // select keypad top row |
malcolmlear | 3:8eee79f59b30 | 80 | int keys = (in0 << 15) + (in1 << 14) + (in2 << 13) + (in3 << 12); |
malcolmlear | 3:8eee79f59b30 | 81 | selinput(1); // select keypad second row |
malcolmlear | 3:8eee79f59b30 | 82 | keys = keys + (in0 << 3) + (in1 << 6) + (in2 << 9) + (in3 << 11); |
malcolmlear | 3:8eee79f59b30 | 83 | selinput(2); // select keypad third row |
malcolmlear | 3:8eee79f59b30 | 84 | keys = keys + (in0 << 2) + (in1 << 5) + (in2 << 8) + in3; |
malcolmlear | 3:8eee79f59b30 | 85 | selinput(3); // select keypad forth row |
malcolmlear | 3:8eee79f59b30 | 86 | keys = keys + (in0 << 1) + (in1 << 4) + (in2 << 7) + (in3 << 10); |
malcolmlear | 3:8eee79f59b30 | 87 | return (keys ^ 0xffff); // return inverted (key press active high) |
malcolmlear | 3:8eee79f59b30 | 88 | } |
malcolmlear | 0:416329806e55 | 89 | |
malcolmlear | 3:8eee79f59b30 | 90 | int readkey(int keyno) { |
malcolmlear | 3:8eee79f59b30 | 91 | keyno = keyno & 0x000f; // limit key number 0 to 15 (0 to F) |
malcolmlear | 3:8eee79f59b30 | 92 | int keystate = readkeys(); |
malcolmlear | 3:8eee79f59b30 | 93 | keystate = keystate >> keyno; // shift selected key state into ls bit |
malcolmlear | 3:8eee79f59b30 | 94 | return (keystate & 0x0001); // mask out and return key state |
malcolmlear | 3:8eee79f59b30 | 95 | } |
malcolmlear | 0:416329806e55 | 96 | |
malcolmlear | 0:416329806e55 | 97 | float readtemp() { |
malcolmlear | 2:caa0e6b2b436 | 98 | char cmd[3]; |
malcolmlear | 2:caa0e6b2b436 | 99 | cmd[0] = 0x01; // pointer register value |
malcolmlear | 2:caa0e6b2b436 | 100 | cmd[1] = 0x60; // byte 1 of the configuration register |
malcolmlear | 2:caa0e6b2b436 | 101 | cmd[2] = 0xa0; // byte 2 of the configuration register |
malcolmlear | 2:caa0e6b2b436 | 102 | i2c.write(tmp102addr, cmd, 3); // select configuration register and write 0x60a0 to it |
malcolmlear | 2:caa0e6b2b436 | 103 | wait(0.5); // wait for conversion |
malcolmlear | 2:caa0e6b2b436 | 104 | cmd[0] = 0x00; // pointer register value |
malcolmlear | 2:caa0e6b2b436 | 105 | i2c.write(tmp102addr, cmd, 1); // select temperature register |
malcolmlear | 2:caa0e6b2b436 | 106 | i2c.read(tmp102addr, cmd, 2); // read 16-bit temperature register |
malcolmlear | 2:caa0e6b2b436 | 107 | return (float((cmd[0]<<8)|cmd[1]) / 256.0); // divide by 256 and return temperature |
malcolmlear | 0:416329806e55 | 108 | } |
malcolmlear | 0:416329806e55 | 109 | |
malcolmlear | 0:416329806e55 | 110 | int main() { |
malcolmlear | 0:416329806e55 | 111 | |
malcolmlear | 0:416329806e55 | 112 | initleds(); |
malcolmlear | 0:416329806e55 | 113 | |
malcolmlear | 0:416329806e55 | 114 | while(1) { |
malcolmlear | 1:04e1ee8faa04 | 115 | int a,b; |
malcolmlear | 1:04e1ee8faa04 | 116 | for (b = 0; b < 4; b++ ) { |
malcolmlear | 2:caa0e6b2b436 | 117 | for (a = 1; a < 9; a++ ) { |
malcolmlear | 1:04e1ee8faa04 | 118 | setled (a,b); |
malcolmlear | 3:8eee79f59b30 | 119 | wait(.1); |
malcolmlear | 1:04e1ee8faa04 | 120 | } |
malcolmlear | 1:04e1ee8faa04 | 121 | } |
malcolmlear | 3:8eee79f59b30 | 122 | for (a= 1; a < 9; a++ ) { |
malcolmlear | 3:8eee79f59b30 | 123 | setled (a,(readswitch(a) * 3)); |
malcolmlear | 3:8eee79f59b30 | 124 | } |
malcolmlear | 3:8eee79f59b30 | 125 | float temp = readtemp(); |
malcolmlear | 4:e2310d494d19 | 126 | lcd.cls(); |
malcolmlear | 4:e2310d494d19 | 127 | lcd.printf("Tmp = %f\n", temp); |
malcolmlear | 3:8eee79f59b30 | 128 | wait(1); |
malcolmlear | 4:e2310d494d19 | 129 | lcd.cls(); |
malcolmlear | 4:e2310d494d19 | 130 | int switches = readswitches(); |
malcolmlear | 4:e2310d494d19 | 131 | lcd.printf("Switches = %d\n", switches); |
malcolmlear | 3:8eee79f59b30 | 132 | int keys = readkeys(); |
malcolmlear | 3:8eee79f59b30 | 133 | lcd.printf("Keys = %d\n", keys); |
malcolmlear | 0:416329806e55 | 134 | } |
malcolmlear | 0:416329806e55 | 135 | } |