Skip to content

Instantly share code, notes, and snippets.

@TakehikoShimojima
Last active February 1, 2021 00:24
Show Gist options
  • Save TakehikoShimojima/1fafdf729771d6cd76686c8a52f51067 to your computer and use it in GitHub Desktop.
Save TakehikoShimojima/1fafdf729771d6cd76686c8a52f51067 to your computer and use it in GitHub Desktop.
#include "M5Atom.h"
#include <ModbusRTU.h>
#define RX_PIN 22 // RS485キットのRXピン
#define TX_PIN 19 // RS485キットのTXピン
#define REGN 10
#define SLAVE_ID 1
ModbusRTU mb;
uint8_t FSM = 0;
void toggleLED() {
switch (FSM++ % 4) {
case 0:
M5.dis.drawpix(0, 0xf00000);
break;
case 1:
M5.dis.drawpix(0, 0x00f000);
break;
case 2:
M5.dis.drawpix(0, 0x0000f0);
break;
case 3:
M5.dis.drawpix(0, 0x707070);
break;
default:
break;
}
}
uint16_t value = 0;
void setup() {
M5.begin(true, false, true);
delay(50);
Serial2.begin(115200, SERIAL_8N1, RX_PIN, TX_PIN); // Serial2を初期設定
mb.begin(&Serial2); // Serial2のアドレスを渡してmodbusを初期設定する
mb.slave(SLAVE_ID); // 自分自身のスレーブIDを設定する
mb.addIreg(REGN); // Iregを使うことを指定する
M5.dis.drawpix(0, 0xf00000);
}
unsigned long lasttime = 0;
void loop() {
if ((millis() - lasttime) > 1 * 1000) {
lasttime = millis();
mb.Ireg(REGN, ++value);
toggleLED();
}
mb.task();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment