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