PORTC=0xBF;
delay_ms(2);
PORTC=0x7F;
delay_ms(2);
}
i=0;
//===============================================
=====//
void dk_thu(void)
{
for(j=0;j<400;j++)
{
PORTA=0xFE ;
PORTC.4=0; PORTC.5=1;PORTC.6=1;PORTC.7=1;
delay_ms(2);
PORTA=0xFD ;
PORTC.4=1; PORTC.5=0;PORTC.6=1;PORTC.7=1;
delay_ms(2);
PORTA=0xFB
;
PORTC.4=0; PORTC.5=1;PORTC.6=1;PORTC.7=1;
delay_ms(2);
PORTA=0xF7
;
delay_ms(2);
}
i=0;j=0;
}
//====================================================
=//
void khoi_tao(void)
{
// Declare your local variables here
// Input/Output Ports initialization
// Port A initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In
Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T
State2=T State1=T State0=T
PORTA=0x00;
DDRA=0xff;
// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In
Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T
State2=T State1=T State0=T
PORTB=0x00;
DDRB=0xFF;
// Port C initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In
Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T
State2=T State1=T State0=T
PORTC=0x00;
DDRC=0xFF;
// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In
Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T
State2=T State1=T State0=T
PORTD=0x00;
DDRD=0x00;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=FFh
// OC0 output: Disconnected
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;
//
//
//
//
//
//
//
//
//
Timer/Counter 1 initialization
Clock source: System Clock
Clock value: Timer 1 Stopped
Mode: Normal top=FFFFh
OC1A output: Discon.
OC1B output: Discon.
Noise Canceler: Off
Input Capture on Falling Edge
Timer 1 Overflow Interrupt: Off
khoi_tao();
while (1)
{
Tuy chan cam ma kich nge
};
}
void runto_xy(unsigned int x,unsigned int y)
{timer1=0;timer2=0;k=0;step=0;l=0;step1=0;rem=1;a=100;
b=100;
TIMSK=0x01;//cho phep ngat timer
while(2)
{
if(k<=abs(x-x_ht))
{
if(x>x_ht)
tien_x();
else
lui_x();
}
else
a=0;
if(step>=20)
{
step=0;
k++;
}
if(l<=abs(y-y_ht))
{
if(y>y_ht)
tien_y();
else
lui_y();
}
else
b=0;
if(step1>=20)
{
step1=0;
l++;
}
if((l>=abs(y-y_ht))&& (k>=abs(x-x_ht)) )
break;
} //end while
TIMSK=0x00;//ngat timer
PORTA=255;PORTC=255;x_ht=x;y_ht=y;rem=0;l=0;k=0 ;
}
void draw_line(unsigned int x1, unsigned int
y1,unsigned int x2,unsigned int y2)
{ unsigned int delta_x,delta_y;
if(x1>=x2)
delta_x=x1-x2;
else
delta_x=x2-x1;
if(y1>=y2)
delta_y=y1-y2;
else
delta_y=y2-y1;
if(delta_x >=delta_y)
//tinh thoi gian phoi hop 2
dong co buoc
{
a=100; //truc x di chuyen nhanh hon
if(y1!=y2)
//neu toa do y1 trung y2 thi ko chay
dc buoc
b=((float)delta_x/delta_y)*100;
else
b=0;
//*************************
timer1=timer2=0;k=0;step=0;rem=0;
TIMSK=0x01;//cho phep ngat timer
while(k<=abs(x2-x1))
{
//xac dinh chieu chay 2 truc
if(x2>=x1)
tien_x();
else
lui_x();
if(b!=0)
{
if(y2>=y1)
tien_y();
else
lui_y();
}
else
PORTA=255;
if(step>=20)
{
step=0;
k++;
}
}
TIMSK=0x00; //ko cho phep ngat timer
}
else
{
b=100;
if(x1!=x2)
a=((float)delta_y/delta_x)*100;
else
a=0;
//*************************
timer1=timer2=0;rem=3;k=0;step=0;
TIMSK=0x01; //cho phep ngat timer
while(k<=abs(y2-y1))
{
if(a!=0)
{
if(x2>=x1)
tien_x();
else
lui_x();
}
else
PORTC=255;
if(y2>=y1)
tien_y();
else
lui_y();
if(step>=20)
//20 buoc
{
step=0;
k++;
}
}
TIMSK=0x00;//tat che do ngat timer
}
k=0;PORTA=255;PORTC=255;rem=0;x_ht=x2;y_ht=y2;a=0;b=0
;
}