Advertisement
miszczo

Untitled

May 24th, 2013
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.52 KB | None | 0 0
  1. /*
  2.  * main.c
  3.  *
  4.  *  Created on: 01-04-2013
  5.  *              15:05:56
  6.  *      Author: miszczo
  7. */
  8.  
  9.  
  10. #include <avr/io.h>
  11. #include <avr/interrupt.h>
  12. #include <util/delay.h>
  13. #include <avr/pgmspace.h>
  14.  
  15. #define TIME 10
  16. #define FRTIME 50
  17. #define RAND 2
  18. #define FULL_RAND 255
  19.  
  20. volatile uint8_t R = 0;
  21. volatile uint8_t G = 0;
  22. volatile uint8_t B = 0;
  23.  
  24.  
  25. //uint32_t get_full_rand(void);
  26. void normal_sequence(void);
  27. void random_sequence(void);
  28. //uint16_t get_rand(void);
  29. uint32_t get_random(uint8_t range);
  30. void full_rand_seq(void);
  31.  
  32.  
  33.  
  34. const uint8_t pwm_val[256] PROGMEM = {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  35.         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,
  36.         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,
  37.         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,
  38.         12, 12, 12, 13, 13, 13, 14, 14, 14, 15, 15, 15, 16, 16, 16, 17, 17, 17, 18, 18, 19, 19, 20, 20,
  39.         20, 21, 21, 22, 22, 23, 23, 24, 25, 25, 26, 26, 27, 27, 28, 29, 29, 30, 31, 31, 32, 33, 34, 34,
  40.         35, 36, 37, 38, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 54, 55, 56, 57, 59,
  41.         60, 61, 63, 64, 65, 67, 68, 70, 71, 73, 75, 76, 78, 80, 82, 83, 85, 87, 89, 91, 93, 95, 97, 100,
  42.         102, 104, 106, 109, 111, 114, 116, 119, 122, 124, 127, 130, 133, 136, 139, 142, 145, 148, 152,
  43.         155, 158, 162, 166, 169, 173, 177, 181, 185, 189, 193, 197, 202, 206, 211, 216, 220, 225, 230,
  44.         236, 241, 246, 252, 255};
  45.  
  46. int main(void)
  47. {
  48.     //wyłączenie komparatora
  49.     ADCSRB = (1<<ACME);
  50.  
  51.     // wybór kanału dla adc ADC3 = PB3
  52.     ADMUX = (1<<MUX0) | (1<<MUX1);
  53.  
  54.  
  55.     // ustawienia portow
  56.     DDRB |= (1<<PB0) | (1<<PB1) | (1<<PB2) ;
  57.  
  58.  
  59.     //konfiguracja timera
  60.     TCCR0B |= (1<<CS00);        //preskaler 1
  61.     TIMSK0 |= (1<<TOIE0);       //zezwolenie na przerwanie przy przepelnieniu
  62.  
  63.     sei();
  64.  
  65.  
  66.     while(1)
  67.     {
  68.         //normal_sequence();
  69.         full_rand_seq();
  70.         //random_sequence();
  71.  
  72.  
  73.     }
  74.  
  75.  
  76. }
  77.  
  78.  
  79.  
  80.  
  81. /**************tutaj będzie migac jak pojebana********************/
  82.  
  83. void full_rand_seq(void)
  84. {
  85.     uint32_t a;
  86.     a = get_random(255);
  87.     R = a;
  88.     G = (a>>8);
  89.     B = (a>>16);
  90.     _delay_ms(FRTIME);
  91.  
  92. }
  93.  
  94.  
  95. /***********tu bedzie losowo w góre i w dol************************/
  96. void  random_sequence(void)
  97. {
  98.     uint16_t a;
  99.     a = get_random(2);
  100.  
  101.     int i;
  102.     for(i=0;i<255;i++)
  103.     {
  104.         if(((a & 0b11) == 1) && (R > 0)) R--;
  105.         if(((a & 0b11) == 2) && (R < 255)) R++;
  106.  
  107.         if((((a>>2) & 0b11) == 1) && (R > 0)) G--;
  108.         if((((a>>2) & 0b11) == 2) && (R < 255)) G++;
  109.  
  110.         if((((a>>4) & 0b11) == 1) && (R > 0)) B--;
  111.         if((((a>>4) & 0b11) == 2) && (R < 255)) B++;
  112.  
  113.         _delay_ms(TIME);
  114.  
  115.     }
  116.  
  117. }
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125. /********************** a tu po kolei*********************/
  126.  
  127. void normal_sequence(void)
  128. {
  129.     uint8_t i;
  130.     char kanter;
  131.  
  132.     for(kanter = 1; kanter<9; kanter++)
  133.     {
  134.         for(i=0;i<255;i++)
  135.         {
  136.             if(kanter == 1)     B = pgm_read_byte(pwm_val[i]);          //R = 0 , G = 0
  137.             if(kanter == 2)     G = pgm_read_byte(pwm_val[i]);          //R = 0 , B =255
  138.             if(kanter == 3)     B = pwm_val[255 - i];   //R =0 , G = 255
  139.             if(kanter == 4)     R = pwm_val[i];         //G = 255 , B = 0
  140.             if(kanter == 5)     B = pwm_val[i];             //R = 255 , G = 255
  141.             if(kanter == 6)     G = pwm_val[255-i];         //R = 255 , B = 255
  142.             if(kanter == 7)     B = pwm_val[255-i];         // G = 0 , R = 255
  143.             if(kanter == 8)     R = pwm_val[255-i];     // G = 0 , B = 0
  144.             _delay_ms(TIME);
  145.         }
  146.     }
  147. }
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154.  
  155.  
  156.  
  157. uint32_t get_random(uint8_t range)
  158. {
  159.     uint32_t wartosc = 0 ;
  160.     uint8_t i;
  161.  
  162.     for(i = 0;i<24;i++)
  163.     {
  164.         //ustawienia adc, adc enable , preskaler 64 , start conversion
  165.         ADCSRA = (1<<ADEN) | (1<<ADPS2) | (1<<ADPS1) | (1<<ADSC);
  166.         while(ADCSRA&(1<<ADSC));        //wait for end conversion
  167.  
  168.         wartosc = ((wartosc<<1) & (ADCL | 0x01));
  169.     }
  170.  
  171.     return wartosc;
  172. }
  173.  
  174.  
  175.  
  176. /*
  177. uint32_t get_random(uint8_t range)
  178. {
  179.     uint32_t rand_value = 0;
  180.  
  181.     uint8_t i;
  182.     uint8_t z;
  183.  
  184.     for(z=0;z<3;z++)
  185.     {
  186.         uint8_t temp = 0;
  187.         for(i=0;i<range;i++)
  188.         {
  189.             //ustawienia adc, adc enable , preskaler 64 , start conversion
  190.             ADCSRA = (1<<ADEN) | (1<<ADPS2) | (1<<ADPS1) | (1<<ADSC);
  191.             while(ADCSRA&(1<<ADSC));        //wait for end conversion
  192.  
  193.             temp = (temp + (ADCL & 0x01));
  194.         }
  195.         if(range == 255) rand_value |= (temp << (8*z));
  196.         else rand_value |= (temp << (2*z));
  197.         //rand_value = temp;
  198.     }
  199.  
  200.     return rand_value;
  201.  
  202. }
  203.  
  204.  
  205.  
  206. */
  207.  
  208.  
  209.  
  210. ISR ( TIM0_OVF_vect )
  211. {
  212.     static uint8_t licznik;
  213.  
  214.     if(licznik>=R) PORTB |= (1<<PB0); else PORTB &= ~(1<<PB0);
  215.     if(licznik>=G) PORTB |= (1<<PB1); else PORTB &= ~(1<<PB1);
  216.     if(licznik>=B) PORTB |= (1<<PB2); else PORTB &= ~(1<<PB2);
  217.  
  218.     licznik++;
  219. }
  220.  
  221.  
  222.  
  223.  
  224.  
  225.  
  226.  
  227.  
  228.  
  229.  
  230. /*
  231.  
  232.  
  233. uint16_t get_rand(void)
  234. {
  235.     uint16_t rand_value = 0;
  236.     uint8_t i;
  237.  
  238.     uint8_t z;
  239.  
  240.     for(z=0;z<3;z++)
  241.     {
  242.         uint8_t temp = 0;
  243.         for(i=0;i<2;i++)
  244.         {
  245.             //ustawienia adc, adc enable, channel pb5 , preskaler 64 , start conversion
  246.             ADCSRA = (1<<ADEN) | (1<<ADPS2) | (1<<ADPS1) | (1<<ADSC);
  247.             while(ADCSRA&(1<<ADSC));        //wait for end conversion
  248.  
  249.             temp = (temp + (ADCL & 0x01));
  250.         }
  251.  
  252.         rand_value |= (temp << (2*z));
  253.     }
  254.  
  255.     return rand_value;
  256.  
  257. }
  258.  
  259.  
  260.  
  261. uint32_t get_full_rand(void)
  262. {
  263.     uint32_t rand_value = 0;
  264.     uint8_t i;
  265.     uint8_t z;
  266.  
  267.     for(z=0;z<3;z++)
  268.     {
  269.         uint8_t temp = 0;
  270.         for(i=0;i<255;i++)
  271.         {
  272.             //ustawienia adc, adc enable , preskaler 64 , start conversion
  273.             ADCSRA = (1<<ADEN) | (1<<ADPS2) | (1<<ADPS1) | (1<<ADSC);
  274.             while(ADCSRA&(1<<ADSC));        //wait for end conversion
  275.  
  276.             temp = (temp + (ADCL & 0x01));
  277.         }
  278.  
  279.             rand_value |= (temp << (8*z));
  280.         }
  281.  
  282.         return rand_value;
  283.  
  284.  
  285.  
  286.  
  287.  
  288. }
  289. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement