unsigned char ADC_Read(unsigned char channel) { unsigned int result = 0; unsigned char i; unsigned char j; 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; _nop_(); ADC0832_DO = 1; _nop_(); ADC0832_CLK = 1; _nop_(); ADC0832_CLK = 0; _nop_(); ADC0832_DO = 1; _nop_(); ADC0832_CLK = 1; _nop_(); ADC0832_CLK = 0; for(i = 0;i < 8;i++) // 读取ADC值 { result|=ADC0832_DO; ADC0832_CLK = 1; _nop_(); _nop_(); result <<= 1; if(ADC0832_DO) result |= 0x0001; ADC0832_CLK = 0; } for (i = 0;i < 8;i++) { j = 0; j = j | ADC0832_DO; ADC0832_CLK = 1; _nop_(); _nop_(); ADC0832_CLK = 0; _nop_(); _nop_(); j = j << 7; nres = nres | j; if (i < 7) nres >>= 1; } return nres; } 这段代码无法达成读取MPX4115检测值的功能,请修改
时间: 2024-01-17 10:03:33 浏览: 170
unsigned char数组转成string 用16进制表示
首先,需要了解MPX4115是一种压力传感器,其输出为模拟电压信号,需要通过ADC模块转换为数字量才能进行处理。因此,需要将MPX4115的输出接到ADC模块的输入端,然后通过调用ADC_Read函数来读取ADC模块转换后的数字量。
假设MPX4115的输出接到了ADC0832模块的第1路输入端,那么需要将ADC_Read函数中的channel参数改为1,即将switch语句中的case 1:下的代码保留,其余的删除。
另外,由于ADC0832模块的输出为8位数字量,而MPX4115的输出为10位数字量,需要修改ADC_Read函数中的两个for循环来适配MPX4115的输出精度。具体修改如下:
```
unsigned int ADC_Read(unsigned char channel)
{
unsigned int result = 0;
unsigned char i;
switch(channel) // 选择ADC0832
{
case 1:
ADC0832_CS1 = 0;
ADC0832_CS2 = 1;
ADC0832_CS3 = 1;
ADC0832_CS4 = 1;
break;
default:
return 0;
}
// 开始转换
ADC0832_CLK = 0;
ADC0832_DO = 1;
ADC0832_CLK = 1;
_nop_();
_nop_();
ADC0832_CLK = 0;
_nop_();
_nop_();
ADC0832_DO = 0;
ADC0832_CLK = 1;
_nop_();
_nop_();
ADC0832_CLK = 0;
_nop_();
_nop_();
ADC0832_DO = 1;
ADC0832_CLK = 1;
_nop_();
_nop_();
ADC0832_CLK = 0;
_nop_();
_nop_();
ADC0832_DO = 0;
ADC0832_CLK = 1;
_nop_();
_nop_();
ADC0832_CLK = 0;
_nop_();
_nop_();
// 读取转换结果
for(i = 0; i < 10; i++)
{
result <<= 1;
ADC0832_CLK = 1;
_nop_();
_nop_();
if(ADC0832_DO)
result |= 0x0001;
ADC0832_CLK = 0;
}
return result;
}
```
注意,这里假设ADC模块的输入范围为0~5V,如果MPX4115的输出范围不在此范围内,还需要进行电平转换或者放大缩小等处理。
阅读全文