私はライン フォロワーのコードを書いています。さまざまなチャネルから 7 つのアナログ値を読み取り、それに応じてロボットを動作させる必要があります。この問題に関するさまざまな種類のチュートリアルを読み、そこで読んだ内容を適用しようとしましたが、成功しませんでした。ここに私のコードがあります:
//all these # below set up the PIC
#include <16F877A.h>
#device adc=10
#FUSES NOWDT //No Watch Dog Timer
#FUSES HS //Highspeed Osc > 4mhz
#FUSES PUT //Power Up Timer
#FUSES NOPROTECT //Code not protected from reading
#FUSES NODEBUG //No Debug mode for ICD
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD //No EE protection
#use delay(clock=20000000) //crystal oscillator at 20000000 hertz
void fwrd(int16,int16);
void left(int16,int16,int16);
void right(int16,int16,int16);
unsigned long threshold = /*0b0011101100;*/ 0b1011001100;
unsigned long a0,a1,a2,a3,a4,a5,a6,S_dec,S_dec_last;
unsigned short d0,d1,d2,d3,d4,d5,d6;
unsigned short big=40;
unsigned short middle=30;
unsigned short small=20;
unsigned short current_duty=59;
unsigned short current_duty1=41,gtime=80;
//main program starts here
void main() {
setup_adc(ADC_CLOCK_DIV_32); //configure analog to digiral converter
setup_adc_ports(ALL_ANALOG); //set pins AN0-AN7 to analog
while(true){
set_adc_channel(0);//set the pic to read from AN0
delay_us(20);//delay 20 microseconds to allow PIC to switch to analog channel 0
a0=read_adc(); //read input from pin AN0: 0<=photo<=255
set_adc_channel(1);//set the pic to read from AN1
delay_us(20);
a1=read_adc();
set_adc_channel(2); //set the pic to read from AN2
delay_us(20);
a2 = read_adc();
set_adc_channel(0);//set the pic to read from AN0
delay_us(20);//delay 20 microseconds to allow PIC to switch to analog channel 0
a0=read_adc(); //read input from pin AN0: 0<=photo<=255
set_adc_channel(1);//set the pic to read from AN1
delay_us(20);
a1=read_adc();
set_adc_channel(2); //set the pic to read from AN2
delay_us(20);
a2 = read_adc();
set_adc_channel(3);//set the pic to read from AN4
delay_us(20);//delay 20 microseconds to allow PIC to switch to analog channel 4
a3=read_adc();
set_adc_channel(4);//set the pic to read from AN1
delay_us(20);
a4=read_adc();
set_adc_channel(5); //set the pic to read from AN5
delay_us(20);
a5 = read_adc();
set_adc_channel(6);//set the pic to read from AN6
delay_us(20);//delay 20 microseconds to allow PIC to switch to analog channel 4
a6=read_adc();
if (a0<threshold)
d0=0;
else d0=1;
if (a1<threshold)
d1=0;
else d1=1;
if (a2<threshold)
d2=0 ;
else d2=1;
if (a3<threshold)
d3=0 ;
else d3=1;
if (a4<threshold)
d4=0 ;
else d4=1;
if (a5<threshold)
d5=0 ;
else d5=1;
if (a6<threshold)
d6=0 ;
else d6=1;
S_dec=d6*64+d5*32+d4*16+d3*8+d2*4+d1*2+d0;
if (S_dec!=0b0000000)
{S_dec_last=S_dec;
switch (S_dec)
{
case 0b1101000:
case 0b1111000:
case 0b1111100:
case 0b1111110:
case 0b1101100:
case 0b1001100:
case 0b1011000:
left(4.4*gtime,current_duty1-big,current_duty+big);
break;
case 0b1000000:
case 0b1100000:
case 0b1110000:
left(3.6*gtime,current_duty1-big,current_duty+big);
break;
case 0b0100000:
case 0b0110000:
case 0b0111000:
left(3*gtime,current_duty-big,current_duty+big);
break;
case 0b0010000:
case 0b0011000:
left(1.2*gtime,current_duty-middle,current_duty+small);
break;
case 0b0001000:
case 0b0011100:
fwrd(gtime,current_duty+middle);
break;
case 0b0001100:
case 0b0000100:
right(1.2*gtime,current_duty-middle,current_duty+small);
break;
case 0b0000110:
case 0b0000010:
case 0b0001110:
right(3*gtime,current_duty-middle,current_duty+big);
break;
case 0b0000111:
case 0b0000011:
case 0b0000001:
right(3.6*gtime,current_duty1-big,current_duty+big);
break;
case 0b0001011:
case 0b0001111:
case 0b0011111:
case 0b0111111:
case 0b0011011:
case 0b0011001:
case 0b0001101:
right(4.4*gtime,current_duty1-big,current_duty+big);
break;
}
} else
S_dec=S_dec_last;
switch (S_dec)
{
case 0b1101000:
case 0b1111000:
case 0b1111100:
case 0b1111110:
case 0b1101100:
case 0b1001100:
case 0b1011000:
left(4.4*gtime,current_duty1-big,current_duty+big);
break;
case 0b1000000:
case 0b1100000:
case 0b1110000:
left(3.6*gtime,current_duty1-big,current_duty+big);
break;
case 0b0100000:
case 0b0110000:
case 0b0111000:
left(3*gtime,current_duty-big,current_duty+big);
break;
case 0b0010000:
case 0b0011000:
left(1.2*gtime,current_duty-middle,current_duty+small);
break;
case 0b0001000:
case 0b0011100:
fwrd(gtime,current_duty+middle);
break;
case 0b0001100:
case 0b0000100:
right(1.2*gtime,current_duty-middle,current_duty+small);
break;
case 0b0000110:
case 0b0000010:
case 0b0001110:
right(3*gtime,current_duty-middle,current_duty+big);
break;
case 0b0000111:
case 0b0000011:
case 0b0000001:
right(3.6*gtime,current_duty1-big,current_duty+big);
break;
case 0b0001011:
case 0b0001111:
case 0b0011111:
case 0b0111111:
case 0b0011011:
case 0b0011001:
case 0b0001101:
right(4.4*gtime,current_duty1-big,current_duty+big);
break;
}
}
}
お役に立てば幸いです。