master

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
brahimir
Date:
Sat Feb 09 13:02:17 2019 +0000
Commit message:
master

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Sat Feb 09 13:02:17 2019 +0000
@@ -0,0 +1,102 @@
+#include "mbed.h"
+//master-transmitter i master-receiver
+
+I2C i2c_master(PB_3, PB_10); //sda, scl ADRESE!!!!!!!!!!!
+Serial pc(USBTX, USBRX);
+PwmOut motor(PB_4); //fan
+
+const int addr_senzor = 0x90; //adresa senzora
+const int addr_slave = 0x80; //adresa slave mbed-a
+float temp;
+float temp_ref;
+char podaci[2];
+char config_t[3]; //config polje
+char temp_read[2]; //polje za 16 bita podataka sa senzora
+
+void config_senzor(){
+    config_t[0]=0x01;
+    config_t[1]=0x60;
+    config_t[2]=0xA0;
+    i2c_master.write(addr_senzor, config_t, 3);
+    config_t[0]=0x00; 
+    }
+
+float ocitaj_temp(){
+        i2c_master.read(addr_senzor, temp_read, 2); // ocitanje s TMP102 senzora
+        temp = 0.0625 * (((temp_read[0] << 8) + temp_read[1]) >> 4); //konverzija u float vrijednost deg C
+        pc.printf("Poslana T: %2f\n\r", temp);
+        //float temp_k = temp + 273.15;
+        //float temp_f = (temp*(9/5)) + 32;
+        return temp;
+        }
+        
+void salji_temp(){
+        //master-transmitter
+        i2c_master.write(addr_slave<<1, temp_read, 2, false);
+        pc.printf("Poslani podaci:%x,%x\n\r", podaci[0], podaci[1]);
+        wait_us(50);
+        }   
+        
+//void upali_motor(){
+//    motor.period_us(50);
+//    motor.pulsewidth_us(30);
+//    }
+
+float dohvati_ref(){
+        //master-receiver
+        i2c_master.read(addr_slave<<1, podaci, 2);
+        pc.printf("Primljeni podaci:%x,%x\n\r", podaci[0], podaci[1]);
+        wait_us(50);
+        float ref = (podaci[0]<<8) + podaci[1];
+        temp_ref = (float)ref/100;
+        pc.printf("Primljena T_ref: %f\n\r", temp_ref);
+        return temp_ref;
+        }
+        
+//void usporedi_temp(){
+//        if (temp_ref < temp){
+//            upali_motor();
+//            }else{
+//                motor = 0;
+//               }
+//        }       
+        
+
+int main(){
+    //char podaci[3] = {0x25, 0xAB, 0x11};
+    //char config_t[3]; //config polje
+    //char temp_read[2]; //polje za 16 bita podataka sa senzora
+    //const int addr_senzor = 0x90; //adresa senzora
+    //const int addr_slave = 0x80; //adresa slave mbed-a
+    
+    motor = 0;
+    config_senzor();
+    i2c_master.frequency(100000); //100kHz
+  
+    while(1){
+        ocitaj_temp();
+        dohvati_ref();
+        //usporedi_temp();
+        //if (temp_ref < temp){
+        //    motor.period_us(50);
+        //    motor.pulsewidth_us(30);
+        //    }else{
+        //        motor = 0;
+        //       }
+        salji_temp();
+        //prihvati_mod(); //treba napraviti funkciju kojom ce se birati mod c, k, f
+        
+        
+        //master-transmitter
+        //i2c_master.write(addr_slave<<1, temp_read, 2, false);
+        //pc.printf("Poslani podaci:%x,%x,\n", podaci[0], podaci[1]);
+        //wait_us(50);
+        
+        //master-reicever
+        //i2c_master.read(addr_slave<<1, podaci, 2, false);
+        //pc.printf("Primljeni podaci:%x,%x\n", podaci[0], podaci[1]);
+        
+        wait(0.5);
+        
+        }
+    }
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Sat Feb 09 13:02:17 2019 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/mbed_official/code/mbed/builds/3a7713b1edbc
\ No newline at end of file