Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <system.h>
- #include "altera_up_avalon_parallel_port.h"
- #include "sys/alt_irq.h"
- #include <sys/alt_alarm.h>
- #define LED0 0x00000001
- #define LED1 0x00000002
- #define LED2 0x00000004
- #define LED3 0x00000008
- #define LED4 0x00000010
- #define LED5 0x00000020
- #define LED6 0x00000040
- #define LED7 0x00000080
- #define LED8 0x00000100
- #define LED9 0x00000200
- #define LED10 0x00000400
- #define LED11 0x00000800
- #define LED12 0x00001000
- #define LED13 0x00002000
- #define LED14 0x00004000
- #define LED15 0x00008000
- #define LED16 0x00010000
- #define LED17 0x00020000
- #define SEGA 0x01
- #define SEGB 0x02
- #define SEGC 0x04
- #define SEGD 0x08
- #define SEGE 0x10
- #define SEGF 0x20
- #define SEGG 0x40
- #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 SW10 0x00000400
- #define SW11 0x00000800
- #define SW12 0x00001000
- #define SW13 0x00002000
- #define SW14 0x00004000
- #define SW15 0x00008000
- #define SW16 0x00010000
- #define SW17 0x00020000
- #define KEY1 0x00000002
- #define KEY2 0x00000004
- #define KEY3 0x00000008
- struct alt_up_dev
- {
- alt_up_parallel_port_dev *uchwytSW;
- alt_up_parallel_port_dev *uchwytLEDG;
- alt_up_parallel_port_dev *uchwytLEDR;
- alt_up_parallel_port_dev *uchwytHEX3;
- alt_up_parallel_port_dev *uchwytHEX7;
- };
- int NumberTo7SEG(int number)
- {
- int cHEX = 0;
- switch (x)
- {
- case 0:
- cHEX = SEGA | SEGB | SEGC | SEGD | SEGE | SEGF;
- break;
- case 1:
- cHEX = SEGB | SEGC;
- break;
- case 2:
- cHEX = SEGA | SEGB | SEGG | SEGE | SEGD;
- break;
- case 3:
- cHEX = SEGA | SEGB | SEGG | SEGC | SEGD;
- break;
- case 4:
- cHEX = SEGF | SEGG | SEGB | SEGC;
- break;
- case 5:
- cHEX = SEGA | SEGF | SEGG | SEGD | SEGC;
- break;
- case 6:
- cHEX = SEGA | SEGF | SEGE | SEGG | SEGD | SEGC;
- break;
- case 7:
- cHEX = SEGA | SEGB | SEGC;
- break;
- case 8:
- cHEX = SEGA | SEGB | SEGC | SEGD | SEGE | SEGF | SEGG;
- break;
- // ToDo: add 9 :)
- default:
- cHEX = -1;
- }
- return cHEX;
- }
- void printHEX0(int x, struct alt_up_dev *up_dev)
- {
- int cHEX = NumberTo7SEG(x);
- int error = 0;
- if (cHEX != -1)
- alt_up_parallel_port_write_data(up_dev->uchwytHEX3, cHEX);
- else
- {
- int errHEX0 = SEGE | SEGG;
- int errHEX1 = SEGE | SEGG;
- int errHEX2 = SEGA | SEGF | SEGG | SEGE | SEGD;
- alt_up_parallel_port_write_data(up_dev->uchwytHEX3, (errHEX2 << 16) | (errHEX1 << 8) | errHEX0);
- }
- }
- void printHEX1(int x, struct alt_up_dev *up_dev)
- {
- int cHEX = NumberTo7SEG(x);
- if (cHEX == -1)
- cHEX = 0;
- alt_up_parallel_port_write_data(up_dev->uchwytHEX7, cHEX);
- }
- void printLEDS(int leds, struct alt_up_dev *up_dev)
- {
- alt_up_parallel_port_write_data(up_dev->uchwytLEDR, leds);
- }
- void printLEDSDelay(int leds, struct alt_up_dev *up_dev)
- {
- const int optionOnDelay = 1;
- const int optionOffDelay = 2;
- const int errorOnDelay = 3;
- const int errorOffDelay = 2;
- int isError = 0;
- int currentAltTicks = 0;
- int currentDelay = 0;
- // obecnie zaświecone ledy
- int previousLEDS = alt_up_parallel_port_read_data(up_dev->uchwytLEDR);
- if (previousLEDS == leds)
- {
- return;
- }
- // sprawdzam czy akcja dotyczy errora, czy był zaswiecony lub dopiero ma byc
- if (leds == LED17 || previousLEDS == LED17)
- {
- isError = 1;
- }
- // light the LEDS - nowy uklad ledów (liczba wynikająca z sum logicznych ledów) jest wieksza
- // od poprzedniego => tak - zapalamy, nie - gasimy
- if (leds > previousLEDS)
- {
- if (isError)
- {
- // set delay 3s
- currentDelay = errorOnDelay;
- }
- else
- {
- // set delay 1s
- currentDelay = optionOnDelay;
- }
- }
- // off
- else
- {
- if (isError)
- {
- // set delay 2s
- currentDelay = errorOffDelay;
- }
- else
- {
- // set delay 2s
- currentDelay = optionOffDelay;
- }
- }
- currentAltTicks = alt_nticks();
- while (alt_nticks() - currentAltTicks < currentDelay * alt_ticks_per_second()){
- //empty loop - waiting...
- };
- alt_up_parallel_port_write_data(up_dev->uchwytLEDR, leds);
- }
- void interrupt_handler(struct alt_up_dev *up_dev, alt_u32 id)
- {
- int currentLEDS = 0;
- int currentHEX0 = 0;
- int currentHEX1 = 0;
- int currentSW = 0;
- // Nie należy używać funkcji wykorzystujących przerwania (np. printf).
- // Należy pamiętać o wyzerowaniu źródła przerwania np. clear_edge_capture
- currentSW = alt_up_parallel_port_read_data(up_dev->uchwytSW);
- int tryb = currentSW & SW0;
- option1 = currentSW & SW1 + tryb;
- option2 = currentSW & SW2 + tryb;
- option3 = currentSW & SW3 + tryb;
- option4 = currentSW & SW4 + tryb;
- option5 = currentSW & SW5 + tryb;
- option6 = currentSW & SW6 + tryb;
- option7 = currentSW & SW7 + tryb;
- option8 = currentSW & SW8 + tryb;
- if (tryb)
- {
- curretnLEDS = LED0;
- curretnHEX0 = 1;
- }
- else
- {
- curretnLEDS = LED1;
- curretnHEX0 = 2;
- }
- switch (currentSW)
- {
- case option1:
- currentLEDS = | LED2;
- currentHEX1 = 1;
- break;
- case option2:
- currentLEDS = | LED3;
- currentHEX1 = 2;
- break;
- case option3:
- currentLEDS = | LED4;
- currentHEX1 = 3;
- break;
- case option4:
- currentLEDS = | LED5;
- currentHEX1 = 4;
- break;
- case option5:
- currentLEDS = | LED5;
- currentHEX1 = 5;
- break;
- case option6:
- currentLEDS = | LED6;
- currentHEX1 = 6;
- break;
- case option7:
- currentLEDS = | LED7;
- currentHEX1 = 7;
- break;
- case option8:
- if (tryb)
- {
- currentLEDS = | LED8;
- currentHEX1 = 8;
- }
- else
- {
- //second mode have only 7 options
- currentHEX0 = -1;
- currentHEX1 = -1;
- currentLEDS = LED17;
- }
- break;
- default:
- currentHEX0 = -1;
- currentHEX1 = -1;
- currentLEDS = LED17;
- }
- printHEX0(currentHEX0, up_dev);
- printHEX1(currentHEX1, up_dev);
- //printLEDS(currentLEDS, up_dev);
- printLEDSDelay(currentLEDS, up_dev)
- }
- int main()
- {
- struct alt_up_dev up_dev;
- alt_up_parallel_port_dev *uchwytSW = alt_up_parallel_port_open_dev("/dev/SW_SLIDERS");
- alt_up_parallel_port_dev *uchwytLEDG = alt_up_parallel_port_open_dev("/dev/LEDS_GREEN");
- alt_up_parallel_port_dev *uchwytLEDR = alt_up_parallel_port_open_dev("/dev/LEDS_RED");
- alt_up_parallel_port_dev *uchwytHEX3 = alt_up_parallel_port_open_dev("/dev/HEX_3");
- alt_up_parallel_port_dev *uchwytHEX7 = alt_up_parallel_port_open_dev("/dev/HEX_7");
- if (uchwytSW != NULL)
- {
- up_dev.uchwytSW = uchwytSW;
- }
- if (uchwytLEDG != NULL)
- {
- up_dev.uchwytLEDG = uchwytLEDG;
- }
- if (uchwytLEDR != NULL)
- {
- up_dev.uchwytLEDR = uchwytLEDR;
- }
- if (uchwytHEX3 != NULL)
- {
- up_dev.uchwytHEX3 = uchwytHEX3;
- }
- if (uchwytHEX7 != NULL)
- {
- up_dev.uchwytHEX7 = uchwytHEX7;
- }
- alt_irq_register(1, (void *)&up_dev, (void *)interrupt_handler);
- alt_up_parallel_port_set_interrupt_mask(uchwytSW, 0xff);
- while (true)
- ;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement