Marek Trojan / _8_CONV

Dependencies:   mbed-src

Fork of _8_CONV_1_SLAVE by Marek Trojan

Revision:
6:b9578f756c9c
Parent:
5:d622f8a4a016
--- a/main.cpp	Thu Dec 17 00:03:01 2015 +0000
+++ b/main.cpp	Thu Dec 17 18:33:21 2015 +0000
@@ -1,9 +1,15 @@
 // Conv1 Slave
 #include "main.h"
-
+    volatile int i = 0;
+    volatile int discovered_address = 0;
 int main()
 {
     init();
+    int prev_state = 1;
+    int temp_state;
+    uint8_t address = 0;
+
+    int states[9];
     while (1) {}
 }
 
@@ -61,39 +67,34 @@
 void init(void)
 {    
     device.baud(921600);
-    SDA_interrupt.fall(&on_SDA_falling_slope_interrupt);
+    SCL_interrupt.rise(&on_SCL_rising_slope_interrupt);
 }
 
-void on_SDA_falling_slope_interrupt(void)
+void on_SCL_rising_slope_interrupt(void)
 {
-    SDA_interrupt.disable_irq();
-    int prev_state = 1;
-    int temp_state;
-    uint8_t address = 0;
-    for (int i = 7; i >= 0; i--) //for every clock cycle in frame
-    {
-        while(1) //sample CLK line state
+    
+    //=(char)SDA_state
+    if (i<9)
         {
-            if ((temp_state = SCL_state) == prev_state) 
-                continue;
-            else
-            {
-                if (SDA_state == 1) 
-                    address |= (0x01 << i); 
-                prev_state = temp_state;
-                //device.putc((char)SDA_state);
-                break;
-            }
+            if (SDA_state) 
+                discovered_address |= (0x01<<(7-i));
+            i++;
         }
+    else
+    { 
+        SCL_interrupt.disable_irq();
+        device.putc((char)discovered_address); 
+        i = 0;
+        discovered_address = 0;
     }
-    
+                
     // now we (probably) know the Slave address, so we can initialize i2c bus
     // and set this Conv as Conv1 and send info to second Conv to act as Conv2
     
     //device.putc(SET_CONV2_FUNCT);
-    device.putc(address);
+    //device.putc(address);
     //init_I2C(address, 100000);
-    SDA_interrupt.enable_irq();
+    //SDA_interrupt.enable_irq();
 }
 
 void init_I2C(char addr, int freq)