Advertisement
Guest User

Untitled

a guest
Jan 12th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.52 KB | None | 0 0
  1. /*
  2. * "Hello World" example.
  3. *
  4. * This example prints 'Hello from Nios II' to the STDOUT stream. It runs on
  5. * the Nios II 'standard', 'full_featured', 'fast', and 'low_cost' example
  6. * designs. It runs with or without the MicroC/OS-II RTOS and requires a STDOUT
  7. * device in your system's hardware.
  8. * The memory footprint of this hosted application is ~69 kbytes by default
  9. * using the standard reference design.
  10. *
  11. * For a reduced footprint version of this template, and an explanation of how
  12. * to reduce the memory footprint for a given application, see the
  13. * "small_hello_world" template.
  14. *
  15. */
  16.  
  17. #include <stdio.h>
  18. #include "system.h"
  19. #include <math.h>
  20. #include <stdlib.h>
  21. #include <time.h>
  22. #include "altera_avalon_pio_regs.h"
  23.  
  24. int cont(int arr[], int n, int x);
  25. void CreateLED(int count);
  26. int BitCount(unsigned int u);
  27. long double diffclock(clock_t clock1, clock_t clock2);
  28. float MittelWert(float value[], int count);
  29. void CreateLED1(int count);
  30. void clrex();
  31. struct ranking
  32. {
  33. int index;
  34. char name[64];
  35. float time[1];
  36. };
  37.  
  38.  
  39. struct ranking table[2];
  40.  
  41. int main()
  42. {
  43. //char dbug[2];
  44. printf("Hallo Willkommen zu diesem Shit!\n");
  45. // Integer for loop for 3 Persons
  46. int person = 0;
  47. for(person = 0; person < 3; person++)
  48. {
  49. // Spiel 1
  50. table[person].index = person;
  51. printf("Wie ist dein name?"); scanf("%s", table[person].name);
  52.  
  53. printf("Willkomen %s.\n Dieses Nios II System beinhaltet 2 Spiele mit denen die Reaktionszeit gemessen wird.\n", table[person].name);
  54.  
  55.  
  56. int spiel1count = 0;
  57. float score[3];
  58. for(spiel1count = 0; spiel1count < 3; spiel1count++)
  59. {
  60.  
  61. printf("Drücke eine taste um zu starten außer Enter");
  62.  
  63. char s[1];
  64. scanf("%c", s);
  65.  
  66.  
  67.  
  68. CreateLED(5);
  69. // scanf("%s",dbug);
  70. clock_t start = clock();
  71. while(IORD_ALTERA_AVALON_PIO_DATA(LEDR_BASE))
  72. {
  73.  
  74. clock_t between = clock();
  75. long double a = diffclock(between, start);
  76. if(a > 3000)
  77. break;
  78. // Hier noch verbleibende Zeit auf 7 Segment ausgeben.
  79. int led=IORD_ALTERA_AVALON_PIO_DATA(LEDR_BASE);
  80. int sw=IORD_ALTERA_AVALON_PIO_DATA(SWITCHES_BASE);
  81. led=(led) & ~(sw);
  82. IOWR_ALTERA_AVALON_PIO_DATA(LEDR_BASE,led);
  83. }
  84.  
  85. clock_t end = clock();
  86. printf("%Lf \n", diffclock(end, start));
  87. if(diffclock(end, start) > 3000)
  88. {
  89.  
  90. }
  91. int led1=IORD_ALTERA_AVALON_PIO_DATA(LEDR_BASE);
  92. if(BitCount(led1) > 0)
  93. {
  94. // Berechnung
  95. score[spiel1count] = (5 - BitCount(led1)) / 3;
  96. }
  97. else if(BitCount(led1) == 0)
  98. {
  99. score[spiel1count] = 5/3;
  100. }
  101. printf("Spiel %d beendet. \n Bitte schalte alle Switches wieder aus.", spiel1count);
  102.  
  103. }
  104. table[person].time[0] = MittelWert(score, 3);
  105.  
  106. // Spiel 2
  107.  
  108. int spiel2count = 0;
  109. float score2[10];
  110. printf("Drücke eine taste um zu starten außer Enter");
  111. char s1[1];
  112. scanf("%c", s1);
  113.  
  114. for(spiel2count = 0; spiel2count < 10; spiel2count++)
  115. {
  116. CreateLED1(1);
  117. clock_t start1 = clock();
  118. while(IORD_ALTERA_AVALON_PIO_DATA(LEDR_BASE))
  119. {
  120. // Hier noch verbleibende Zeit auf 7 Segment ausgeben.
  121. int led=IORD_ALTERA_AVALON_PIO_DATA(LEDR_BASE);
  122. int sw=IORD_ALTERA_AVALON_PIO_DATA(SWITCHES_BASE);
  123. led=(led) & ~(sw);
  124. IOWR_ALTERA_AVALON_PIO_DATA(LEDR_BASE,led);
  125. }
  126.  
  127. clock_t end1 = clock();
  128. printf("%Lf \n", diffclock(end1, start1));
  129.  
  130.  
  131. score2[spiel2count] = diffclock(end1, start1);
  132. printf("Spiel %d beendet", spiel2count);
  133. }
  134.  
  135. table[person].time[1] = MittelWert(score2, 10);
  136.  
  137.  
  138.  
  139. }
  140.  
  141. printf("Programm fertig!!!");
  142.  
  143. return 0;
  144. }
  145.  
  146. int cont(int arr[], int n, int x)
  147. {
  148. while (n--)
  149. {
  150. if (arr[n] == x) return 1;
  151. }
  152. return 0;
  153. }
  154.  
  155. void CreateLED(int count)
  156. {
  157. int exsp[count];
  158. srand(time(NULL));
  159. int i = 0;
  160. for (i = 0; i < count; i++)
  161. {
  162. do {
  163. exsp[i] = rand() % 18;
  164.  
  165. } while (cont(exsp, i, exsp[i]));
  166. }
  167.  
  168.  
  169. float dez=0;
  170. int n = 0;
  171.  
  172. for(n = 0; n < count; n++)
  173. {
  174. printf("%d\n",exsp[n]);
  175. dez += pow(2, exsp[n]);
  176. }
  177.  
  178. IOWR_ALTERA_AVALON_PIO_DATA(LEDR_BASE, (int)dez);
  179. }
  180. int exsp1[10];
  181. void clrex()
  182. {
  183. int i = 0;
  184. for(i = 0; i < sizeof(exsp1); i++)
  185. {
  186. exsp1[i] = 0;
  187. }
  188. }
  189.  
  190.  
  191. int b = 0;
  192. void CreateLED1(int count)
  193. {
  194. srand(time(NULL));
  195. int i = 0;
  196. for (i = 0; i < count; i++)
  197. {
  198. do {
  199. exsp1[b] = rand() % 18;
  200.  
  201. } while (cont(exsp1, b, exsp1[b]));
  202. }
  203.  
  204.  
  205. float dez=0;
  206. int n = 0;
  207.  
  208. for(n = 0; n < count; n++)
  209. {
  210. printf("%d\n",exsp1[n]);
  211. dez += pow(2, exsp1[n]);
  212. }
  213. b++;
  214. IOWR_ALTERA_AVALON_PIO_DATA(LEDR_BASE, (int)dez);
  215. }
  216.  
  217.  
  218. //Counts 1's in a unsigned int
  219. int BitCount(unsigned int u)
  220. {
  221. int count = 0;
  222. int i = 0;
  223. for(i = 0; i < 18; i++)
  224. {
  225. if (u % 2 == 1)
  226. {
  227. count++;
  228. }
  229. u = u >> 1;
  230. }
  231. return count;
  232. }
  233.  
  234. // Diffrence between Clocks in Milliseconds
  235. long double diffclock(clock_t clock1, clock_t clock2)
  236. {
  237. double diffticks = clock1 - clock2;
  238. double diffms = (diffticks) / (CLOCKS_PER_SEC / 1000);
  239. return diffms;
  240. }
  241.  
  242. float MittelWert(float value[], int count)
  243. {
  244. float t = 0;
  245. int i = 0;
  246. for(i = 0; i < count; i++)
  247. {
  248. t += value[i];
  249. }
  250.  
  251. return t / count;
  252. }
  253.  
  254. void sevenseg_set_hex(int hex, char segment[])
  255. {
  256. static alt_u8 segments[16] = {
  257. 0x81, 0xCF, 0x92, 0x86, 0xCC, 0xA4, 0xA0, 0x8F, 0x80, 0x84, /* 0-9 */
  258. 0x88, 0xE0, 0xF2, 0xC2, 0xB0, 0xB8 }; /* a-f */
  259.  
  260. unsigned int data = segments[hex & 15] | (segments[(hex >> 4) & 15] << 8);
  261. IOWR_ALTERA_AVALON_PIO_DATA(segment, data);
  262. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement