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