0

我正在从传感器接收数据到我的 pic24f 板,但只显示来自 an0 的值,而不显示 an1。

我是微芯片板的新手,需要帮助。我知道只有 1 个 A/DC,所以需要一个循环来处理每个模拟输入。

这是我使用的下面的代码。

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 有效。我什至将 an0 放在 case1 中,但值不是 an0 给我的。

4

0 回答 0