sensor color con joystick y motores paso a paso mas pantalla LCD

Dependencies:   mbed

Revision:
12:b8ee724b42b1
Parent:
11:6b3570e525a1
--- a/main.cpp	Fri May 17 03:56:46 2019 +0000
+++ b/main.cpp	Thu May 30 20:41:52 2019 +0000
@@ -53,21 +53,26 @@
 #define MI  3000
 #define FA  3500
 
-
 /* ||Definicion de Puertos || */
-    Serial command(USBTX, USBRX);
+   
     //                     S0,    S1,   S2,   S3,    OUT
-    scolor_TCS3200 scolor( PA_9, PC_7, PB_6, PA_7, PA_6 ) ;
-    DigitalOut stepper_step ( PB_4 ) ;
-    DigitalOut steppeer_dir ( PB_5 ) ;
-    DigitalOut stepper_step2 ( PB_10 ) ;
-    DigitalOut steppeer_dir2 ( PA_8 ) ;
-    AnalogIn analog_value0 ( A0 ) ;
-    AnalogIn analog_value1 ( A1 ) ;
-    PwmOut mybuzzer( PB_9 ) ;
+    scolor_TCS3200 scolor( PA_9, PC_7, PB_6, PA_4, PB_3 ) ;//PA_9, PC_7, PB_6, PA_7, PA_6 //PA_9, PC_7, PB_6, PA_4, PB_3 
+    Serial command(USBTX, USBRX);
+    
+    DigitalOut stepper_step (PB_4);
+    DigitalOut steppeer_dir (PB_5);
+    DigitalOut stepper_step2 (PB_10);
+    DigitalOut steppeer_dir2 (PA_8);
+        
+    AnalogIn analog_value0 (A0);
+    AnalogIn analog_value1 (A1);
+    PwmOut mybuzzer(PB_9) ;
+    
     Ticker tk1;
+    Ticker tk2;
+    
     volatile int Exit = 0 ;
-    
+
 /*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||/                                                                             /                                                      
 / |                                                                           |/     
 / |              Interrupcion del Joystick- Eliminando ruido                  |/
@@ -112,28 +117,158 @@
 
 /*Definicion de variables codigo principal*/
 
-    uint32_t VELOCITY = 400 ; // Tiempo en micro segundos
-    int16_t Lectura [ 2 ] = {} ;
+    uint32_t VELOCITY = 1000; // Tiempo en micro segundos
+
     double In [ 1 ] = {} ; 
     
 // Definición de las funciones bucle principal
-    void setup_uart ();
     void leer_datos ();
     void leer_color ();
+  
     void funcionesrobot ( uint8_t _Parametro, uint8_t _Comando );
+    int16_t Lectura [ 2 ] = {} ;
+    void setup_uart ();
+    
+    
+    SPI spi(PA_7, PA_6, PA_5); //  MOSI, MISO, SCK
 
+DigitalOut cs(PB_0);    // TFT chipselect pin
+DigitalOut dc(PC_0);    // TFT data command select pin
+DigitalOut rst(PC_1);  // TFT reset pin
+
+#define TFT_WIDTH 320
+#define TFT_HEIGHT 240
+
+#define TFT_BLUE    0x1F00
+#define TFT_RED     0x00F8
+#define TFT_GREEN   0xE007
+#define TFT_YELLOW  0xE0FF
+#define TFT_WHITE   0xFFFF
+
+/*-------------------------------------------------------------------*/
+/*  Write command                                                    */
+/*-------------------------------------------------------------------*/
+void write_cmd(uint8_t cmd)
+{
+    dc = 0;
+    spi.write(cmd);
+}
+  
+/*-------------------------------------------------------------------*/
+/*  Write data                                                       */
+/*-------------------------------------------------------------------*/
+void write_data(uint8_t data)
+{
+    dc = 1;
+    spi.write(data);
+}
+
+/*-------------------------------------------------------------------*/
+/*  TFT reset                                                        */
+/*-------------------------------------------------------------------*/
+void tft_reset()
+{
+    wait_ms(200);
+    cs = 1;
+    dc = 1;
+    rst = 1;
+    wait_ms(200);
+    rst = 0;
+    wait_us(10);
+    rst = 1;
+    wait_ms(120);
+    cs = 0;
+    wait_ms(10);
+        
+    write_cmd(0x3A); // Pixel Format    
+    write_data(0x55); // 16bit Color
+    
+    write_cmd(0xB1); // Frame Control    
+    write_data(0);
+    write_data(0x1f);
+        
+    write_cmd(0x36); // Memory Access Control 
+    write_data(0xE8); // MY MX MV BGR
+
+    write_cmd(0x11); // Sleep Out
+    wait_ms(5);
+        
+    write_cmd(0x29); // Display On    
+}
+
+/*-------------------------------------------------------------------*/
+/*  Set the windows area and Start memory write.                     */
+/*-------------------------------------------------------------------*/
+void tft_set_window(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1)
+{
+  write_cmd(0x2A); // Column Address Set
+  write_data(x0 >> 8);
+  write_data(x0);
+  write_data(x1 >> 8);
+  write_data(x1);
+
+  write_cmd(0x2B); // Page Address Set
+  write_data(y0 >> 8);
+  write_data(y0);
+  write_data(y1 >> 8);
+  write_data(y1);
+
+  write_cmd(0x2C); // Memory Write
+  
+  wait_us(20);
+  
+  dc = 1;
+}
+
+/*-------------------------------------------------------------------*/
+/*  Clear screen                                                     */
+/*-------------------------------------------------------------------*/
+void tft_clear(uint16_t color)
+{
+    tft_set_window(0, 0, TFT_WIDTH, TFT_HEIGHT);
+
+    for (int i = 0; i < TFT_WIDTH * TFT_HEIGHT; ++i)
+    {
+        spi.write(color & 0xff);
+        spi.write(color >> 8);        
+    }    
+}
+    
+    
+    
+    
+    
+    
+    
+    
+    
+    
+    
+    
+    
+    
+    
+    
+    
+    
+    
+    
+    
+    
 int main() {
 
     setup_uart();
-    tk1.attach ( &leer_color, 0.8 ); // Se dirigue a la funcion leer_color cada 0.8 segundos
+    tk1.attach (&leer_color, 10); // Se dirigue a la funcion leer_color cada 0.8 segundos
     Button_joy.fall(callback(Button_joy_onpressed_cb)); // Llama a la funcion Button_joy_onpressed_cb al precionar el botón 
+    spi.frequency(45000000);
     
-    while(1){  
-        
+    while(1){ 
+     
+        tft_reset();
         //Interrupcion boton
         leer_datos();
-        funcionesrobot ( Lectura [ 1 ], Lectura [ 0 ] );
-        
+        funcionesrobot (Lectura [ 1 ], Lectura [ 0 ]);
+        //Lcd();
     }
 }
 
@@ -153,17 +288,31 @@
     for ( i = 0 ; i < 2 ; i++){
         
         Lectura [ i ] = command.getc (); 
-        //printf ( " %4d ", Lectura [ i ]);
+        printf ( " %4d ", Lectura [ i ]);
         
         }
 }
 
 void Rx_interrupt() {
-    Exit = 1;
+    
+    if ( command.readable () == 1 ){
+                        
+    uint8_t l1, l2 ;
+    
+    while ( command.getc()!= INITCMD ) ;
+        l1 = command.getc () ; 
+        l2 = command.getc () ;
+    
+        if ( l1 == 10 and l2 == 2 ){
+            
+            //NVIC_SystemReset();
+            Exit = 1 ;
+        }
+
+    }
 }
 void leer_color(){
     
-    mybuzzer.write(0);
     long     red = scolor.ReadRed();
     long     green = scolor.ReadGreen();
     long     blue = scolor.ReadBlue();
@@ -251,6 +400,9 @@
     //printf ( txt5 , "%02X" , enviar5 );
     command.putc( 0xFE );
     command.putc( 0x00 );
+   // tft_reset();
+   tft_clear(TFT_WHITE);  
+
     
     break ;
     
@@ -260,7 +412,9 @@
     //char txt1 [6] ;
     command.putc( 0xFE );
     command.putc( 0x01 );
-    sel_color = 0;
+    sel_color = 0;  
+ //   tft_reset();
+    tft_clear(TFT_RED); 
     
     break ;
     
@@ -271,6 +425,8 @@
     command.putc ( 0xFE );
     command.putc ( 0x02 );
     sel_color = 0;
+   // tft_reset();
+   tft_clear(TFT_GREEN);
     
     break ;
     
@@ -282,6 +438,10 @@
     command.putc ( 0x03 );
     sel_color = 0;
     
+//tft_reset();
+    tft_clear(TFT_BLUE);               
+
+    
     break ;
     
     case 4 :
@@ -291,12 +451,14 @@
     command.putc ( 0xFE );
     command.putc ( 0x04 );
     sel_color = 0;
-    
+    //tft_reset();
+    tft_clear(TFT_YELLOW);
+      
     break ; 
 }
 
 }
-    void funcionesrobot ( uint8_t _Parametro, uint8_t _Comando ){
+void funcionesrobot ( uint8_t _Parametro, uint8_t _Comando ){
         
         /*  °°Declaración de contadores°°  */
         uint8_t i ;
@@ -305,11 +467,27 @@
         switch ( _Comando ){
             // Acciones que ejerce el robot
             
-            /*case 0 :
+            case 0 :
             
-                leer_color() ;
+                tft_reset();
+    
+                tft_clear(TFT_RED);    
+                
+                wait(1);
+                
+                tft_clear(TFT_GREEN);    
+                
+                wait(1);
                 
-            break ; */
+                tft_clear(TFT_BLUE);    
+                
+                wait(1);
+                
+                tft_clear(TFT_YELLOW);
+                
+                wait(1);
+                
+            break ; 
             
             case 1 :
             
@@ -360,7 +538,7 @@
             
                     for ( i= 0 ; i <=  200 ; i++ ){
                  
-                        stepper_step = 1 ; 
+                        stepper_step  = 1 ; 
                         stepper_step2 = 1;
                         wait_us( VELOCITY );
                         stepper_step = 0;
@@ -472,7 +650,7 @@
                 while ( !Exit ){ 
                 
                     In [ 0 ] = analog_value0.read(); // Converts and read the analog input value (value from 0.0 to 1.0)    
-                    //printf(" X = %.04f \n", In[0]);                
+                   // printf(" X = %.04f \n", In[0]);                
                     if ( In [ 0 ] > 0.6 ){ 
  
                         steppeer_dir = 1;
@@ -569,5 +747,6 @@
             break ;
             
             }
-        }
+}
 
+