From 3edb55948a2e0afc573e301712c291db4375c746 Mon Sep 17 00:00:00 2001 From: zcy <290198252@qq.com> Date: Fri, 6 Oct 2023 00:38:32 +0800 Subject: [PATCH] no message --- arduino/arduino.ino | 36 +++++++++++++ arduino/arduino/stepper.ino/stepper.ino.ino | 57 --------------------- arduino/modbus_slave.ino | 55 ++++++++++++++++++++ 3 files changed, 91 insertions(+), 57 deletions(-) create mode 100644 arduino/arduino.ino delete mode 100644 arduino/arduino/stepper.ino/stepper.ino.ino create mode 100644 arduino/modbus_slave.ino diff --git a/arduino/arduino.ino b/arduino/arduino.ino new file mode 100644 index 0000000..03850ce --- /dev/null +++ b/arduino/arduino.ino @@ -0,0 +1,36 @@ + + +int incomedate = 0; + +void setup() { + // put your setup code here, to run once: + pinMode(13, OUTPUT); + Serial.begin(9600); //设置串口波特率9600 + Serial.println(78, BIN);// "1001110" + Serial.println(78, OCT);// "116" + Serial.println(78, DEC);// "78" + Serial.println(78, HEX);// "4E" + Serial.println(1.23456, 0);// "1" + Serial.println(1.23456, 2);// "1.23" + Serial.println(1.23456, 4);// "1.2346" + Serial.println('N');// "N" + Serial.println("Hello world.");// "Hello world." + +} + +void loop() { + // put your main code here, to run repeatedly: + digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level) + delay(1000); // wait for a second + digitalWrite(13, LOW); // turn the LED off by making the voltage LOW + delay(1000); // wait for a second + if (Serial.available() > 0)//串口接收到数据 + { + incomedate = Serial.read();//获取串口接收到的数据 + if (incomedate == 'H') + { + Serial.println("Good Job!"); + } + } + +} diff --git a/arduino/arduino/stepper.ino/stepper.ino.ino b/arduino/arduino/stepper.ino/stepper.ino.ino deleted file mode 100644 index 35af2db..0000000 --- a/arduino/arduino/stepper.ino/stepper.ino.ino +++ /dev/null @@ -1,57 +0,0 @@ -/* - Stepper Motor Control - one revolution - This program drives a unipolar or bipolar stepper motor. - The motor is attached to digital pins 8 - 11 of the Arduino. - The motor should revolve one revolution in one direction, then - one revolution in the other direction. - Created 11 Mar. 2007 - Modified 30 Nov. 2009 - by Tom Igoe - */ -#include -#include - -const int stepsPerRevolution = 20; // change this to fit the number of steps per revolution -// for your motor -Servo myservo; // create servo object to control a servo - -// initialize the stepper library on pins 8 through 11: - -#define CLK 10 -#define CW 9 -#define EN 8 - -void setup() { - // set the speed at 60 rpm: - pinMode(CLK,OUTPUT); - pinMode(CW,OUTPUT); - pinMode(EN,OUTPUT); - // initialize the serial port: - Serial.begin(115200); - digitalWrite(EN,LOW); - myservo.attach(12); // attaches the servo on pin 9 to the servo object -} - -void loop() { - char data[20] = {0}; - digitalWrite(EN,LOW); - - // step one revolution in one direction: - if(Serial.available()){ //if number of bytes (characters) available for reading from { - Serial.readBytes(data,20); - Serial.print("I received:"); //print I received - Serial.write(data); //send what you read - String strInt = data; // string to hold input - int ints = strInt.toInt(); - Serial.print(ints); //print I received - myservo.write(ints); // sets the servo position according to the scaled value - for(int cnt = 0;cnt < ints;cnt++){ - delay(100); - digitalWrite(EN,LOW); - delay(100); - digitalWrite(EN,HIGH); - - } - ints = 0; - } -} diff --git a/arduino/modbus_slave.ino b/arduino/modbus_slave.ino new file mode 100644 index 0000000..c0124e3 --- /dev/null +++ b/arduino/modbus_slave.ino @@ -0,0 +1,55 @@ +#include "ModBusSlave0.h" + + +#define TRANSMIT_PIN 6 +#define MODBUS_ADDRESS 100 + +ModBusSlave0 mod(false); + +ISR(USART0_RX_vect) +{ + mod.receiveHandler(); +} + +ISR(USART0_TX_vect) +{ + mod.transmitHandler(); +} + +uint16_t get_addr(uint16_t addr){ + switch(addr){ + case 1: + return 1; + default: + return addr; + } +} + +uint8_t prepareResponse03(uint8_t *frame, uint8_t* data) { + if(frame[1] == 0x03){ + uint16_t addr = uint16_t(frame[2])*256 + uint16_t(frame[3]); + uint16_t len = uint16_t(frame[4])*256 + uint16_t(frame[5]); + + int i = 0; + while(len --){ + data[i] = (get_addr(addr + i/2)&0xff00)>>8; + data[i + 1] = get_addr(addr + i/2)&0x00ff; + i +=2 ; + } + return i; + } + if(frame[1] == 0x06){ + + } + +} + +void setup() { + mod.begin(57600, TRANSMIT_PIN, MODBUS_ADDRESS); + mod.prepareResponse03 = *prepareResponse03; +} + +void loop() { + mod.process(); +} +