#include #include //#include #include //#include "servo.h" #include "usart.h" #define OUTDDR DDRC #define OUTPORT PORTC #define P_GREEN 2 #define P_GREEN_ 3 #define P_RED 0 #define P_RED_ 1 #define P_BLUE 4 #define P_BLUE_ 5 #define MAX 70 //int i =0; int color[3],color_[3],count[3],color_num=0; unsigned short get_seed(void); void set(color_num); int d2i(double d); int main(void){ TCCR0 = 0x01; //prescaller =1 // TIMSK=0x01;//0x65; //timer0 and timer2 interupt enable //TIMSK |= (1 << TOIE0); TIMSK |= (1 << TOIE1); //TIMSK |= (1 << TOIE2); TCCR2=0x01; TCCR1B=0x01; ASSR=0x00; //Asynchronous Status Register ­ ASSR don't know TCNT2=70; TCNT0=70; //preload the timer with 96 to get 10us interrupts TCNT1L=70; OUTDDR=0xff; //set pin 0,1 at outputport as output OUTPORT=0x2a; //set motor-pin and servo-pin to 0 usart_init0(0x00,0x67); sei(); usart_put0(0xff); srand (get_seed()); for( int i=0 ; i<=2 ; i++ ) { count[i]=rand(); color[i]=rand(); if(color[i] > 0x46) { color[i]=color[i] % 0x46; } usart_put0(color[i]); color_[i]=color[i]; } while(1) { }; } int d2i(double d) { return d<0?d-.5:d+.5; } unsigned short get_seed(void) { unsigned short seed = 0; unsigned short *p = (unsigned short*) (RAMEND+1); extern unsigned short __heap_start; while (p >= &__heap_start + 1) seed ^= * (--p); return seed; } void set(color_num){ if( count[color_num] == 0 ) { if( color_[color_num]==color[color_num] ) { color[color_num]=rand(); if( color[color_num] > 0x46 ) { color[color_num]=color[color_num] % 0x46; color[color_num]=color[color_num]+20; } } if( color_[color_num] > color[color_num] ) { color_[color_num]--; }else{ color_[color_num]++; } usart_put0(color_[color_num]); count[color_num]=rand(); } count[color_num]--; } int up[3]; SIGNAL(TIMER0_OVF_vect){ //timer0 interrupt TCNT0=70;//color_[0]; //preload timer set(0); if( up[0]==0 ) { up[0]=1; OUTPORT |= (1 << P_RED); OUTPORT &= ~(1 << P_RED_); } else { up[0]=0; OUTPORT |= (1 << P_RED_); OUTPORT &= ~(1 << P_RED); } } SIGNAL(TIMER2_OVF_vect){ //timer0 interrupt TCNT2=70;//color_[1]; //preload timer set(1); if( up[1]==0 ) { up[1]=1; OUTPORT |= (1 << P_GREEN); OUTPORT &= ~(1 << P_GREEN_); } else { up[1]=0; OUTPORT |= (1 << P_GREEN_); OUTPORT &= ~(1 << P_GREEN); } } SIGNAL(TIMER1_OVF_vect){ //timer0 interrupt TCNT1H=0xff; TCNT1L=70;//color_[2]; //preload timer set(2); if( up[2]==0 ) { up[2]=1; OUTPORT |= (1 << P_BLUE); OUTPORT &= ~(1 << P_BLUE_); } else { up[2]=0; OUTPORT |= (1 << P_BLUE_); OUTPORT &= ~(1 << P_BLUE); } }