Sandro Djuric
/
Test1_SandroDjuric
funktionen
main.cpp@6:de6ff5fc4192, 2020-01-13 (annotated)
- Committer:
- sandrodjuric
- Date:
- Mon Jan 13 17:21:04 2020 +0000
- Revision:
- 6:de6ff5fc4192
- Parent:
- 5:0b09788019ab
- Child:
- 7:39a959781934
nibbleleds funk definieren
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sandrodjuric | 0:8c6f1d850a3c | 1 | #include "mbed.h" |
sandrodjuric | 0:8c6f1d850a3c | 2 | |
sandrodjuric | 2:5b052d18a113 | 3 | #define BUTTON1 p14 |
sandrodjuric | 2:5b052d18a113 | 4 | |
sandrodjuric | 1:2beb3ea0c40f | 5 | int modifyBit(int x, unsigned char position, bool State); |
sandrodjuric | 1:2beb3ea0c40f | 6 | int lauflicht(bool richtung, int time, int &anz); |
sandrodjuric | 1:2beb3ea0c40f | 7 | void nibbleLeds(int value); |
sandrodjuric | 2:5b052d18a113 | 8 | void printb(uint8_t x); |
sandrodjuric | 2:5b052d18a113 | 9 | |
sandrodjuric | 2:5b052d18a113 | 10 | BusOut myleds(LED1,LED2,LED3,LED4); |
sandrodjuric | 2:5b052d18a113 | 11 | DigitalIn button(BUTTON1); |
sandrodjuric | 2:5b052d18a113 | 12 | |
sandrodjuric | 2:5b052d18a113 | 13 | const int INIT = 0x03; |
sandrodjuric | 3:57ccaba4a24d | 14 | |
sandrodjuric | 3:57ccaba4a24d | 15 | int main(){ |
sandrodjuric | 3:57ccaba4a24d | 16 | int anzahl,anz; |
sandrodjuric | 3:57ccaba4a24d | 17 | uint8_t value = INIT; |
sandrodjuric | 3:57ccaba4a24d | 18 | } |
sandrodjuric | 3:57ccaba4a24d | 19 | |
sandrodjuric | 3:57ccaba4a24d | 20 | int modifyBit(int x, unsigned char position, bool State) |
sandrodjuric | 3:57ccaba4a24d | 21 | { |
sandrodjuric | 3:57ccaba4a24d | 22 | int mask = 1 << position; |
sandrodjuric | 3:57ccaba4a24d | 23 | int state = int(State); |
sandrodjuric | 3:57ccaba4a24d | 24 | return (x & ~mask)|((state << position) & mask); |
sandrodjuric | 4:25e070866cf0 | 25 | } |
sandrodjuric | 4:25e070866cf0 | 26 | |
sandrodjuric | 4:25e070866cf0 | 27 | int lauflicht(bool richtung, int time, int &anz) |
sandrodjuric | 4:25e070866cf0 | 28 | { |
sandrodjuric | 4:25e070866cf0 | 29 | int i; |
sandrodjuric | 4:25e070866cf0 | 30 | uint8_t lauf = 0x01; |
sandrodjuric | 4:25e070866cf0 | 31 | |
sandrodjuric | 4:25e070866cf0 | 32 | if(!richtung) |
sandrodjuric | 4:25e070866cf0 | 33 | lauf = 0x08; |
sandrodjuric | 4:25e070866cf0 | 34 | while(1){ |
sandrodjuric | 4:25e070866cf0 | 35 | nibbleLeds(lauf&0x0F); |
sandrodjuric | 4:25e070866cf0 | 36 | if(richtung){ |
sandrodjuric | 4:25e070866cf0 | 37 | lauf = lauf<<1; |
sandrodjuric | 4:25e070866cf0 | 38 | if(lauf>8) |
sandrodjuric | 4:25e070866cf0 | 39 | lauf=0x01; |
sandrodjuric | 4:25e070866cf0 | 40 | } |
sandrodjuric | 4:25e070866cf0 | 41 | else{ |
sandrodjuric | 4:25e070866cf0 | 42 | lauf=lauf >> 1; |
sandrodjuric | 4:25e070866cf0 | 43 | if(lauf==0) |
sandrodjuric | 4:25e070866cf0 | 44 | lauf=0x08; |
sandrodjuric | 4:25e070866cf0 | 45 | } |
sandrodjuric | 4:25e070866cf0 | 46 | if(button) |
sandrodjuric | 4:25e070866cf0 | 47 | break; |
sandrodjuric | 4:25e070866cf0 | 48 | wait_ms(time); |
sandrodjuric | 4:25e070866cf0 | 49 | anz++; |
sandrodjuric | 4:25e070866cf0 | 50 | } |
sandrodjuric | 4:25e070866cf0 | 51 | return anz; |
sandrodjuric | 5:0b09788019ab | 52 | } |
sandrodjuric | 5:0b09788019ab | 53 | |
sandrodjuric | 5:0b09788019ab | 54 | void printb(uint8_t x) |
sandrodjuric | 5:0b09788019ab | 55 | { |
sandrodjuric | 5:0b09788019ab | 56 | for(int i=sizeof(x)<<3;i;i--) |
sandrodjuric | 5:0b09788019ab | 57 | putchar('0'+((x>>(i-1))&1)); |
sandrodjuric | 5:0b09788019ab | 58 | printf("\n"); |
sandrodjuric | 6:de6ff5fc4192 | 59 | } |
sandrodjuric | 6:de6ff5fc4192 | 60 | |
sandrodjuric | 6:de6ff5fc4192 | 61 | void nibbleLeds(int value) |
sandrodjuric | 6:de6ff5fc4192 | 62 | { |
sandrodjuric | 6:de6ff5fc4192 | 63 | myleds=value%16; |
sandrodjuric | 5:0b09788019ab | 64 | } |