Giỏ hàng
Danh mục sản phẩm

Lập trình HMI Arduno,Lập trình Modbus Arduino

Đăng bởi Buiprohd@gmail.com ngày bình luận

Hôm nay mình giới thiệu các bạn chạy Arduino với màn hình HMI SAMKOON 

Đầu tiên các bạn cài đặt phần mền HHMI SAMKOON bản mới nhất 

Đầu tiên chúng ta tìm hiểu khung truyền modbus 

.Phần cứng HMI mosbus để chống nhiễu và cách ly GND dùng ADUM3251 hoặc ADUM1201 kết hợp MAX3232IDR thêm nguồn ISO IB0505

1.Khung truyền modbus

2.Các địa chỉ modbus

01 READ COIL STATUS
02 READ INPUT STATUS
03 READ HOLDING REGISTERS
04 READ INPUT REGISTERS
05 WRITE SINGLE COIL
06 WRITE SINGLE REGISTER
15 WRITE MULTIPLE COILS
16 WRITE MULTIPLE REGISTERS

địa chỉ :0000-9999 một thiết bị master có thể đọc và ghi địa chỉ này

địa chỉ :1000-19999 digital Input

địa chỉ :3000-39999 analog  Input

địa chỉ :4000-49999 analog  output

Các bạn tham khảo thêm trang chủ modbus :https://www.modbustools.com/modbus.html

ở đây chúng ta quan tâm đến hàm 01 :01 READ COIL STATUS như đọc các trạng thái nút bấm ,led hiển thị

Hàm :03 READ HOLDING REGISTERS Ghi giá trị vào thanh ghi 16bit để hiển thị các giá trị số đếm 

Hàm:04 READ INPUT REGISTERS vừa ghi vừa đọc được giá trị ,để nhận giữ liệu HMi

Tiếp theo chúng ta đển màn hình HMI 

Giao diện đơn giản dùng HMI SAMKOON

Như vậy song quá trình Cài đặt HMI ,nếu không có màn hình chúng ta có thể chạy online HMI với Arduino qua USB to com

Đối với mạch thực tế cần qua MAX3232IDR hoặc ADUM3251 iso

Code RTOS Arduino HMI:

/*
 * Example to demonstrate thread definition, semaphores, and thread sleep.
 */
#include <NilRTOS.h>
 
#include <Modbus.h>
#include <ModbusSerial.h>
//#include <Arduino_FreeRTOS.h>

// define two tasks for Blink & AnalogRead

// Modbus Registers Offsets (0-9999)
const int LAMP1_COIL =1; 
const int LAMP2_COIL =1; 
const int LAMP3_COIL =2; 
const int LAMP4_COIL =3; 
const int len_hol = 16; 
// Used Pins
const int ledPin = 13;
const int LED1_VAL = 0; //primeiro led posição 0
const int LED2_VAL = 1; //segundo led posição 1
long int cnt_mb=0;


const int JK1_VAL = 1;
const int JK2_VAL = 0;
const int K1 = 0;
const int K2 = 1;
const int K3 = 2;
const int K4 = 3;
const int K5 = 4;
const int K6 = 5;
// ModbusSerial object
ModbusSerial mb;

// The LED is attached to pin 13 on Arduino.
const uint8_t LED_PIN = 13;

// Declare a semaphore with an inital counter value of zero.
SEMAPHORE_DECL(sem, 0);
//------------------------------------------------------------------------------
/*
 * Thread 1, turn the LED off when signalled by thread 2.
 */
// Declare a stack with 128 bytes beyond context switch and interrupt needs.
NIL_WORKING_AREA(waThread1, 128);

// Declare the thread function for thread 1.
NIL_THREAD(Thread1, arg) {
  while (TRUE) {
    
    // Wait for signal from thread 2.
    nilSemWait(&sem);
    
    // Turn LED off.
  
  }
}
//------------------------------------------------------------------------------
/*
 * Thread 2, turn the LED on and signal thread 1 to turn the LED off.
 */
// Declare a stack with 128 bytes beyond context switch and interrupt needs. 
NIL_WORKING_AREA(waThread2, 128);

// Declare the thread function for thread 2.
NIL_THREAD(Thread2, arg) {

  pinMode(LED_PIN, OUTPUT);
  
  while (TRUE) {
    // Turn LED on.
  
    
    // Sleep for 200 milliseconds.
    nilThdSleepMilliseconds(1);
    
    // Signal thread 1 to turn LED off.
    nilSemSignal(&sem);
    
    // Sleep for 200 milliseconds.   
    nilThdSleepMilliseconds(1);
  }
}
//------------------------------------------------------------------------------
/*
 * Threads static table, one entry per thread.  A thread's priority is
 * determined by its position in the table with highest priority first.
 * 
 * These threads start with a null argument.  A thread's name may also
 * be null to save RAM since the name is currently not used.
 */
NIL_THREADS_TABLE_BEGIN()
NIL_THREADS_TABLE_ENTRY("thread1", Thread1, NULL, waThread1, sizeof(waThread1))
NIL_THREADS_TABLE_ENTRY("thread2", Thread2, NULL, waThread2, sizeof(waThread2))
NIL_THREADS_TABLE_END()
//------------------------------------------------------------------------------
void setup() {
  // Start Nil RTOS.
    mb.config(&Serial1, 9600, SERIAL_8N1);
    // Set the Slave ID (1-247)
    mb.setSlaveId(1);  
    
    // Set ledPin mode
    pinMode(ledPin, OUTPUT);
    // Add LAMP1_COIL register - Use addCoil() for digital outputs
    mb.addCoil(LAMP1_COIL);
    mb.addCoil(LAMP2_COIL);
    mb.addCoil(LAMP3_COIL);
    mb.addCoil(LAMP4_COIL);
   // mb.addIreg(JK1_VAL);
    mb.addIreg(JK1_VAL);
    mb.addHreg(K2);
    mb.addHreg(K3);
    mb.addHreg(K4);
    mb.addHreg(K5);
    mb.addHreg(K6);
      Serial.begin(9600);
  Serial.println(F("In Setup function"));
  nilSysBegin();
}
//------------------------------------------------------------------------------
// Loop is the idle thread.  The idle thread must not invoke any 
// kernel primitive able to change its state to not runnable.
void loop() {
  // Not used.
  
   mb.task();
   if(++cnt_mb>65535) cnt_mb=0;
      //  mb.Ireg(JK1_VAL,cnt_mb);
     mb.Ireg(JK1_VAL,cnt_mb);
     mb.Hreg(K2,121);
     mb.Hreg(K3,121);
     mb.Hreg(K4,123);
     mb.Hreg(K5,145);
      mb.Hreg(K6,441);
      if(mb.Coil(LAMP1_COIL)==1)   digitalWrite(LED_PIN, LOW);
      else   digitalWrite(LED_PIN, HIGH);
      //  digitalWrite(ledPin, mb.Coil(LAMP1_COIL));
}

 

Liên hệ với linhkienhdshop.com để lấy code và thư viện RTOS

 

 


← Cũ hơn Mới hơn →