利用Arduino Uno进行电压电流和温度采集,请教程序问题。

      我使用的温度传感器是DS18B20,电表是一款可以进行RS485串口通信的过功能电表,可以读取电压、电流、功率等信息。编程时读取温度和读取电压电流;两部分都可以单独运行,但是将两个程序合到一起时编译会报错,实在不懂问题出在哪里,如何解决,在此请教大家!

读取温度并存入SD卡的程序:

#include
#include
#include
#include
#include  


#define ONE_WIRE_BUS1 2
#define ONE_WIRE_BUS2 5
#define ONE_WIRE_BUS3 8


rgb_lcd lcd;
const int colorR = 10;
const int colorG = 10;
const int colorB = 10;


 
// 初始连接在单总线上的单总线设备
OneWire oneWire1(ONE_WIRE_BUS1);
OneWire oneWire2(ONE_WIRE_BUS2);
OneWire oneWire3(ONE_WIRE_BUS3);
DallasTemperature sensors1(&oneWire1);
DallasTemperature sensors2(&oneWire2);
DallasTemperature sensors3(&oneWire3);


File File1;
  //用库中的File命令创建一个类的实例,命名为File1


void setup() 
{
  
// 设置串口通信波特率
  Serial.begin(9600);


while (!Serial){ }
    //检查串口是否有输出
  Serial.println("Initializing the SD Card...");
  if(!SD.begin())
  {
   Serial.println("Initializition Failed!");
   return; 
  }
  Serial.println("Initialization Complete.\n");
   
  
  Serial.println("Looking for file 'testfile.txt'...\n"); 
    //"\n"是换行符
  if (SD.exists("testfile.txt"))
    //SD.exists功能是检测查找的文件是否存在
  {
    Serial.println("testfile.txt already exists.\n");
   }
   else
   {
   Serial.println("testfile.txt doesn't exists.") ;
   Serial.println("Creating file testfile.txt...\n") ;
   }
  
 
  
  Serial.println("Dallas Temperature IC Control Library Demo");
  // 初始库
  sensors1.begin();
  sensors2.begin();
  sensors3.begin();
  
  //LCD
  lcd.begin(16, 2);
  lcd.setRGB(colorR, colorG, colorB);
  lcd.setCursor(9, 0);
  lcd.print("Tem");
  lcd.setCursor(0, 0);
  lcd.print("Null");
  lcd.setCursor(0, 1);
  lcd.print("Null");
  lcd.setCursor(9, 1);
  lcd.print("Null");    
  delay(1000);
  
  
}


void loop() {


  Serial.println("Requesting temperatures...");
  sensors1.requestTemperatures(); // 发送命令获取温度
  Serial.print("Temperature of device 1 (index 0) is: ");
  Serial.println(sensors1.getTempCByIndex(0));  
  lcd.setCursor(0, 0);
  lcd.print(sensors1.getTempCByIndex(0));  
  delay(100);
  sensors2.requestTemperatures(); // 发送命令获取温度
  Serial.print("Temperature of device 2 (index 0) is: ");
  Serial.println(sensors2.getTempCByIndex(0));   
  lcd.setCursor(0, 1);
  lcd.print(sensors2.getTempCByIndex(0)); 
  delay(100);
  sensors3.requestTemperatures(); // 发送命令获取温度
  Serial.print("Temperature of device 3 (index 0) is: ");
  Serial.println(sensors3.getTempCByIndex(0));    
  lcd.setCursor(9, 1);
  lcd.print(sensors3.getTempCByIndex(0));   
  delay(100);
  Serial.println("DONE");
   


    double TemString1; //将datastring变量设为String类型
    double TemString2;
    double TemString3;




   File1 = SD.open("testfile.txt", FILE_WRITE);   
  if(File1)
   {
     Serial.println("File Opened");
     TemString1 = sensors1.getTempCByIndex(0);


     TemString2 = sensors2.getTempCByIndex(0);
 
     TemString3 = sensors3.getTempCByIndex(0);
     
     File1.print("Tem1: ");
     File1.println(TemString1); 
     delay(200);
     File1.print("Tem2: ");
     File1.println(TemString2);    
     delay(200); 
     File1.print("Tem3: ");
     File1.println(TemString3);
     delay(1000);
   }
   File1.close();


读取电压电流的程序:



#include //modbusmaster库
#include //arduino自带的库


#include
#include
#include


#include
#include


rgb_lcd lcd;


const int colorR = 60;
const int colorG = 0;
const int colorB = 30;


uint16_t m_startAddress = 20;//要读取的从站起始地址
uint16_t m_startAddress1 = 28;


uint16_t m_length = 1;//从从站起始地址开始你需要读取的数据的长度
uint16_t m_length1 = 1;
uint16_t result;//串口通信结果,不用明白是什么,只要知道这不是你最终需要的数据就行
uint16_t result1;




ModbusMaster node(1);//对1号从站进行通信
ModbusMaster node1(1);
void setup() {
  node.begin(9600);//定义arduino与从站之间的串口波特率
  node1.begin(9600);
  lcd.begin(16, 2);//初始化lcd1602
  lcd.setRGB(colorR, colorG, colorB);
  }


void loop() {


  result = node.readHoldingRegisters(m_startAddress, m_length);//调用相关函数
  result1 = node1.readHoldingRegisters(m_startAddress1, m_length1);
  if (result == node.ku8MBSuccess) {//如果通信成功
    lcd.setCursor(0, 0);//打印lcd第1行信息
    lcd.print("UA:");
    for (uint16_t j = 0; j < m_length; j++)
    {
      lcd.print( node.getResponseBuffer(j), DEC );
    }


    lcd.setCursor(0, 1);//打印lcd第2行信息
    lcd.print("IA:");
    for (uint16_t j = 0; j < m_length1; j++)
    {
      lcd.print( node1.getResponseBuffer(j), DEC );//以DEC(十)进制显示你要的数据
    }
  }
  else {
    lcd.setCursor(0, 1);//打印lcd第2行信息
    lcd.print("ERROR:");
    lcd.print(result, HEX);//以HEX(16)进制显示错误信息(E2:你的TX,RX断线,E0:你的485转ttl板子有问题)
  }
delay(500);
lcd.clear();
}


拼到一起之后的头文件:



#include //modbusmaster库


#include
#include
#include
#include
#include

void setup() {
  // put your setup code here, to run once:


}

void loop() {
  // put your main code here, to run repeatedly:


}


此时编译便会出现如下错误提示:



















D:\Working Softwares\Arduino items\libraries\OneWire\OneWire.cpp: In static member function 'static uint16_t OneWire::crc16(const uint8_t*, uint16_t, uint16_t)':


D:\Working Softwares\Arduino items\libraries\OneWire\OneWire.cpp:554:42: error: '_crc16_update' was not declared in this scope


         crc = _crc16_update(crc, input[i]);


                                          ^


exit status 1

Error compiling for board Arduino/Genuino Uno.


请问如何解决?谢谢!!!!!!!!!

你可能感兴趣的:(利用Arduino Uno进行电压电流和温度采集,请教程序问题。)