Advertisement
Guest User

Praktikum 5

a guest
Jan 22nd, 2020
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.19 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. #define SIZE 10
  4.  
  5. int convertCharIntToHexBits(int key); //Funktion N1
  6. // Funktion um von ascii in Hex umzuwandeln
  7.  
  8. void setStatus(int& status, int& keybits); //Funktion N2
  9. // Status von LED's werden immer wieder auf 0 gesetzt
  10. //Status von Tasten werden übernommen
  11. //Operatoren werden verwendet
  12.  
  13. void negateBits(int& bits); //Funktion N5
  14. //Einerkomplement bilden
  15.  
  16. const char* verifyManupulation(int status);//Funktion N3
  17. //damit die Hex-Zahlen in x und o's gemacht werden
  18.  
  19. void AusgabeStatus(int output); //Funktion N4
  20. //für die Ausgabe am Ende (mit Tastendruck: ... usw)
  21.  
  22.  
  23.  
  24. int main() {
  25.  
  26. int status = 0x00000000;
  27. int key;
  28. int addedKey[SIZE]; //um mehrere Tasten einzulesen, das ist mein container
  29. int stopReading = 0;
  30. int stopProgram = 0;
  31. 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");
  32. while (1)
  33. {
  34. stopReading = 0;
  35. for (int i = 0; i < SIZE; i++)
  36. {
  37. if (stopReading == 1)
  38. {
  39. break;
  40. }
  41. while (true)
  42. {
  43.  
  44. key = getchar(); // key wird eingelesen und wandelt es in ascii um
  45. if (key != 10) //!=10 weil sonst einfach , also in ascii 10, eingelesen wird
  46. //bugfix - ansonsten wurde die Ausgabe 2x eingelesen
  47. {
  48. if (key == 115)
  49. {
  50. stopReading = 1;
  51. addedKey[i] = 115; //115 = s
  52. break;
  53. }
  54. addedKey[i] = key; //key wird in addedKey eingespeichert, um mehrere Tasten drücken zu können
  55. break;
  56. }
  57. }
  58. }
  59.  
  60. for (int i = 0; i < SIZE; i++)
  61. {
  62.  
  63. if (addedKey[i] == 115) {
  64. break;
  65. }
  66. int keyBits = convertCharIntToHexBits(addedKey[i]); // hier wird der key in die funktion gebracht
  67. //[keyBits = convertierte Version von Key!]
  68.  
  69. switch (addedKey[i]) {
  70. case 48: //0
  71. printf("Mikrocontroller ist resetet...");
  72. status = 0x0;
  73. break;
  74. case 49: //1
  75. setStatus(status, keyBits);
  76. break;
  77. case 50: //2
  78. setStatus(status, keyBits);
  79. break;
  80. case 51: //3
  81. setStatus(status, keyBits);
  82. break;
  83. case 52://4
  84. setStatus(status, keyBits);
  85. break;
  86. case 53://5
  87. setStatus(status, keyBits);
  88. break;
  89. case 54://6
  90. setStatus(status, keyBits);
  91. break;
  92. case 55://7
  93. setStatus(status, keyBits);
  94. break;
  95. case 56://8
  96. setStatus(status, keyBits);
  97. break;
  98. }
  99.  
  100. if (addedKey[i] == 57) { // 57 = 9
  101. stopProgram = 1;
  102. printf("Mikrocontroller ist ausgeschaltet...");
  103. break;
  104. }
  105. }
  106. if (stopProgram == 1)
  107. {
  108. break;
  109. }
  110. AusgabeStatus(status); //siehe unten bei der Funktion AusgabeStatus
  111. 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");
  112. }
  113. printf("\n\nProgramm ist beendet!");
  114. }
  115.  
  116. int convertCharIntToHexBits(int key)
  117. {
  118. int keyBits = 0x00; //keyBits gebe ich zurück
  119. switch (key)//geordneter als if-Anweisungen
  120. {
  121. case 49: keyBits = keyBits | 0x1; //1
  122. break;
  123. case 50: keyBits = keyBits | 0x2; //2
  124. break;
  125. case 51: keyBits = keyBits | 0x4; //3
  126. break;
  127. case 52: keyBits = keyBits | 0x8; //4
  128. break;
  129. case 53: keyBits = keyBits | 0x10; //5
  130. break;
  131. case 54: keyBits = keyBits | 0x20; //6
  132. break;
  133. case 55: keyBits = keyBits | 0x40; //7
  134. break;
  135. case 56: keyBits = keyBits | 0x80; //8
  136. break;
  137. default:
  138. break;
  139. }
  140. return keyBits;
  141. }
  142.  
  143. void setStatus(int& status, int& keyBits) {
  144.  
  145. //Bit 0-7
  146. //Status von LED's werden immer wieder auf 0 gesetzt
  147. //Status von Tasten werden übernommen
  148.  
  149. status = status | keyBits; //Status wird mit derückter Taste geODERt
  150. status = status & 255; // Status der LED's wird mit 1111 1111 geUNDet, damit dieser (für die Rechnung)wieder auf 0 gesetzt
  151. //sonst wird das Ergebnis verfälscht
  152.  
  153. int taste1bis4; //Taste1 wird definiert
  154. taste1bis4 = status & 15; //1111 - 15, damit NUR die Taste 1 gelesen wird und alles vorher auf 0 gesetzt wird, wird geUNDet
  155.  
  156. int taste5bis8 = status & 240; // 1111 0000
  157. taste5bis8 = taste5bis8 >> 4; //weils es von hinten liest
  158.  
  159. //Operatoren werden verwendet
  160.  
  161. //Bit 8-11
  162. int undbits = taste1bis4 & taste5bis8;
  163. undbits = undbits << 8; // 1111 0000 0000 - 8x shiften, damits b´zum UNdBit kommt
  164. status = status | undbits; //Status wird mit geshifteten UNDBits geODERt
  165. //Bit 12-15
  166. int oderbits = taste1bis4 | taste5bis8;
  167. oderbits = oderbits << 12; // 1111 0000 0000 0000
  168. status = status | oderbits;
  169. //Bit 16-19
  170. int xorbits = taste1bis4 ^ taste5bis8;
  171. xorbits = xorbits << 16; // 1111 0000 0000 0000 0000
  172. status = status | xorbits;
  173. //Bit 20-23
  174. int negierteBits = taste1bis4; //die gedrückte Taste wird in negierteBits gespeichert
  175. negateBits(negierteBits); //negierteBits werden negiert
  176. negierteBits = negierteBits << 20; // 1111 0000 0000 0000 0000 0000 um 20 STellen nach links verschoben
  177. status = status | negierteBits;
  178.  
  179. }
  180. void negateBits(int& bits)
  181. {
  182. switch (bits)
  183. {
  184. case 1: bits = 14; //LED 2, 3 und 4 sind gedrückt (1110), weil 1110 ist in Hex 14 bzw in Dez. eigentlich
  185. //1 (0001)negiert = 14 (1110)
  186. break;
  187. case 2: bits = 13;
  188. break;
  189. case 3: bits = 12;
  190. break;
  191. case 4: bits = 11;
  192. break;
  193. case 5: bits = 10;
  194. break;
  195. case 6: bits = 9;
  196. break;
  197. case 7: bits = 8;
  198. break;
  199. case 8: bits = 7;
  200. break;
  201. case 9: bits = 6;
  202. break;
  203. case 10: bits = 5;
  204. break;
  205. case 11: bits = 4;
  206. break;
  207. case 12: bits = 3;
  208. break;
  209. case 13: bits = 2;
  210. break;
  211. case 14: bits = 1; //Taste 1 ist gedrückt (0001)
  212. break;
  213. case 15: bits = 0;
  214. break;
  215. default:
  216. break;
  217. }
  218. }
  219.  
  220.  
  221. void AusgabeStatus(int Ausgabe)
  222. {
  223. //Ausgabe
  224. printf("\n\nTastendruck:\n");
  225. //Bit 0-3
  226. printf("Status der Tasten 1-4:\n");
  227. int bit = Ausgabe;
  228. 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
  229. printf(verifyManupulation(bit));//als x und o ausgeben
  230. //Bit 4-7
  231. printf("Status der Tasten 5-8:\n");
  232. bit = Ausgabe;
  233. bit = bit & 240; //240 = 1111 0000
  234. bit = bit >> 4;
  235. //char* value = verifyManupulation(bit);
  236. printf(verifyManupulation(bit));//als x und o ausgeben
  237. printf("%s", "----------------------------");
  238. printf("%s", "\nStatus der LEDs: \n");
  239. printf("%s", "UND: \n");
  240. bit = Ausgabe;
  241. bit = bit & 3840;//1111 0000 0000
  242. bit = bit >> 8;
  243. printf(verifyManupulation(bit));
  244. printf("%s", "ODER: \n");
  245. bit = Ausgabe;
  246. bit = bit & 61440; //1111 0000 0000 0000
  247. bit = bit >> 12;
  248. printf(verifyManupulation(bit));
  249.  
  250. printf("%s", "EXOR: \n");
  251. bit = Ausgabe;
  252. bit = bit & 983040; //1111 0000 0000 0000 0000
  253. bit = bit >> 16;
  254. printf(verifyManupulation(bit));
  255.  
  256. printf("%s", "Negieren: \n");
  257. bit = Ausgabe;
  258. bit = bit & 15728640; //1111 0000 0000 0000 0000 0000
  259. bit = bit >> 20;
  260. printf(verifyManupulation(bit));
  261.  
  262. }
  263.  
  264.  
  265. const char* verifyManupulation(int status) {//zuerst wird etwas zurückgegeben, dann ausgegeben
  266. const char* value = "0000\n\n";
  267. const char* value1 = "x000\n\n";
  268. const char* value2 = "0x00\n\n";
  269. const char* value3 = "xx00\n\n";
  270. const char* value4 = "00x0\n\n";
  271. const char* value5 = "x0x0\n\n";
  272. const char* value6 = "0xx0\n\n";
  273. const char* value7 = "xxx0\n\n";
  274. const char* value8 = "000x\n\n";
  275. const char* value9 = "x00x\n\n";
  276. const char* value10 = "0x0x\n\n";
  277. const char* value11 = "xx0x\n\n";
  278. const char* value12 = "00xx\n\n";
  279. const char* value13 = "x0xx\n\n";
  280. const char* value14 = "0xxx\n\n";
  281. const char* value15 = "xxxx\n\n";
  282. if (status == 0)
  283. {
  284. return value;
  285. }
  286. if (status == 1)
  287. {
  288. return value1;
  289. }
  290. if (status == 2)
  291. {
  292. return value2;
  293. }
  294. if (status == 3)
  295. {
  296. return value3;
  297. }
  298. if (status == 4)
  299. {
  300. return value4;
  301. }
  302. if (status == 5)
  303. {
  304. return value5;
  305. }
  306. if (status == 6)
  307. {
  308. return value6;
  309. }
  310. if (status == 7)
  311. {
  312. return value7;
  313. }
  314. if (status == 8)
  315. {
  316. return value8;
  317. }
  318. if (status == 9)
  319. {
  320. return value9;
  321. }
  322. if (status == 10)
  323. {
  324. return value10;
  325. }
  326. if (status == 11)
  327. {
  328. return value11;
  329. }
  330. if (status == 12)
  331. {
  332. return value12;
  333. }
  334. if (status == 13)
  335. {
  336. return value13;
  337. }
  338. if (status == 14)
  339. {
  340. return value14;
  341. }
  342. if (status == 15)
  343. {
  344. return value15;
  345. }
  346.  
  347. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement