树莓派与物理环境交互:传感器应用与信号调理
1. 温度传感器示例
在与物理环境交互中,温度测量是常见的应用。下面是一个使用树莓派(RPi)和TMP36模拟温度传感器的代码示例:
using namespace std;
float getTemperature(int adc_value) { // from the TMP36 datasheet
float cur_voltage = adc_value * (3.30f/4096.0f); // Vcc = 3.3V, 12-bit
float diff_degreesC = (cur_voltage-0.75f)/0.01f;
return (25.0f + diff_degreesC);
}
int main(){
cout << "Starting the RPi TMP36 example" << endl;
SPIDevice *busDevice = new SPIDevice(0,0);
busDevice->setSpeed(5000000);
busDevice->setMode(SPIDevice::MODE0);
unsigned char send[3], receive[3];
send[0] = 0b00000110; // Reading single-ended input from channel 0
send[1] = 0b00000000; // Use 0b00000001 and 0b10000000 for MCP3008
超级会员免费看
订阅专栏 解锁全文
60

被折叠的 条评论
为什么被折叠?



