Marko Marinović
/
ProjektKTM_SLAVE_v16
slave
main.cpp@0:e1ce6b0a884d, 2019-02-09 (annotated)
- Committer:
- brahimir
- Date:
- Sat Feb 09 13:03:22 2019 +0000
- Revision:
- 0:e1ce6b0a884d
slave
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
brahimir | 0:e1ce6b0a884d | 1 | #include "mbed.h" |
brahimir | 0:e1ce6b0a884d | 2 | #include "TextLCD.h" |
brahimir | 0:e1ce6b0a884d | 3 | //slave |
brahimir | 0:e1ce6b0a884d | 4 | |
brahimir | 0:e1ce6b0a884d | 5 | AnalogIn pot(PTB1); // postavi pin |
brahimir | 0:e1ce6b0a884d | 6 | InterruptIn button(PTD4); |
brahimir | 0:e1ce6b0a884d | 7 | DigitalOut mod1(PTD0); |
brahimir | 0:e1ce6b0a884d | 8 | DigitalOut mod2(PTD2); |
brahimir | 0:e1ce6b0a884d | 9 | DigitalOut mod3(PTD3); |
brahimir | 0:e1ce6b0a884d | 10 | Timer debounce; |
brahimir | 0:e1ce6b0a884d | 11 | I2CSlave i2c_slave(PTC11, PTC10); //sda, scl |
brahimir | 0:e1ce6b0a884d | 12 | TextLCD lcd(PTB8,PTB9,PTB10,PTB11,PTE2,PTE3,TextLCD::LCD16x2); // LCD |
brahimir | 0:e1ce6b0a884d | 13 | Serial pc(USBTX,USBRX); |
brahimir | 0:e1ce6b0a884d | 14 | |
brahimir | 0:e1ce6b0a884d | 15 | float temp; |
brahimir | 0:e1ce6b0a884d | 16 | float temp_k; |
brahimir | 0:e1ce6b0a884d | 17 | float temp_f; |
brahimir | 0:e1ce6b0a884d | 18 | int rec; |
brahimir | 0:e1ce6b0a884d | 19 | char podaci[2]; |
brahimir | 0:e1ce6b0a884d | 20 | char temp_read[2]; |
brahimir | 0:e1ce6b0a884d | 21 | const int addr_slave = 0x80; |
brahimir | 0:e1ce6b0a884d | 22 | int state = 0; |
brahimir | 0:e1ce6b0a884d | 23 | int old = 0; |
brahimir | 0:e1ce6b0a884d | 24 | //int buttonPoll = 0; |
brahimir | 0:e1ce6b0a884d | 25 | |
brahimir | 0:e1ce6b0a884d | 26 | float pretvori_temp(){ |
brahimir | 0:e1ce6b0a884d | 27 | temp = 0.0625 * (((temp_read[0] << 8) + temp_read[1]) >> 4); //konverzija u float vrijednost deg C |
brahimir | 0:e1ce6b0a884d | 28 | return temp; |
brahimir | 0:e1ce6b0a884d | 29 | } |
brahimir | 0:e1ce6b0a884d | 30 | |
brahimir | 0:e1ce6b0a884d | 31 | void isr1(){ |
brahimir | 0:e1ce6b0a884d | 32 | if (debounce.read_ms() > 200) { |
brahimir | 0:e1ce6b0a884d | 33 | state = old + 1; |
brahimir | 0:e1ce6b0a884d | 34 | old = state; |
brahimir | 0:e1ce6b0a884d | 35 | debounce.reset(); |
brahimir | 0:e1ce6b0a884d | 36 | } |
brahimir | 0:e1ce6b0a884d | 37 | } |
brahimir | 0:e1ce6b0a884d | 38 | |
brahimir | 0:e1ce6b0a884d | 39 | |
brahimir | 0:e1ce6b0a884d | 40 | int main(){ |
brahimir | 0:e1ce6b0a884d | 41 | mod1 = 1; |
brahimir | 0:e1ce6b0a884d | 42 | mod2 = 0; |
brahimir | 0:e1ce6b0a884d | 43 | mod3 = 0; |
brahimir | 0:e1ce6b0a884d | 44 | |
brahimir | 0:e1ce6b0a884d | 45 | lcd.cls(); |
brahimir | 0:e1ce6b0a884d | 46 | wait(0.01); |
brahimir | 0:e1ce6b0a884d | 47 | i2c_slave.frequency(100000); |
brahimir | 0:e1ce6b0a884d | 48 | i2c_slave.address(addr_slave<<1); |
brahimir | 0:e1ce6b0a884d | 49 | |
brahimir | 0:e1ce6b0a884d | 50 | debounce.start(); |
brahimir | 0:e1ce6b0a884d | 51 | button.rise(&isr1); |
brahimir | 0:e1ce6b0a884d | 52 | |
brahimir | 0:e1ce6b0a884d | 53 | while(1){ |
brahimir | 0:e1ce6b0a884d | 54 | float Ain = pot.read(); |
brahimir | 0:e1ce6b0a884d | 55 | float temp_ref = 17.0 + ((float)Ain*10.0); // 17 - 27 C T_ref |
brahimir | 0:e1ce6b0a884d | 56 | short temp_ref_100 = temp_ref*100; |
brahimir | 0:e1ce6b0a884d | 57 | |
brahimir | 0:e1ce6b0a884d | 58 | rec = i2c_slave.receive(); |
brahimir | 0:e1ce6b0a884d | 59 | |
brahimir | 0:e1ce6b0a884d | 60 | switch(rec){ |
brahimir | 0:e1ce6b0a884d | 61 | case 0: break; |
brahimir | 0:e1ce6b0a884d | 62 | case 1: |
brahimir | 0:e1ce6b0a884d | 63 | wait_us(50); //clock-stretching |
brahimir | 0:e1ce6b0a884d | 64 | podaci[0] = temp_ref_100>>8; //MSB |
brahimir | 0:e1ce6b0a884d | 65 | podaci[1] = temp_ref_100; //LSB |
brahimir | 0:e1ce6b0a884d | 66 | i2c_slave.write(podaci,2); |
brahimir | 0:e1ce6b0a884d | 67 | break; |
brahimir | 0:e1ce6b0a884d | 68 | case 2: |
brahimir | 0:e1ce6b0a884d | 69 | case 3: |
brahimir | 0:e1ce6b0a884d | 70 | i2c_slave.read(temp_read,2); |
brahimir | 0:e1ce6b0a884d | 71 | pretvori_temp(); |
brahimir | 0:e1ce6b0a884d | 72 | temp_k= temp + 273.15; // T(K) = t(C); |
brahimir | 0:e1ce6b0a884d | 73 | temp_f= (temp*1.8)+32; //(0°C × 9/5) + 32 = 32°F |
brahimir | 0:e1ce6b0a884d | 74 | lcd.cls(); |
brahimir | 0:e1ce6b0a884d | 75 | wait(0.01); |
brahimir | 0:e1ce6b0a884d | 76 | switch(state){ |
brahimir | 0:e1ce6b0a884d | 77 | case 1: //celsius |
brahimir | 0:e1ce6b0a884d | 78 | mod1 = 1; |
brahimir | 0:e1ce6b0a884d | 79 | mod2 = 0; |
brahimir | 0:e1ce6b0a884d | 80 | mod3 = 0; |
brahimir | 0:e1ce6b0a884d | 81 | lcd.printf("Mod 1 - Celsius\nT = %.2f C", temp); |
brahimir | 0:e1ce6b0a884d | 82 | //pc.printf("Primljena temp: %2f C\n\r", temp); |
brahimir | 0:e1ce6b0a884d | 83 | wait(0.5); |
brahimir | 0:e1ce6b0a884d | 84 | break; |
brahimir | 0:e1ce6b0a884d | 85 | case 2: //kelvin |
brahimir | 0:e1ce6b0a884d | 86 | mod1 = 0; |
brahimir | 0:e1ce6b0a884d | 87 | mod2 = 1; |
brahimir | 0:e1ce6b0a884d | 88 | mod3 = 0; |
brahimir | 0:e1ce6b0a884d | 89 | lcd.printf("Mod 2 - Kelvin\nT = %.2f K", temp_k); |
brahimir | 0:e1ce6b0a884d | 90 | //pc.printf("Primljena temp: %2f K\n\r", temp_k); |
brahimir | 0:e1ce6b0a884d | 91 | wait(0.5); |
brahimir | 0:e1ce6b0a884d | 92 | break; |
brahimir | 0:e1ce6b0a884d | 93 | case 3: //fahrenheit |
brahimir | 0:e1ce6b0a884d | 94 | mod1 = 0; |
brahimir | 0:e1ce6b0a884d | 95 | mod2 = 0; |
brahimir | 0:e1ce6b0a884d | 96 | mod3 = 1; |
brahimir | 0:e1ce6b0a884d | 97 | lcd.printf("Mod 3 - Fahr\nT = %.2f F", temp_f); |
brahimir | 0:e1ce6b0a884d | 98 | //pc.printf("Primljena temp: %2f F\n\r", temp_f); |
brahimir | 0:e1ce6b0a884d | 99 | wait(0.5); |
brahimir | 0:e1ce6b0a884d | 100 | break; |
brahimir | 0:e1ce6b0a884d | 101 | case 4: |
brahimir | 0:e1ce6b0a884d | 102 | mod1 = 0; |
brahimir | 0:e1ce6b0a884d | 103 | mod2 = 0; |
brahimir | 0:e1ce6b0a884d | 104 | mod3 = 0; |
brahimir | 0:e1ce6b0a884d | 105 | lcd.printf("Temp ref:\nT = %.2f C", temp_ref); |
brahimir | 0:e1ce6b0a884d | 106 | //pc.printf("Poslana temp_ref: %2f C\n\r", temp_f); |
brahimir | 0:e1ce6b0a884d | 107 | wait(0.5); |
brahimir | 0:e1ce6b0a884d | 108 | break; |
brahimir | 0:e1ce6b0a884d | 109 | default: //celsius |
brahimir | 0:e1ce6b0a884d | 110 | old = 1; |
brahimir | 0:e1ce6b0a884d | 111 | mod1 = 1; |
brahimir | 0:e1ce6b0a884d | 112 | mod2 = 0; |
brahimir | 0:e1ce6b0a884d | 113 | mod3 = 0; |
brahimir | 0:e1ce6b0a884d | 114 | lcd.printf("Mod 1 - Celsius\nT = %.2f C", temp); |
brahimir | 0:e1ce6b0a884d | 115 | //pc.printf("Primljena temp: %2f C\n\r", temp); |
brahimir | 0:e1ce6b0a884d | 116 | wait(0.5); |
brahimir | 0:e1ce6b0a884d | 117 | break; |
brahimir | 0:e1ce6b0a884d | 118 | } |
brahimir | 0:e1ce6b0a884d | 119 | break; |
brahimir | 0:e1ce6b0a884d | 120 | default:break; |
brahimir | 0:e1ce6b0a884d | 121 | } |
brahimir | 0:e1ce6b0a884d | 122 | |
brahimir | 0:e1ce6b0a884d | 123 | } |
brahimir | 0:e1ce6b0a884d | 124 | } |