Marko Marinović
/
ProjektKTM_MASTER_v13
master
main.cpp@0:77a172b1f970, 2019-02-09 (annotated)
- Committer:
- brahimir
- Date:
- Sat Feb 09 13:02:17 2019 +0000
- Revision:
- 0:77a172b1f970
master
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
brahimir | 0:77a172b1f970 | 1 | #include "mbed.h" |
brahimir | 0:77a172b1f970 | 2 | //master-transmitter i master-receiver |
brahimir | 0:77a172b1f970 | 3 | |
brahimir | 0:77a172b1f970 | 4 | I2C i2c_master(PB_3, PB_10); //sda, scl ADRESE!!!!!!!!!!! |
brahimir | 0:77a172b1f970 | 5 | Serial pc(USBTX, USBRX); |
brahimir | 0:77a172b1f970 | 6 | PwmOut motor(PB_4); //fan |
brahimir | 0:77a172b1f970 | 7 | |
brahimir | 0:77a172b1f970 | 8 | const int addr_senzor = 0x90; //adresa senzora |
brahimir | 0:77a172b1f970 | 9 | const int addr_slave = 0x80; //adresa slave mbed-a |
brahimir | 0:77a172b1f970 | 10 | float temp; |
brahimir | 0:77a172b1f970 | 11 | float temp_ref; |
brahimir | 0:77a172b1f970 | 12 | char podaci[2]; |
brahimir | 0:77a172b1f970 | 13 | char config_t[3]; //config polje |
brahimir | 0:77a172b1f970 | 14 | char temp_read[2]; //polje za 16 bita podataka sa senzora |
brahimir | 0:77a172b1f970 | 15 | |
brahimir | 0:77a172b1f970 | 16 | void config_senzor(){ |
brahimir | 0:77a172b1f970 | 17 | config_t[0]=0x01; |
brahimir | 0:77a172b1f970 | 18 | config_t[1]=0x60; |
brahimir | 0:77a172b1f970 | 19 | config_t[2]=0xA0; |
brahimir | 0:77a172b1f970 | 20 | i2c_master.write(addr_senzor, config_t, 3); |
brahimir | 0:77a172b1f970 | 21 | config_t[0]=0x00; |
brahimir | 0:77a172b1f970 | 22 | } |
brahimir | 0:77a172b1f970 | 23 | |
brahimir | 0:77a172b1f970 | 24 | float ocitaj_temp(){ |
brahimir | 0:77a172b1f970 | 25 | i2c_master.read(addr_senzor, temp_read, 2); // ocitanje s TMP102 senzora |
brahimir | 0:77a172b1f970 | 26 | temp = 0.0625 * (((temp_read[0] << 8) + temp_read[1]) >> 4); //konverzija u float vrijednost deg C |
brahimir | 0:77a172b1f970 | 27 | pc.printf("Poslana T: %2f\n\r", temp); |
brahimir | 0:77a172b1f970 | 28 | //float temp_k = temp + 273.15; |
brahimir | 0:77a172b1f970 | 29 | //float temp_f = (temp*(9/5)) + 32; |
brahimir | 0:77a172b1f970 | 30 | return temp; |
brahimir | 0:77a172b1f970 | 31 | } |
brahimir | 0:77a172b1f970 | 32 | |
brahimir | 0:77a172b1f970 | 33 | void salji_temp(){ |
brahimir | 0:77a172b1f970 | 34 | //master-transmitter |
brahimir | 0:77a172b1f970 | 35 | i2c_master.write(addr_slave<<1, temp_read, 2, false); |
brahimir | 0:77a172b1f970 | 36 | pc.printf("Poslani podaci:%x,%x\n\r", podaci[0], podaci[1]); |
brahimir | 0:77a172b1f970 | 37 | wait_us(50); |
brahimir | 0:77a172b1f970 | 38 | } |
brahimir | 0:77a172b1f970 | 39 | |
brahimir | 0:77a172b1f970 | 40 | //void upali_motor(){ |
brahimir | 0:77a172b1f970 | 41 | // motor.period_us(50); |
brahimir | 0:77a172b1f970 | 42 | // motor.pulsewidth_us(30); |
brahimir | 0:77a172b1f970 | 43 | // } |
brahimir | 0:77a172b1f970 | 44 | |
brahimir | 0:77a172b1f970 | 45 | float dohvati_ref(){ |
brahimir | 0:77a172b1f970 | 46 | //master-receiver |
brahimir | 0:77a172b1f970 | 47 | i2c_master.read(addr_slave<<1, podaci, 2); |
brahimir | 0:77a172b1f970 | 48 | pc.printf("Primljeni podaci:%x,%x\n\r", podaci[0], podaci[1]); |
brahimir | 0:77a172b1f970 | 49 | wait_us(50); |
brahimir | 0:77a172b1f970 | 50 | float ref = (podaci[0]<<8) + podaci[1]; |
brahimir | 0:77a172b1f970 | 51 | temp_ref = (float)ref/100; |
brahimir | 0:77a172b1f970 | 52 | pc.printf("Primljena T_ref: %f\n\r", temp_ref); |
brahimir | 0:77a172b1f970 | 53 | return temp_ref; |
brahimir | 0:77a172b1f970 | 54 | } |
brahimir | 0:77a172b1f970 | 55 | |
brahimir | 0:77a172b1f970 | 56 | //void usporedi_temp(){ |
brahimir | 0:77a172b1f970 | 57 | // if (temp_ref < temp){ |
brahimir | 0:77a172b1f970 | 58 | // upali_motor(); |
brahimir | 0:77a172b1f970 | 59 | // }else{ |
brahimir | 0:77a172b1f970 | 60 | // motor = 0; |
brahimir | 0:77a172b1f970 | 61 | // } |
brahimir | 0:77a172b1f970 | 62 | // } |
brahimir | 0:77a172b1f970 | 63 | |
brahimir | 0:77a172b1f970 | 64 | |
brahimir | 0:77a172b1f970 | 65 | int main(){ |
brahimir | 0:77a172b1f970 | 66 | //char podaci[3] = {0x25, 0xAB, 0x11}; |
brahimir | 0:77a172b1f970 | 67 | //char config_t[3]; //config polje |
brahimir | 0:77a172b1f970 | 68 | //char temp_read[2]; //polje za 16 bita podataka sa senzora |
brahimir | 0:77a172b1f970 | 69 | //const int addr_senzor = 0x90; //adresa senzora |
brahimir | 0:77a172b1f970 | 70 | //const int addr_slave = 0x80; //adresa slave mbed-a |
brahimir | 0:77a172b1f970 | 71 | |
brahimir | 0:77a172b1f970 | 72 | motor = 0; |
brahimir | 0:77a172b1f970 | 73 | config_senzor(); |
brahimir | 0:77a172b1f970 | 74 | i2c_master.frequency(100000); //100kHz |
brahimir | 0:77a172b1f970 | 75 | |
brahimir | 0:77a172b1f970 | 76 | while(1){ |
brahimir | 0:77a172b1f970 | 77 | ocitaj_temp(); |
brahimir | 0:77a172b1f970 | 78 | dohvati_ref(); |
brahimir | 0:77a172b1f970 | 79 | //usporedi_temp(); |
brahimir | 0:77a172b1f970 | 80 | //if (temp_ref < temp){ |
brahimir | 0:77a172b1f970 | 81 | // motor.period_us(50); |
brahimir | 0:77a172b1f970 | 82 | // motor.pulsewidth_us(30); |
brahimir | 0:77a172b1f970 | 83 | // }else{ |
brahimir | 0:77a172b1f970 | 84 | // motor = 0; |
brahimir | 0:77a172b1f970 | 85 | // } |
brahimir | 0:77a172b1f970 | 86 | salji_temp(); |
brahimir | 0:77a172b1f970 | 87 | //prihvati_mod(); //treba napraviti funkciju kojom ce se birati mod c, k, f |
brahimir | 0:77a172b1f970 | 88 | |
brahimir | 0:77a172b1f970 | 89 | |
brahimir | 0:77a172b1f970 | 90 | //master-transmitter |
brahimir | 0:77a172b1f970 | 91 | //i2c_master.write(addr_slave<<1, temp_read, 2, false); |
brahimir | 0:77a172b1f970 | 92 | //pc.printf("Poslani podaci:%x,%x,\n", podaci[0], podaci[1]); |
brahimir | 0:77a172b1f970 | 93 | //wait_us(50); |
brahimir | 0:77a172b1f970 | 94 | |
brahimir | 0:77a172b1f970 | 95 | //master-reicever |
brahimir | 0:77a172b1f970 | 96 | //i2c_master.read(addr_slave<<1, podaci, 2, false); |
brahimir | 0:77a172b1f970 | 97 | //pc.printf("Primljeni podaci:%x,%x\n", podaci[0], podaci[1]); |
brahimir | 0:77a172b1f970 | 98 | |
brahimir | 0:77a172b1f970 | 99 | wait(0.5); |
brahimir | 0:77a172b1f970 | 100 | |
brahimir | 0:77a172b1f970 | 101 | } |
brahimir | 0:77a172b1f970 | 102 | } |