Advertisement
Guest User

Untitled

a guest
Dec 18th, 2014
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.25 KB | None | 0 0
  1. #define F_CPU 1000000L
  2. #include <avr/io.h>
  3. #include <util/delay.h>
  4. #include <avr/interrupt.h>
  5.  
  6. volatile int trybBIALY = 0;
  7. volatile int trybZOLTY = 0;
  8. volatile int trybRAZEM = 0;
  9. volatile int trybZMIANE = 0;
  10.  
  11. volatile int licznik = 0;
  12. volatile int predkosc = 80;
  13. volatile int zmiana = 0;
  14. volatile int zmianaBialych;
  15. volatile int zmianaZoltych;
  16. int main(void){
  17.  
  18.     TCCR0 |= (1<<CS00) | (1<<CS01);
  19.     TIMSK |= 1<<TOIE0;
  20.     TCNT0 = 131;
  21.      
  22.     sei();
  23.      
  24.     DDRC = 0xff;    //ustawienie portu C jako wyjść
  25.     DDRD = 0;       //ustawienie portu D jako wejść
  26.      
  27.     PORTC = 0x0;    //ustawienie wartości na wszystkich pinach
  28.     PORTD = 0xFF;
  29.      
  30.  
  31. while(1){
  32.         if(!(PIND & 0X1)){
  33.             trybBIALY++;
  34.             trybBIALY%=4;
  35.             _delay_ms(100);
  36.            
  37.         }
  38.         else if(!(PIND & 0X2)){
  39.             trybZOLTY++;
  40.             trybZOLTY%=4;
  41.             _delay_ms(100);
  42.         }
  43.         else if(!(PIND & 0X4)){
  44.             trybRAZEM++;
  45.             trybRAZEM%=4;
  46.             _delay_ms(100);
  47.         }
  48.         else if(!(PIND & 0X8)){
  49.         trybZMIANE++;
  50.         trybZMIANE%=4;
  51.         _delay_ms(100);
  52.         }
  53.     }
  54. }
  55.      
  56. SIGNAL(SIG_OVERFLOW0){
  57.      
  58.     licznik++;
  59.      
  60. /////////////////////////////////////////////////////////
  61.            
  62.     if(trybBIALY == 0){
  63.         PORTC &= 0x0; //zgaszone
  64.     }
  65.     else if(trybBIALY == 1){
  66.         PORTC |= 0x1; //stałe świecenie
  67.         PORTC |= 0x2;
  68.     }
  69.     else if(trybBIALY == 2){
  70.         if (licznik > predkosc){
  71.             if (zmianaBialych == 0){
  72.                 PORTC &= 0xFC;
  73.                 PORTC |= 0x1;
  74.                 zmianaBialych = 1;
  75.             }
  76.             else {
  77.                 PORTC &= 0xFC;
  78.                 PORTC |= 0x2;
  79.                 zmianaBialych = 0;
  80.             }
  81.         }
  82.     }
  83.     else if(trybBIALY == 3){
  84.         if(licznik > predkosc){ //białe razem zapalają się i gasną
  85.             PORTC ^= 0x1;
  86.             PORTC ^= 0x2;
  87.         }              
  88.     }
  89.            
  90. ////////////////////////////////////////////////////////
  91.      
  92.     if(trybZOLTY == 0){
  93.         PORTC &= 0x0; //zgaszone
  94.     }
  95.     else if(trybZOLTY == 1){
  96.        
  97.         trybRAZEM = -1;
  98.         trybZMIANE = -1;
  99.        
  100.         PORTC |= 0x4; //stałe świecenie
  101.         PORTC |= 0x8;
  102.     }
  103.     else if(trybZOLTY == 2){
  104.        
  105.         trybRAZEM = -1;
  106.         trybZMIANE = -1;   
  107.        
  108.         if (licznik > predkosc){
  109.             if (zmianaZoltych == 0){
  110.                 PORTC &= 0xF3;
  111.                 PORTC |= 0x4;
  112.                 zmianaZoltych = 1;
  113.             }
  114.             else {
  115.                 PORTC &= 0xF3;
  116.                 PORTC |= 0x8;
  117.                 zmianaZoltych = 0;
  118.             }
  119.         }
  120.     }
  121.     else if(trybZOLTY == 3){
  122.        
  123.         trybRAZEM = -1;
  124.         trybZMIANE = -1;
  125.        
  126.         PORTC ^= 0x4; //żółte razem zapalają się i gasną
  127.         PORTC ^= 0x8;
  128.     }
  129.      
  130. /////////////////////////////////////////////////////////
  131.      
  132.     if(trybRAZEM == 0){
  133.         PORTC &= 0x0;
  134.     }
  135.     else if(trybRAZEM == 1){
  136.        
  137.         trybBIALY = -1;
  138.         trybZOLTY = -1;
  139.        
  140.         PORTC |= 0x1;   //wszystkie diody świecą
  141.         PORTC |= 0x2;
  142.         PORTC |= 0x4;
  143.         PORTC |= 0x8;
  144.     }
  145.     else if(trybRAZEM == 2){
  146.        
  147.         trybBIALY = -1;
  148.         trybZOLTY = -1;
  149.        
  150.         predkosc = 50;
  151.         if(licznik > predkosc){ //wszystkie diody migają z jedną prędkością
  152.             PORTC ^= 0x0f;
  153.         }
  154.     }
  155.     else if(trybRAZEM == 3){
  156.        
  157.         trybBIALY = -1;
  158.         trybZOLTY = -1;
  159.        
  160.         predkosc = 80;
  161.         if(licznik > predkosc){ //wszystkie diody migają z jedną prędkością
  162.             PORTC ^= 0x0f;
  163.         }
  164.     }        
  165.      
  166. ////////////////////////////////////////////////////////
  167.      
  168.      
  169.     if(trybZMIANE == 0){
  170.         PORTC &= 0x0;
  171.     }
  172.     else if(trybZMIANE == 1){
  173.        
  174.         trybBIALY = -1;
  175.         trybZOLTY = -1;
  176.        
  177.         if (licznik > predkosc){ //na zmiane biale i zolte
  178.             PORTC = zmiana || zmiana << 1;
  179.             zmiana = zmiana << 1;
  180.             if (zmiana >= 0x8) zmiana = 0x1;
  181.         }
  182.     }
  183.     else if(trybZMIANE == 2){
  184.        
  185.         trybBIALY = -1;
  186.         trybZOLTY = -1;
  187.        
  188.         if (licznik > predkosc){ //po kolei z jedna predkoscia
  189.             PORTC = zmiana;
  190.             zmiana = zmiana << 1;
  191.             if (zmiana >= 0x8) zmiana = 0x1;
  192.         }
  193.     }
  194.     else if(trybZMIANE == 3){
  195.    
  196.         trybBIALY = -1;
  197.         trybZOLTY = -1;
  198.        
  199.         if (licznik > predkosc){ //po kolei z druga predkoscia
  200.             PORTC = zmiana;
  201.             zmiana = zmiana << 1;
  202.             if (zmiana >= 0x8) zmiana = 0x1;
  203.         }
  204.     }
  205.            
  206.         if(licznik > predkosc){
  207.             licznik = 0;
  208.         }
  209.         TCNT0 = 131;
  210. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement