This shows you the differences between two versions of the page.
pm:lab:lab6-2021 [2021/04/17 19:10] jan.vaduva |
pm:lab:lab6-2021 [2021/04/22 13:43] (current) dumitru.brigalda |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ===== Laboratorul 5: I2C (Inter-Integrated Circuit) ===== | + | ===== Laboratorul 6: I2C (Inter-Integrated Circuit) ===== |
Acest laborator acoperă noțiunea de I2C. Pentru aprofundarea acestui topic, consultați [[https://ww1.microchip.com/downloads/en/DeviceDoc/Atmel-7810-Automotive-Microcontrollers-ATmega328P_Datasheet.pdf|Datasheet ATmega328P]] și {{https://en.wikipedia.org/wiki/I%C2%B2C|Inter-Integrated Circuit}}. | Acest laborator acoperă noțiunea de I2C. Pentru aprofundarea acestui topic, consultați [[https://ww1.microchip.com/downloads/en/DeviceDoc/Atmel-7810-Automotive-Microcontrollers-ATmega328P_Datasheet.pdf|Datasheet ATmega328P]] și {{https://en.wikipedia.org/wiki/I%C2%B2C|Inter-Integrated Circuit}}. | ||
Line 49: | Line 49: | ||
</note> | </note> | ||
- | ===== 3. Regiștrii configurare I2C ===== | + | ===== 3. Registre configurare I2C ===== |
Arduino poate funcționa atât în modul I2C Master cât și I2C Slave. | Arduino poate funcționa atât în modul I2C Master cât și I2C Slave. | ||
Line 82: | Line 82: | ||
{{ pm:lab:master_sender_bb.png?500 }} | {{ pm:lab:master_sender_bb.png?500 }} | ||
- | **Master_reader_example** | + | **Master Writer** |
+ | <code c> | ||
+ | #include <Wire.h> | ||
+ | |||
+ | void setup() | ||
+ | { | ||
+ | Wire.begin(); // join i2c bus (address optional for master) | ||
+ | } | ||
+ | |||
+ | byte x = 0; | ||
+ | |||
+ | void loop() | ||
+ | { | ||
+ | Wire.beginTransmission(4); // transmit to device #4 | ||
+ | Wire.write("x is "); // sends five bytes | ||
+ | Wire.write(x); // sends one byte | ||
+ | Wire.endTransmission(); // stop transmitting | ||
+ | |||
+ | x++; | ||
+ | delay(500); | ||
+ | } | ||
+ | </code> | ||
+ | |||
+ | **Slave Reader** | ||
+ | <code c> | ||
+ | #include <Wire.h> | ||
+ | |||
+ | void setup() | ||
+ | { | ||
+ | Wire.begin(4); // join i2c bus with address #4 | ||
+ | Wire.onReceive(receiveEvent); // register event | ||
+ | Serial.begin(9600); // start serial for output | ||
+ | } | ||
+ | |||
+ | void loop() | ||
+ | { | ||
+ | delay(100); | ||
+ | } | ||
+ | |||
+ | // function that executes whenever data is received from master | ||
+ | // this function is registered as an event, see setup() | ||
+ | void receiveEvent(int howMany) | ||
+ | { | ||
+ | while(1 < Wire.available()) // loop through all but the last | ||
+ | { | ||
+ | char c = Wire.read(); // receive byte as a character | ||
+ | Serial.print(c); // print the character | ||
+ | } | ||
+ | int x = Wire.read(); // receive byte as an integer | ||
+ | Serial.println(x); // print the integer | ||
+ | } | ||
+ | </code> | ||
+ | |||
+ | |||
+ | **Master Reader** | ||
<code c> | <code c> | ||
#include <Wire.h> | #include <Wire.h> | ||
Line 103: | Line 157: | ||
</code> | </code> | ||
- | **Slave_writer_example** | + | **Slave Writer** |
<code c> | <code c> | ||
Line 126: | Line 180: | ||
- | ===== Exerciții ===== | + | ===== 5. Exerciții ===== |
+ | |||
+ | **Task 0** Folosiți codul Arduino pentru a implementa un Master Reader și un Slave Writer. Master-ul va avea atașat un led, iar Slave-ul un buton. Master-ul trebuie să interogheze Slave-ul despre starea butonului și să aprindă/stingă led-ul în funcție de starea butonului (apăsat/neapăsat). Cele două board-uri vor comunica pe I2C. Pentru realizarea comunicării, Master-ul trebuie să facă request de citire a 3 caractere, iar Slave-ul să răspundă cu starea butonului: "ON!" sau "OFF". | ||
+ | {{ :pm:lab:lab_i2c_task0.png?500 |}} | ||
+ | |||
+ | **Task 1** Folosiți codul Arduino pentru a implementa un Master Writer și un Slave Reader. Master-ul va avea atașate 2 butoane, iar Slave-ul un led. Master-ul trebuie să trimită Slave-ului una din cele 2 comenzi: "ON!" sau "OFF". Cele două board-uri vor comunica pe I2C. Pentru realizarea comunicării, la apăsarea primului buton, Master-ul trebuie să trimită comanda "ON!", iar la apăsarea butonului 2, comanda "OFF". În funcție de comanda recepționată, Slave-ul va aprinde/stinge led-ul. | ||
+ | {{ :pm:lab:lab_i2c_task1.png?500 |}} | ||
+ | |||
+ | <hidden> | ||
+ | {{https://www.tinkercad.com/things/aWsNXOGkVBR|Soluție Task0 și Task1}} | ||
+ | </hidden> | ||
+ | |||
+ | <hidden> | ||
+ | **Task 2** Folosiți codul Arduino pentru a implementa un ceas digital folosind un model de LCD pornind de la codul disponibil: [[https://www.tinkercad.com/things/i5JAD1DytMu-copy-of-arduino-digital-clock-without-rtc-module/editel?sharecode=NA25XBlf6CdMtIZ9UNoAWGDuaUopZQh8aAye2WJZUEU|Arduino LCD Digital Clock]] | ||
+ | </hidden> | ||
- | ===== Resurse ===== | + | ===== 6. Resurse ===== |
* {{:pm:atmel-7810-automotive-microcontrollers-atmega328p_datasheet.pdf|Datasheet Atmega 328p}} | * {{:pm:atmel-7810-automotive-microcontrollers-atmega328p_datasheet.pdf|Datasheet Atmega 328p}} | ||
Line 135: | Line 203: | ||
* Responsabil: [[dbrigalda@gmail.com | Alexandru Vaduva]] | * Responsabil: [[dbrigalda@gmail.com | Alexandru Vaduva]] | ||
- | ===== 6. Linkuri Utile ===== | + | ===== 7. Linkuri Utile ===== |
* [[https://www.arduino.cc/en/Reference/Wire|Arduino I2C]] | * [[https://www.arduino.cc/en/Reference/Wire|Arduino I2C]] | ||
* [[https://ww1.microchip.com/downloads/en/DeviceDoc/Atmel-7810-Automotive-Microcontrollers-ATmega328P_Datasheet.pdf|Datasheet ATmega328p]] | * [[https://ww1.microchip.com/downloads/en/DeviceDoc/Atmel-7810-Automotive-Microcontrollers-ATmega328P_Datasheet.pdf|Datasheet ATmega328p]] | ||