Arduino 代碼
- #include <OneWire.h>
- #include <DallasTemperature.h>
- #define ONE_WIRE_BUS 2 //DS18B20接至Arduino數(shù)字口2
- OneWire oneWire(ONE_WIRE_BUS);
- DallasTemperature sensors(&oneWire);
- byte comdata[3]={0}; //定義數(shù)組數(shù)據(jù),存放串口命令數(shù)據(jù)
- //int LED = 13; //定義LED連接的管腳
- void receive_data(void); //接受串口數(shù)據(jù)
- void test_do_data(void); //測試串口數(shù)據(jù)是否正確,并更新數(shù)據(jù)
- void setup()
- {
- Serial.begin(9600);
- //pinMode(LED, OUTPUT);
- // Start up the library
- sensors.begin();
- }
- void loop()
- {
- while (Serial.available() > 0) //不斷檢測串口是否有數(shù)據(jù)
- {
- receive_data(); //接受串口數(shù)據(jù)
- test_do_data(); //測試數(shù)據(jù)是否正確并更新數(shù)據(jù)
- }
- }
- ?
- void receive_data(void)
- {
- int i ;
- for(i=0;i<3;i++)
- {
- comdata[ i] = Serial.read();
- //延時(shí)一會,讓串口緩存準(zhǔn)備好下一個字節(jié),不延時(shí)可能會導(dǎo)致數(shù)據(jù)丟失,
- delay(2);
- }
- }
- void test_do_data(void) // 測試并執(zhí)行命令
- {
- if(comdata[0] == 0x55) //0x55和0xAA均為判斷是否為有效命令
- {
- if(comdata[1] == 0xAA)
- {
- if(comdata[2] == 0xFF)
- {
- sensors.requestTemperatures(); // 請求傳感器測量溫度
- float tempC = sensors.getTempCByIndex(0); // 讀取溫度值
- Serial.print(tempC);
- }
- }
- }
- }
復(fù)制代碼
LabVIEW設(shè)計(jì)LabVIEW 前面板設(shè)計(jì)如下 相應(yīng)的程序圖如下 ?
轉(zhuǎn)載請注明來源和作者,如有問題請回帖或Email聯(lián)系(lijinlei0907 @163.com) |