Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #define SIZE 10
- int convertCharIntToHexBits(int key); //Funktion N1
- // Funktion um von ascii in Hex umzuwandeln
- void setStatus(int& status, int& keybits); //Funktion N2
- // Status von LED's werden immer wieder auf 0 gesetzt
- //Status von Tasten werden übernommen
- //Operatoren werden verwendet
- void negateBits(int& bits); //Funktion N5
- //Einerkomplement bilden
- const char* verifyManupulation(int status);//Funktion N3
- //damit die Hex-Zahlen in x und o's gemacht werden
- void AusgabeStatus(int output); //Funktion N4
- //für die Ausgabe am Ende (mit Tastendruck: ... usw)
- int main() {
- int status = 0x00000000;
- int key;
- int addedKey[SIZE]; //um mehrere Tasten einzulesen, das ist mein container
- int stopReading = 0;
- int stopProgram = 0;
- printf("%s","Druecken Sie eine Taste von 1-8, um den Status der LED's bzw. der Tasten zu ändern;\n0=reset; 9=beenden:\ns=Beendung der Tasteneingabe\n");
- while (1)
- {
- stopReading = 0;
- for (int i = 0; i < SIZE; i++)
- {
- if (stopReading == 1)
- {
- break;
- }
- while (true)
- {
- key = getchar(); // key wird eingelesen und wandelt es in ascii um
- if (key != 10) //!=10 weil sonst einfach , also in ascii 10, eingelesen wird
- //bugfix - ansonsten wurde die Ausgabe 2x eingelesen
- {
- if (key == 115)
- {
- stopReading = 1;
- addedKey[i] = 115; //115 = s
- break;
- }
- addedKey[i] = key; //key wird in addedKey eingespeichert, um mehrere Tasten drücken zu können
- break;
- }
- }
- }
- for (int i = 0; i < SIZE; i++)
- {
- if (addedKey[i] == 115) {
- break;
- }
- int keyBits = convertCharIntToHexBits(addedKey[i]); // hier wird der key in die funktion gebracht
- //[keyBits = convertierte Version von Key!]
- switch (addedKey[i]) {
- case 48: //0
- printf("Mikrocontroller ist resetet...");
- status = 0x0;
- break;
- case 49: //1
- setStatus(status, keyBits);
- break;
- case 50: //2
- setStatus(status, keyBits);
- break;
- case 51: //3
- setStatus(status, keyBits);
- break;
- case 52://4
- setStatus(status, keyBits);
- break;
- case 53://5
- setStatus(status, keyBits);
- break;
- case 54://6
- setStatus(status, keyBits);
- break;
- case 55://7
- setStatus(status, keyBits);
- break;
- case 56://8
- setStatus(status, keyBits);
- break;
- }
- if (addedKey[i] == 57) { // 57 = 9
- stopProgram = 1;
- printf("Mikrocontroller ist ausgeschaltet...");
- break;
- }
- }
- if (stopProgram == 1)
- {
- break;
- }
- AusgabeStatus(status); //siehe unten bei der Funktion AusgabeStatus
- printf("\n\nDruecken Sie eine Taste von 1-8, um den Status der LED's bzw. der Tasten zu ändern;\n0=reset; 9=beenden:\ns=Beendung der Tasteneingabe\n");
- }
- printf("\n\nProgramm ist beendet!");
- }
- int convertCharIntToHexBits(int key)
- {
- int keyBits = 0x00; //keyBits gebe ich zurück
- switch (key)//geordneter als if-Anweisungen
- {
- case 49: keyBits = keyBits | 0x1; //1
- break;
- case 50: keyBits = keyBits | 0x2; //2
- break;
- case 51: keyBits = keyBits | 0x4; //3
- break;
- case 52: keyBits = keyBits | 0x8; //4
- break;
- case 53: keyBits = keyBits | 0x10; //5
- break;
- case 54: keyBits = keyBits | 0x20; //6
- break;
- case 55: keyBits = keyBits | 0x40; //7
- break;
- case 56: keyBits = keyBits | 0x80; //8
- break;
- default:
- break;
- }
- return keyBits;
- }
- void setStatus(int& status, int& keyBits) {
- //Bit 0-7
- //Status von LED's werden immer wieder auf 0 gesetzt
- //Status von Tasten werden übernommen
- status = status | keyBits; //Status wird mit derückter Taste geODERt
- status = status & 255; // Status der LED's wird mit 1111 1111 geUNDet, damit dieser (für die Rechnung)wieder auf 0 gesetzt
- //sonst wird das Ergebnis verfälscht
- int taste1bis4; //Taste1 wird definiert
- taste1bis4 = status & 15; //1111 - 15, damit NUR die Taste 1 gelesen wird und alles vorher auf 0 gesetzt wird, wird geUNDet
- int taste5bis8 = status & 240; // 1111 0000
- taste5bis8 = taste5bis8 >> 4; //weils es von hinten liest
- //Operatoren werden verwendet
- //Bit 8-11
- int undbits = taste1bis4 & taste5bis8;
- undbits = undbits << 8; // 1111 0000 0000 - 8x shiften, damits b´zum UNdBit kommt
- status = status | undbits; //Status wird mit geshifteten UNDBits geODERt
- //Bit 12-15
- int oderbits = taste1bis4 | taste5bis8;
- oderbits = oderbits << 12; // 1111 0000 0000 0000
- status = status | oderbits;
- //Bit 16-19
- int xorbits = taste1bis4 ^ taste5bis8;
- xorbits = xorbits << 16; // 1111 0000 0000 0000 0000
- status = status | xorbits;
- //Bit 20-23
- int negierteBits = taste1bis4; //die gedrückte Taste wird in negierteBits gespeichert
- negateBits(negierteBits); //negierteBits werden negiert
- negierteBits = negierteBits << 20; // 1111 0000 0000 0000 0000 0000 um 20 STellen nach links verschoben
- status = status | negierteBits;
- }
- void negateBits(int& bits)
- {
- switch (bits)
- {
- case 1: bits = 14; //LED 2, 3 und 4 sind gedrückt (1110), weil 1110 ist in Hex 14 bzw in Dez. eigentlich
- //1 (0001)negiert = 14 (1110)
- break;
- case 2: bits = 13;
- break;
- case 3: bits = 12;
- break;
- case 4: bits = 11;
- break;
- case 5: bits = 10;
- break;
- case 6: bits = 9;
- break;
- case 7: bits = 8;
- break;
- case 8: bits = 7;
- break;
- case 9: bits = 6;
- break;
- case 10: bits = 5;
- break;
- case 11: bits = 4;
- break;
- case 12: bits = 3;
- break;
- case 13: bits = 2;
- break;
- case 14: bits = 1; //Taste 1 ist gedrückt (0001)
- break;
- case 15: bits = 0;
- break;
- default:
- break;
- }
- }
- void AusgabeStatus(int Ausgabe)
- {
- //Ausgabe
- printf("\n\nTastendruck:\n");
- //Bit 0-3
- printf("Status der Tasten 1-4:\n");
- int bit = Ausgabe;
- bit = bit & 15; //Beispiel: 0100 0001 0010 0100 0100 0101 mit 15 (0000 0000 0000 0000 0000 1111) verUNDet damit auf Taste 1-4 zugegriffen wird
- printf(verifyManupulation(bit));//als x und o ausgeben
- //Bit 4-7
- printf("Status der Tasten 5-8:\n");
- bit = Ausgabe;
- bit = bit & 240; //240 = 1111 0000
- bit = bit >> 4;
- //char* value = verifyManupulation(bit);
- printf(verifyManupulation(bit));//als x und o ausgeben
- printf("%s", "----------------------------");
- printf("%s", "\nStatus der LEDs: \n");
- printf("%s", "UND: \n");
- bit = Ausgabe;
- bit = bit & 3840;//1111 0000 0000
- bit = bit >> 8;
- printf(verifyManupulation(bit));
- printf("%s", "ODER: \n");
- bit = Ausgabe;
- bit = bit & 61440; //1111 0000 0000 0000
- bit = bit >> 12;
- printf(verifyManupulation(bit));
- printf("%s", "EXOR: \n");
- bit = Ausgabe;
- bit = bit & 983040; //1111 0000 0000 0000 0000
- bit = bit >> 16;
- printf(verifyManupulation(bit));
- printf("%s", "Negieren: \n");
- bit = Ausgabe;
- bit = bit & 15728640; //1111 0000 0000 0000 0000 0000
- bit = bit >> 20;
- printf(verifyManupulation(bit));
- }
- const char* verifyManupulation(int status) {//zuerst wird etwas zurückgegeben, dann ausgegeben
- const char* value = "0000\n\n";
- const char* value1 = "x000\n\n";
- const char* value2 = "0x00\n\n";
- const char* value3 = "xx00\n\n";
- const char* value4 = "00x0\n\n";
- const char* value5 = "x0x0\n\n";
- const char* value6 = "0xx0\n\n";
- const char* value7 = "xxx0\n\n";
- const char* value8 = "000x\n\n";
- const char* value9 = "x00x\n\n";
- const char* value10 = "0x0x\n\n";
- const char* value11 = "xx0x\n\n";
- const char* value12 = "00xx\n\n";
- const char* value13 = "x0xx\n\n";
- const char* value14 = "0xxx\n\n";
- const char* value15 = "xxxx\n\n";
- if (status == 0)
- {
- return value;
- }
- if (status == 1)
- {
- return value1;
- }
- if (status == 2)
- {
- return value2;
- }
- if (status == 3)
- {
- return value3;
- }
- if (status == 4)
- {
- return value4;
- }
- if (status == 5)
- {
- return value5;
- }
- if (status == 6)
- {
- return value6;
- }
- if (status == 7)
- {
- return value7;
- }
- if (status == 8)
- {
- return value8;
- }
- if (status == 9)
- {
- return value9;
- }
- if (status == 10)
- {
- return value10;
- }
- if (status == 11)
- {
- return value11;
- }
- if (status == 12)
- {
- return value12;
- }
- if (status == 13)
- {
- return value13;
- }
- if (status == 14)
- {
- return value14;
- }
- if (status == 15)
- {
- return value15;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement