Advertisement
miszczo

Untitled

May 26th, 2013
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.07 KB | None | 0 0
  1. /*
  2.  * main.c
  3.  *
  4.  *  Created on: 26-05-2013
  5.  *      Author: miszczo
  6.  */
  7.  
  8.  
  9.  
  10.  
  11. #include <avr/io.h>
  12. #include <avr/interrupt.h>
  13. #include <util/delay.h>
  14. #include <avr/pgmspace.h>
  15. #include <avr/eeprom.h>
  16.  
  17. #define TIME 10
  18. #define FRTIME 200
  19. #define UP  1
  20. #define DOWN 0
  21. #define RED  2
  22. #define BLUE  0
  23. #define GREEN  1
  24.  
  25. volatile uint8_t kolory[4];
  26.  
  27. volatile uint8_t mode;
  28.  
  29. void normal_sequence(void);
  30. void random_sequence(void);
  31. uint32_t get_random(void);
  32. void full_rand_seq(void);
  33. void bright (uint8_t kolor );
  34.  
  35.  
  36. const uint8_t pwm_val[256] PROGMEM = {0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  37.         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3,
  38.         3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6,
  39.         6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 10, 10, 10, 10, 11, 11, 11, 11, 12,
  40.         12, 12, 12, 13, 13, 13, 14, 14, 14, 15, 15, 15, 16, 16, 16, 17, 17, 17, 18, 18, 19, 19, 20, 20,
  41.         20, 21, 21, 22, 22, 23, 23, 24, 25, 25, 26, 26, 27, 27, 28, 29, 29, 30, 31, 31, 32, 33, 34, 34,
  42.         35, 36, 37, 38, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 54, 55, 56, 57, 59,
  43.         60, 61, 63, 64, 65, 67, 68, 70, 71, 73, 75, 76, 78, 80, 82, 83, 85, 87, 89, 91, 93, 95, 97, 100,
  44.         102, 104, 106, 109, 111, 114, 116, 119, 122, 124, 127, 130, 133, 136, 139, 142, 145, 148, 152,
  45.         155, 158, 162, 166, 169, 173, 177, 181, 185, 189, 193, 197, 202, 206, 211, 216, 220, 225, 230,
  46.         236, 241, 246, 255, 255};
  47.  
  48. int main(void)
  49. {
  50.  
  51.  
  52.     // wybór kanału dla adc ADC3 = PB3
  53.     ADMUX = (1<<MUX0) | (1<<MUX1);
  54.  
  55.     PORTB = 0x00;
  56.     // ustawienia portow
  57.     DDRB |= (1<<PB0) | (1<<PB1) | (1<<PB2) ;
  58.  
  59.  
  60.     //konfiguracja timera
  61.     TCCR0B |= (1<<CS00);        //preskaler 1
  62.     TIMSK0 |= (1<<TOIE0);       //zezwolenie na przerwanie przy przepelnieniu
  63.  
  64.     sei();
  65.  
  66.  
  67.     while(1)
  68.     {
  69.         mode = 2;
  70.         if(mode == 0)   full_rand_seq();
  71.         else if(mode == 1) normal_sequence();
  72.         else if(mode == 2) random_sequence();
  73.         //normal_sequence();
  74.     }
  75.  
  76.  
  77. }
  78.  
  79.  
  80.  
  81.  
  82. /**************tutaj będzie migac jak pojebana************************/
  83.  
  84. void full_rand_seq(void)
  85. {
  86.     uint32_t a;
  87.     a = get_random();
  88.     kolory[0] = a;
  89.     kolory[1] = (a>>8);
  90.     kolory[2] = (a>>16);
  91.     _delay_ms(FRTIME);
  92.  
  93. }
  94.  
  95.  
  96. /***********tu bedzie losowo w góre i w dol************************/
  97. void  random_sequence(void)
  98. {
  99.     uint8_t y;
  100.     y = get_random();
  101.  
  102.  
  103.         if(((y & 0x01) == 0) && (kolory[0] > 0)) kolory[0]--;
  104.         if(((y & 0x01) == 1) && (kolory[0] < 255)) kolory[0]++;
  105.  
  106.         if((((y>>2) & 0x01) == 0) && (kolory[1] > 0)) kolory[1]--;
  107.         if((((y>>2) & 0x01) == 1) && (kolory[1] < 255)) kolory[1]++;
  108.  
  109.         if((((y>>4) & 0x01) == 0) && (kolory[2] > 0)) kolory[2]--;
  110.         if((((y>>4) & 0x01) == 1) && (kolory[2] < 255)) kolory[2]++;
  111.  
  112.         _delay_ms(TIME);
  113.  
  114.  
  115.  
  116. }
  117.  
  118. void second_random(void)
  119. {
  120.     uint8_t a;
  121.  
  122.     a = get_random();
  123.  
  124.  
  125. }
  126.  
  127.  
  128. void normal_sequence(void)
  129. {
  130.     kolory[0] = 0;
  131.     kolory[1] = 0;
  132.     kolory[2] = 0;
  133.  
  134.  
  135.     bright(BLUE);
  136.     bright(GREEN);
  137.     bright(BLUE);
  138.     bright(RED);
  139.     bright(BLUE);
  140.     bright(GREEN);
  141.     bright(BLUE);
  142.     bright(RED);
  143.  
  144.  
  145. }
  146.  
  147. void bright(uint8_t kolor)
  148. {
  149.     int z;
  150.  
  151.  
  152.     if(kolory[kolor] == 0)
  153.     {
  154.         for(z = 0; z<255; z++)
  155.         {
  156.             kolory[kolor] = pgm_read_byte(&pwm_val[z]);
  157.  
  158.             _delay_ms(TIME);
  159.         }
  160.     }
  161.     else if(kolory[kolor] == 255)
  162.     {
  163.         for(z = 0; z<255; z++)
  164.         {
  165.             kolory[kolor] = pgm_read_byte(&pwm_val[255-z]);
  166.             _delay_ms(TIME);
  167.         }
  168.  
  169.     }
  170.  
  171. }
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178. uint32_t get_random(void)
  179. {
  180.     uint32_t wartosc = 0 ;
  181.     uint8_t i;
  182.  
  183.     for(i = 0;i<24;i++)
  184.     {
  185.         //ustawienia adc, adc enable , preskaler 64 , start conversion
  186.         ADCSRA = (1<<ADEN) | (1<<ADPS0) | (1<<ADPS1) | (1<<ADSC);
  187.         while(ADCSRA&(1<<ADSC));        //wait for end conversion
  188.  
  189.         wartosc = (wartosc<<1);
  190.         wartosc |= (ADCW & 0x01);
  191.     }
  192.  
  193.     return wartosc;
  194. }
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201. ISR ( TIM0_OVF_vect )
  202. {
  203.     static uint8_t licznik;
  204.  
  205.  
  206.     if(licznik>=kolory[0]) PORTB |= (1<<PB0); else PORTB &= ~(1<<PB0);
  207.     if(licznik>=kolory[1]) PORTB |= (1<<PB1); else PORTB &= ~(1<<PB1);
  208.     if(licznik>=kolory[2]) PORTB |= (1<<PB2); else PORTB &= ~(1<<PB2);
  209.  
  210.     licznik++;
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement