Embedded Networking and Communications

Week assignments In this assignment I have to: * Add an output device to a microcontroller board I've designed, and program it to do something.

Serial Communication: Data between microcontrollers or peripheral devices may be exchanged by using parallel or serial techniques. With parallel techniques, an entire byte of data is typically sent simultaneously from the transmitting device to the receiver device. Although this is efficient from in serial transmission, a byte of data is sent a single bit at a time. Once 8 bits have been received at the receiver, the data byte is reconstructed. Although this is inefficient from a time point of view, it only requires a line (or two) to transmit the data. A time point of view, it requires eight separate lines for the data transfer. The ATtiny44 is equipped with a host of different serial communication subsystems, including the serial USART, SPI, and TWI. Serial USART: The serial USART is used for full duplex (two-way) communication between a receiver and transmitter. This is accomplished by equipping the ATmega16 with independent hardware for the transmitter and receiver. The USART is typically used for asynchronous communication. That is, there is not a common clock between the transmitter and receiver to keep them synchronized with one another. To maintain synchronization between the transmitter and receiver, framing start and stop bits are used at the beginning and end of each data byte in a transmission sequence. Serial Peripheral Interface: SPI can also be used for two-way serial communication between a transmitter and a receiver. In the SPI system, the transmitter and receiver share a common clock source. This requires an additional clock line between the transmitter and receiver but allows for higher data transmission rates as compared with the USART. The SPI may be viewed as a synchronous 16-bit shift register with an 8-bit half residing in the transmitter and the other 8-bit half residing in the receiver. The transmitter is designated the master because it provides the synchronizing clock source between the transmitter and the receiver. The receiver is designated as the slave. Two-Wire Serial Interface: The TWI subsystem allows the system designer to network a number of related devices (microcontrollers, transducers, displays, etc.) together into a system using a two-wire interconnecting scheme. The TWI allows a maximum of 128 devices to be connected together. Each device has its own unique address and may both transmit and receive over the two-wire bus at frequencies up to 400 kHz. This allows the device to freely exchange information with other devices in the network within a small area. The RS-232 Communication Protocol: When serial transmission occurs over a long distance, additional techniques may be used to ensure data integrity. Over long distances, logic levels degrade and may be corrupted by noise. At the receiving end, it is difficult to discern a logic high from a logic low. The RS-232 standard has been around for some time. With the RS-232 standard (EIA-232), a logic 1 is represented with a −12-VDC level, whereas a logic 0 is represented by a +12-VDC level. Chips are commonly available (e.g., MAX232) that convert the 5- and 0-V output levels from a transmitter to RS-232- compatible levels and convert back to 5- and 0-V levels at the receiver. The RS-232 standard also specifies other features for this communication protocol. Baud Rate Data transmission rates are typically specified as a baud or bits per second rate. For example, 9600 baud indicates data are being transferred at 9600 bits per second. I²C I²C (Inter-Integrated Circuit, eye-squared-C), alternatively known as I2C or IIC, is a synchronous, multi-controller/multi-target (master/slave), packet switched, single-ended, serial communication bus invented in 1982 by Philips Semiconductors. It is widely used for attaching lower-speed peripheral ICs to processors and microcontrollers in short-distance, intra-board communication Wikipedia . In this assignment I used I²C to communicate between my board I made in week7 and Arduino UNO. I didn't found many references about using serial in ATtiny44, I tested more than one example but every time I don't get any response from my board. Finally after more search I found Faisal Alkilani experiment, which was a very helpful. One of the problem of using serial with ATtiny44 is "wire.h" library. This library not declared in ATtiny44. The solution is by using another library "USIWire.h" Codes: We need two codes, one for master "ATtiny44" and another for arduino. In this test, I control a servo motor attached to Arduino UNO by sending motor degree from ATtiny board via serial communication. The degree is increasing by 5 in ATtiny44 and is snet to the Arduino. Sender Code:

#include 
  #include 
  
  const int rxPin = 1;
  const int txPin = 2;
  
  void setup() {
   Wire.begin();
   delay(100);
   pinMode (0, OUTPUT);
   digitalWrite(0, HIGH);
  }
  byte x = 0;
  void loop() {
   Wire.beginTransmission(4); 
   Wire.write("x is ");        
   Wire.write(x);             
   Wire.endTransmission();    
   x=x+5;
   delay(50);
  }
   
Receiver Code:
#include 
  #include 
  Servo myservo;
  void setup(){
   Wire.begin(4);                
   Wire.onReceive(receiveEvent); 
   Serial.begin(9600);           
   myservo.attach(9);
   myservo.write(0); 
   }
  
  void loop(){
   delay(100);}
  
  void receiveEvent(int howMany)
  {
    while(1 < Wire.available()) 
    {
      char c = Wire.read(); 
      Serial.print(c);         
    }
    int x = Wire.read();    
    Serial.println(x);         
   myservo.write(x); 
  }

Second Test: In this test I added potentiometer to control the motor. ATtiny44 reads potentiometer value then send it to the Arduino. In Arduino "map" function convert the value from "0-1024" to "0-256" which is the range of the PWM (pulse-width modelation) of the Arduino UNO. Sender Code:
#include 
    #include 
    
    const int rxPin = 1;
    const int txPin = 2;
    int pot_read;
    void setup() {
     Wire.begin();
     delay(100);
     pinMode (0, OUTPUT);
     digitalWrite(0, HIGH);
    }
    byte x = 0;
    void loop() {
      pot_read=analogRead(A3);
     Wire.beginTransmission(4); 
     Wire.write("x is ");        
     Wire.write(pot_read);             
     Wire.endTransmission();    
    // x=x+5;
     delay(50);
    } 
Receiver Code:
#include 
    #include 
    Servo myservo;
    void setup(){
     Wire.begin(4);                
     Wire.onReceive(receiveEvent); 
     Serial.begin(9600);           
     myservo.attach(9);
     myservo.write(0); 
     }
    
    void loop(){
     delay(100);}
    
    void receiveEvent(int howMany)
    {
      while(1 < Wire.available()) 
      {
        char c = Wire.read(); 
        Serial.print(c);         
      }
      int x = Wire.read();    
      Serial.println(x);
     int y=map(x,0,1023,0,255);         
     myservo.write(y); 
    }

Download Files Conting Codes: potentiometer Codes: