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.
Diff: main.cpp
- Revision:
- 0:e24b497adfce
- Child:
- 1:e8ff4ff5a726
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp Mon Oct 13 13:27:05 2014 +0000
@@ -0,0 +1,60 @@
+#include "mbed.h"
+
+DigitalOut myled(LED1);
+Serial pc(USBTX, USBRX); // tx, rx
+I2CSlave slave(p28, p27);
+
+const int SLAVE_ADDRESS = 0x85;
+const int MEMORY_SIZE = 16;
+
+int memory[MEMORY_SIZE];
+
+void initializeMemory(void)
+{
+ for (int i = 0; i < MEMORY_SIZE; i++) {
+ memory[i] = 0;
+ }
+}
+
+int main() {
+ pc.baud(115200);
+ pc.printf("I am the slave device (SLAVE_ADDRESS = 0x%x)\r\n", SLAVE_ADDRESS);
+ pc.printf("Size of integer is %d bytes\r\n", sizeof(int));
+ pc.printf("Size of memory buffer is %d elements\r\n", MEMORY_SIZE);
+
+ // Make sure memory buffer is zeroed
+ initializeMemory();
+
+
+ char buffer[10];
+ char msg[] = "Slave!";
+
+ slave.address(SLAVE_ADDRESS);
+
+ while (1) {
+ int i = slave.receive();
+ switch (i) {
+ case I2CSlave::ReadAddressed:
+ pc.printf("Slave received command: %d [ReadAddressed]\r\n", i);
+
+ if (slave.write(msg, strlen(msg) + 1) == 0) {
+ printf("Sending %s to master\r\n", msg);
+ }
+ break;
+
+ case I2CSlave::WriteAddressed:
+ pc.printf("Slave received command: %d [WriteAddressed]\r\n", i);
+
+ if (slave.read(buffer, 10) == 0) {
+ pc.printf("Received %s from master\r\n", buffer);
+ }
+ break;
+ }
+ for (int i = 0; i < 10; i++) buffer[i] = 0; // Clear buffer
+
+ /*myled = 1;
+ wait(1);
+ myled = 0;
+ wait(1);*/
+ }
+}