Dominic Harg
/
LED_uebung_TINF
verschiedene Funktionen mit Interrupt
main.cpp@0:1c9547d9c57c, 2020-01-22 (annotated)
- Committer:
- haunsi
- Date:
- Wed Jan 22 15:42:27 2020 +0000
- Revision:
- 0:1c9547d9c57c
LED Spielereien mit Nucleo + Board
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
haunsi | 0:1c9547d9c57c | 1 | #include "mbed.h" |
haunsi | 0:1c9547d9c57c | 2 | |
haunsi | 0:1c9547d9c57c | 3 | DigitalOut myled(LED1); |
haunsi | 0:1c9547d9c57c | 4 | |
haunsi | 0:1c9547d9c57c | 5 | // D13 A6 D12 D11 D9 D6 D3 D2 |
haunsi | 0:1c9547d9c57c | 6 | BusOut lb(PB_3,PA_7,PB_4, PB_5, PA_8, PB_1, PB_0, PA_12); |
haunsi | 0:1c9547d9c57c | 7 | DigitalOut red (D1), green(D0), blue(D10); |
haunsi | 0:1c9547d9c57c | 8 | |
haunsi | 0:1c9547d9c57c | 9 | DigitalIn btn1(A1); |
haunsi | 0:1c9547d9c57c | 10 | DigitalIn btn2(A2); |
haunsi | 0:1c9547d9c57c | 11 | |
haunsi | 0:1c9547d9c57c | 12 | InterruptIn sw1(A1); |
haunsi | 0:1c9547d9c57c | 13 | |
haunsi | 0:1c9547d9c57c | 14 | void runlightright(); |
haunsi | 0:1c9547d9c57c | 15 | void runlightleft(); |
haunsi | 0:1c9547d9c57c | 16 | void btntest(); |
haunsi | 0:1c9547d9c57c | 17 | void BtnLedTest1(); |
haunsi | 0:1c9547d9c57c | 18 | void RGB(); |
haunsi | 0:1c9547d9c57c | 19 | void interrupt(); |
haunsi | 0:1c9547d9c57c | 20 | void runlightstep(); |
haunsi | 0:1c9547d9c57c | 21 | |
haunsi | 0:1c9547d9c57c | 22 | int main() { |
haunsi | 0:1c9547d9c57c | 23 | //runlightright(); |
haunsi | 0:1c9547d9c57c | 24 | //runlightleft(); |
haunsi | 0:1c9547d9c57c | 25 | //btntest(); |
haunsi | 0:1c9547d9c57c | 26 | //RGB(); |
haunsi | 0:1c9547d9c57c | 27 | /*while(1){ |
haunsi | 0:1c9547d9c57c | 28 | BtnLedTest1(); |
haunsi | 0:1c9547d9c57c | 29 | }*/ |
haunsi | 0:1c9547d9c57c | 30 | sw1.rise(interrupt); |
haunsi | 0:1c9547d9c57c | 31 | |
haunsi | 0:1c9547d9c57c | 32 | while(1) |
haunsi | 0:1c9547d9c57c | 33 | {} |
haunsi | 0:1c9547d9c57c | 34 | } |
haunsi | 0:1c9547d9c57c | 35 | |
haunsi | 0:1c9547d9c57c | 36 | //LEDs leuchten abwechselnd von Rechts nach Links |
haunsi | 0:1c9547d9c57c | 37 | void runlightright() |
haunsi | 0:1c9547d9c57c | 38 | { |
haunsi | 0:1c9547d9c57c | 39 | while(1){ |
haunsi | 0:1c9547d9c57c | 40 | lb=1; |
haunsi | 0:1c9547d9c57c | 41 | for(int i = 0; i <= 8; i++) |
haunsi | 0:1c9547d9c57c | 42 | { |
haunsi | 0:1c9547d9c57c | 43 | wait_ms(200); |
haunsi | 0:1c9547d9c57c | 44 | lb = lb << 1; |
haunsi | 0:1c9547d9c57c | 45 | } |
haunsi | 0:1c9547d9c57c | 46 | |
haunsi | 0:1c9547d9c57c | 47 | } |
haunsi | 0:1c9547d9c57c | 48 | } |
haunsi | 0:1c9547d9c57c | 49 | |
haunsi | 0:1c9547d9c57c | 50 | //LEDs leuchten abwechselnd von Links nach Rechts |
haunsi | 0:1c9547d9c57c | 51 | void runlightleft() |
haunsi | 0:1c9547d9c57c | 52 | { |
haunsi | 0:1c9547d9c57c | 53 | while(1){ |
haunsi | 0:1c9547d9c57c | 54 | lb=128; |
haunsi | 0:1c9547d9c57c | 55 | for(int i = 0; i <= 8; i++) |
haunsi | 0:1c9547d9c57c | 56 | { |
haunsi | 0:1c9547d9c57c | 57 | wait_ms(200); |
haunsi | 0:1c9547d9c57c | 58 | lb = lb >> 1; |
haunsi | 0:1c9547d9c57c | 59 | } |
haunsi | 0:1c9547d9c57c | 60 | } |
haunsi | 0:1c9547d9c57c | 61 | } |
haunsi | 0:1c9547d9c57c | 62 | void btntest(){ |
haunsi | 0:1c9547d9c57c | 63 | |
haunsi | 0:1c9547d9c57c | 64 | while(1){ |
haunsi | 0:1c9547d9c57c | 65 | if(btn1.read() == 0) |
haunsi | 0:1c9547d9c57c | 66 | { |
haunsi | 0:1c9547d9c57c | 67 | lb = lb | 0xFF; |
haunsi | 0:1c9547d9c57c | 68 | } |
haunsi | 0:1c9547d9c57c | 69 | if(btn2.read() == 0) |
haunsi | 0:1c9547d9c57c | 70 | { |
haunsi | 0:1c9547d9c57c | 71 | lb = lb | 2; |
haunsi | 0:1c9547d9c57c | 72 | } |
haunsi | 0:1c9547d9c57c | 73 | else |
haunsi | 0:1c9547d9c57c | 74 | { |
haunsi | 0:1c9547d9c57c | 75 | lb = 0; |
haunsi | 0:1c9547d9c57c | 76 | } |
haunsi | 0:1c9547d9c57c | 77 | } |
haunsi | 0:1c9547d9c57c | 78 | } |
haunsi | 0:1c9547d9c57c | 79 | |
haunsi | 0:1c9547d9c57c | 80 | void BtnLedTest1() |
haunsi | 0:1c9547d9c57c | 81 | { |
haunsi | 0:1c9547d9c57c | 82 | if( btn1.read()==0 ) |
haunsi | 0:1c9547d9c57c | 83 | lb = lb | 0x0F; |
haunsi | 0:1c9547d9c57c | 84 | if( btn2.read()==0 ) |
haunsi | 0:1c9547d9c57c | 85 | lb = lb | 0xF0; |
haunsi | 0:1c9547d9c57c | 86 | wait_ms(200); |
haunsi | 0:1c9547d9c57c | 87 | lb = 0; |
haunsi | 0:1c9547d9c57c | 88 | wait_ms(200); |
haunsi | 0:1c9547d9c57c | 89 | } |
haunsi | 0:1c9547d9c57c | 90 | |
haunsi | 0:1c9547d9c57c | 91 | void RGB() |
haunsi | 0:1c9547d9c57c | 92 | { |
haunsi | 0:1c9547d9c57c | 93 | while(1) |
haunsi | 0:1c9547d9c57c | 94 | { |
haunsi | 0:1c9547d9c57c | 95 | red =1; |
haunsi | 0:1c9547d9c57c | 96 | wait_ms(5); |
haunsi | 0:1c9547d9c57c | 97 | green = 1; |
haunsi | 0:1c9547d9c57c | 98 | wait_ms(5); |
haunsi | 0:1c9547d9c57c | 99 | |
haunsi | 0:1c9547d9c57c | 100 | red =0; |
haunsi | 0:1c9547d9c57c | 101 | wait_ms(5); |
haunsi | 0:1c9547d9c57c | 102 | green = 0; |
haunsi | 0:1c9547d9c57c | 103 | wait_ms(5); |
haunsi | 0:1c9547d9c57c | 104 | } |
haunsi | 0:1c9547d9c57c | 105 | } |
haunsi | 0:1c9547d9c57c | 106 | void interrupt() |
haunsi | 0:1c9547d9c57c | 107 | { |
haunsi | 0:1c9547d9c57c | 108 | if(sw1.read() == 0) |
haunsi | 0:1c9547d9c57c | 109 | runlightstep(); |
haunsi | 0:1c9547d9c57c | 110 | } |
haunsi | 0:1c9547d9c57c | 111 | void runlightstep() |
haunsi | 0:1c9547d9c57c | 112 | { |
haunsi | 0:1c9547d9c57c | 113 | if(lb==0) |
haunsi | 0:1c9547d9c57c | 114 | lb = 1; |
haunsi | 0:1c9547d9c57c | 115 | else |
haunsi | 0:1c9547d9c57c | 116 | lb = lb << 1; |
haunsi | 0:1c9547d9c57c | 117 | |
haunsi | 0:1c9547d9c57c | 118 | } |