Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <io.h>
- #include <system.h>
- #include "alt_types.h"
- #include "altera_avalon_pio_regs.h"
- #include "sys/alt_irq.h"
- #include "sys/alt_timestamp.h"
- #include <sys/alt_alarm.h>
- // Define switch and LED masks
- #define SW0 0x00000001
- #define SW1 0x00000002
- #define SW2 0x00000004
- #define SW3 0x00000008
- #define SW4 0x00000010
- #define SW5 0x00000020
- #define SW6 0x00000040
- #define SW7 0x00000080
- #define SW8 0x00000100
- #define SW9 0x00000200
- #define LED0 0x00000001
- #define LED1 0x00000002
- #define LED2 0x00000004
- #define LED3 0x00000008
- #define LED4 0x00000010
- #define LED5 0x00000020
- #define LED9 0x00000200
- // Use arrays for cnt and switch states
- volatile int cnt[5] = {0}; // s1 to s5
- volatile int prev_switches = 0; // Previous states for SW0 to SW9
- volatile int prev_buttons = 0;
- volatile int in_room = 0;
- volatile int leds = 0;
- // Array of LED masks corresponding to cnt
- const int led_masks[5] = {LED1, LED2, LED3, LED4, LED5};
- // Tablica kodów dla wyświetlaczy 7-segmentowych (dla cyfr 0-9)
- const alt_u8 hex_digits[16] = {
- 0x3F, // 0
- 0x06, // 1
- 0x5B, // 2
- 0x4F, // 3
- 0x66, // 4
- 0x6D, // 5
- 0x7D, // 6
- 0x07, // 7
- 0x7F, // 8
- 0x6F, // 9
- 0x77, // A
- 0x7C, // B
- 0x39, // C
- 0x5E, // D
- 0x79, // E
- 0x71 // F
- };
- const alt_u8 error_code[5] = {
- 0x79, // E
- 0x50, // r
- 0x50, // r
- 0x5C, // o
- 0x50 // r
- };
- // Funkcja do aktualizacji wyświetlaczy HEX na podstawie wartości liczników
- void update_hex_display()
- {
- for (int i = 0; i < 5; i++)
- {
- if (cnt[i] < 0)
- {
- // Jeśli licznik jest ujemny, wyświetlamy "Error"
- // alt_u32 error_value = 0;
- for (int j = 0; j < 5; j++)
- {
- // error_value |= (error_code[j] << (j * 8));
- IOWR(HEX_BASE, j, error_code[4 - j]);
- }
- // IOWR(HEX_BASE, 0, error_value);
- return;
- }
- }
- alt_u32 hex_value = 0;
- for (int i = 0; i < 5; i++)
- {
- alt_u8 digit = cnt[i] & 0xF; // Pobierz najmłodsze 4 bity
- IOWR(HEX_BASE, i, hex_digits[digit]);
- }
- }
- void handle_pushbuttons_interrupt(void *context, alt_u32 id)
- {
- alt_u32 edge = IORD_ALTERA_AVALON_PIO_EDGE_CAP(PUSHBUTTON_BASE);
- if (edge & (1 << 1))
- { // Sprawdzenie, który bit (przycisk) został ustawiony
- if (in_room)
- {
- cnt[2] += 1; // s3
- cnt[3] -= 1; // s4
- }
- else
- {
- cnt[2] -= 1; // s3
- cnt[3] += 1; // s4
- }
- }
- // Wyczyść rejestr edge capture, aby przerwanie nie było powtarzane
- IOWR_ALTERA_AVALON_PIO_EDGE_CAP(PUSHBUTTON_BASE, 0);
- // Update LEDs based on cnt
- int leds = 0;
- for (int i = 0; i < 5; i++)
- {
- if (cnt[i] > 0)
- {
- leds |= led_masks[i];
- }
- }
- if (in_room)
- {
- leds |= LED9;
- }
- IOWR_ALTERA_AVALON_PIO_DATA(LEDS_BASE, leds);
- // update HEX display
- update_hex_display();
- }
- /*
- void startTimer(int index)
- {
- if (timerForRoom[index] == 0)
- {
- timerForRoom[index] = alt_nticks();
- }
- }
- void deleteTimer(int index)
- {
- timerForRoom[index] = 0;
- }
- void checkTimeOut(int index)
- {
- alt_u32 currTime = alt_nticks();
- if ((timerForRoom[index] != 0 ) && (currTime - timerForRoom[index]) >= alt_ticks_per_second() * 2);
- {
- ledsStatus[index] = 0;
- deleteTimer(index);
- }
- }
- */
- void update_leds()
- {
- IOWR_ALTERA_AVALON_PIO_DATA(LEDS_BASE, leds);
- }
- void handle_sliders_interrupt(void *context, alt_u32 id)
- {
- // Read the edge capture register
- int edge = IORD_ALTERA_AVALON_PIO_EDGE_CAP(SW_SLIDERS_BASE);
- // Clear the edge capture register
- IOWR_ALTERA_AVALON_PIO_EDGE_CAP(SW_SLIDERS_BASE, edge);
- // Read current switch states
- int switches = IORD_ALTERA_AVALON_PIO_DATA(SW_SLIDERS_BASE);
- // Determine which switches have changed
- int changed = prev_switches ^ switches;
- // Handle SW0 (in_room)
- if (changed & SW0)
- {
- if (switches & SW0)
- in_room = 1; // SW0 turned on
- else
- in_room = 0; // SW0 turned off
- }
- // Handle switches SW1 to SW9
- for (int i = 1; i <= 9; i++)
- {
- int mask = 1 << i;
- int sw_changed = changed & mask;
- int sw_on = switches & mask;
- if (sw_changed && sw_on)
- {
- if (in_room)
- {
- // Perform increment operations
- switch (i)
- {
- case 1:
- cnt[0] += 1; // s1
- cnt[4] -= 1;
- break;
- case 2:
- cnt[0] -= 1; // s3
- break;
- case 3:
- cnt[0] -= 1; // s1
- break;
- case 4:
- cnt[4] -= 1; // s2
- break;
- case 5:
- cnt[3] += 1;
- break;
- case 6:
- cnt[0] += 1;
- cnt[3] -= 1;
- break;
- case 7:
- cnt[3] -= 1; // s5
- break;
- case 8:
- cnt[3] += 1; // s2
- break;
- case 9:
- cnt[1] += 1;
- cnt[2] -= 1;
- break;
- }
- }
- else
- {
- // Perform decrement operations
- switch (i)
- {
- case 1:
- cnt[0] -= 1; // s1
- cnt[4] += 1;
- break;
- case 2:
- cnt[0] += 1; // s3
- break;
- case 3:
- cnt[0] += 1; // s1
- break;
- case 4:
- cnt[4] += 1; // s2
- break;
- case 5:
- cnt[1] += 1; // s4
- cnt[3] -= 1; // s4
- break;
- case 6:
- cnt[0] -= 1;
- cnt[3] += 1;
- break;
- case 7:
- cnt[3] += 1; // s5
- break;
- case 8:
- cnt[3] -= 1; // s2
- break;
- case 9:
- cnt[1] -= 1;
- cnt[2] += 1;
- break;
- }
- }
- }
- }
- // Update the previous switch states
- prev_switches = switches;
- // Update LEDs based on cnt
- int leds = 0;
- for (int i = 0; i < 5; i++)
- {
- if (cnt[i] < 0)
- {
- cnt[i] = 0
- }
- }
- IOWR_ALTERA_AVALON_PIO_EDGE_CAP(SW_SLIDERS_BASE, 0);
- update_hex_display();
- if (in_room)
- {
- leds |= LED9;
- }
- else{
- leds &= ~LED9;
- }
- IOWR_ALTERA_AVALON_PIO_DATA(LEDS_BASE, leds);
- }
- int main()
- {
- // Initialize previous switch states
- prev_switches = IORD_ALTERA_AVALON_PIO_DATA(SW_SLIDERS_BASE);
- // Enable interrupts for all switches
- IOWR_ALTERA_AVALON_PIO_IRQ_MASK(SW_SLIDERS_BASE, 0xFFFFFFFF);
- // Clear any pending interrupts
- IOWR_ALTERA_AVALON_PIO_EDGE_CAP(SW_SLIDERS_BASE, 0xFFFFFFFF);
- // Register the interrupt handler
- alt_ic_isr_register(
- SW_SLIDERS_IRQ_INTERRUPT_CONTROLLER_ID,
- SW_SLIDERS_IRQ,
- handle_sliders_interrupt,
- NULL,
- 0x0);
- alt_ic_isr_register(
- PUSHBUTTON_IRQ_INTERRUPT_CONTROLLER_ID,
- PUSHBUTTON_IRQ,
- handle_pushbuttons_interrupt,
- NULL,
- 0x0);
- // Enable interrupts
- alt_ic_irq_enable(SW_SLIDERS_IRQ_INTERRUPT_CONTROLLER_ID, SW_SLIDERS_IRQ);
- alt_ic_irq_enable(PUSHBUTTON_IRQ_INTERRUPT_CONTROLLER_ID, PUSHBUTTON_BASE);
- int prevCnt[5] = {0};
- int t[5] = {0};
- while(1)
- {
- for(int i=0; i<5; i++){
- //jesli liczba osób w pokoju się zmieniła
- if(cnt[i] != prevCnt[i]){
- t[i] = alt_nticks();
- prevCnt[i] = cnt[i];
- }
- if(t[i] != 0){
- if(cnt[i] > 0){
- //po 1s zapal led
- if((alt_nticks() - t[i]) >= alt_ticks_per_second()){
- leds |= led_masks[i];
- t[i] = 0;
- }
- }
- else{
- //zgas po 2s
- if((alt_nticks() - t[i]) >= alt_ticks_per_second() * 2){
- leds &= ~led_masks[i];
- }
- }
- }
- }
- update_leds();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement