About Switching Between DigitalWrite() and Serial1.write()

Asked 1 months ago, Updated 1 months ago, 354 views

What do you want to do

I would like to switch between digitalWrite() and serial1.write() in Arduino Due.
However, Serial1.write() from digitalWrite() will not be able to communicate properly.
If you send serial data, the first transmission will succeed, but 0 will be sent after the second transmission.
By the way, Arduino Mega can switch and communicate.
I would like to know the cause and how to make the following ideal reception data.
この This target Due is communicating with another Due, Serial1 tx/rx, and the other Due is sending the data received in Serial1 to the serial monitor of the PC connected by a USB cable in hexadecimal.

Source Codes Affected

void setup(){
    const byte txData[6] = {0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF};

    // Serial1.write (1st time)
    Serial1.begin(9600);
    Serial1.write(txData, 6);
    Serial1.end();
    delay(1000);

    // digitalWrite
    pinMode(18, OUTPUT);
    digitalWrite (18, LOW);
    delay(50);
    digitalWrite (18, HIGH);
    delay(100);

    // Serial1.write (second and later)
    for (byte i=0; i<5;i++)
    {
        Serial1.begin(9600);
        Serial1.write(txData,3);
        Serial1.flush();
        Serial1.end();
        delay(1000);
    }
}

void loop() {
    
}

Serial Monitor (ideal incoming data)

AA BB CC DD EFF
AA BBCC
AA BBCC
AA BBCC
AA BBCC
AA BBCC

Serial Monitor (Actual Receive Data)

AA BB CC DD EFF
00

Connection Diagram

Enter a description of the image here

Supplementary Information

dArduino Due <
https://store-usa.arduino.cc/collections/boards/products/arduino-due
https://content.arduino.cc/assets/A000056-full-pinout.pdf

[Data Sheet]
https://ww1.microchip.com/downloads/en/DeviceDoc/Atmel-11057-32-bit-Cortex-M3-Microcontroller-SAM3X-SAM3A_Datasheet.pdf

Multipost

https://teratail.com/questions/h3km9w6nkmhjgm
https://qiita.com/alyn/questions/78d9963e81f53ab13f75
About switching between digitalWrite() and Serial1.write()

arduino

2022-10-26 10:08

1 Answers

If you rewrite it as follows, it seems that you can make it work the same way.
It's not a fundamental solution, but I'll fix it now.

before change:

digitalWrite(18,LOW);delay(50);

after modification:

uint8_t Zero[1]={0};Serial1.begin(160);Serial1.write(Zero,1);


2022-10-26 10:08

If you have any answers or tips


© 2022 OneMinuteCode. All rights reserved.