问题描述
我正在从传感器接收数据到我的 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 有效。我什至为 case1 设置了 an0,但值不是 an0 给我的。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)