Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define RXD1 32
- #define TXD1 25
- #define EN_RS485 33
- #define LED 12
- #include <ModbusMaster.h>
- ModbusMaster node;
- #define SLAVE_ADDR ((uint16_t)0x0C)
- #define TEMP_CPT_SEL_BIT ((uint16_t)0x01)
- #define TEMP_CPT_ENABLE_BIT ((uint16_t)0x01 << 1)
- #define MEASURE_MODE_BIT ((uint16_t)0x01 << 2)
- #define MEASURE_TRIG_BIT ((uint16_t)0x01 << 3)
- uint16_t trigger = 0;
- float jarak = 0.0;
- float suhu = 0.0;
- void preTransmission()
- {
- digitalWrite(EN_RS485, 1);
- digitalWrite(LED, 1);
- }
- void postTransmission()
- {
- digitalWrite(EN_RS485, 0);
- digitalWrite(LED, 0);
- }
- void setup() {
- // put your setup code here, to run once:
- pinMode(EN_RS485, OUTPUT);
- pinMode(LED, OUTPUT);
- Serial.begin(115200);
- Serial1.begin(115200, SERIAL_8N1, RXD1, TXD1);
- node.preTransmission(preTransmission);
- node.postTransmission(postTransmission);
- node.begin(SLAVE_ADDR, Serial1);
- delay(1000);
- trigger |= MEASURE_MODE_BIT;//Set bit2 , Set to trigger mode
- trigger &= ~(uint16_t)TEMP_CPT_SEL_BIT;//Select internal temperature compensation
- trigger &= ~(uint16_t)TEMP_CPT_ENABLE_BIT;//enable temperature compensation
- delay(1000);
- conf_autoDistance();
- }
- void loop() {
- // put your main code here, to run repeatedly:
- baca_urm14();
- delay(2000);
- }
- void baca_urm14() {
- Serial.println("baca urm14");
- uint8_t result;
- uint16_t data[6];
- trigger |= MEASURE_TRIG_BIT;//Set trig bit
- result = node.writeSingleRegister(3, trigger);
- Serial.print("result_t: ");
- Serial.println(result);
- delay(300);
- result = node.readHoldingRegisters(0, 9);
- Serial.print("result_r: ");
- Serial.println(result);
- if (result == node.ku8MBSuccess)
- {
- jarak = node.getResponseBuffer(5) / 10;
- suhu = node.getResponseBuffer(6) / 10;
- Serial.print("jarak: ");
- Serial.print(jarak);
- Serial.println(" mm");
- Serial.print("suhu: ");
- Serial.print(suhu);
- Serial.println(" C");
- Serial.println("-----------------------");
- }else{
- Serial.println("modbus fail");
- }
- }
- void conf_autoDistance(){
- uint8_t result = node.writeSingleRegister(0x08, 0); // write address 0x08 with data 0000 /
- Serial.print("write config: ");
- Serial.println(result);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement