Lee Nam Cheol / Mbed OS lab04-i2c-master

Files at this revision

API Documentation at this revision

Comitter:
namcheol
Date:
Mon Apr 27 07:57:55 2020 +0000
Parent:
1:89d23c8072af
Commit message:
lab04-i2c-master

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Tue Apr 21 12:20:31 2020 +0000
+++ b/main.cpp	Mon Apr 27 07:57:55 2020 +0000
@@ -1,16 +1,19 @@
 #include "mbed.h"
 
-PwmOut servo(PTA1);   //servo = PTA1
+I2C i2c(I2C_SDA, I2C_SCL);  //i2c = (I2C_SDA, I2C_SCL)
+DigitalIn sw(D0, PullDown); //sw = D0(PullDown)
+
+const int addr = 0xa0;      //slave addr (even no.)
 
 int main()
 {
-    float w;
-
-    servo.period(20.0 / 1000.0);    //period = 20ms for SG90 servo
-    while (true) { //500 ~ 2500us for degree 0 to 180
-        for(w = 500; w <= 2500; w += 100) {
-            servo.write(w / (20.0 * 1000.0));
-            thread_sleep_for(1000);
+    while(true) {
+        if(sw == 1) {       //if switch is on(pressed)
+            i2c.start();    //start condition
+            i2c.write(addr | 0x00); //write (addr | WRTIE)
+            i2c.write('1');    //write '1'
+            i2c.stop();        //stop condition
+            thread_sleep_for(200);
         }
     }
 }
\ No newline at end of file