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