Daniel Padierna
/
PWM_APP
.
main.cpp@0:b029806382bc, 2017-11-30 (annotated)
- Committer:
- dpadiernav
- Date:
- Thu Nov 30 14:43:42 2017 +0000
- Revision:
- 0:b029806382bc
Sliders para app;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dpadiernav | 0:b029806382bc | 1 | #include "mbed.h" |
dpadiernav | 0:b029806382bc | 2 | #include "stdio.h" |
dpadiernav | 0:b029806382bc | 3 | #include "string.h" |
dpadiernav | 0:b029806382bc | 4 | |
dpadiernav | 0:b029806382bc | 5 | PwmOut rojo(LED1); // R |
dpadiernav | 0:b029806382bc | 6 | PwmOut verde(LED2); // G |
dpadiernav | 0:b029806382bc | 7 | PwmOut azul(LED3); // B |
dpadiernav | 0:b029806382bc | 8 | |
dpadiernav | 0:b029806382bc | 9 | Serial usart(PTA2,PTA1); //puertos del FRDM para el modem |
dpadiernav | 0:b029806382bc | 10 | Serial pc(USBTX,USBRX); |
dpadiernav | 0:b029806382bc | 11 | |
dpadiernav | 0:b029806382bc | 12 | Timer tiempo; // Variable para contar |
dpadiernav | 0:b029806382bc | 13 | |
dpadiernav | 0:b029806382bc | 14 | int selector; |
dpadiernav | 0:b029806382bc | 15 | int valor_Rojo; |
dpadiernav | 0:b029806382bc | 16 | int valor_Verde; |
dpadiernav | 0:b029806382bc | 17 | int valor_Azul; |
dpadiernav | 0:b029806382bc | 18 | float pwm_Rojo = 1.0f; |
dpadiernav | 0:b029806382bc | 19 | float pwm_Verde = 1.0f; |
dpadiernav | 0:b029806382bc | 20 | float pwm_Azul = 1.0f; |
dpadiernav | 0:b029806382bc | 21 | |
dpadiernav | 0:b029806382bc | 22 | char buffer[8]; |
dpadiernav | 0:b029806382bc | 23 | |
dpadiernav | 0:b029806382bc | 24 | int readBuffer(char *buffer,int count); |
dpadiernav | 0:b029806382bc | 25 | void cleanBuffer(char *buffer, int count); |
dpadiernav | 0:b029806382bc | 26 | int reconocerColor(char *buffer); |
dpadiernav | 0:b029806382bc | 27 | int tomarValor (char *buffer); |
dpadiernav | 0:b029806382bc | 28 | |
dpadiernav | 0:b029806382bc | 29 | //esta funcion lee un bufer de datos |
dpadiernav | 0:b029806382bc | 30 | int readBuffer(char *buffer,int count){ |
dpadiernav | 0:b029806382bc | 31 | |
dpadiernav | 0:b029806382bc | 32 | int i=0; |
dpadiernav | 0:b029806382bc | 33 | tiempo.start(); //CUENTA EL TIEMPO DE CONEXION E INICIA |
dpadiernav | 0:b029806382bc | 34 | while(1) { |
dpadiernav | 0:b029806382bc | 35 | while (usart.readable()) { |
dpadiernav | 0:b029806382bc | 36 | char c = usart.getc(); |
dpadiernav | 0:b029806382bc | 37 | if (c == '\r' || c == '\n') c = '%';//si se envia fin de linea o de caracxter inserta $ |
dpadiernav | 0:b029806382bc | 38 | buffer[i++] = c;//mete al bufer el caracter leido |
dpadiernav | 0:b029806382bc | 39 | if(i > count)break;//sale del loop si ya detecto terminacion |
dpadiernav | 0:b029806382bc | 40 | } |
dpadiernav | 0:b029806382bc | 41 | if(i > count)break; |
dpadiernav | 0:b029806382bc | 42 | if(tiempo.read() > 0.5) { //MAS DE UN SEGUNDO DE ESPERA SE SALE Y REINICA EL RELOJ Y SE SALE |
dpadiernav | 0:b029806382bc | 43 | tiempo.stop(); |
dpadiernav | 0:b029806382bc | 44 | tiempo.reset(); |
dpadiernav | 0:b029806382bc | 45 | break; |
dpadiernav | 0:b029806382bc | 46 | } |
dpadiernav | 0:b029806382bc | 47 | } |
dpadiernav | 0:b029806382bc | 48 | return 0; |
dpadiernav | 0:b029806382bc | 49 | } |
dpadiernav | 0:b029806382bc | 50 | |
dpadiernav | 0:b029806382bc | 51 | //esta funcion limpia el bufer |
dpadiernav | 0:b029806382bc | 52 | void cleanBuffer(char *buffer, int count){ |
dpadiernav | 0:b029806382bc | 53 | |
dpadiernav | 0:b029806382bc | 54 | for(int i=0; i < count; i++) { |
dpadiernav | 0:b029806382bc | 55 | buffer[i] = '\0'; |
dpadiernav | 0:b029806382bc | 56 | } |
dpadiernav | 0:b029806382bc | 57 | |
dpadiernav | 0:b029806382bc | 58 | } |
dpadiernav | 0:b029806382bc | 59 | |
dpadiernav | 0:b029806382bc | 60 | int reconocerColor (char *buffer){ |
dpadiernav | 0:b029806382bc | 61 | |
dpadiernav | 0:b029806382bc | 62 | int opcion; |
dpadiernav | 0:b029806382bc | 63 | switch(buffer[0]){ |
dpadiernav | 0:b029806382bc | 64 | case 'R': opcion = 1; break; |
dpadiernav | 0:b029806382bc | 65 | case 'G': opcion = 2; break; |
dpadiernav | 0:b029806382bc | 66 | case 'B': opcion = 3; break; |
dpadiernav | 0:b029806382bc | 67 | default : opcion = 0; break; |
dpadiernav | 0:b029806382bc | 68 | } |
dpadiernav | 0:b029806382bc | 69 | return opcion; |
dpadiernav | 0:b029806382bc | 70 | |
dpadiernav | 0:b029806382bc | 71 | } |
dpadiernav | 0:b029806382bc | 72 | |
dpadiernav | 0:b029806382bc | 73 | int tomarValor (char *buffer){ |
dpadiernav | 0:b029806382bc | 74 | |
dpadiernav | 0:b029806382bc | 75 | int valor; |
dpadiernav | 0:b029806382bc | 76 | int paro = 0; |
dpadiernav | 0:b029806382bc | 77 | int unidades; |
dpadiernav | 0:b029806382bc | 78 | int decenas; |
dpadiernav | 0:b029806382bc | 79 | int centenas; |
dpadiernav | 0:b029806382bc | 80 | |
dpadiernav | 0:b029806382bc | 81 | if (buffer[1] == '\r' || buffer[1] == '\n' || buffer[1]-47 < 1 || buffer[1]-48 > 9){ |
dpadiernav | 0:b029806382bc | 82 | valor = 0; |
dpadiernav | 0:b029806382bc | 83 | paro = 1; |
dpadiernav | 0:b029806382bc | 84 | } else { |
dpadiernav | 0:b029806382bc | 85 | unidades = buffer[1]-48; |
dpadiernav | 0:b029806382bc | 86 | } |
dpadiernav | 0:b029806382bc | 87 | |
dpadiernav | 0:b029806382bc | 88 | if (buffer[2] == '\r' || buffer[2] == '\n' || buffer[2]-47 < 1 || buffer[2]-48 > 9){ |
dpadiernav | 0:b029806382bc | 89 | valor = unidades; |
dpadiernav | 0:b029806382bc | 90 | } else if (paro == 0) { |
dpadiernav | 0:b029806382bc | 91 | decenas = unidades; |
dpadiernav | 0:b029806382bc | 92 | unidades = buffer[2]-48; |
dpadiernav | 0:b029806382bc | 93 | } |
dpadiernav | 0:b029806382bc | 94 | |
dpadiernav | 0:b029806382bc | 95 | if (buffer[3] == '\r' || buffer[3] == '\n' || buffer[3]-47 < 1 || buffer[3]-48 > 9){ |
dpadiernav | 0:b029806382bc | 96 | valor = decenas*10 + unidades; |
dpadiernav | 0:b029806382bc | 97 | } else if (paro == 0){ |
dpadiernav | 0:b029806382bc | 98 | centenas = decenas; |
dpadiernav | 0:b029806382bc | 99 | decenas = unidades; |
dpadiernav | 0:b029806382bc | 100 | unidades = buffer[3]-48; |
dpadiernav | 0:b029806382bc | 101 | valor = centenas*100 + decenas*10 + unidades; |
dpadiernav | 0:b029806382bc | 102 | } |
dpadiernav | 0:b029806382bc | 103 | |
dpadiernav | 0:b029806382bc | 104 | return valor; |
dpadiernav | 0:b029806382bc | 105 | |
dpadiernav | 0:b029806382bc | 106 | } |
dpadiernav | 0:b029806382bc | 107 | |
dpadiernav | 0:b029806382bc | 108 | int main() { |
dpadiernav | 0:b029806382bc | 109 | |
dpadiernav | 0:b029806382bc | 110 | usart.baud(9600); |
dpadiernav | 0:b029806382bc | 111 | usart.format(8,Serial::None,1); |
dpadiernav | 0:b029806382bc | 112 | |
dpadiernav | 0:b029806382bc | 113 | rojo.period(0.02f); // 4 second period |
dpadiernav | 0:b029806382bc | 114 | verde.period(0.02f); // 4 second period |
dpadiernav | 0:b029806382bc | 115 | azul.period(0.02f); // 4 second period |
dpadiernav | 0:b029806382bc | 116 | |
dpadiernav | 0:b029806382bc | 117 | rojo.write(pwm_Rojo); // 0% duty cycle |
dpadiernav | 0:b029806382bc | 118 | verde.write(pwm_Verde); // 0% duty cycle |
dpadiernav | 0:b029806382bc | 119 | azul.write(pwm_Azul); // 0% duty cycle |
dpadiernav | 0:b029806382bc | 120 | |
dpadiernav | 0:b029806382bc | 121 | while(1){ |
dpadiernav | 0:b029806382bc | 122 | |
dpadiernav | 0:b029806382bc | 123 | while (usart.readable()){ |
dpadiernav | 0:b029806382bc | 124 | |
dpadiernav | 0:b029806382bc | 125 | readBuffer(buffer, 7); |
dpadiernav | 0:b029806382bc | 126 | pc.printf("buffer= %s \n\r ",buffer); |
dpadiernav | 0:b029806382bc | 127 | selector = reconocerColor(buffer); |
dpadiernav | 0:b029806382bc | 128 | pc.printf("opcion= %u \n\r ",selector); |
dpadiernav | 0:b029806382bc | 129 | switch(selector){ |
dpadiernav | 0:b029806382bc | 130 | case 1: |
dpadiernav | 0:b029806382bc | 131 | valor_Rojo = tomarValor(buffer); |
dpadiernav | 0:b029806382bc | 132 | pwm_Rojo = (float)valor_Rojo / 999.0f; |
dpadiernav | 0:b029806382bc | 133 | pwm_Rojo = 1.0f - pwm_Rojo; |
dpadiernav | 0:b029806382bc | 134 | pc.printf("pwm_Rojo= %u \n\r ",pwm_Rojo); |
dpadiernav | 0:b029806382bc | 135 | rojo.write(pwm_Rojo); |
dpadiernav | 0:b029806382bc | 136 | break; |
dpadiernav | 0:b029806382bc | 137 | case 2: |
dpadiernav | 0:b029806382bc | 138 | valor_Verde = tomarValor(buffer); |
dpadiernav | 0:b029806382bc | 139 | pwm_Verde = (float)valor_Verde / 999.0f; |
dpadiernav | 0:b029806382bc | 140 | pc.printf("pwm_Verde= %u \n\r ",pwm_Verde); |
dpadiernav | 0:b029806382bc | 141 | pwm_Verde = 1.0f - pwm_Verde; |
dpadiernav | 0:b029806382bc | 142 | verde.write(pwm_Verde); |
dpadiernav | 0:b029806382bc | 143 | break; |
dpadiernav | 0:b029806382bc | 144 | case 3: |
dpadiernav | 0:b029806382bc | 145 | valor_Azul = tomarValor(buffer); |
dpadiernav | 0:b029806382bc | 146 | pwm_Azul = (float)valor_Azul / 999.0f; |
dpadiernav | 0:b029806382bc | 147 | pc.printf("pwm_Azul= %u \n\r ",pwm_Azul); |
dpadiernav | 0:b029806382bc | 148 | pwm_Azul = 1.0f - pwm_Azul; |
dpadiernav | 0:b029806382bc | 149 | azul.write(pwm_Azul); |
dpadiernav | 0:b029806382bc | 150 | break; |
dpadiernav | 0:b029806382bc | 151 | default: |
dpadiernav | 0:b029806382bc | 152 | break; |
dpadiernav | 0:b029806382bc | 153 | } |
dpadiernav | 0:b029806382bc | 154 | |
dpadiernav | 0:b029806382bc | 155 | } |
dpadiernav | 0:b029806382bc | 156 | |
dpadiernav | 0:b029806382bc | 157 | } |
dpadiernav | 0:b029806382bc | 158 | |
dpadiernav | 0:b029806382bc | 159 | } |
dpadiernav | 0:b029806382bc | 160 | |
dpadiernav | 0:b029806382bc | 161 |