Тема: AVR
Показать сообщение отдельно
Старый 18.11.2013, 18:02   #14
Pilot
 
Адрес: Киев
Сообщений: 335
Длина: 5700мкм
Диаметр: 34мм
Отправить сообщение для Pilot с помощью ICQ
По умолчанию

ну в общем, есть вариант (недопиленный пока что) программы без GoTO (на функциях):

/*
* encoder.cpp
*
* Created: 28.10.2013 16:50:50
* Author: PVD
*/


#include <avr/io.h>
#include <delay/io.h>



char com;
char comander;
char enc;









//Задатчик;

//Таблица соответствия состояния энкодера / задатчика положения / индикации положения;

// № | PA4 PA5 PA6 | PB0 PB1 PB2 PB3 | PD2-6;
// --------------------------------------------------;
// 1 | 0 1 1 | 1 1 x 1 | 00010;
// 2 | 0 0 1 | 0 1 0 1 | 11010;
// 3 | 1 0 1 | 0 1 1 1 | 10010;
// 4 | 1 0 0 | x x x 0 | 00001;
// 5 | 1 1 0 | - - - - | 00001;
// 6 | 0 1 0 | 0 0 x 1 | 11110;
// 7 | 0 1 0 | 1 0 x 1 | 00110;








int GetEncoder(int)
{
enc=(PORTA & 0b0111000);
if (enc==0b01100000) {(PORTD=0b00010000); return 1;}
else if (enc==0b01000000) {(PORTD=0b00010110); return 2;}
else if (enc==0b01010000) {(PORTD=0b00010010); return 3;}
else if (enc==0b00010000) {(PORTD=0b00100000); return 4;}
else if (enc==0b00110000) {(PORTD=0b00100000); return 5;}
else if (enc==0b00100000) {(PORTD=0b00011110); return 6;}
else
{
PORTD=0b10000000;
if (PORTB0==1) {ErrorHi();}
else {ErrorLo();}
}
}



int ErrorHi(void)
{
PORTC=0b00000000;
while (PORTA0==0) {PORTC0=1;};
PORTC=0b00000000;
PORTD=0b10011110;
while (PORTB0==1)
{
PORTC2=PINB0;
if (PINA3==0 && PINC2==0) {(PIND0=0); (PIND1=0);}
else if (PINA3==0 && PINC2==1) {(PIND0=0); (PIND1=1);}
else if (PINA3==1 && PINC2==0) {(PIND0=0); (PIND1=1);}
else (PINA3==1 && PINC2==1) {(PIND0=1); (PIND1=1);}
while (PINB7=0) {Manual();}
}
ErrorLo();
}



int ErrorLo(void)
{
PORTC=0b00000000;
while (PORTA0==0) {PORTC1=1};
PORTC=0b00000000;
PORTD=0b10000010;
while (PORTB0==0)
{
while (PINB7=0) {Manual();}
}
ErrorHi();
}



int Manual(int)
{
PORTC2=PINB0;
if (PINA3==0 && PINC2==0) {(PIND0=0); (PIND1=0);}
else if (PINA3==0 && PINC2==1) {(PIND0=0); (PIND1=1);}
else if (PINA3==1 && PINC2==0) {(PIND0=0); (PIND1=1);}
else (PINA3==1 && PINC2==1) {(PIND0=1); (PIND1=1);}

enc=(PORTA & 0b0111000);
if (enc==0b01100000) {PORTD=0b00010000;}
else if (enc==0b01000000) {PORTD=0b00010110;}
else if (enc==0b01010000) {PORTD=0b00010010;}
else if (enc==0b00010000) {PORTD=0b00100000;}
else if (enc==0b00110000) {PORTD=0b00100000;}
else if (enc==0b00100000) {PORTD=0b00011110;}
else PORTD=0b00011110;
}



int Critical(void)
{
while (1)
{
PORTD=0b10000000;
_delay_ms(1000);
PORTD=0b00000000;
_delay_ms(1000);
}
}



while(1)
{
int main(void)


PORTA=0b11111111;
DDRA=0b00000000;

PORTB=0b11111111;
DDRB=0b00000000;

PORTC=0b00000000;
DDRC=0b11111111;

PORTD=0b00000000;
DDRD=0b11111111;



PORTD=0b11111111;
_delay_ms(2000);
PORTD=0b00000000;

while (1)
{
{
while (PINB7=0) {Manual()};

{
com=(PORTB & 0b00001111)
while (comander=0)
{
_delay_ms(100)
if (com==0b00001011) comander=1
else if (com==0b00001010) comander=2
else if (com==0b00001110) comander=3
else if (com==0b00000000) comander=4
else if (com==0b00001000) comander=6
else if (com==0b00001001) comander=6
else comander=0;
}
}

if (GetEncoder() < comander)
{
PORTC=0b00000000;
while (GetEncoder() < comander) {PORTC=0b00000001};
while ((GetEncoder() = comander) && (PINA7 = 1)) {PORTC=0b00001001};
PORTC=0b00000000;
if (GetEncoder() = comander) {}
else {ErrorHi()};
}

else if (GetEncoder() > comander)
{
PORTC=0b00000000;
while (GetEncoder() > comander) {PORTC=0b00000010};
while ((GetEncoder() = comander) && (PINA7 = 1)) {PORTC=0b00001010}
PORTC=0b00000000;
if (GetEncoder() = comander) {}
else {ErrorLo()}
}

else
{
PORTC2=PINB01;
if (PINA3==0 && PINC2==0) {(PIND0=0) (PIND1=0)}
else if (PINA3==0 && PINC2==1) {(PIND0=0) (PIND1=1)}
else if (PINA3==1 && PINC2==0) {(PIND0=0) (PIND1=1)}
else (PINA3==1 && PINC2==1) {(PIND0=1) (PIND1=1)};
}
}
}
__________________
Надейся на лучшее, расчитывай на худшее
Люблю Таврооргию. Такого пох№зма и подъ№балова нигде не встретишь! (c) Lusi

Последний раз редактировалось Pilot; 18.11.2013 в 18:22.
Pilot вне форума   Ответить с цитированием