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@1:ac7a077c0039, 2022-04-25 (annotated)
- Committer:
- micros22eq7
- Date:
- Mon Apr 25 18:36:36 2022 +0000
- Revision:
- 1:ac7a077c0039
- Parent:
- 0:96fa55991f93
- Child:
- 2:247fd31aff31
Conexion entre microcontroladores KL25Z por protocolo de comunicacion I2C. Modulo para MASTER / MAESTRO.
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| micros22eq7 | 1:ac7a077c0039 | 1 | //CÓDIGO 2-A |
| micros22eq7 | 1:ac7a077c0039 | 2 | |
| fernando_moraless | 0:96fa55991f93 | 3 | #include "mbed.h" |
| fernando_moraless | 0:96fa55991f93 | 4 | |
| fernando_moraless | 0:96fa55991f93 | 5 | #define BUFFER_SIZE 8 |
| fernando_moraless | 0:96fa55991f93 | 6 | #define SLAVE_ADDR 0xA0 |
| fernando_moraless | 0:96fa55991f93 | 7 | |
| fernando_moraless | 0:96fa55991f93 | 8 | Serial pc(USBTX, USBRX); |
| fernando_moraless | 0:96fa55991f93 | 9 | I2C master(I2C_SDA, I2C_SCL); |
| fernando_moraless | 0:96fa55991f93 | 10 | |
| fernando_moraless | 0:96fa55991f93 | 11 | static const char* to_send[] = { "abcde", "12345", "EFGHI" }; |
| fernando_moraless | 0:96fa55991f93 | 12 | int main() |
| fernando_moraless | 0:96fa55991f93 | 13 | { |
| micros22eq7 | 1:ac7a077c0039 | 14 | pc.printf("\x1b[2J"); //CLEAR |
| fernando_moraless | 0:96fa55991f93 | 15 | pc.printf("\r"); //Mueve cursor al origen |
| micros22eq7 | 1:ac7a077c0039 | 16 | pc.printf("I'M MASTER\r\n"); |
| fernando_moraless | 0:96fa55991f93 | 17 | |
| fernando_moraless | 0:96fa55991f93 | 18 | master.frequency (100000); |
| fernando_moraless | 0:96fa55991f93 | 19 | char buf[BUFFER_SIZE]; |
| fernando_moraless | 0:96fa55991f93 | 20 | int send_index = 0; |
| fernando_moraless | 0:96fa55991f93 | 21 | while (1) |
| fernando_moraless | 0:96fa55991f93 | 22 | { |
| fernando_moraless | 0:96fa55991f93 | 23 | strcpy(buf, to_send[send_index]); |
| fernando_moraless | 0:96fa55991f93 | 24 | |
| fernando_moraless | 0:96fa55991f93 | 25 | // Write the new message to the slave |
| fernando_moraless | 0:96fa55991f93 | 26 | if (master.write(SLAVE_ADDR, buf, BUFFER_SIZE) == 0) |
| fernando_moraless | 0:96fa55991f93 | 27 | { |
| fernando_moraless | 0:96fa55991f93 | 28 | pc.printf("Written to slave: %s\r\n", buf); |
| fernando_moraless | 0:96fa55991f93 | 29 | } |
| fernando_moraless | 0:96fa55991f93 | 30 | // Read what the slave has (should be identical) |
| fernando_moraless | 0:96fa55991f93 | 31 | |
| fernando_moraless | 0:96fa55991f93 | 32 | if (master.read(SLAVE_ADDR, buf, BUFFER_SIZE) == 0) { |
| fernando_moraless | 0:96fa55991f93 | 33 | pc.printf("Read from slave: %s\r\n", buf); |
| fernando_moraless | 0:96fa55991f93 | 34 | } |
| fernando_moraless | 0:96fa55991f93 | 35 | |
| fernando_moraless | 0:96fa55991f93 | 36 | // Change the message we're writing to the slave |
| fernando_moraless | 0:96fa55991f93 | 37 | send_index++; |
| fernando_moraless | 0:96fa55991f93 | 38 | if (send_index > 2) { |
| fernando_moraless | 0:96fa55991f93 | 39 | send_index = 0; |
| fernando_moraless | 0:96fa55991f93 | 40 | } |
| fernando_moraless | 0:96fa55991f93 | 41 | } |
| fernando_moraless | 0:96fa55991f93 | 42 | } |