Andrés Quesada / Mbed 2 deprecated Esclavo

Dependencies:   TextLCD mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 
00002 /*CONFIGURACION SLAVE*
00003 /*Remember, you will need a pull-up resistor on sda and scl.
00004 All drivers on the I2C bus are required to be open collector, and 
00005 so it is necessary for pull up resistors to be used on the two signals.  
00006 A typical value for the pullup resistors is around 2.2k ohms, 
00007 connected between the pin and 3v3. https://developer.mbed.org/handbook/I2C*/
00008 #include "mbed.h"
00009 #include "TextLCD.h"
00010 TextLCD lcd(p21, p22, p23, p24, p25, p26); // rs, e, d4, d5, d6, d7
00011 //------CONFIGURACION PINES DE TR/RE-----------------
00012 SPISlave ser_port(p5, p6, p7, p8); //mosi, miso, sclk, ssel el p8 al digout del master
00013 I2CSlave slave(p9, p10); //Configure I2C slave
00014 Serial async_port(p13, p14); //set up TX and RX on pins 28 and 27
00015 //-------CONFIGURACION LEDS--------------------------------
00016 DigitalOut red_led(p19); //red led
00017 DigitalOut green_led(p28); //green led OJO    
00018 DigitalOut strobe(p11); //a strobe to trigger the scope
00019 DigitalOut cs(p12); //this acts as “slave select”
00020 
00021 
00022 //-------CONFIGURACION INTERRUPTORES
00023 DigitalIn pulsa15(p15);//palanca PARA 1 o 2
00024 InterruptIn buttonp16(p16);//palanca para modos de transmision i2c
00025 InterruptIn buttonp17(p17); //spi
00026 InterruptIn buttonp18(p18); //uart
00027 //-------CARACTERES DE INTERCAMBIO
00028 char switch_word ; //word we will send
00029 char recd_val; //value return from slave
00030 //---------VAARIABLE CONVERSION VOLTAJE
00031 float milivolts;
00032 //---------TIMER-------------------------
00033 Timer t2; // define Timer with name “t”
00034 //--------FUNCIONES------------------
00035 void comprueba(){
00036      red_led=0; //preset both to 0
00037     green_led=0;
00038     lcd.cls();//limpiamos el charco
00039     recd_val=recd_val & 0x03; //AND out unwanted bits
00040     if (recd_val==1)
00041      {   red_led=1;
00042         lcd.printf("El valor  es 1");
00043         wait(0.5);
00044         }//no debe de salir
00045     if (recd_val==2)
00046       {  green_led=1;
00047       lcd.printf("El valor  es 2");
00048       wait(0.5);}
00049     if (recd_val==3){
00050         red_led=1;
00051         green_led=1;
00052         lcd.printf("El valor es 3");
00053         wait(0.5);
00054                 }
00055     
00056     }
00057 void i2cpP(){
00058      lcd.printf("Prueba i2cp");
00059         wait(2);
00060         lcd.cls();
00061   while(1){  
00062     slave.write(switch_word); //load up word to send
00063         //test for I2C, and act accordingly
00064         int i = slave.receive();
00065         if (i == 3)
00066             { 
00067             //slave is addressed, Master will write
00068              lcd.printf("entra en if"); 
00069       wait(2);
00070             recd_val= slave.read();
00071             comprueba();
00072             }
00073     }
00074     
00075     }
00076 void spiP(){
00077     lcd.printf("Prueba spi");
00078         wait(2);
00079         lcd.cls();
00080         t2.reset();// reseteamos el timer
00081         t2.start(); //start the timer
00082 while(t2.read()<10){
00083        if(ser_port.receive()){
00084             milivolts=ser_port.read();    
00085         }
00086         lcd.printf("%1.2f",milivolts);
00087         wait(0.05);
00088         lcd.cls(); 
00089         
00090         }
00091         /*
00092   while(1){  
00093     if(ser_port.receive()) { //test if data transfer has occurred
00094      lcd.printf("entra en if"); 
00095       wait(2);
00096             recd_val = ser_port.read(); // Read byte from master
00097             ser_port.reply(switch_word); // Make this the next reply
00098         comprueba();
00099         }
00100   */
00101     
00102     
00103     }
00104 void uartP(){
00105     lcd.printf("Prueba uart");
00106         wait(2);
00107         lcd.cls();
00108         t2.reset();// reseteamos el timer
00109         t2.start(); //start the timer
00110 while(t2.read()<10){ // le damos 10 segundos de prueba
00111         strobe =1; //short strobe pulse
00112         wait_us(10);
00113         strobe=0;
00114         async_port.putc(switch_word); //transmit switch_word
00115         if (async_port.readable()==1) //is there a character to be read?
00116         {recd_val=async_port.getc(); //if yes, then read it
00117         lcd.printf("entra en if"); 
00118         wait(1);
00119          lcd.printf("%c \n\r",recd_val);
00120           wait(1);
00121         comprueba(); }
00122         lcd.printf("Postcomprueba"); 
00123         wait(1);
00124          lcd.cls();
00125    
00126     }
00127     t2.stop();
00128     }
00129     
00130 int main() {
00131     async_port.baud(9600); 
00132     slave.address(0x52);
00133     while(1) {
00134         switch_word=0xa0; //set up a recognizable output pattern
00135     if (pulsa15==1)
00136         switch_word=switch_word | 0x01; //OR in lsb
00137     if (pulsa15==0)
00138         switch_word=switch_word | 0x02; //OR in next lsb
00139         
00140       //preparados para acceder a los modos de comunicación  
00141       buttonp16.rise(&i2cpP);
00142       buttonp17.rise(&spiP);
00143       buttonp18.rise(&uartP);   
00144         
00145     }
00146      
00147     
00148     
00149     
00150 }