Anda di halaman 1dari 8

/*****************************************************

Project : LE HUU BACH


Version :
50 NTU
Date
: 3/31/2011
Author : F4CG
Company : F4CG
Comments:
Chip type
: ATmega32
Program type
: Application
Clock frequency
: 11.059200 MHz
Memory model
: Small
External SRAM size : 0
Data Stack size
: 512
*****************************************************/
#include <mega32.h>
#include <delay.h>
int i,j,tam;
unsigned char mang1[]={0xFE,0xFD,0xFB,0xF7}; //
unsigned char mang2[]={0xFE,0xFD,0xFB,0xF7}; // FULL
STEEP
unsigned char mang3[]={0xF7,0xFB,0xFD,0xFE} ; //
//mang dieu khien dong co buoc
char
du_lieu1[8]={0xFE,0xFC,0xFD,0xF9,0xFB,0xF3,0xF7,0xF6};/
/truc y tang // //half steep
char
du_lieu2[8]={0xFE,0xFC,0xFD,0xF9,0xFB,0xF3,0xF7,0xF6};/
/truc x giam //
char
du_lieu3[8]={0xF6,0xF7,0xF3,0xFB,0xF9,0xFD,0xFC,0xFE};/
/truc x tang
//
void
//===================================================/
/
void dk_dc1(void)
{
for(i=0;i<400;i++)
{
PORTC=0xEF ;
delay_ms(2);
PORTC=0xDF ;
delay_ms(2);

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

// Input Capture Interrupt: Off


// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;
// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// INT2: Off
MCUCR=0x00;
MCUCSR=0x00;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x00;
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1:
Off
ACSR=0x80;
SFIOR=0x00;
}
void main(void)
{

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
;
}

Anda mungkin juga menyukai