unsigned int ADC_Read(unsigned char channel) { unsigned int result = 0; unsigned char i; unsigned char j; unsigned int out = 0; unsigned char nres = 0; switch(channel) // 选择ADC0832 { case 1: ADC0832_CS1 = 0; ADC0832_CS2 = 1; ADC0832_CS3 = 1; ADC0832_CS4 = 1; break; case 2: ADC0832_CS1 = 1; ADC0832_CS2 = 0; ADC0832_CS3 = 1; ADC0832_CS4 = 1; break; case 3: ADC0832_CS1 = 1; ADC0832_CS2 = 1; ADC0832_CS3 = 0; ADC0832_CS4 = 1; break; case 4: ADC0832_CS1 = 1; ADC0832_CS2 = 1; ADC0832_CS3 = 1; ADC0832_CS4 = 0; break; default: return 0; } ADC0832_CLK = 0; ADC0832_DO = 1; ADC0832_CLK = 1; ADC0832_CLK = 0; ADC0832_DO = 1; ADC0832_CLK = 1; ADC0832_CLK = 0; result=0; 修改这段代码,使得4个ADC0832分别接收4个MPX4115的压力信号
时间: 2024-01-17 11:05:42 浏览: 87
首先,需要将MPX4115的压力信号接到ADC0832的输入端口上。假设分别将MPX4115的压力信号接到了ADC0832的CH0、CH1、CH2、CH3输入端口上。
然后,需要修改ADC_Read函数的switch语句部分,使得不同的通道对应不同的ADC0832输入通道。修改后的代码如下:
```
switch(channel)
{
case 1:
ADC0832_CS1 = 0;
ADC0832_CS2 = 1;
ADC0832_CS3 = 1;
ADC0832_CS4 = 1;
break;
case 2:
ADC0832_CS1 = 1;
ADC0832_CS2 = 0;
ADC0832_CS3 = 1;
ADC0832_CS4 = 1;
break;
case 3:
ADC0832_CS1 = 1;
ADC0832_CS2 = 1;
ADC0832_CS3 = 0;
ADC0832_CS4 = 1;
break;
case 4:
ADC0832_CS1 = 1;
ADC0832_CS2 = 1;
ADC0832_CS3 = 1;
ADC0832_CS4 = 0;
break;
default:
return 0;
}
// 将ADC0832的CH0、CH1、CH2、CH3输入通道对应到MPX4115的压力信号
switch(channel)
{
case 1:
ADC0832_CLK = 0;
ADC0832_DO = (MPX4115_CH0 > 0) ? 1 : 0;
ADC0832_CLK = 1;
ADC0832_CLK = 0;
ADC0832_DO = (MPX4115_CH0 > 1) ? 1 : 0;
ADC0832_CLK = 1;
ADC0832_CLK = 0;
ADC0832_DO = (MPX4115_CH0 > 2) ? 1 : 0;
ADC0832_CLK = 1;
ADC0832_CLK = 0;
ADC0832_DO = (MPX4115_CH0 > 3) ? 1 : 0;
ADC0832_CLK = 1;
break;
case 2:
ADC0832_CLK = 0;
ADC0832_DO = (MPX4115_CH1 > 0) ? 1 : 0;
ADC0832_CLK = 1;
ADC0832_CLK = 0;
ADC0832_DO = (MPX4115_CH1 > 1) ? 1 : 0;
ADC0832_CLK = 1;
ADC0832_CLK = 0;
ADC0832_DO = (MPX4115_CH1 > 2) ? 1 : 0;
ADC0832_CLK = 1;
ADC0832_CLK = 0;
ADC0832_DO = (MPX4115_CH1 > 3) ? 1 : 0;
ADC0832_CLK = 1;
break;
case 3:
ADC0832_CLK = 0;
ADC0832_DO = (MPX4115_CH2 > 0) ? 1 : 0;
ADC0832_CLK = 1;
ADC0832_CLK = 0;
ADC0832_DO = (MPX4115_CH2 > 1) ? 1 : 0;
ADC0832_CLK = 1;
ADC0832_CLK = 0;
ADC0832_DO = (MPX4115_CH2 > 2) ? 1 : 0;
ADC0832_CLK = 1;
ADC0832_CLK = 0;
ADC0832_DO = (MPX4115_CH2 > 3) ? 1 : 0;
ADC0832_CLK = 1;
break;
case 4:
ADC0832_CLK = 0;
ADC0832_DO = (MPX4115_CH3 > 0) ? 1 : 0;
ADC0832_CLK = 1;
ADC0832_CLK = 0;
ADC0832_DO = (MPX4115_CH3 > 1) ? 1 : 0;
ADC0832_CLK = 1;
ADC0832_CLK = 0;
ADC0832_DO = (MPX4115_CH3 > 2) ? 1 : 0;
ADC0832_CLK = 1;
ADC0832_CLK = 0;
ADC0832_DO = (MPX4115_CH3 > 3) ? 1 : 0;
ADC0832_CLK = 1;
break;
default:
return 0;
}
```
其中,MPX4115_CH0、MPX4115_CH1、MPX4115_CH2、MPX4115_CH3分别表示四个MPX4115的压力信号,假设其取值范围为0~15,表示0~5V的输入电压分别经过了ADC的0~15个刻度,需要将其转换为二进制信号输入到ADC0832中。
具体实现方式是:首先将ADC0832的CLK置为0,然后判断MPX4115的压力信号是否大于当前输入刻度,如果大于,则将DO置为1,否则置为0;然后将CLK置为1,再将CLK置为0,依次输入四个信号位,最后将CLK置为1,完成一次输入。
阅读全文