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