Anda di halaman 1dari 4

#include

#include
#include
#include
#include

<avr/io.h>
<avr/interrupt.h>
<stdlib.h>
<util/delay.h>
<avr/wdt.h>

void write(uint8_t data);


unsigned char matrix[8] ;
unsigned char matrix1[8] ;
unsigned char matrix2[8] ;
static int k;
static int z;
static int w;
static int m;
void clearmatrix()
{for(int i=0; i<8; i++)
{
matrix[i] = 0x00;
}
}
void clearmatrix1()
{for(int i=0; i<8; i++)
{
matrix1[i] = 0x00;
}
}
void enemy(unsigned char *s,int x,int y)
{
unsigned char ch;
ch= *(s+y);
*(s+y) = (ch|(1<<x));
}
void setpixel(unsigned char *s,int x,int y)
{
unsigned char ch1;
ch1 = *(s+(y-1));
*(s+(y-1)) = (ch1 | (128>>x)|(64>>x));
unsigned char ch2;
ch2 = *(s+(y+1));
*(s+(y+1)) = (ch2 | (128>>x)|(64>>x));
unsigned char ch3;
int i= 0;
ch3 = *(s+y);
if(bit_is_clear(PINB,4))
{while(x+i !=8)
{
*(s+y) = (ch3 | (128 >> x+ (i++)));
_delay_ms(2);
if(x+i==w && y==z) {clearmatrix1();z=rand()%4;w=0;}
}

}
else
ch3 = *(s+(y));
*(s+(y)) = (ch3 | (64>>x)|(32>>x));}

void resetpixel(unsigned char *s , int x ,int y)


{
unsigned char ch ;
ch = *(s+y);
*(s+y) = (ch & ~(128>>x));
}
void ispixel(unsigned char *s ,int x,int y )
{
unsigned char ch;
ch = *(s+y);
if( (ch & (128>>x)) != 0 )
return 1;
else
return 0;
}

int main(void)
{
wdt_disable();
DDRD |= 0b11111111;
DDRC |= 0b00000111;
DDRB |= 0b00000000;
PORTC = 0x00;
PORTB = 0xff;
PORTD = 0b00000000;
// TIMER1 INITIALIZATION
TCCR1B |= (1 << WGM12);
TIMSK |=(1 << OCIE1A);
OCR1A = 124;
TCCR1B |= (1 << CS10) | (1 << CS11);
clearmatrix();
sei();
int x=0, y=0; int i =0;int j =0;
while(1)
{
if(bit_is_clear(PINB,2))
x--;
if(bit_is_clear(PINB,3))
x++;
if(bit_is_clear(PINB,1))
y--;

if(bit_is_clear(PINB,0))
y++;
if(x<0) x=0;
if(x>7) x=7;
if(y<0) y=0;
if(y>4) y=4;
if(i == 7){
if( j==7){ z=rand()%5;j=0;}
w++;
if(w>7) w=0;
j++;
}
clearmatrix();
enemy(matrix1,w,z);
setpixel(matrix,x,y);
_delay_ms(5);
i++;
if(i>7) i=0; }
}
ISR(TIMER1_COMPA_vect)
{ if(m%2)
{write(matrix[k]);
PORTD = (1<<k) ;
k++;
if (k>4) k=0;
m++;
}
else
{
write(matrix1[z]);
PORTD = (1<<z) ;
m++;
}
}

void write(uint8_t data)


{
for (uint8_t i=0 ; i<8 ; i++)
{if (data & 0b10000000)
{PORTC |= (1<<PINC0) ;}
else
{PORTC &= ~(1<< PINC0);}
PORTC |= (1 << PINC1) ;
_delay_us(1);
PORTC &= ~(1 << PINC1) ;
_delay_us(1);
data = data<<1 ;
}
PORTC |= (1 << PINC2) ;
_delay_us(10) ;

PORTC &= ~ (1 << PINC2) ;


_delay_us(10) ;
}

Anda mungkin juga menyukai