Andrés Quesada / Mbed 2 deprecated Maestro

Dependencies:   TextLCD mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*CONFIGURACION MAESTRO
00002 Mediante las palancas(1,2,3) seleccionamos el modo de comunicacion
00003 La cuarta palanca sirve para hacer un or con 1 o 2 al uart
00004 SPI y uart estan preparados para 10 segundos de demo mediante timer
00005 SPI Maestro transmite la lectura del sensor de temperatura conectado a l pin 20
00006 al equipo esclavo
00007 
00008 */
00009 #include "mbed.h"
00010 #include "TextLCD.h"
00011 TextLCD lcd(p21, p22, p23, p24, p25, p26); // rs, rw, d4, d5, d6, d7
00012 //------CONFIGURACION PINES DE TR/RE-----------------
00013 SPI ser_port(p5, p6, p7); //mosi, miso, sclk
00014 I2C i2c_port(p9, p10);//SDA,SCL
00015 Serial async_port(p13, p14); //CAMBIADOS AL 13 Y 14///set up TX and RX on pins 28 and 27
00016 //-------CONFIGURACION LEDS--------------------------------
00017 DigitalOut red_led(p19); //red led
00018 DigitalOut green_led(p28); //green led
00019 DigitalOut strobe(p11); //a strobe to trigger the scope
00020 DigitalOut cs(p12); //this acts as “slave select”
00021 //----------------SELECT SPI----AL P8 DEL ESCLAVO
00022 DigitalOut selec(p27);//para seleccionar CONECTAR AL PIN8 DEL ESCLAVO
00023 //-------CONFIGURACION INTERRUPTORES
00024 DigitalIn pulsa15(p15);//palanca PARA 1 o 2
00025 InterruptIn buttonp16(p16);//palanca para modos de transmision
00026 InterruptIn buttonp17(p17);
00027 InterruptIn buttonp18(p18);
00028 //---------VAARIABLE CONVERSION VOLTAJE
00029 float milivolts;
00030 //-----Entrada termometro
00031 AnalogIn tem(p20);
00032 //---------TIMER-------------------------
00033 Timer t2; // define Timer with name “t”
00034 //-------CARACTERES DE INTERCAMBIO
00035 char switch_word ; //word we will send
00036 char recd_val; //value return from slave
00037 const int addr = 0x52; //the I2C slave address, an arbitrary even number <-- ojo con esto
00038 //--------FUNCIONES------------------
00039 void comprueba(){
00040      red_led=0; //preset both to 0
00041     green_led=0;
00042     lcd.cls();//limpiamos el charco
00043     recd_val=recd_val & 0x03; //AND out unwanted bits
00044     if (recd_val==1)
00045      {   red_led=1;
00046         lcd.printf("LLega %c \n\r",recd_val);
00047         wait(0.5);
00048         }//no debe de salir
00049     if (recd_val==2)
00050       {  green_led=1;
00051       lcd.printf("El valor enviado es 2");
00052       wait(0.5);}
00053     if (recd_val==3){
00054         red_led=1;
00055         green_led=1;
00056         lcd.printf("El valor enviado es 3");
00057         wait(0.5);
00058                 }
00059     
00060     }
00061 void i2cpP(){
00062      
00063      /*Remember, you will need a pull-up resistor on sda and scl.
00064 All drivers on the I2C bus are required to be open collector, and 
00065 so it is necessary for pull up resistors to be used on the two signals.  
00066 A typical value for the pullup resistors is around 2.2k ohms, 
00067 connected between the pin and 3v3. https://developer.mbed.org/handbook/I2C*/
00068      lcd.printf("Prueba i2cp");
00069         wait(2);
00070         lcd.cls();
00071         lcd.printf("COMIENZO ");
00072    //send a single byte of data, in correct I2C package
00073         i2c_port.start(); //force a start condition
00074         i2c_port.write(addr); //send the address
00075         i2c_port.write(switch_word); //send one byte of data, ie switch_word
00076         i2c_port.stop(); //force a stop condition
00077         wait(0.002);
00078         lcd.printf("POST WAIT");
00079         //receive a single byte of data, in correct I2C package 
00080         i2c_port.start();
00081         i2c_port.write(addr | 0x01); //send address, with R/W bit set to Read  <-- ojo con esto
00082         recd_val=i2c_port.read(addr); //Read and save the received byte
00083         i2c_port.stop(); //force a stop condition
00084         comprueba();
00085     
00086     
00087     }
00088 void spiP(){
00089     lcd.printf("Prueba spi");
00090         wait(2);
00091         lcd.cls();
00092         t2.reset();// reseteamos el timer
00093         t2.start(); //start the timer
00094 while(t2.read()<10)
00095 {
00096    milivolts = (tem / 1023.0) * 500;// /5000*10
00097    cs = 0; //select slave
00098         ser_port.write(milivolts);
00099         cs = 1;
00100    }
00101    /*
00102     cs = 0; //select slave
00103     recd_val=ser_port.write(switch_word); //send switch_word and receive data
00104     cs = 1;
00105     wait(0.01);
00106     //set leds according to incoming word from slave
00107     
00108     */
00109   // comprueba();
00110    
00111     }
00112 void uartP(){
00113     lcd.printf("Prueba uart");
00114         wait(2);
00115         lcd.cls();
00116     t2.reset();// reseteamos el timer
00117         t2.start(); //start the timer
00118 while(t2.read()<10)
00119 {
00120         strobe =1; //short strobe pulse
00121         wait_us(10);
00122         strobe=0;
00123         // lcd.printf("POST STROBE");
00124          wait(2);
00125         async_port.putc(switch_word); //transmit switch_word
00126         if (async_port.readable()==1) //is there a character to be read?
00127         recd_val=async_port.getc(); //if yes, then read it
00128         // lcd.printf("PRECOMPRUEBA");
00129           wait(2);
00130         comprueba();  
00131   // lcd.printf("POSTCOMPRUEBA");
00132           wait(2);
00133     }
00134     t2.stop();//paramos el timer
00135     }
00136     
00137 int main() {
00138      lcd.printf("Prueba ");
00139     async_port.baud(9600); 
00140     while(1) {
00141         switch_word=0xa0; //set up a recognizable output pattern
00142     if (pulsa15==1)
00143         switch_word=switch_word | 0x01; //OR in lsb
00144     if (pulsa15==0)
00145         switch_word=switch_word | 0x02; //OR in next lsb
00146       buttonp16.rise(&i2cpP);
00147       buttonp17.rise(&spiP);
00148       buttonp18.rise(&uartP);   
00149         
00150     }
00151     
00152     
00153 }