センサーから pic24f ボードにデータを受信していますが、an1 ではなく an0 からの値のみが表示されます。
マイクロチップ ボードを初めて使用するので、助けが必要です。A/DC が 1 つしかないことはわかっているので、各アナログ入力を処理するループが必要です。
これが私が作業している以下のコードです。
void SelectPort(int ch)
{
AD1CON1bits.ADON = 0; // Turn off the ADC to reconfigure
switch(ch) // set values based on the channel to use
{
case 0: // select AN0 as analog input
AD1PCFG = 0xFFFE; //0xFFFE
break;
case 1:
AD1PCFG = 0xFFFE;//0xFFFE; // select AN1 as analog input
break;
case 2:
AD1PCFG = 0xFFFB; // select AN2 as analog input
break;
// there's only so many options here, so there's not really a default case
}
AD1CON1bits.ADON = 1; // Turn on the ADC
AD1CHS = ch; // 1. select analog input channel
}
/**
* @brief Read value from ADC based on selected channel via SelectPort()
*
* @return Value from ADC
*/
int ReadADCTest(void)
{
AD1CON1bits.SAMP = 1; // 2. Start sampling.
while (!AD1CON1bits.DONE); //5. wait for conversion to complete
AD1CON1bits.DONE = 0; // 6. clear flag. We are responsible see text.
return ADC1BUF0; // 7. read the conversion results
}
main() {
InitADC(0xFFEF); // initialize the ADC and analog inputs
char x_string [12];
char y_string [12];
char buffer [40];
TRISB = 1; // all PORTB pins as outputs
InitPMP(); // Initialize the Parallel Master Port
InitLCD(); // Initialize the LCD
float x_val;
float y_val;
float z_val;
float x_axis, y_axis, z_axis;
int i;
I2Cinit(157);
InitU2();
while (1) // main loop
{
SelectPort(0);
x_axis= ReadADCTest();
x_val= ((((x_axis * 3.3)/ 1024)-1.58)/0.330)*1000 ;
sprintf(x_string, "X: %0.2f ", x_val);
SelectPort(1);
y_axis= ReadADCTest();
y_val= ((((y_axis * 3.3)/ 1024)-1.58)/0.330)*1000 ;
sprintf(y_string, "Y: %0.2f ", y_val);
}
}
アナログ入力が機能するように初期化しているかどうかわかりません。明らかに an0 が動作します。私は case1 に an0 を入れましたが、値は an0 が私に与えているものではありません。