Abhi
- include "mbed.h"
- include "rtos.h" DigitalOut myled1(LED1); DigitalOut myled2(LED2); void led2_thread(void const*args){ while(true){ myled2 = !myled2; Thread::wait(1000); } } int main() { Thread thread(led2_thread); while(1){ myled1 =!myled1; Thread::wait(500); } }
- include "mbed.h"
- include "rtos.h" Serial pc(USBTX,USBRX); Semaphore two_slots(2); void test_thread(void const *name){ while(true){ two_slots.wait(); pc.printf("%s\n\r",(const char*)name); Thread::wait(1000); two_slots.release(); } } int main(void){ Thread t2(test_thread,(void *)"Th 2"); Thread t3(test_thread,(void *)"Th 3"); test_thread((void *)"Th 1"); }
- include "mbed.h"
- include "rtos.h" Mutex stdio_mutex;
void notify(const char* name, int state) { stdio_mutex.lock(); printf("%s: %d\n\r", name, state); stdio_mutex.unlock(); }
void test_thread(void const *args) { while (true) { notify((const char*)args, 0); wait(1); notify((const char*)args, 1); wait(1); } } int main() { Thread t2(test_thread, (void *)"I\n"); Thread t3(test_thread, (void *)"am\n");
test_thread((void *)"Ironman\n"); }
Master code:
- include "mbed.h" Serial pc (USBTX,USBRX); I2C i2c(p28, p27); int main() { int address = 0xA0; char data[20]; pc.printf("enter data to be sent"); pc.scanf("%s",&data); pc.printf("%s",data); int l=strlen(data); i2c.write(address, data, l); wait(10); }
Slave code:
- include <mbed.h> Serial pc(USBTX, USBRX); I2CSlave slave(p28, p27); int main() { char buf[20]; char msg[] = "Slave!"; slave.address(0xA0); while (1) { int i = slave.receive(); switch (i) { case I2CSlave::ReadAddressed:slave.write(msg, strlen(msg) + 1); Includes null char break; case I2CSlave::WriteGeneral:slave.read(buf, 20); pc.printf("Read : %s\n", buf); break; case I2CSlave::WriteAddressed:slave.read(buf, 20); pc.printf("Read : %s\n", buf); break; } for(int i = 0; i< 20; i++) { buf[i] = 0; } } }
Please log in to post comments.