Супермодератор
Сообщений всего: 403
Дата рег-ции: Нояб. 2008
|
Помогите разобраться с задержкой ,задний мост весит на порте D 2 и 3,передний на порте D 4 и 5
#include <mega16.h>
#include <stdlib.h>
unsigned int gtime;
int v, z, e, n, k, l;
unsigned char d[7][7]={
// STO F FR FL B BR BL
/*STO*/ {1, 18, 18, 18, 18, 17, 13},
/*F */ {7, 26, 21, 21, 10, 8, 7},
/*FR */ {7, 23, 23, 27, 8, 6, 6},
/*FL */ {7, 23, 7, 43, 8, 6, 6},
/*B */ { 4, 10, 10, 5, 28, 22, 21},
/*BR */ { 6, 13, 9, 8, 35, 22, 7},
/*BL */ {16, 8, 6, 6, 35, 8, 21}
};
unsigned char now, h;
int gTimeOfMove = 1000;
interrupt [TIM1_COMPA] void timer1_compa_isr(void)
{
if (gtime!=0)--gtime;
}
void delay (unsigned int time){
gtime=time;
while(gtime!=0){};
}
void f(int t){
PORTD.4=0;
PORTD.5=0;
PORTD.2=0;
if(t!=0) delay(t);
while (v>0) {
PORTD.3=1;
delay(150);
PORTD.3=0;
delay(150);
}
}
void b(int t){
PORTD.3=0;
PORTD.4=0;
PORTD.5=0;
if(t!=0) delay(t);
while (n>0) {
PORTD.2=1;
delay(150);
PORTD.2=0;
delay(150);
}
}
void fl(int t){
PORTD.2=0;
PORTD.4=0;
PORTD.5=1;
if(t!=0) delay(t);
while (e>0) {
PORTD.3=1;
delay(150);
PORTD.3=0;
delay(150);
}
}
void fr(int t){
PORTD.2=0;
PORTD.4=1;
PORTD.5=0;
if(t!=0) delay(t);
while (z>0) {
PORTD.3=1;
delay(150);
PORTD.3=0;
delay(150);
}
}
void bl(int t){
PORTD.3=0;
PORTD.4=0;
PORTD.5=1;
if(t!=0) delay(t);
while (l>0) {
PORTD.2=1;
delay(150);
PORTD.2=0;
delay(150);
}
}
void br(int t){
PORTD.3=0;
PORTD.4=1;
PORTD.5=0;
if(t!=0) delay(t);
while (k>0) {
PORTD.2=1;
delay(150);
PORTD.2=0;
delay(150);
}
}
(Добавление)
void stop(int t){
PORTD.2=0;
PORTD.3=0;
PORTD.4=0;
PORTD.5=0;
if(t!=0) delay(t);
}
void led_on(void){
PORTD.6=1;}
void led_off(void){
PORTD.6=0;}
void go(unsigned char napr){
switch(napr){
case 1:
stop(gTimeOfMove);
break;
case 2:
f(gTimeOfMove);
v=1;
break;
case 3:
fr(gTimeOfMove);
z=1;
break;
case 4:
fl(gTimeOfMove);
e=1;
break;
case 5:
b(gTimeOfMove);
n=1;
break;
case 6:
br(gTimeOfMove);
k=1;
break;
case 7:
bl(gTimeOfMove);
l=1;
break;
};
}
unsigned char next_move(void){
int a, b, c;
a = rand()/327;
c=0;
for (b=1; b<=7; b++){
if(a > c && a < (d[now][b]+c) ) break;
c=c+d[now][b];
};
if (b==8)b=1;
now = b;
return b;
};
void start(void){
while(1){
h=next_move();
go(h);
led_on();
delay(100);
led_off();
}
};
void main(void)
{ int i;
PORTB=0x00;
DDRB=0x00;
PORTC=0x00;
DDRC=0x7F;
PORTA=0x00;
DDRA=0xFF;
TCCR0=0x00;
TCNT0=0x00;
TCCR1A=0x00;
TCCR1B=0x0D;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x0F;
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
MCUCR=0x00;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x10;
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;
// Global enable interrupts
#asm("sei"
while (1)
{
now=1;
for (i=0;i<=5;i++) {
led_on();
delay(500);
led_off();
delay(500);
};
start();
};
} |