Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp@4:a139a18be4a5, 2022-04-26 (annotated)
- Committer:
- micros22eq7
- Date:
- Tue Apr 26 05:21:44 2022 +0000
- Revision:
- 4:a139a18be4a5
- Parent:
- 3:edb0044a1052
v5.2
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| micros22eq7 | 1:bc94073c58bd | 1 | //CÓDIGO 2-B |
| micros22eq7 | 1:bc94073c58bd | 2 | |
| fernando_moraless | 0:f1c4da32d334 | 3 | #include <mbed.h> |
| fernando_moraless | 0:f1c4da32d334 | 4 | |
| micros22eq7 | 2:bdf472bb6179 | 5 | Serial pc(USBTX, USBRX); |
| fernando_moraless | 0:f1c4da32d334 | 6 | I2CSlave slave(I2C_SDA, I2C_SCL); |
| fernando_moraless | 0:f1c4da32d334 | 7 | |
| fernando_moraless | 0:f1c4da32d334 | 8 | char addr = 0xA0; |
| fernando_moraless | 0:f1c4da32d334 | 9 | |
| fernando_moraless | 0:f1c4da32d334 | 10 | int main() |
| fernando_moraless | 0:f1c4da32d334 | 11 | { |
| micros22eq7 | 2:bdf472bb6179 | 12 | char buf[30]; |
| micros22eq7 | 2:bdf472bb6179 | 13 | char msg[] = "Respuesta de Slave"; |
| micros22eq7 | 2:bdf472bb6179 | 14 | pc.printf("\r"); |
| micros22eq7 | 1:bc94073c58bd | 15 | pc.printf("I'M SLAVE\r\n"); |
| micros22eq7 | 2:bdf472bb6179 | 16 | |
| fernando_moraless | 0:f1c4da32d334 | 17 | slave.address(addr); |
| fernando_moraless | 0:f1c4da32d334 | 18 | slave.frequency (100000); |
| micros22eq7 | 2:bdf472bb6179 | 19 | pc.printf("SLAVE ID: %d\r\n",addr); |
| micros22eq7 | 2:bdf472bb6179 | 20 | |
| micros22eq7 | 2:bdf472bb6179 | 21 | while (1) |
| fernando_moraless | 0:f1c4da32d334 | 22 | { |
| fernando_moraless | 0:f1c4da32d334 | 23 | int i = slave.receive(); |
| fernando_moraless | 0:f1c4da32d334 | 24 | switch (i) { |
| fernando_moraless | 0:f1c4da32d334 | 25 | case I2CSlave::ReadAddressed: |
| micros22eq7 | 3:edb0044a1052 | 26 | slave.write(msg, strlen(msg) + 1); |
| micros22eq7 | 3:edb0044a1052 | 27 | NVIC_SystemReset(); |
| fernando_moraless | 0:f1c4da32d334 | 28 | break; |
| fernando_moraless | 0:f1c4da32d334 | 29 | case I2CSlave::WriteGeneral: |
| micros22eq7 | 2:bdf472bb6179 | 30 | slave.read(buf, 30); |
| micros22eq7 | 2:bdf472bb6179 | 31 | printf("Read General: %s\n", buf); |
| fernando_moraless | 0:f1c4da32d334 | 32 | break; |
| fernando_moraless | 0:f1c4da32d334 | 33 | case I2CSlave::WriteAddressed: |
| micros22eq7 | 2:bdf472bb6179 | 34 | slave.read(buf, 30); |
| micros22eq7 | 2:bdf472bb6179 | 35 | printf("Read Addressed: %s\n", buf); |
| fernando_moraless | 0:f1c4da32d334 | 36 | break; |
| fernando_moraless | 0:f1c4da32d334 | 37 | } |
| micros22eq7 | 4:a139a18be4a5 | 38 | for (int i = 0; i < 30; i++) |
| fernando_moraless | 0:f1c4da32d334 | 39 | buf[i] = 0; // Clear buffer |
| fernando_moraless | 0:f1c4da32d334 | 40 | } |
| fernando_moraless | 0:f1c4da32d334 | 41 | } |