Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * main.c
- *
- * Created on: 01-04-2013
- * 15:05:56
- * Author: miszczo
- */
- #include <avr/io.h>
- #include <avr/interrupt.h>
- #include <util/delay.h>
- #include <avr/pgmspace.h>
- #define TIME 10
- #define FRTIME 50
- #define RAND 2
- #define FULL_RAND 255
- volatile uint8_t R = 0;
- volatile uint8_t G = 0;
- volatile uint8_t B = 0;
- //uint32_t get_full_rand(void);
- void normal_sequence(void);
- void random_sequence(void);
- //uint16_t get_rand(void);
- uint32_t get_random(uint8_t range);
- void full_rand_seq(void);
- 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,
- 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,
- 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,
- 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,
- 12, 12, 12, 13, 13, 13, 14, 14, 14, 15, 15, 15, 16, 16, 16, 17, 17, 17, 18, 18, 19, 19, 20, 20,
- 20, 21, 21, 22, 22, 23, 23, 24, 25, 25, 26, 26, 27, 27, 28, 29, 29, 30, 31, 31, 32, 33, 34, 34,
- 35, 36, 37, 38, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 54, 55, 56, 57, 59,
- 60, 61, 63, 64, 65, 67, 68, 70, 71, 73, 75, 76, 78, 80, 82, 83, 85, 87, 89, 91, 93, 95, 97, 100,
- 102, 104, 106, 109, 111, 114, 116, 119, 122, 124, 127, 130, 133, 136, 139, 142, 145, 148, 152,
- 155, 158, 162, 166, 169, 173, 177, 181, 185, 189, 193, 197, 202, 206, 211, 216, 220, 225, 230,
- 236, 241, 246, 252, 255};
- int main(void)
- {
- //wyłączenie komparatora
- ADCSRB = (1<<ACME);
- // wybór kanału dla adc ADC3 = PB3
- ADMUX = (1<<MUX0) | (1<<MUX1);
- // ustawienia portow
- DDRB |= (1<<PB0) | (1<<PB1) | (1<<PB2) ;
- //konfiguracja timera
- TCCR0B |= (1<<CS00); //preskaler 1
- TIMSK0 |= (1<<TOIE0); //zezwolenie na przerwanie przy przepelnieniu
- sei();
- while(1)
- {
- //normal_sequence();
- full_rand_seq();
- //random_sequence();
- }
- }
- /**************tutaj będzie migac jak pojebana********************/
- void full_rand_seq(void)
- {
- uint32_t a;
- a = get_random(255);
- R = a;
- G = (a>>8);
- B = (a>>16);
- _delay_ms(FRTIME);
- }
- /***********tu bedzie losowo w góre i w dol************************/
- void random_sequence(void)
- {
- uint16_t a;
- a = get_random(2);
- int i;
- for(i=0;i<255;i++)
- {
- if(((a & 0b11) == 1) && (R > 0)) R--;
- if(((a & 0b11) == 2) && (R < 255)) R++;
- if((((a>>2) & 0b11) == 1) && (R > 0)) G--;
- if((((a>>2) & 0b11) == 2) && (R < 255)) G++;
- if((((a>>4) & 0b11) == 1) && (R > 0)) B--;
- if((((a>>4) & 0b11) == 2) && (R < 255)) B++;
- _delay_ms(TIME);
- }
- }
- /********************** a tu po kolei*********************/
- void normal_sequence(void)
- {
- uint8_t i;
- char kanter;
- for(kanter = 1; kanter<9; kanter++)
- {
- for(i=0;i<255;i++)
- {
- if(kanter == 1) B = pgm_read_byte(pwm_val[i]); //R = 0 , G = 0
- if(kanter == 2) G = pgm_read_byte(pwm_val[i]); //R = 0 , B =255
- if(kanter == 3) B = pwm_val[255 - i]; //R =0 , G = 255
- if(kanter == 4) R = pwm_val[i]; //G = 255 , B = 0
- if(kanter == 5) B = pwm_val[i]; //R = 255 , G = 255
- if(kanter == 6) G = pwm_val[255-i]; //R = 255 , B = 255
- if(kanter == 7) B = pwm_val[255-i]; // G = 0 , R = 255
- if(kanter == 8) R = pwm_val[255-i]; // G = 0 , B = 0
- _delay_ms(TIME);
- }
- }
- }
- uint32_t get_random(uint8_t range)
- {
- uint32_t wartosc = 0 ;
- uint8_t i;
- for(i = 0;i<24;i++)
- {
- //ustawienia adc, adc enable , preskaler 64 , start conversion
- ADCSRA = (1<<ADEN) | (1<<ADPS2) | (1<<ADPS1) | (1<<ADSC);
- while(ADCSRA&(1<<ADSC)); //wait for end conversion
- wartosc = ((wartosc<<1) & (ADCL | 0x01));
- }
- return wartosc;
- }
- /*
- uint32_t get_random(uint8_t range)
- {
- uint32_t rand_value = 0;
- uint8_t i;
- uint8_t z;
- for(z=0;z<3;z++)
- {
- uint8_t temp = 0;
- for(i=0;i<range;i++)
- {
- //ustawienia adc, adc enable , preskaler 64 , start conversion
- ADCSRA = (1<<ADEN) | (1<<ADPS2) | (1<<ADPS1) | (1<<ADSC);
- while(ADCSRA&(1<<ADSC)); //wait for end conversion
- temp = (temp + (ADCL & 0x01));
- }
- if(range == 255) rand_value |= (temp << (8*z));
- else rand_value |= (temp << (2*z));
- //rand_value = temp;
- }
- return rand_value;
- }
- */
- ISR ( TIM0_OVF_vect )
- {
- static uint8_t licznik;
- if(licznik>=R) PORTB |= (1<<PB0); else PORTB &= ~(1<<PB0);
- if(licznik>=G) PORTB |= (1<<PB1); else PORTB &= ~(1<<PB1);
- if(licznik>=B) PORTB |= (1<<PB2); else PORTB &= ~(1<<PB2);
- licznik++;
- }
- /*
- uint16_t get_rand(void)
- {
- uint16_t rand_value = 0;
- uint8_t i;
- uint8_t z;
- for(z=0;z<3;z++)
- {
- uint8_t temp = 0;
- for(i=0;i<2;i++)
- {
- //ustawienia adc, adc enable, channel pb5 , preskaler 64 , start conversion
- ADCSRA = (1<<ADEN) | (1<<ADPS2) | (1<<ADPS1) | (1<<ADSC);
- while(ADCSRA&(1<<ADSC)); //wait for end conversion
- temp = (temp + (ADCL & 0x01));
- }
- rand_value |= (temp << (2*z));
- }
- return rand_value;
- }
- uint32_t get_full_rand(void)
- {
- uint32_t rand_value = 0;
- uint8_t i;
- uint8_t z;
- for(z=0;z<3;z++)
- {
- uint8_t temp = 0;
- for(i=0;i<255;i++)
- {
- //ustawienia adc, adc enable , preskaler 64 , start conversion
- ADCSRA = (1<<ADEN) | (1<<ADPS2) | (1<<ADPS1) | (1<<ADSC);
- while(ADCSRA&(1<<ADSC)); //wait for end conversion
- temp = (temp + (ADCL & 0x01));
- }
- rand_value |= (temp << (8*z));
- }
- return rand_value;
- }
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement