slave

Dependencies:   mbed TextLCD

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "TextLCD.h"
00003 //slave
00004 
00005 AnalogIn pot(PTB1);  // postavi pin
00006 InterruptIn button(PTD4);
00007 DigitalOut mod1(PTD0);
00008 DigitalOut mod2(PTD2);
00009 DigitalOut mod3(PTD3);
00010 Timer debounce;
00011 I2CSlave i2c_slave(PTC11, PTC10); //sda, scl
00012 TextLCD lcd(PTB8,PTB9,PTB10,PTB11,PTE2,PTE3,TextLCD::LCD16x2); // LCD
00013 Serial pc(USBTX,USBRX);
00014 
00015 float temp;
00016 float temp_k;
00017 float temp_f;
00018 int rec;
00019 char podaci[2];
00020 char temp_read[2];
00021 const int addr_slave = 0x80;
00022 int state = 0;
00023 int old = 0;
00024 //int buttonPoll = 0;
00025 
00026 float pretvori_temp(){
00027     temp = 0.0625 * (((temp_read[0] << 8) + temp_read[1]) >> 4); //konverzija u float vrijednost deg C
00028     return temp;
00029     }
00030     
00031 void isr1(){
00032   if (debounce.read_ms() > 200) {
00033     state = old + 1;
00034     old = state;
00035     debounce.reset();
00036     }
00037 }
00038 
00039 
00040 int main(){
00041     mod1 = 1;
00042     mod2 = 0;
00043     mod3 = 0;
00044     
00045     lcd.cls();
00046     wait(0.01);
00047     i2c_slave.frequency(100000);
00048     i2c_slave.address(addr_slave<<1);
00049     
00050     debounce.start();
00051     button.rise(&isr1);
00052     
00053     while(1){
00054         float Ain = pot.read();
00055         float temp_ref = 17.0 + ((float)Ain*10.0); // 17 - 27 C T_ref
00056         short temp_ref_100 = temp_ref*100;
00057         
00058         rec = i2c_slave.receive();
00059         
00060         switch(rec){
00061             case 0: break;
00062             case 1:
00063             wait_us(50); //clock-stretching
00064             podaci[0] = temp_ref_100>>8; //MSB
00065             podaci[1] = temp_ref_100; //LSB
00066             i2c_slave.write(podaci,2);
00067             break;
00068             case 2:
00069             case 3:
00070             i2c_slave.read(temp_read,2);
00071             pretvori_temp();
00072             temp_k= temp + 273.15; // T(K) = t(C);
00073             temp_f= (temp*1.8)+32; //(0°C × 9/5) + 32 = 32°F
00074             lcd.cls();
00075             wait(0.01);
00076             switch(state){
00077                 case 1: //celsius
00078                 mod1 = 1;
00079                 mod2 = 0;
00080                 mod3 = 0;
00081                 lcd.printf("Mod 1 - Celsius\nT = %.2f C", temp);
00082                 //pc.printf("Primljena temp: %2f C\n\r", temp);
00083                 wait(0.5);
00084                 break;
00085                 case 2: //kelvin
00086                 mod1 = 0;
00087                 mod2 = 1;
00088                 mod3 = 0;
00089                 lcd.printf("Mod 2 - Kelvin\nT = %.2f K", temp_k); 
00090                 //pc.printf("Primljena temp: %2f K\n\r", temp_k);
00091                 wait(0.5);
00092                 break;
00093                 case 3: //fahrenheit
00094                 mod1 = 0;
00095                 mod2 = 0;
00096                 mod3 = 1;
00097                 lcd.printf("Mod 3 - Fahr\nT = %.2f F", temp_f); 
00098                 //pc.printf("Primljena temp: %2f F\n\r", temp_f);
00099                 wait(0.5);
00100                 break;
00101                 case 4:
00102                 mod1 = 0;
00103                 mod2 = 0;
00104                 mod3 = 0;
00105                 lcd.printf("Temp ref:\nT = %.2f C", temp_ref);
00106                 //pc.printf("Poslana temp_ref: %2f C\n\r", temp_f);
00107                 wait(0.5);
00108                 break;
00109                 default: //celsius
00110                 old = 1;
00111                 mod1 = 1;
00112                 mod2 = 0;
00113                 mod3 = 0;
00114                 lcd.printf("Mod 1 - Celsius\nT = %.2f C", temp);
00115                 //pc.printf("Primljena temp: %2f C\n\r", temp);
00116                 wait(0.5);
00117                 break;
00118                 }
00119             break;
00120             default:break;
00121             }
00122             
00123         }
00124     }