Advertisement
ddeexxiikk

Untitled

Oct 28th, 2024
14
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.05 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <io.h>
  3. #include <system.h>
  4. #include "alt_types.h"
  5. #include "altera_avalon_pio_regs.h"
  6. #include "sys/alt_irq.h"
  7. #include "sys/alt_timestamp.h"
  8. #include <sys/alt_alarm.h>
  9.  
  10. // Define switch and LED masks
  11. #define SW0 0x00000001
  12. #define SW1 0x00000002
  13. #define SW2 0x00000004
  14. #define SW3 0x00000008
  15. #define SW4 0x00000010
  16. #define SW5 0x00000020
  17. #define SW6 0x00000040
  18. #define SW7 0x00000080
  19. #define SW8 0x00000100
  20. #define SW9 0x00000200
  21.  
  22. #define LED0 0x00000001
  23. #define LED1 0x00000002
  24. #define LED2 0x00000004
  25. #define LED3 0x00000008
  26. #define LED4 0x00000010
  27. #define LED5 0x00000020
  28. #define LED9 0x00000200
  29.  
  30. // Use arrays for cnt and switch states
  31. volatile int cnt[5] = {0}; // s1 to s5
  32. volatile int prev_switches = 0; // Previous states for SW0 to SW9
  33. volatile int prev_buttons = 0;
  34. volatile int in_room = 0;
  35. volatile int leds = 0;
  36.  
  37. // Array of LED masks corresponding to cnt
  38. const int led_masks[5] = {LED1, LED2, LED3, LED4, LED5};
  39.  
  40. // Tablica kodów dla wyświetlaczy 7-segmentowych (dla cyfr 0-9)
  41. const alt_u8 hex_digits[16] = {
  42. 0x3F, // 0
  43. 0x06, // 1
  44. 0x5B, // 2
  45. 0x4F, // 3
  46. 0x66, // 4
  47. 0x6D, // 5
  48. 0x7D, // 6
  49. 0x07, // 7
  50. 0x7F, // 8
  51. 0x6F, // 9
  52. 0x77, // A
  53. 0x7C, // B
  54. 0x39, // C
  55. 0x5E, // D
  56. 0x79, // E
  57. 0x71 // F
  58. };
  59.  
  60. const alt_u8 error_code[5] = {
  61. 0x79, // E
  62. 0x50, // r
  63. 0x50, // r
  64. 0x5C, // o
  65. 0x50 // r
  66. };
  67.  
  68. // Funkcja do aktualizacji wyświetlaczy HEX na podstawie wartości liczników
  69. void update_hex_display()
  70. {
  71.  
  72. for (int i = 0; i < 5; i++)
  73. {
  74. if (cnt[i] < 0)
  75. {
  76. // Jeśli licznik jest ujemny, wyświetlamy "Error"
  77. // alt_u32 error_value = 0;
  78. for (int j = 0; j < 5; j++)
  79. {
  80. // error_value |= (error_code[j] << (j * 8));
  81. IOWR(HEX_BASE, j, error_code[4 - j]);
  82. }
  83. // IOWR(HEX_BASE, 0, error_value);
  84. return;
  85. }
  86. }
  87. alt_u32 hex_value = 0;
  88.  
  89. for (int i = 0; i < 5; i++)
  90. {
  91. alt_u8 digit = cnt[i] & 0xF; // Pobierz najmłodsze 4 bity
  92. IOWR(HEX_BASE, i, hex_digits[digit]);
  93. }
  94. }
  95.  
  96. void handle_pushbuttons_interrupt(void *context, alt_u32 id)
  97. {
  98.  
  99. alt_u32 edge = IORD_ALTERA_AVALON_PIO_EDGE_CAP(PUSHBUTTON_BASE);
  100.  
  101. if (edge & (1 << 1))
  102. { // Sprawdzenie, który bit (przycisk) został ustawiony
  103. if (in_room)
  104. {
  105. cnt[2] += 1; // s3
  106. cnt[3] -= 1; // s4
  107. }
  108. else
  109. {
  110. cnt[2] -= 1; // s3
  111. cnt[3] += 1; // s4
  112. }
  113. }
  114.  
  115. // Wyczyść rejestr edge capture, aby przerwanie nie było powtarzane
  116. IOWR_ALTERA_AVALON_PIO_EDGE_CAP(PUSHBUTTON_BASE, 0);
  117.  
  118. // Update LEDs based on cnt
  119. int leds = 0;
  120. for (int i = 0; i < 5; i++)
  121. {
  122. if (cnt[i] > 0)
  123. {
  124. leds |= led_masks[i];
  125. }
  126. }
  127. if (in_room)
  128. {
  129. leds |= LED9;
  130. }
  131.  
  132. IOWR_ALTERA_AVALON_PIO_DATA(LEDS_BASE, leds);
  133. // update HEX display
  134. update_hex_display();
  135. }
  136.  
  137. /*
  138. void startTimer(int index)
  139. {
  140. if (timerForRoom[index] == 0)
  141. {
  142. timerForRoom[index] = alt_nticks();
  143. }
  144. }
  145.  
  146. void deleteTimer(int index)
  147. {
  148. timerForRoom[index] = 0;
  149. }
  150.  
  151. void checkTimeOut(int index)
  152. {
  153. alt_u32 currTime = alt_nticks();
  154. if ((timerForRoom[index] != 0 ) && (currTime - timerForRoom[index]) >= alt_ticks_per_second() * 2);
  155. {
  156. ledsStatus[index] = 0;
  157. deleteTimer(index);
  158. }
  159. }
  160. */
  161.  
  162. void update_leds()
  163. {
  164. IOWR_ALTERA_AVALON_PIO_DATA(LEDS_BASE, leds);
  165. }
  166.  
  167. void handle_sliders_interrupt(void *context, alt_u32 id)
  168. {
  169. // Read the edge capture register
  170. int edge = IORD_ALTERA_AVALON_PIO_EDGE_CAP(SW_SLIDERS_BASE);
  171.  
  172. // Clear the edge capture register
  173. IOWR_ALTERA_AVALON_PIO_EDGE_CAP(SW_SLIDERS_BASE, edge);
  174.  
  175. // Read current switch states
  176. int switches = IORD_ALTERA_AVALON_PIO_DATA(SW_SLIDERS_BASE);
  177.  
  178. // Determine which switches have changed
  179. int changed = prev_switches ^ switches;
  180.  
  181. // Handle SW0 (in_room)
  182. if (changed & SW0)
  183. {
  184. if (switches & SW0)
  185. in_room = 1; // SW0 turned on
  186. else
  187. in_room = 0; // SW0 turned off
  188. }
  189. // Handle switches SW1 to SW9
  190. for (int i = 1; i <= 9; i++)
  191. {
  192. int mask = 1 << i;
  193. int sw_changed = changed & mask;
  194. int sw_on = switches & mask;
  195. if (sw_changed && sw_on)
  196. {
  197. if (in_room)
  198. {
  199. // Perform increment operations
  200. switch (i)
  201. {
  202. case 1:
  203. cnt[0] += 1; // s1
  204. cnt[4] -= 1;
  205. break;
  206. case 2:
  207. cnt[0] -= 1; // s3
  208. break;
  209. case 3:
  210. cnt[0] -= 1; // s1
  211. break;
  212. case 4:
  213. cnt[4] -= 1; // s2
  214. break;
  215. case 5:
  216. cnt[3] += 1;
  217. break;
  218. case 6:
  219. cnt[0] += 1;
  220. cnt[3] -= 1;
  221. break;
  222. case 7:
  223. cnt[3] -= 1; // s5
  224. break;
  225. case 8:
  226. cnt[3] += 1; // s2
  227. break;
  228. case 9:
  229. cnt[1] += 1;
  230. cnt[2] -= 1;
  231. break;
  232. }
  233. }
  234. else
  235. {
  236. // Perform decrement operations
  237. switch (i)
  238. {
  239. case 1:
  240. cnt[0] -= 1; // s1
  241. cnt[4] += 1;
  242. break;
  243. case 2:
  244. cnt[0] += 1; // s3
  245. break;
  246. case 3:
  247. cnt[0] += 1; // s1
  248. break;
  249. case 4:
  250. cnt[4] += 1; // s2
  251. break;
  252. case 5:
  253. cnt[1] += 1; // s4
  254. cnt[3] -= 1; // s4
  255. break;
  256. case 6:
  257. cnt[0] -= 1;
  258. cnt[3] += 1;
  259. break;
  260. case 7:
  261. cnt[3] += 1; // s5
  262. break;
  263. case 8:
  264. cnt[3] -= 1; // s2
  265. break;
  266. case 9:
  267. cnt[1] -= 1;
  268. cnt[2] += 1;
  269. break;
  270. }
  271. }
  272. }
  273. }
  274.  
  275. // Update the previous switch states
  276. prev_switches = switches;
  277.  
  278. // Update LEDs based on cnt
  279. int leds = 0;
  280.  
  281. for (int i = 0; i < 5; i++)
  282. {
  283. if (cnt[i] < 0)
  284. {
  285. cnt[i] = 0
  286. }
  287. }
  288.  
  289. IOWR_ALTERA_AVALON_PIO_EDGE_CAP(SW_SLIDERS_BASE, 0);
  290. update_hex_display();
  291.  
  292. if (in_room)
  293. {
  294. leds |= LED9;
  295. }
  296. else{
  297. leds &= ~LED9;
  298. }
  299.  
  300. IOWR_ALTERA_AVALON_PIO_DATA(LEDS_BASE, leds);
  301. }
  302.  
  303. int main()
  304. {
  305. // Initialize previous switch states
  306. prev_switches = IORD_ALTERA_AVALON_PIO_DATA(SW_SLIDERS_BASE);
  307.  
  308. // Enable interrupts for all switches
  309. IOWR_ALTERA_AVALON_PIO_IRQ_MASK(SW_SLIDERS_BASE, 0xFFFFFFFF);
  310.  
  311. // Clear any pending interrupts
  312. IOWR_ALTERA_AVALON_PIO_EDGE_CAP(SW_SLIDERS_BASE, 0xFFFFFFFF);
  313.  
  314. // Register the interrupt handler
  315. alt_ic_isr_register(
  316. SW_SLIDERS_IRQ_INTERRUPT_CONTROLLER_ID,
  317. SW_SLIDERS_IRQ,
  318. handle_sliders_interrupt,
  319. NULL,
  320. 0x0);
  321. alt_ic_isr_register(
  322. PUSHBUTTON_IRQ_INTERRUPT_CONTROLLER_ID,
  323. PUSHBUTTON_IRQ,
  324. handle_pushbuttons_interrupt,
  325. NULL,
  326. 0x0);
  327.  
  328. // Enable interrupts
  329. alt_ic_irq_enable(SW_SLIDERS_IRQ_INTERRUPT_CONTROLLER_ID, SW_SLIDERS_IRQ);
  330. alt_ic_irq_enable(PUSHBUTTON_IRQ_INTERRUPT_CONTROLLER_ID, PUSHBUTTON_BASE);
  331.  
  332. int prevCnt[5] = {0};
  333. int t[5] = {0};
  334.  
  335. while(1)
  336. {
  337. for(int i=0; i<5; i++){
  338. //jesli liczba osób w pokoju się zmieniła
  339. if(cnt[i] != prevCnt[i]){
  340. t[i] = alt_nticks();
  341. prevCnt[i] = cnt[i];
  342. }
  343.  
  344. if(t[i] != 0){
  345. if(cnt[i] > 0){
  346. //po 1s zapal led
  347. if((alt_nticks() - t[i]) >= alt_ticks_per_second()){
  348. leds |= led_masks[i];
  349. t[i] = 0;
  350. }
  351. }
  352. else{
  353. //zgas po 2s
  354. if((alt_nticks() - t[i]) >= alt_ticks_per_second() * 2){
  355. leds &= ~led_masks[i];
  356. }
  357. }
  358.  
  359. }
  360. }
  361. update_leds();
  362. }
  363. return 0;
  364. }
  365.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement