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