Riad_ Bajraktarevic
/
Bajraktarevic_Test
Main
main.cpp@5:eb2e43da141c, 2020-01-13 (annotated)
- Committer:
- riad
- Date:
- Mon Jan 13 17:24:52 2020 +0000
- Revision:
- 5:eb2e43da141c
- Parent:
- 4:9e935689c3c4
- Child:
- 6:eaf7d563b10e
Main 2
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
riad | 0:7925301ed8e4 | 1 | #include "mbed.h" |
riad | 0:7925301ed8e4 | 2 | #define BUTTON1 p14 |
riad | 0:7925301ed8e4 | 3 | #define BUTTON! A1 |
riad | 0:7925301ed8e4 | 4 | |
riad | 0:7925301ed8e4 | 5 | int modifyBit(int x, unsingned char position, bool State); |
riad | 0:7925301ed8e4 | 6 | int lauflicht(bool richtung, int time, int &anz); |
riad | 0:7925301ed8e4 | 7 | void nibbleLeds(int value); |
riad | 2:47d9c6c7eb9e | 8 | void printb(uint8_t x); |
riad | 0:7925301ed8e4 | 9 | |
riad | 0:7925301ed8e4 | 10 | |
riad | 0:7925301ed8e4 | 11 | BusOut myleds(LED1,LED2,LED3,LED4); |
riad | 0:7925301ed8e4 | 12 | DigitalIn button(BUTTON1); |
riad | 0:7925301ed8e4 | 13 | |
riad | 0:7925301ed8e4 | 14 | const int INIT = 0x03; |
riad | 0:7925301ed8e4 | 15 | |
riad | 2:47d9c6c7eb9e | 16 | int main () |
riad | 2:47d9c6c7eb9e | 17 | { |
riad | 0:7925301ed8e4 | 18 | int anzahl, anz; |
riad | 0:7925301ed8e4 | 19 | uint8_t value = INIT; |
riad | 5:eb2e43da141c | 20 | |
riad | 5:eb2e43da141c | 21 | anzahl = lauflicht (true, 400, anz=0); |
riad | 5:eb2e43da141c | 22 | printf("anzahl = %d\n", anzahl); |
riad | 5:eb2e43da141c | 23 | |
riad | 5:eb2e43da141c | 24 | nibbleLeds(value); |
riad | 5:eb2e43da141c | 25 | printb(value); |
riad | 5:eb2e43da141c | 26 | value = modifyBit(value, 2, 1); |
riad | 5:eb2e43da141c | 27 | printb(myleds); |
riad | 5:eb2e43da141c | 28 | printb(modifyBit(INIT, 3, 1)); |
riad | 5:eb2e43da141c | 29 | wait(0.1); |
riad | 5:eb2e43da141c | 30 | |
riad | 1:a3d6a365dc15 | 31 | } |
riad | 1:a3d6a365dc15 | 32 | |
riad | 2:47d9c6c7eb9e | 33 | int modifyBit (int x, uint8_t position, bool State) |
riad | 2:47d9c6c7eb9e | 34 | { |
riad | 1:a3d6a365dc15 | 35 | int mask = 1 << position; |
riad | 1:a3d6a365dc15 | 36 | int state = int(State); |
riad | 1:a3d6a365dc15 | 37 | return (x & ~mask) | ((state << position) & mask); |
riad | 2:47d9c6c7eb9e | 38 | } |
riad | 2:47d9c6c7eb9e | 39 | |
riad | 2:47d9c6c7eb9e | 40 | int lauflicht (bool richtung, int time, int &anz) |
riad | 2:47d9c6c7eb9e | 41 | { |
riad | 2:47d9c6c7eb9e | 42 | int i; |
riad | 2:47d9c6c7eb9e | 43 | uint8_t lauf = 0x01; |
riad | 2:47d9c6c7eb9e | 44 | |
riad | 2:47d9c6c7eb9e | 45 | if(!richtung) |
riad | 2:47d9c6c7eb9e | 46 | lauf = 0x08; |
riad | 2:47d9c6c7eb9e | 47 | while(1) { |
riad | 2:47d9c6c7eb9e | 48 | nibbleLeds(lauf&0x0F); |
riad | 2:47d9c6c7eb9e | 49 | if(richtung) { |
riad | 2:47d9c6c7eb9e | 50 | lauf = lauf << 1; |
riad | 2:47d9c6c7eb9e | 51 | if(lauf > 8) |
riad | 2:47d9c6c7eb9e | 52 | lauf = 0x01; |
riad | 2:47d9c6c7eb9e | 53 | } else { |
riad | 2:47d9c6c7eb9e | 54 | lauf = lauf >> 1; |
riad | 2:47d9c6c7eb9e | 55 | if(lauf == 0) |
riad | 2:47d9c6c7eb9e | 56 | lauf = 0x08; |
riad | 2:47d9c6c7eb9e | 57 | } |
riad | 2:47d9c6c7eb9e | 58 | if(button) |
riad | 2:47d9c6c7eb9e | 59 | break; |
riad | 2:47d9c6c7eb9e | 60 | wait_ms(time); |
riad | 2:47d9c6c7eb9e | 61 | anz++; |
riad | 2:47d9c6c7eb9e | 62 | } |
riad | 2:47d9c6c7eb9e | 63 | return anz; |
riad | 3:f80e65dd4ee3 | 64 | } |
riad | 3:f80e65dd4ee3 | 65 | |
riad | 3:f80e65dd4ee3 | 66 | void printb(uint8_t x) |
riad | 3:f80e65dd4ee3 | 67 | { |
riad | 3:f80e65dd4ee3 | 68 | for(int i=sizeof (x) << 3; i; i--) |
riad | 3:f80e65dd4ee3 | 69 | putchar('0' + ((x>>(i-1))&1)); |
riad | 3:f80e65dd4ee3 | 70 | printf("\n"); |
riad | 4:9e935689c3c4 | 71 | } |
riad | 4:9e935689c3c4 | 72 | |
riad | 4:9e935689c3c4 | 73 | void nibbleLeds(int value) |
riad | 4:9e935689c3c4 | 74 | { |
riad | 4:9e935689c3c4 | 75 | myleds = value%16; |
riad | 2:47d9c6c7eb9e | 76 | } |