Sandro Djuric
/
Test1_SandroDjuric
funktionen
main.cpp@8:2637f87eccde, 2020-01-13 (annotated)
- Committer:
- sandrodjuric
- Date:
- Mon Jan 13 17:24:31 2020 +0000
- Revision:
- 8:2637f87eccde
- Parent:
- 7:39a959781934
- Child:
- 9:6b36cc59d72c
main
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 | 8:2637f87eccde | 3 | #define BUTTON1 A1 |
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 | 7:39a959781934 | 18 | |
sandrodjuric | 7:39a959781934 | 19 | anzahl= lauflicht(true, 400, anz=0); |
sandrodjuric | 7:39a959781934 | 20 | printf("anzahl= %d\n", anzahl); |
sandrodjuric | 7:39a959781934 | 21 | |
sandrodjuric | 7:39a959781934 | 22 | nibbleLeds(value); |
sandrodjuric | 7:39a959781934 | 23 | printb(value); |
sandrodjuric | 7:39a959781934 | 24 | value=modifyBit(value,2,1); |
sandrodjuric | 7:39a959781934 | 25 | printb(myleds); |
sandrodjuric | 7:39a959781934 | 26 | printb(modifyBit(INIT,3,1)); |
sandrodjuric | 7:39a959781934 | 27 | wait(0.1); |
sandrodjuric | 3:57ccaba4a24d | 28 | } |
sandrodjuric | 3:57ccaba4a24d | 29 | |
sandrodjuric | 3:57ccaba4a24d | 30 | int modifyBit(int x, unsigned char position, bool State) |
sandrodjuric | 3:57ccaba4a24d | 31 | { |
sandrodjuric | 3:57ccaba4a24d | 32 | int mask = 1 << position; |
sandrodjuric | 3:57ccaba4a24d | 33 | int state = int(State); |
sandrodjuric | 3:57ccaba4a24d | 34 | return (x & ~mask)|((state << position) & mask); |
sandrodjuric | 4:25e070866cf0 | 35 | } |
sandrodjuric | 4:25e070866cf0 | 36 | |
sandrodjuric | 4:25e070866cf0 | 37 | int lauflicht(bool richtung, int time, int &anz) |
sandrodjuric | 4:25e070866cf0 | 38 | { |
sandrodjuric | 4:25e070866cf0 | 39 | int i; |
sandrodjuric | 4:25e070866cf0 | 40 | uint8_t lauf = 0x01; |
sandrodjuric | 4:25e070866cf0 | 41 | |
sandrodjuric | 4:25e070866cf0 | 42 | if(!richtung) |
sandrodjuric | 4:25e070866cf0 | 43 | lauf = 0x08; |
sandrodjuric | 4:25e070866cf0 | 44 | while(1){ |
sandrodjuric | 4:25e070866cf0 | 45 | nibbleLeds(lauf&0x0F); |
sandrodjuric | 4:25e070866cf0 | 46 | if(richtung){ |
sandrodjuric | 4:25e070866cf0 | 47 | lauf = lauf<<1; |
sandrodjuric | 4:25e070866cf0 | 48 | if(lauf>8) |
sandrodjuric | 4:25e070866cf0 | 49 | lauf=0x01; |
sandrodjuric | 4:25e070866cf0 | 50 | } |
sandrodjuric | 4:25e070866cf0 | 51 | else{ |
sandrodjuric | 4:25e070866cf0 | 52 | lauf=lauf >> 1; |
sandrodjuric | 4:25e070866cf0 | 53 | if(lauf==0) |
sandrodjuric | 4:25e070866cf0 | 54 | lauf=0x08; |
sandrodjuric | 4:25e070866cf0 | 55 | } |
sandrodjuric | 4:25e070866cf0 | 56 | if(button) |
sandrodjuric | 4:25e070866cf0 | 57 | break; |
sandrodjuric | 4:25e070866cf0 | 58 | wait_ms(time); |
sandrodjuric | 4:25e070866cf0 | 59 | anz++; |
sandrodjuric | 4:25e070866cf0 | 60 | } |
sandrodjuric | 4:25e070866cf0 | 61 | return anz; |
sandrodjuric | 5:0b09788019ab | 62 | } |
sandrodjuric | 5:0b09788019ab | 63 | |
sandrodjuric | 5:0b09788019ab | 64 | void printb(uint8_t x) |
sandrodjuric | 5:0b09788019ab | 65 | { |
sandrodjuric | 5:0b09788019ab | 66 | for(int i=sizeof(x)<<3;i;i--) |
sandrodjuric | 5:0b09788019ab | 67 | putchar('0'+((x>>(i-1))&1)); |
sandrodjuric | 5:0b09788019ab | 68 | printf("\n"); |
sandrodjuric | 6:de6ff5fc4192 | 69 | } |
sandrodjuric | 6:de6ff5fc4192 | 70 | |
sandrodjuric | 6:de6ff5fc4192 | 71 | void nibbleLeds(int value) |
sandrodjuric | 6:de6ff5fc4192 | 72 | { |
sandrodjuric | 6:de6ff5fc4192 | 73 | myleds=value%16; |
sandrodjuric | 5:0b09788019ab | 74 | } |