تمرین ۲۰ - ارتباط سریال ۲

متن تمرین

اصلی

دو میکروکنتلر ATMEGA32 را از طریق واحد USART شبکه‌بندی کنید به نحوی که میکروی فرستنده با ارسال اطلاعات به گیرنده، یک موتور DC را به صورت راست‌گرد راه‌اندازی کند. (با و بدون مکانیزم وقفه داخلی)

بازنویسی شده برای آردوینو UNO

دو برد آردوینو را از طریق واحد UART شبکه‌بندی کنید به نحوی که میکروی فرستنده با ارسال اطلاعات به گیرنده، یک موتور DC را به صورت راست‌گرد راه‌اندازی کند.

مدار

کد برنامه

فرستنده

/*
 * Assignment #20 - Sender
 *
 * Send commands over Serial (UART)
 *
 * The circuit:
 * Connect pin 1 (TX) of sender (this) Arduino
 * to pin 0 (RX) of reciver Arduino
 * Connect the ground of both Arduinos together
 *
 * https://mehsen.com/arduino/assignments/
 *
 * To the extent possible under law,
 * Mohsen Dastjerdi Zade (mehsen.com) has waived all copyright
 * and related or neighboring rights to Arduino Assignments.
 * https://creativecommons.org/publicdomain/zero/1.0/
 */


// the setup routine runs once when you press reset:
void setup() {
  // initialize serial (USART):
  Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
  delay(1000);
  Serial.println("1");
  delay(1000);
  Serial.println("0");
}


گیرنده

/*
 * Assignment #20 - Reciver
 *
 * Turn on/off a DC motor accourding to the incoming Serial (UART) command
 *
 * The circuit:
 * Connect pin 1 (TX) of sender Arduino
 * to pin 0 (RX) of reciver (this) Arduino
 * Connect the ground of both Arduinos together
 * Connect a transistor (TIP122) to pin 2 and wire it to the DC motor
 *
 * https://mehsen.com/arduino/assignments/
 *
 * To the extent possible under law,
 * Mohsen Dastjerdi Zade (mehsen.com) has waived all copyright
 * and related or neighboring rights to Arduino Assignments.
 * https://creativecommons.org/publicdomain/zero/1.0/
 */

// the setup routine runs once when you press reset:
void setup() {
  // initialize serial (USART):
  Serial.begin(9600);
  pinMode(2, OUTPUT);
}

// the loop routine runs over and over again forever:
void loop() {
  while (Serial.available()) {
    // parse integer
    int command = Serial.parseInt();
    // if incoming number was 1 trun on and if 0 turn off the motor
    digitalWrite(2, command);
  }
}