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 "stdio.h" 00003 #include "string.h" 00004 00005 PwmOut rojo(LED1); // R 00006 PwmOut verde(LED2); // G 00007 PwmOut azul(LED3); // B 00008 00009 Serial usart(PTA2,PTA1); //puertos del FRDM para el modem 00010 Serial pc(USBTX,USBRX); 00011 00012 Timer tiempo; // Variable para contar 00013 00014 int selector; 00015 int valor_Rojo; 00016 int valor_Verde; 00017 int valor_Azul; 00018 float pwm_Rojo = 1.0f; 00019 float pwm_Verde = 1.0f; 00020 float pwm_Azul = 1.0f; 00021 00022 char buffer[8]; 00023 00024 int readBuffer(char *buffer,int count); 00025 void cleanBuffer(char *buffer, int count); 00026 int reconocerColor(char *buffer); 00027 int tomarValor (char *buffer); 00028 00029 //esta funcion lee un bufer de datos 00030 int readBuffer(char *buffer,int count){ 00031 00032 int i=0; 00033 tiempo.start(); //CUENTA EL TIEMPO DE CONEXION E INICIA 00034 while(1) { 00035 while (usart.readable()) { 00036 char c = usart.getc(); 00037 if (c == '\r' || c == '\n') c = '%';//si se envia fin de linea o de caracxter inserta $ 00038 buffer[i++] = c;//mete al bufer el caracter leido 00039 if(i > count)break;//sale del loop si ya detecto terminacion 00040 } 00041 if(i > count)break; 00042 if(tiempo.read() > 0.5) { //MAS DE UN SEGUNDO DE ESPERA SE SALE Y REINICA EL RELOJ Y SE SALE 00043 tiempo.stop(); 00044 tiempo.reset(); 00045 break; 00046 } 00047 } 00048 return 0; 00049 } 00050 00051 //esta funcion limpia el bufer 00052 void cleanBuffer(char *buffer, int count){ 00053 00054 for(int i=0; i < count; i++) { 00055 buffer[i] = '\0'; 00056 } 00057 00058 } 00059 00060 int reconocerColor (char *buffer){ 00061 00062 int opcion; 00063 switch(buffer[0]){ 00064 case 'R': opcion = 1; break; 00065 case 'G': opcion = 2; break; 00066 case 'B': opcion = 3; break; 00067 default : opcion = 0; break; 00068 } 00069 return opcion; 00070 00071 } 00072 00073 int tomarValor (char *buffer){ 00074 00075 int valor; 00076 int paro = 0; 00077 int unidades; 00078 int decenas; 00079 int centenas; 00080 00081 if (buffer[1] == '\r' || buffer[1] == '\n' || buffer[1]-47 < 1 || buffer[1]-48 > 9){ 00082 valor = 0; 00083 paro = 1; 00084 } else { 00085 unidades = buffer[1]-48; 00086 } 00087 00088 if (buffer[2] == '\r' || buffer[2] == '\n' || buffer[2]-47 < 1 || buffer[2]-48 > 9){ 00089 valor = unidades; 00090 } else if (paro == 0) { 00091 decenas = unidades; 00092 unidades = buffer[2]-48; 00093 } 00094 00095 if (buffer[3] == '\r' || buffer[3] == '\n' || buffer[3]-47 < 1 || buffer[3]-48 > 9){ 00096 valor = decenas*10 + unidades; 00097 } else if (paro == 0){ 00098 centenas = decenas; 00099 decenas = unidades; 00100 unidades = buffer[3]-48; 00101 valor = centenas*100 + decenas*10 + unidades; 00102 } 00103 00104 return valor; 00105 00106 } 00107 00108 int main() { 00109 00110 usart.baud(9600); 00111 usart.format(8,Serial::None,1); 00112 00113 rojo.period(0.02f); // 4 second period 00114 verde.period(0.02f); // 4 second period 00115 azul.period(0.02f); // 4 second period 00116 00117 rojo.write(pwm_Rojo); // 0% duty cycle 00118 verde.write(pwm_Verde); // 0% duty cycle 00119 azul.write(pwm_Azul); // 0% duty cycle 00120 00121 while(1){ 00122 00123 while (usart.readable()){ 00124 00125 readBuffer(buffer, 7); 00126 pc.printf("buffer= %s \n\r ",buffer); 00127 selector = reconocerColor(buffer); 00128 pc.printf("opcion= %u \n\r ",selector); 00129 switch(selector){ 00130 case 1: 00131 valor_Rojo = tomarValor(buffer); 00132 pwm_Rojo = (float)valor_Rojo / 999.0f; 00133 pwm_Rojo = 1.0f - pwm_Rojo; 00134 pc.printf("pwm_Rojo= %u \n\r ",pwm_Rojo); 00135 rojo.write(pwm_Rojo); 00136 break; 00137 case 2: 00138 valor_Verde = tomarValor(buffer); 00139 pwm_Verde = (float)valor_Verde / 999.0f; 00140 pc.printf("pwm_Verde= %u \n\r ",pwm_Verde); 00141 pwm_Verde = 1.0f - pwm_Verde; 00142 verde.write(pwm_Verde); 00143 break; 00144 case 3: 00145 valor_Azul = tomarValor(buffer); 00146 pwm_Azul = (float)valor_Azul / 999.0f; 00147 pc.printf("pwm_Azul= %u \n\r ",pwm_Azul); 00148 pwm_Azul = 1.0f - pwm_Azul; 00149 azul.write(pwm_Azul); 00150 break; 00151 default: 00152 break; 00153 } 00154 00155 } 00156 00157 } 00158 00159 } 00160 00161
Generated on Sun Aug 21 2022 16:53:42 by
1.7.2