Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <util/delay.h>
- #include <avr/io.h>
- #include <math.h>
- #include <stdint.h>
- #define F_CPU 1000000UL
- #define CZERWONA 1
- #define ZOLTA 2
- #define ZIELONA 3
- #define STRZALKA 4
- #define ZAPAL 0x07
- #define ZGAS 0x06
- #define CLK0 0x04
- #define CZAS_MIGANIA 5000
- void zapalanie(void)
- {
- PORTB=ZAPAL;
- PORTB=CLK0;
- }
- void gaszenie(void)
- {
- PORTB=ZGAS;
- PORTB=CLK0;
- }
- void etap1(void);
- void etap1a(void);
- void etap1b(void);
- void etap1c(void);
- void etap2(void);
- void etap2a(void);
- void etap2b(void);
- void etap2c(void);
- void etap3(void);
- void etap3a(void);
- void etap3b(void);
- void etap3c(void);
- void etap4(void);
- void etap4a(void);
- void etap4b(void);
- void etap4c(void);
- void reset(void);
- int i=0;
- int j=0;
- int ledy[48]={1,2,3,4,
- 1,3,
- 3,1,
- 1,2,3,4,
- 1,3,
- 3,1,
- 1,2,3,4,
- 1,3,
- 1,3,
- 1,3,
- 3,1,
- 3,2,1,
- 0,
- 1,2,3,4,
- 1,3,
- 1,3,
- 0,
- 1,3,
- 3,1,
- 3,2,1};
- void reset(void)
- {
- for (i=0; i<48; i++)
- {
- PORTB=0x3E;
- PORTB=0x3C;
- }
- }
- void zolte(void)
- {
- for (i=47; i>=0; i--)
- {
- if (ledy[i]==ZOLTA)
- {
- PORTB=ZAPAL;
- PORTB=CLK0;
- }
- else
- {
- PORTB=ZGAS;
- PORTB=CLK0;
- }
- }
- }
- int stan=0;
- void przycisk(void)
- {
- int przycisk=PINB;
- if ((przycisk&0x80)==0)
- {
- stan=1;
- }
- if ((przycisk&0x40)==0)
- {
- stan=0;
- }
- }
- //----------------------------------------------------------------------------------------------------------
- // etap 1 - góra i lewo
- void etap1(void)
- {
- for (i=47; i>=0; i--)
- {
- if (i==5 || i==6 || i==21 || i==23 || i==34 || i==42 || i==43 || i==45) //światła zielone
- {
- PORTB=ZAPAL;
- PORTB=CLK0;
- }
- else if (i==0 || i==8 || i==12 || i==15 || i==16 || i==24 || i==27 || i==30 || i==38 || i==44 || i==36) //światła czerwone
- {
- PORTB=ZAPAL;
- PORTB=CLK0;
- }
- else if (i==3 || i==11) //strzałki
- {
- PORTB=ZAPAL;
- PORTB=CLK0;
- }
- else
- {
- PORTB=ZGAS;
- PORTB=CLK0;
- }
- }
- }
- // etap 1a - mruganie świateł na przejściu dla pieszych
- void etap1a(void)
- {
- for (j=0; j<2; j++)
- {
- for (i=47; i>=0; i--)
- {
- if (i==5 || i==6 || i==34 || i==45) //światła zielone
- {
- PORTB=ZAPAL;
- PORTB=CLK0;
- }
- else if (i==0 || i==8 || i==12 || i==15 || i==16 || i==24 || i==27 || i==30 || i==38 || i==36) //światła czerwone
- {
- PORTB=ZAPAL;
- PORTB=CLK0;
- }
- else if (i==3 || i==11) //strzałki
- {
- PORTB=ZAPAL;
- PORTB=CLK0;
- }
- else
- {
- PORTB=ZGAS;
- PORTB=CLK0;
- }
- }
- _delay_ms(3000);
- reset();
- etap1();
- _delay_ms(3000);
- reset();
- }
- }
- // etap 1b - czerwone na przejściu dla pieszych i żółte na skręcie w lewo
- void etap1b(void)
- {
- for (i=47; i>=0; i--)
- {
- if (i==5 || i==6 || i==34) //światła zielone
- {
- PORTB=ZAPAL;
- PORTB=CLK0;
- }
- else if (i==0 || i==8 || i==12 || i==15 || i==16 || i==24 || i==27 || i==30 || i==38 || i==44 || i==36 || i==20 || i==22 || i==41) //światła czerwone
- {
- PORTB=ZAPAL;
- PORTB=CLK0;
- }
- else if (i==3 || i==11) //strzałki
- {
- PORTB=ZAPAL;
- PORTB=CLK0;
- }
- else if (i==46) // pomarańczowe
- {
- PORTB=ZAPAL;
- PORTB=CLK0;
- }
- else
- {
- PORTB=ZGAS;
- PORTB=CLK0;
- }
- }
- }
- // etap 1c - czerwone na przejściu dla pieszych, na skręcie w lewo; start w dół (czerwone+żółte)
- void etap1c(void)
- {
- for (i=47; i>=0; i--)
- {
- if (i==5 || i==6 || i==34) //światła zielone
- {
- PORTB=ZAPAL;
- PORTB=CLK0;
- }
- else if (i==0 || i==8 || i==12 || i==15 || i==16 || i==24 || i==27 || i==30 || i==38 || i==44 || i==20 || i==22 || i==47 || i==41 || i==36) //światła czerwone
- {
- PORTB=ZAPAL;
- PORTB=CLK0;
- }
- else if (i==3 || i==11) //strzałki
- {
- PORTB=ZAPAL;
- PORTB=CLK0;
- }
- else if (i==17) // pomarańczowe
- {
- PORTB=ZAPAL;
- PORTB=CLK0;
- }
- else
- {
- PORTB=ZGAS;
- PORTB=CLK0;
- }
- }
- }
- //----------------------------------------------------------------------------------------------------------
- // etap 2 - góra i dół
- void etap2(void)
- {
- for (i=47; i>=0; i--)
- {
- if (i==5 || i==6 || i==13 || i==14 || i==18 || i==34) //światła zielone
- {
- PORTB=ZAPAL;
- PORTB=CLK0;
- }
- else if (i==0 || i==8 || i==20 || i==22 || i==24 || i==27 || i==30 || i==38 || i==44 || i==47 || i==41 || i==36) //światła czerwone
- {
- PORTB=ZAPAL;
- PORTB=CLK0;
- }
- else if (i==3 || i==11) //strzałki
- {
- PORTB=ZAPAL;
- PORTB=CLK0;
- }
- else
- {
- PORTB=ZGAS;
- PORTB=CLK0;
- }
- }
- }
- // etap 2a - mruganie świateł na przejściu dla pieszych
- void etap2a(void)
- {
- for (j=0; j<2; j++)
- {
- for (i=47; i>=0; i--)
- {
- if (i==13 || i==14 || i==18 || i==34) //światła zielone
- {
- PORTB=ZAPAL;
- PORTB=CLK0;
- }
- else if (i==0 || i==8 || i==20 || i==22 || i==24 || i==27 || i==30 || i==38 || i==44 || i==47 || i==41 || i==38 || i==36) //światła czerwone
- {
- PORTB=ZAPAL;
- PORTB=CLK0;
- }
- else if (i==3 || i==11) //strzałki
- {
- PORTB=ZAPAL;
- PORTB=CLK0;
- }
- else
- {
- PORTB=ZGAS;
- PORTB=CLK0;
- }
- }
- _delay_ms(3000);
- reset();
- etap2();
- _delay_ms(3000);
- reset();
- }
- }
- // etap 2b - czerwone na przejściu dla pieszych, żółte do góry
- void etap2b(void)
- {
- for (i=47; i>=0; i--)
- {
- if (i==13 || i==14 || i==18) //światła zielone
- {
- PORTB=ZAPAL;
- PORTB=CLK0;
- }
- else if (i==0 || i==8 || i==20 || i==22 || i==24 || i==27 || i==30 || i==38 || i==44 || i==47 || i==4 || i==7 || i==41 || i==36) //światła czerwone
- {
- PORTB=ZAPAL;
- PORTB=CLK0;
- }
- else if (i==3 || i==11) //strzałki
- {
- PORTB=ZAPAL;
- PORTB=CLK0;
- }
- else if (i==33) // pomarańczowe
- {
- PORTB=ZAPAL;
- PORTB=CLK0;
- }
- else
- {
- PORTB=ZGAS;
- PORTB=CLK0;
- }
- }
- }
- // etap 2c - czerwone na przejściu dla pieszych, jeździe w górę; start w prawo (czerwone+żółte)
- void etap2c(void)
- {
- for (i=47; i>=0; i--)
- {
- if (i==13 || i==14 || i==18) //światła zielone
- {
- PORTB=ZAPAL;
- PORTB=CLK0;
- }
- else if (i==0 || i==8 || i==20 || i==22 || i==24 || i==27 || i==30 || i==38 || i==44 || i==47 || i==4 || i==7 || i==32 || i==41 || i==36) //światła czerwone
- {
- PORTB=ZAPAL;
- PORTB=CLK0;
- }
- else if (i==3 || i==11) //strzałki
- {
- PORTB=ZAPAL;
- PORTB=CLK0;
- }
- else if (i==29) // pomarańczowe
- {
- PORTB=ZAPAL;
- PORTB=CLK0;
- }
- else
- {
- PORTB=ZGAS;
- PORTB=CLK0;
- }
- }
- }
- //----------------------------------------------------------------------------------------------------------
- // etap 3 - dół i lewo
- void etap3(void)
- {
- for (i=47; i>=0; i--)
- {
- if (i==13 || i==14 || i==18 || i==25 || i==26 || i==28 || i==37 || i==39) //światła zielone
- {
- PORTB=ZAPAL;
- PORTB=CLK0;
- }
- else if (i==0 || i==4 || i==7 || i==8 || i==20 || i==22 || i==32 || i==41 || i==44 || i==47) //światła czerwone
- {
- PORTB=ZAPAL;
- PORTB=CLK0;
- }
- else if (i==3 || i==11) //strzałki
- {
- PORTB=ZAPAL;
- PORTB=CLK0;
- }
- else
- {
- PORTB=ZGAS;
- PORTB=CLK0;
- }
- }
- }
- // etap 3a - mruganie świateł na przejściu dla pieszych
- void etap3a(void)
- {
- for (j=0; j<2; j++)
- {
- for (i=47; i>=0; i--)
- {
- if (i==18 || i==25 || i==26 || i==28 || i==37 || i==39) //światła zielone
- {
- PORTB=ZAPAL;
- PORTB=CLK0;
- }
- else if (i==0 || i==4 || i==7 || i==8 || i==20 || i==22 || i==32 || i==41 || i==44 || i==47) //światła czerwone
- {
- PORTB=ZAPAL;
- PORTB=CLK0;
- }
- else if (i==3 || i==11) //strzałki
- {
- PORTB=ZAPAL;
- PORTB=CLK0;
- }
- else
- {
- PORTB=ZGAS;
- PORTB=CLK0;
- }
- }
- _delay_ms(3000);
- reset();
- etap3();
- _delay_ms(3000);
- reset();
- }
- }
- // etap 3b - czerwone na przejściu dla pieszych, żółte w dół
- void etap3b(void)
- {
- for (i=47; i>=0; i--)
- {
- if (i==25 || i==26 || i==37 || i==39) //światła zielone
- {
- PORTB=ZAPAL;
- PORTB=CLK0;
- }
- else if (i==0 || i==4 || i==7 || i==8 || i==20 || i==22 || i==32 || i==41 || i==44 || i==47 || i==15 || i==12) //światła czerwone
- {
- PORTB=ZAPAL;
- PORTB=CLK0;
- }
- else if (i==3 || i==11) //strzałki
- {
- PORTB=ZAPAL;
- PORTB=CLK0;
- }
- else if (i==17 || i==29) // pomarańczowe
- {
- PORTB=ZAPAL;
- PORTB=CLK0;
- }
- else
- {
- PORTB=ZGAS;
- PORTB=CLK0;
- }
- }
- }
- // etap 3c - czerwone na przejściu dla pieszych, w dół; start w poprzek (czerwone+żółte)
- void etap3c(void)
- {
- for (i=47; i>=0; i--)
- {
- if (i==25 || i==26 || i==37 || i==39) //światła zielone
- {
- PORTB=ZAPAL;
- PORTB=CLK0;
- }
- else if (i==0 || i==4 || i==7 || i==8 || i==20 || i==22 || i==32 || i==41 || i==44 || i==47 || i==16 || i==30 || i==9 || i==15 || i==12) //światła czerwone
- {
- PORTB=ZAPAL;
- PORTB=CLK0;
- }
- else if (i==3 || i==11) //strzałki
- {
- PORTB=ZAPAL;
- PORTB=CLK0;
- }
- else if (i==1 || i==9) // pomarańczowe
- {
- PORTB=ZAPAL;
- PORTB=CLK0;
- }
- else
- {
- PORTB=ZGAS;
- PORTB=CLK0;
- }
- }
- }
- //----------------------------------------------------------------------------------------------------------
- // etap 4 - w poprzek
- void etap4(void)
- {
- for (i=47; i>=0; i--)
- {
- if (i==2 || i==10 || i==21 || i==23 || i==25 || i==26 || i==37 || i==39 || i==42 || i==43) //światła zielone
- {
- PORTB=ZAPAL;
- PORTB=CLK0;
- }
- else if (i==4 || i==7 || i==12 || i==15 || i==16 || i==30 || i==32 || i==47) //światła czerwone
- {
- PORTB=ZAPAL;
- PORTB=CLK0;
- }
- else if (i==19 || i==35) //strzałki
- {
- PORTB=ZAPAL;
- PORTB=CLK0;
- }
- else
- {
- PORTB=ZGAS;
- PORTB=CLK0;
- }
- }
- }
- // etap 4a - mruganie świateł na przejściu dla pieszych
- void etap4a(void)
- {
- for (j=0; j<2; j++)
- {
- for (i=47; i>=0; i--)
- {
- if (i==2 || i==10 || i==21 || i==23 || i==42 || i==43) //światła zielone
- {
- PORTB=ZAPAL;
- PORTB=CLK0;
- }
- else if (i==4 || i==7 || i==12 || i==15 || i==16 || i==30 || i==32 || i==47) //światła czerwone
- {
- PORTB=ZAPAL;
- PORTB=CLK0;
- }
- else if (i==19 || i==35) //strzałki
- {
- PORTB=ZAPAL;
- PORTB=CLK0;
- }
- else
- {
- PORTB=ZGAS;
- PORTB=CLK0;
- }
- }
- _delay_ms(3000);
- reset();
- etap4();
- _delay_ms(3000);
- reset();
- }
- }
- // etap 4b - czerwone na przejściu dla pieszych, żółte w poprzek
- void etap4b(void)
- {
- for (i=47; i>=0; i--)
- {
- if (i==21 || i==23 || i==42 || i==43) //światła zielone
- {
- PORTB=ZAPAL;
- PORTB=CLK0;
- }
- else if (i==4 || i==7 || i==12 || i==15 || i==16 || i==30 || i==32 || i==47 || i==24 || i==27 || i==36 || i==38) //światła czerwone
- {
- PORTB=ZAPAL;
- PORTB=CLK0;
- }
- else if (i==19 || i==35) //strzałki
- {
- PORTB=ZAPAL;
- PORTB=CLK0;
- }
- else if (i==1 || i==9) // pomarańczowe
- {
- PORTB=ZAPAL;
- PORTB=CLK0;
- }
- else
- {
- PORTB=ZGAS;
- PORTB=CLK0;
- }
- }
- }
- // etap 4c - czerwone na przejściu dla pieszych, w poprzek; start w górę i lewo (czerwone+żółte)
- void etap4c(void)
- {
- for (i=47; i>=0; i--)
- {
- if (i==21 || i==23 || i==42 || i==43) //światła zielone
- {
- PORTB=ZAPAL;
- PORTB=CLK0;
- }
- else if (i==4 || i==7 || i==12 || i==15 || i==16 || i==30 || i==32 || i==47 || i==24 || i==27 || i==36 || i==38 || i==8 || i==0) //światła czerwone
- {
- PORTB=ZAPAL;
- PORTB=CLK0;
- }
- else if (i==19 || i==35) //strzałki
- {
- PORTB=ZAPAL;
- PORTB=CLK0;
- }
- else if (i==33 || i==46) // pomarańczowe
- {
- PORTB=ZAPAL;
- PORTB=CLK0;
- }
- else
- {
- PORTB=ZGAS;
- PORTB=CLK0;
- }
- }
- }
- //----------------------------------------------------------------------------------------------------------
- /*void awaria(void)
- {
- while(1)
- {
- reset();
- _delay_ms(CZAS_MIGANIA);
- zolte();
- _delay_ms(CZAS_MIGANIA);
- przycisk();
- if (stan==0) break;
- }
- }
- */
- void main (void)
- {
- DDRB=0x3F;
- reset();
- _delay_ms(2000);
- while(1)
- {
- if (stan==1)
- {
- while(1)
- {
- reset();
- _delay_ms(CZAS_MIGANIA);
- zolte();
- _delay_ms(CZAS_MIGANIA);
- przycisk();
- if (stan==0) break;
- }
- }
- else if (stan==0)
- {
- while(1)
- {
- przycisk();
- if (stan==1) break;
- etap1();
- przycisk();
- if (stan==1) break;
- _delay_ms(10000);
- reset();
- przycisk();
- if (stan==1) break;
- etap1a();
- przycisk();
- if (stan==1) break;
- reset();
- przycisk();
- if (stan==1) break;
- etap1b();
- przycisk();
- if (stan==1) break;
- _delay_ms(CZAS_MIGANIA);
- reset();
- przycisk();
- if (stan==1) break;
- etap1c();
- przycisk();
- if (stan==1) break;
- _delay_ms(CZAS_MIGANIA);
- reset();
- przycisk();
- if (stan==1) break;
- etap2();
- przycisk();
- if (stan==1) break;
- _delay_ms(10000);
- reset();
- przycisk();
- if (stan==1) break;
- etap2a();
- przycisk();
- if (stan==1) break;
- reset();
- przycisk();
- if (stan==1) break;
- etap2b();
- przycisk();
- if (stan==1) break;
- _delay_ms(CZAS_MIGANIA);
- reset();
- przycisk();
- if (stan==1) break;
- etap2c();
- przycisk();
- if (stan==1) break;
- _delay_ms(CZAS_MIGANIA);
- reset();
- przycisk();
- if (stan==1) break;
- etap3();
- przycisk();
- if (stan==1) break;
- _delay_ms(10000);
- reset();
- przycisk();
- if (stan==1) break;
- etap3a();
- przycisk();
- if (stan==1) break;
- reset();
- przycisk();
- if (stan==1) break;
- etap3b();
- przycisk();
- if (stan==1) break;
- _delay_ms(CZAS_MIGANIA);
- reset();
- przycisk();
- if (stan==1) break;
- etap3c();
- przycisk();
- if (stan==1) break;
- _delay_ms(CZAS_MIGANIA);
- reset();
- przycisk();
- if (stan==1) break;
- etap4();
- przycisk();
- if (stan==1) break;
- _delay_ms(10000);
- reset();
- przycisk();
- if (stan==1) break;
- etap4a();
- przycisk();
- if (stan==1) break;
- reset();
- przycisk();
- if (stan==1) break;
- etap4b();
- przycisk();
- if (stan==1) break;
- _delay_ms(CZAS_MIGANIA);
- reset();
- przycisk();
- if (stan==1) break;
- etap4c();
- przycisk();
- if (stan==1) break;
- _delay_ms(CZAS_MIGANIA);
- reset();
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment