반응형

CFMEGA2는 2개의 RS232 통신 채널이 있다. 

컴퓨터와 USB로 연결하고 시리얼 통신을 하는 경우는 아래 링크의 예와 같이 그냥 Serial 클래스를 사용하면 되지만 RS232 통신을 위한 핀(TX, RX, SG)을 직접 사용해 통신하는 경우는 Serial1, Serial2 클래스를 사용해야 한다.

 

2021.03.24 - [Embedded] - Digital Temperature Sensor DS18B20 with Arduino - 아두이노 방수 온도 센서

 

 

Serial1(Tx1, Rx1, SG)에 연결

 

#include <OneWire.h>
#include <DallasTemperature.h>
#include <LowPower.h>

// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 2
#define TEMPERATURE_PRECISION 9 // Lower resolution

// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);

int numberOfDevices; // Number of temperature devices found
uint8_t sensor[3][8] = { { 0x28, 0xB8, 0x22, 0x94, 0x65, 0x25, 0x06, 0x5F},
                         { 0x28, 0xBB, 0x38, 0x60, 0x65, 0x25, 0x06, 0xB8},
                         { 0x28, 0x82, 0xBD, 0x0B, 0x92, 0x25, 0x06, 0x29} };

void setup(void)
{
  // start Serial1 port
  Serial1.begin(9600);
  
  // Start up the library
  sensors.begin();

  // Grab a count of devices on the wire
  numberOfDevices = sensors.getDeviceCount();

  // locate devices on the bus
  Serial1.print("Locating devices... ");
  Serial1.print("Found ");
  Serial1.print(numberOfDevices, DEC);
  Serial1.println(" devices.");

  // report parasite power requirements
  Serial1.print("Parasite power is: ");
  if (sensors.isParasitePowerMode())
    Serial1.println("ON");
  else
    Serial1.println("OFF");
  
  Serial1.print("Setting resolution to ");
  Serial1.println(TEMPERATURE_PRECISION, DEC);

  // set the resolution to 9 bit per device
  for (uint8_t i = 0; i < numberOfDevices; i++){
    if (!sensors.setResolution(sensor[i], TEMPERATURE_PRECISION))
      Serial1.print("Failed to set resolution");
  }
  
  for (uint8_t i = 0; i < numberOfDevices; i++){
    Serial1.print("Sensor ");
    Serial1.print(i);
    Serial1.print(" Resolution: ");
    Serial1.println(sensors.getResolution(sensor[i]), DEC);
  }  
}

// function to print the temperature for a device
void printTemperature(DeviceAddress deviceAddress, uint8_t id)
{
  float tempC = sensors.getTempC(deviceAddress);
  if (tempC == DEVICE_DISCONNECTED_C)
  {
    Serial1.println("Error: Could not read temperature data");
    return;
  }
  Serial1.print(id);
  Serial1.print(": ");
  Serial1.print(tempC);  
  Serial1.println("°C");
}

void loop (void)
{
  // call sensors.requestTemperatures() to issue a global temperature
  // request to all devices on the bus
  sensors.requestTemperatures();

  for (uint8_t i = 0; i < numberOfDevices; i++){
    printTemperature(sensor[i], i);
  }
  Serial1.println("-----------");
  Serial1.flush();
  // Serial1.print()가 종료되어도 데이터를 전송하는데 시간이 걸린다.
  // 데이터를 다 보내기도 전에 LowPower.powerDown()가 실행되면 아두이노가 바로 잠들어 버린다.
  // 아두이노가 잠들기 전에 Serial1.flush()로 시리얼 버퍼에 남은 데이터를 모두 전송해야 한다.
  // digitalWrite() 등으로 LED나 다른 하드웨어를 작동시키고 있었다면 delay(100) 같은 명령으로
  // 반응할 수 있는 최소한의 시간을 주어야 한다.

  LowPower.powerDown(SLEEP_1S, ADC_OFF, BOD_OFF);
  // SLEEP_1S: 아두이노를 1초 동안 잠들게 한다.
  // ADC_OFF: 잠자는 동안 아날로그 센서를 읽는 기능(ADC)을 꺼서 전기를 아낀다.
  // BOD_OFF: 저전압 감지 기능(BrownOut Detection)을 꺼서 전기를 더 많이 아낀다.
}

 

Serial1 클래스를 사용해 코드를 작성한다.

 

 

반응형
Posted by J-sean
: