Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 #include "mbed.h" 00002 #include "scolor_TCS3200.h" 00003 00004 /* ***************************************************************************** 00005 Programa que solicita telemetria al sistema embebido, por medio del comando 0xFe 00006 para preguntar por el color que detecta el sensor TCS3200 00007 00008 el sistema embebido recibe el inicio de trama FE y el número de comado: 01 00009 00010 | INITELE | CMD | 00011 | 0xfe | 0x01 | 00012 00013 00014 para enviar los comandos usar el programa Coolterm http://freeware.the-meiers.org/ 00015 00016 00017 @fabeltranm 2019 00018 fbeltranm@ecci.edu.co 00019 00020 ******************************************************************************** 00021 datasheet https://www.mouser.com/catalog/specsheets/TCS3200-E11.pdf 00022 00023 00024 S0 Frequency scaling 00025 S1 Frequency scaling 00026 S2 Photo diode selection 00027 S3 Photo diode selection 00028 OutFreq Frequency 00029 00030 ----------------------------------- 00031 | ____________ ____________ | 00032 ----> | | | | | | ___ ___ 00033 Light | | Photodiode | | Current |--|---OUTPUT_FREQ | |___| |___ 00034 ----> | | Array |---| to | | 00035 | | | | Frequency | | 00036 | |____________| |____________| | 00037 | ^ ^ ^ ^ | 00038 -------|--|-------------|--|------- 00039 | | | | 00040 S2 S3 S0 S1 00041 00042 SO | S1 | OUTPUT FREQUENCY SCALING | | S2 | S3 | PHOTODIODE TYPE | 00043 0 | 0 | power down | | 0 | 0 | Red | 00044 0 | 1 | 2% | | 0 | 1 | Blue | 00045 1 | 0 | 20% | | 1 | 0 | Clear (no filter) | 00046 1 | 1 | 100% | | 1 | 1 | Green | 00047 00048 ******************************************************************************/ 00049 00050 #define INITCMD 0xFF 00051 #define DO 2000 00052 #define RE 2500 00053 #define MI 3000 00054 #define FA 3500 00055 00056 /* ||Definicion de Puertos || */ 00057 00058 // S0, S1, S2, S3, OUT 00059 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 00060 Serial command(USBTX, USBRX); 00061 00062 DigitalOut stepper_step (PB_4); 00063 DigitalOut steppeer_dir (PB_5); 00064 DigitalOut stepper_step2 (PB_10); 00065 DigitalOut steppeer_dir2 (PA_8); 00066 00067 AnalogIn analog_value0 (A0); 00068 AnalogIn analog_value1 (A1); 00069 PwmOut mybuzzer(PB_9) ; 00070 00071 Ticker tk1; 00072 Ticker tk2; 00073 00074 volatile int Exit = 0 ; 00075 00076 /*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||/ / 00077 / | |/ 00078 / | Interrupcion del Joystick- Eliminando ruido |/ 00079 / | |/ 00080 / | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/ 00081 00082 //Definicion de variables 00083 DigitalOut led1(LED1); 00084 InterruptIn Button_joy(USER_BUTTON); 00085 Timeout Button_joy_timeout; // Used for debouncing 00086 00087 00088 //Definicion de las funciones 00089 volatile bool Button_joy_pressed = false; // Used in the main loop 00090 volatile bool Button_joy_enabled = true; // Used for debouncing 00091 00092 // Habilita el botón cuando termina el rebote. 00093 void Button_joy_enabled_cb(void) 00094 { 00095 Button_joy_enabled = true; 00096 } 00097 00098 // ISR Luego de precionar el boton 00099 void Button_joy_onpressed_cb(void) 00100 { 00101 if (Button_joy_enabled) { // Desabilitado mientras e ruido 00102 Button_joy_enabled = false; 00103 Button_joy_pressed = true; // Se cambia el valor para ser leido más adelante 00104 Button_joy_timeout.attach(callback(Button_joy_enabled_cb), 0.3); // Delay de 300 ms 00105 } 00106 if (Button_joy_pressed) { // Etablece cuando se preciona el botn 00107 Button_joy_pressed = false; 00108 //NVIC_SystemReset(); 00109 led1 = !led1; 00110 } 00111 } 00112 /*|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||/ / 00113 / |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||/ 00114 / | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/ 00115 00116 void Rx_interrupt(); 00117 00118 /*Definicion de variables codigo principal*/ 00119 00120 uint32_t VELOCITY = 1000; // Tiempo en micro segundos 00121 00122 double In [ 1 ] = {} ; 00123 00124 // Definición de las funciones bucle principal 00125 void leer_datos (); 00126 void leer_color (); 00127 00128 void funcionesrobot ( uint8_t _Parametro, uint8_t _Comando ); 00129 int16_t Lectura [ 2 ] = {} ; 00130 void setup_uart (); 00131 00132 00133 SPI spi(PA_7, PA_6, PA_5); // MOSI, MISO, SCK 00134 00135 DigitalOut cs(PB_0); // TFT chipselect pin 00136 DigitalOut dc(PC_0); // TFT data command select pin 00137 DigitalOut rst(PC_1); // TFT reset pin 00138 00139 #define TFT_WIDTH 320 00140 #define TFT_HEIGHT 240 00141 00142 #define TFT_BLUE 0x1F00 00143 #define TFT_RED 0x00F8 00144 #define TFT_GREEN 0xE007 00145 #define TFT_YELLOW 0xE0FF 00146 #define TFT_WHITE 0xFFFF 00147 00148 /*-------------------------------------------------------------------*/ 00149 /* Write command */ 00150 /*-------------------------------------------------------------------*/ 00151 void write_cmd(uint8_t cmd) 00152 { 00153 dc = 0; 00154 spi.write(cmd); 00155 } 00156 00157 /*-------------------------------------------------------------------*/ 00158 /* Write data */ 00159 /*-------------------------------------------------------------------*/ 00160 void write_data(uint8_t data) 00161 { 00162 dc = 1; 00163 spi.write(data); 00164 } 00165 00166 /*-------------------------------------------------------------------*/ 00167 /* TFT reset */ 00168 /*-------------------------------------------------------------------*/ 00169 void tft_reset() 00170 { 00171 wait_ms(200); 00172 cs = 1; 00173 dc = 1; 00174 rst = 1; 00175 wait_ms(200); 00176 rst = 0; 00177 wait_us(10); 00178 rst = 1; 00179 wait_ms(120); 00180 cs = 0; 00181 wait_ms(10); 00182 00183 write_cmd(0x3A); // Pixel Format 00184 write_data(0x55); // 16bit Color 00185 00186 write_cmd(0xB1); // Frame Control 00187 write_data(0); 00188 write_data(0x1f); 00189 00190 write_cmd(0x36); // Memory Access Control 00191 write_data(0xE8); // MY MX MV BGR 00192 00193 write_cmd(0x11); // Sleep Out 00194 wait_ms(5); 00195 00196 write_cmd(0x29); // Display On 00197 } 00198 00199 /*-------------------------------------------------------------------*/ 00200 /* Set the windows area and Start memory write. */ 00201 /*-------------------------------------------------------------------*/ 00202 void tft_set_window(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1) 00203 { 00204 write_cmd(0x2A); // Column Address Set 00205 write_data(x0 >> 8); 00206 write_data(x0); 00207 write_data(x1 >> 8); 00208 write_data(x1); 00209 00210 write_cmd(0x2B); // Page Address Set 00211 write_data(y0 >> 8); 00212 write_data(y0); 00213 write_data(y1 >> 8); 00214 write_data(y1); 00215 00216 write_cmd(0x2C); // Memory Write 00217 00218 wait_us(20); 00219 00220 dc = 1; 00221 } 00222 00223 /*-------------------------------------------------------------------*/ 00224 /* Clear screen */ 00225 /*-------------------------------------------------------------------*/ 00226 void tft_clear(uint16_t color) 00227 { 00228 tft_set_window(0, 0, TFT_WIDTH, TFT_HEIGHT); 00229 00230 for (int i = 0; i < TFT_WIDTH * TFT_HEIGHT; ++i) 00231 { 00232 spi.write(color & 0xff); 00233 spi.write(color >> 8); 00234 } 00235 } 00236 00237 00238 00239 00240 00241 00242 00243 00244 00245 00246 00247 00248 00249 00250 00251 00252 00253 00254 00255 00256 00257 00258 int main() { 00259 00260 setup_uart(); 00261 tk1.attach (&leer_color, 10); // Se dirigue a la funcion leer_color cada 0.8 segundos 00262 Button_joy.fall(callback(Button_joy_onpressed_cb)); // Llama a la funcion Button_joy_onpressed_cb al precionar el botón 00263 spi.frequency(45000000); 00264 00265 while(1){ 00266 00267 tft_reset(); 00268 //Interrupcion boton 00269 leer_datos(); 00270 funcionesrobot (Lectura [ 1 ], Lectura [ 0 ]); 00271 //Lcd(); 00272 } 00273 } 00274 00275 //Declaracion de las funciones 00276 00277 00278 void setup_uart(){ 00279 command.baud(115200); 00280 } 00281 00282 00283 void leer_datos(){ 00284 00285 while ( command.getc()!= INITCMD ) ; 00286 uint8_t i ; 00287 00288 for ( i = 0 ; i < 2 ; i++){ 00289 00290 Lectura [ i ] = command.getc (); 00291 printf ( " %4d ", Lectura [ i ]); 00292 00293 } 00294 } 00295 00296 void Rx_interrupt() { 00297 00298 if ( command.readable () == 1 ){ 00299 00300 uint8_t l1, l2 ; 00301 00302 while ( command.getc()!= INITCMD ) ; 00303 l1 = command.getc () ; 00304 l2 = command.getc () ; 00305 00306 if ( l1 == 10 and l2 == 2 ){ 00307 00308 //NVIC_SystemReset(); 00309 Exit = 1 ; 00310 } 00311 00312 } 00313 } 00314 void leer_color(){ 00315 00316 long red = scolor.ReadRed(); 00317 long green = scolor.ReadGreen(); 00318 long blue = scolor.ReadBlue(); 00319 long clear = scolor.ReadClear(); 00320 00321 long frqred; 00322 long frqgreen; 00323 long frqblue; 00324 long frqclear; 00325 int8_t sel_color = 0; 00326 printf("RED: %5d GREEN: %5d BLUE: %5d CLEAR: %5d \n ", red, green, blue, clear); 00327 00328 frqred = ( ( 1.0/red )* 1000.0 ); 00329 frqgreen = ( ( 1.0/green ) * 1000.0); 00330 frqblue = ( (1.0/blue) *1000.0 ); 00331 frqclear = ( (1.0/clear) *1000.0 ); 00332 printf("RED: %5d GREEN: %5d BLUE: %5d CLEAR: %5d \n ", frqred, frqgreen, frqblue,frqclear); 00333 /////////////////////////////////////////////////////////////////////////////// 00334 /*||||||||||||||||Seleccionando los diferentes colores.||||||||||||||||||||||*/ 00335 /*||||||||||||||||||||||||||||||Color rojo|||||||||||||||||||||||||||||||||||*/ 00336 /////////////////////////////////////////////////////////////////////////////// 00337 if ( frqred >= 30.0 and frqred <= 500.0) { 00338 00339 if( frqgreen >= 0.0 and frqgreen <= 20.0 ) { 00340 00341 if ( frqblue >= 5.0 and frqblue <= 29.0 ) { 00342 00343 sel_color = 1; 00344 00345 } 00346 } 00347 } 00348 //////////////////////////////////////////////////////////////////////////////// 00349 /*°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°Color verde°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°*/ 00350 //////////////////////////////////////////////////////////////////////////////// 00351 if ( frqgreen >= 11.0 and frqgreen <= 21.0) { 00352 00353 if( frqred >= 8.0 and frqred <= 20.0 ) { 00354 00355 if ( frqblue >= 10.0 and frqblue <= 26.0 ) { 00356 00357 sel_color = 2; 00358 00359 } 00360 } 00361 } 00362 //////////////////////////////////////////////////////////////////////////////// 00363 /*°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°Color azul°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°*/ 00364 /////////////////////////////////////////////////////////////////////////////// 00365 if ( frqblue >= 10.0 and frqblue <= 70.0) { 00366 00367 if( frqgreen >= 5.0 and frqgreen <= 26.0 ) { 00368 00369 if ( frqred >= 5.0 and frqred <= 20.0 ) { 00370 00371 sel_color = 3; 00372 00373 } 00374 } 00375 } 00376 ///////////////////////////////////////////////////////////////////////////////// 00377 /*°°°°°°°°°°°°°°°°°°°°°°°°°°°°°Color amarillo°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°*/ 00378 //////////////////////////////////////////////////////////////////////////////// 00379 if ( frqblue >= 0.0 and frqblue <= 30.0 ) { 00380 00381 if( frqgreen >= 20.0 and frqgreen <= 40.0) { 00382 00383 if ( frqred >= 20.0 and frqred <= 50.0 ) { 00384 00385 sel_color = 4; 00386 00387 } 00388 } 00389 } 00390 ///////////////////////////////////////////////////////////////////////////////// 00391 /*°°°°°°°°°°°°°°°°°°°°°°°°°°°°°Color no found°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°°*/ 00392 //////////////////////////////////////////////////////////////////////////////// 00393 00394 switch ( sel_color ) { 00395 00396 case 0 : 00397 00398 //int32_t enviar5 = RESPUESTA5 ; 00399 //char txt5 [6] ; 00400 //printf ( txt5 , "%02X" , enviar5 ); 00401 command.putc( 0xFE ); 00402 command.putc( 0x00 ); 00403 // tft_reset(); 00404 tft_clear(TFT_WHITE); 00405 00406 00407 break ; 00408 00409 case 1 : 00410 00411 //int32_t enviar1 = RESPUESTA1 ; 00412 //char txt1 [6] ; 00413 command.putc( 0xFE ); 00414 command.putc( 0x01 ); 00415 sel_color = 0; 00416 // tft_reset(); 00417 tft_clear(TFT_RED); 00418 00419 break ; 00420 00421 case 2 : 00422 00423 //int32_t enviar2 = RESPUESTA2 ; 00424 //char txt2 [6] ; 00425 command.putc ( 0xFE ); 00426 command.putc ( 0x02 ); 00427 sel_color = 0; 00428 // tft_reset(); 00429 tft_clear(TFT_GREEN); 00430 00431 break ; 00432 00433 case 3 : 00434 00435 //int32_t enviar3 = RESPUESTA3; 00436 //char txt3 [6] ; 00437 command.putc ( 0xFE ); 00438 command.putc ( 0x03 ); 00439 sel_color = 0; 00440 00441 //tft_reset(); 00442 tft_clear(TFT_BLUE); 00443 00444 00445 break ; 00446 00447 case 4 : 00448 00449 //int32_t enviar4 = RESPUESTA4; 00450 //char txt4 [6] ; 00451 command.putc ( 0xFE ); 00452 command.putc ( 0x04 ); 00453 sel_color = 0; 00454 //tft_reset(); 00455 tft_clear(TFT_YELLOW); 00456 00457 break ; 00458 } 00459 00460 } 00461 void funcionesrobot ( uint8_t _Parametro, uint8_t _Comando ){ 00462 00463 /* °°Declaración de contadores°° */ 00464 uint8_t i ; 00465 uint8_t j ; 00466 00467 switch ( _Comando ){ 00468 // Acciones que ejerce el robot 00469 00470 case 0 : 00471 00472 tft_reset(); 00473 00474 tft_clear(TFT_RED); 00475 00476 wait(1); 00477 00478 tft_clear(TFT_GREEN); 00479 00480 wait(1); 00481 00482 tft_clear(TFT_BLUE); 00483 00484 wait(1); 00485 00486 tft_clear(TFT_YELLOW); 00487 00488 wait(1); 00489 00490 break ; 00491 00492 case 1 : 00493 00494 printf ( "Frecuencia: 2000 Tiempo: %d s \n" , _Parametro ) ; 00495 mybuzzer.period_us ( DO ) ; 00496 mybuzzer.write ( 0.8 ) ; 00497 wait( _Parametro ) ; 00498 mybuzzer.write ( 0 ) ; 00499 00500 break ; 00501 00502 case 2 : 00503 00504 printf ( "Frecuencia: 2500 Tiempo: %d s \n" , _Parametro ) ; 00505 mybuzzer.period_us ( RE ) ; 00506 mybuzzer.write ( 0.8 ) ; 00507 wait ( _Parametro ) ; 00508 mybuzzer.write ( 0 ) ; 00509 00510 break ; 00511 00512 case 3 : 00513 00514 printf ( " Frecuencia: 3000 " ); printf ( " Tiempo: %d s \n " , _Parametro ) ; 00515 mybuzzer.period_us ( MI ) ; 00516 mybuzzer.write ( 0.8 ) ; 00517 wait( _Parametro ) ; 00518 mybuzzer.write ( 0 ) ; 00519 00520 break ; 00521 00522 case 4 : 00523 00524 printf ( "Frecuencia: 3500 Tiempo: %d s \n" , _Parametro ) ; 00525 mybuzzer.period_us ( FA ); 00526 mybuzzer.write ( 0.8 ); 00527 wait ( 5 ); 00528 mybuzzer.write ( 0 ); 00529 00530 break ; 00531 00532 case 5 : 00533 00534 steppeer_dir = 1 ; 00535 steppeer_dir2 = 0 ; 00536 wait_us ( 1 ); 00537 for ( j = 1 ; j <= _Parametro ; j++){ 00538 00539 for ( i= 0 ; i <= 200 ; i++ ){ 00540 00541 stepper_step = 1 ; 00542 stepper_step2 = 1; 00543 wait_us( VELOCITY ); 00544 stepper_step = 0; 00545 stepper_step2 = 0; 00546 wait_us ( VELOCITY ); 00547 00548 } 00549 } 00550 00551 break ; 00552 00553 case 6 : 00554 00555 steppeer_dir = 0; 00556 steppeer_dir2 = 1; 00557 wait_us ( 1 ); 00558 00559 for ( j = 1 ; j <= _Parametro ; j++){ 00560 00561 for ( i= 0 ; i <= 200 ; i++ ){ 00562 00563 stepper_step = 1 ; 00564 stepper_step2 = 1 ; 00565 wait_us( VELOCITY ) ; 00566 stepper_step = 0 ; 00567 stepper_step2 = 0 ; 00568 wait_us ( VELOCITY ) ; 00569 00570 } 00571 00572 } 00573 00574 break ; 00575 00576 case 7 : 00577 00578 steppeer_dir = 0; 00579 wait_us ( 1 ); 00580 00581 for ( j = 1 ; j <= _Parametro ; j++){ 00582 00583 for ( i= 0 ; i <= 250 ; i++ ){ 00584 00585 stepper_step = 1 ; 00586 //stepper_step2 = 1 ; 00587 wait_us(VELOCITY) ; 00588 stepper_step = 0 ; 00589 //stepper_step2 = 0 ; 00590 wait_us(VELOCITY) ; 00591 00592 } 00593 } 00594 00595 break ; 00596 00597 case 8 : 00598 00599 steppeer_dir2 = 1; 00600 wait_us ( 1 ); 00601 00602 for ( j = 1 ; j <= _Parametro ; j++){ 00603 00604 for ( i= 0 ; i <= 250 ; i++ ){ 00605 00606 //stepper_step = 1 ; 00607 stepper_step2 = 1 ; 00608 wait_us( VELOCITY ) ; 00609 //stepper_step = 0 ; 00610 stepper_step2 = 0 ; 00611 wait_us ( VELOCITY ) ; 00612 00613 } 00614 00615 } 00616 00617 break ; 00618 00619 case 9 : 00620 00621 switch ( _Parametro ){ 00622 00623 case 1: 00624 00625 VELOCITY = 400 ; 00626 00627 break ; 00628 00629 case 2: 00630 00631 VELOCITY = 2500 ; 00632 00633 break ; 00634 00635 case 3: 00636 00637 VELOCITY = 5000 ; 00638 00639 break ; 00640 00641 } 00642 00643 break ; 00644 00645 case 10: 00646 00647 00648 if ( _Parametro == 1){ 00649 00650 while ( !Exit ){ 00651 00652 In [ 0 ] = analog_value0.read(); // Converts and read the analog input value (value from 0.0 to 1.0) 00653 // printf(" X = %.04f \n", In[0]); 00654 if ( In [ 0 ] > 0.6 ){ 00655 00656 steppeer_dir = 1; 00657 wait_us( 1 ); 00658 00659 for ( i= 0 ; i <= 50 ; i++ ){ 00660 00661 stepper_step = 1 ; 00662 //stepper_step2 = 1 ; 00663 wait_us( VELOCITY ) ; 00664 stepper_step = 0 ; 00665 //stepper_step2 = 0 ; 00666 wait_us( VELOCITY ) ; 00667 } 00668 } 00669 00670 if ( In [ 0 ] < 0.3 ){ 00671 00672 steppeer_dir2 = 1; 00673 wait_us( 1 ); 00674 00675 for ( i= 0 ; i <= 50 ; i++ ){ 00676 00677 //stepper_step = 1 ; 00678 stepper_step2 = 1 ; 00679 wait_us( VELOCITY ) ; 00680 //stepper_step = 0 ; 00681 stepper_step2 = 0 ; 00682 wait_us( VELOCITY ) ; 00683 00684 } 00685 } 00686 00687 In [ 1 ] = analog_value1.read(); // Converts and read the analog input value (value from 0.0 to 1.0) 00688 //printf(" Y = %.04f \n", In[0]); 00689 00690 if (In [ 1 ] > 0.6 ){ 00691 00692 steppeer_dir = 1 ; 00693 steppeer_dir2 = 0 ; 00694 wait_us( 1 ); 00695 00696 for ( i= 0 ; i <= 50 ; i++ ){ 00697 00698 stepper_step = 1 ; 00699 stepper_step2 = 1 ; 00700 wait_us( VELOCITY ) ; 00701 stepper_step = 0 ; 00702 stepper_step2 = 0 ; 00703 wait_us( VELOCITY ) ; 00704 00705 } 00706 } 00707 00708 if ( In [ 1 ] < 0.3 ){ 00709 00710 steppeer_dir = 0 ; 00711 steppeer_dir2 = 1 ; 00712 wait_us( 1 ); 00713 00714 for ( i= 0 ; i <= 50 ; i++ ){ 00715 00716 stepper_step = 1 ; 00717 stepper_step2 = 1 ; 00718 wait_us( VELOCITY ) ; 00719 stepper_step = 0 ; 00720 stepper_step2 = 0 ; 00721 wait_us ( VELOCITY ) ; 00722 00723 } 00724 00725 } 00726 00727 command.attach( callback(Rx_interrupt), Serial::RxIrq ); 00728 /*if ( command.readable () == 1 ){ 00729 00730 uint8_t l1, l2 ; 00731 00732 while ( command.getc()!= INITCMD ) ; 00733 l1 = command.getc () ; 00734 l2 = command.getc () ; 00735 00736 if ( l1 == 10 and l2 == 2 ){ 00737 00738 Exit = 1 ; 00739 } 00740 }*/ 00741 00742 wait_us ( 1 ); 00743 00744 } 00745 } 00746 00747 break ; 00748 00749 } 00750 } 00751 00752
Generated on Sat Jul 16 2022 09:58:37 by
