Cristian Castro
/
BuzzerFrequency
This Program generate a audibly frequency on a Buzzer
main.cpp@0:309da55d6e5a, 2019-02-28 (annotated)
- Committer:
- CCastrop1012
- Date:
- Thu Feb 28 17:14:57 2019 +0000
- Revision:
- 0:309da55d6e5a
C
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
CCastrop1012 | 0:309da55d6e5a | 1 | #include "mbed.h" |
CCastrop1012 | 0:309da55d6e5a | 2 | |
CCastrop1012 | 0:309da55d6e5a | 3 | |
CCastrop1012 | 0:309da55d6e5a | 4 | |
CCastrop1012 | 0:309da55d6e5a | 5 | |
CCastrop1012 | 0:309da55d6e5a | 6 | //****************************************************************************** |
CCastrop1012 | 0:309da55d6e5a | 7 | // Definiciones |
CCastrop1012 | 0:309da55d6e5a | 8 | |
CCastrop1012 | 0:309da55d6e5a | 9 | /// Puerto Serial |
CCastrop1012 | 0:309da55d6e5a | 10 | Serial SerialPort(SERIAL_TX, SERIAL_RX, 9600); |
CCastrop1012 | 0:309da55d6e5a | 11 | |
CCastrop1012 | 0:309da55d6e5a | 12 | /// PWM OUTPUTS |
CCastrop1012 | 0:309da55d6e5a | 13 | PwmOut Buzzer(LED1); |
CCastrop1012 | 0:309da55d6e5a | 14 | |
CCastrop1012 | 0:309da55d6e5a | 15 | // Temporizadores |
CCastrop1012 | 0:309da55d6e5a | 16 | Ticker LengthTonoBuzzer; |
CCastrop1012 | 0:309da55d6e5a | 17 | |
CCastrop1012 | 0:309da55d6e5a | 18 | // constantes |
CCastrop1012 | 0:309da55d6e5a | 19 | const int long_vector = 10; // longitud del vector |
CCastrop1012 | 0:309da55d6e5a | 20 | |
CCastrop1012 | 0:309da55d6e5a | 21 | /// Global Variables |
CCastrop1012 | 0:309da55d6e5a | 22 | |
CCastrop1012 | 0:309da55d6e5a | 23 | uint8_t VCOMANDS [long_vector]; // vector en donde se almacena la informacion |
CCastrop1012 | 0:309da55d6e5a | 24 | // Recividos por el PC (Los Comandos) |
CCastrop1012 | 0:309da55d6e5a | 25 | uint8_t VINDICATORS [long_vector]; // vector en donde se almacena la informacion |
CCastrop1012 | 0:309da55d6e5a | 26 | // que se Enviara al PC (Los indicadores) |
CCastrop1012 | 0:309da55d6e5a | 27 | |
CCastrop1012 | 0:309da55d6e5a | 28 | uint8_t i = 0; // Variable usada como Indice de los vectores, |
CCastrop1012 | 0:309da55d6e5a | 29 | // indica la posicion del vector en donde se reciben |
CCastrop1012 | 0:309da55d6e5a | 30 | // ó se almacenan los datos |
CCastrop1012 | 0:309da55d6e5a | 31 | |
CCastrop1012 | 0:309da55d6e5a | 32 | float periodo_Buzzer = 1; |
CCastrop1012 | 0:309da55d6e5a | 33 | uint8_t duracion_Tono = 1; |
CCastrop1012 | 0:309da55d6e5a | 34 | |
CCastrop1012 | 0:309da55d6e5a | 35 | //****************************************************************************** |
CCastrop1012 | 0:309da55d6e5a | 36 | |
CCastrop1012 | 0:309da55d6e5a | 37 | // Prototipo de Funciones |
CCastrop1012 | 0:309da55d6e5a | 38 | |
CCastrop1012 | 0:309da55d6e5a | 39 | void ReadPort(void); // Lee el puerto Serial |
CCastrop1012 | 0:309da55d6e5a | 40 | void MainConfig(void); // Configuracion Inicial de los Perifericos del uC |
CCastrop1012 | 0:309da55d6e5a | 41 | |
CCastrop1012 | 0:309da55d6e5a | 42 | |
CCastrop1012 | 0:309da55d6e5a | 43 | void stopBuzzer(void); // Detiene el Sonido del Buzzer |
CCastrop1012 | 0:309da55d6e5a | 44 | void BuzzerSound(void); // Genera la Frecuencia de Audio en el Buzzer |
CCastrop1012 | 0:309da55d6e5a | 45 | |
CCastrop1012 | 0:309da55d6e5a | 46 | //****************************************************************************** |
CCastrop1012 | 0:309da55d6e5a | 47 | |
CCastrop1012 | 0:309da55d6e5a | 48 | /// Configuración Inicial |
CCastrop1012 | 0:309da55d6e5a | 49 | |
CCastrop1012 | 0:309da55d6e5a | 50 | void MainConfig(void) |
CCastrop1012 | 0:309da55d6e5a | 51 | { |
CCastrop1012 | 0:309da55d6e5a | 52 | |
CCastrop1012 | 0:309da55d6e5a | 53 | // se habilita la interrupción RX |
CCastrop1012 | 0:309da55d6e5a | 54 | SerialPort.attach(&ReadPort, Serial::RxIrq ); |
CCastrop1012 | 0:309da55d6e5a | 55 | |
CCastrop1012 | 0:309da55d6e5a | 56 | |
CCastrop1012 | 0:309da55d6e5a | 57 | // configuracion inicial del PWM Buzzer |
CCastrop1012 | 0:309da55d6e5a | 58 | Buzzer.period(periodo_Buzzer); |
CCastrop1012 | 0:309da55d6e5a | 59 | Buzzer.write(0.50); |
CCastrop1012 | 0:309da55d6e5a | 60 | |
CCastrop1012 | 0:309da55d6e5a | 61 | |
CCastrop1012 | 0:309da55d6e5a | 62 | |
CCastrop1012 | 0:309da55d6e5a | 63 | } |
CCastrop1012 | 0:309da55d6e5a | 64 | |
CCastrop1012 | 0:309da55d6e5a | 65 | |
CCastrop1012 | 0:309da55d6e5a | 66 | |
CCastrop1012 | 0:309da55d6e5a | 67 | /// Función Principal |
CCastrop1012 | 0:309da55d6e5a | 68 | |
CCastrop1012 | 0:309da55d6e5a | 69 | int main() |
CCastrop1012 | 0:309da55d6e5a | 70 | { |
CCastrop1012 | 0:309da55d6e5a | 71 | |
CCastrop1012 | 0:309da55d6e5a | 72 | MainConfig(); |
CCastrop1012 | 0:309da55d6e5a | 73 | // Mensaje Inicial |
CCastrop1012 | 0:309da55d6e5a | 74 | SerialPort.printf("Hello World, System Run !!\n"); |
CCastrop1012 | 0:309da55d6e5a | 75 | |
CCastrop1012 | 0:309da55d6e5a | 76 | |
CCastrop1012 | 0:309da55d6e5a | 77 | |
CCastrop1012 | 0:309da55d6e5a | 78 | |
CCastrop1012 | 0:309da55d6e5a | 79 | while(1) |
CCastrop1012 | 0:309da55d6e5a | 80 | { |
CCastrop1012 | 0:309da55d6e5a | 81 | |
CCastrop1012 | 0:309da55d6e5a | 82 | if(VCOMANDS[2] != 0) // Si hemos enviado el comando de Ejecucion de Tono |
CCastrop1012 | 0:309da55d6e5a | 83 | { |
CCastrop1012 | 0:309da55d6e5a | 84 | BuzzerSound(); // Genera el Tono Seleccionado |
CCastrop1012 | 0:309da55d6e5a | 85 | } |
CCastrop1012 | 0:309da55d6e5a | 86 | |
CCastrop1012 | 0:309da55d6e5a | 87 | /// Visualizamos la informacion que hemos Modificado |
CCastrop1012 | 0:309da55d6e5a | 88 | SerialPort.printf("VCOMANDS[%d] = %d - VCOMANDS[9] = %d\n", i, VCOMANDS[i], VCOMANDS[9]); |
CCastrop1012 | 0:309da55d6e5a | 89 | wait(1); |
CCastrop1012 | 0:309da55d6e5a | 90 | |
CCastrop1012 | 0:309da55d6e5a | 91 | |
CCastrop1012 | 0:309da55d6e5a | 92 | } |
CCastrop1012 | 0:309da55d6e5a | 93 | |
CCastrop1012 | 0:309da55d6e5a | 94 | |
CCastrop1012 | 0:309da55d6e5a | 95 | } |
CCastrop1012 | 0:309da55d6e5a | 96 | |
CCastrop1012 | 0:309da55d6e5a | 97 | //****************************************************************************** |
CCastrop1012 | 0:309da55d6e5a | 98 | |
CCastrop1012 | 0:309da55d6e5a | 99 | // Funciones Secundarias |
CCastrop1012 | 0:309da55d6e5a | 100 | |
CCastrop1012 | 0:309da55d6e5a | 101 | |
CCastrop1012 | 0:309da55d6e5a | 102 | void stopBuzzer(void) // Detiene el Tono del Buzzer, poniendo el ancho de pulso a 0; |
CCastrop1012 | 0:309da55d6e5a | 103 | { |
CCastrop1012 | 0:309da55d6e5a | 104 | Buzzer.period(1); Buzzer.write(0); // Volvemos a la configuracion Inicial del Buzzer |
CCastrop1012 | 0:309da55d6e5a | 105 | LengthTonoBuzzer.attach(&stopBuzzer,10000); // Mientras no se envie un nuevo comando de tiempo para |
CCastrop1012 | 0:309da55d6e5a | 106 | // La interrupcion se deja un tiempo muy alto para generar la interrupcion |
CCastrop1012 | 0:309da55d6e5a | 107 | // Lo menos Posible |
CCastrop1012 | 0:309da55d6e5a | 108 | } |
CCastrop1012 | 0:309da55d6e5a | 109 | |
CCastrop1012 | 0:309da55d6e5a | 110 | void BuzzerSound(void) |
CCastrop1012 | 0:309da55d6e5a | 111 | { |
CCastrop1012 | 0:309da55d6e5a | 112 | |
CCastrop1012 | 0:309da55d6e5a | 113 | // Se calcula el valor del Periodo para el Buzzer limitando el ancho de banda de las |
CCastrop1012 | 0:309da55d6e5a | 114 | // Frecuencias de entradas entre 1 y 20Hz ó 1 y 20KHz |
CCastrop1012 | 0:309da55d6e5a | 115 | |
CCastrop1012 | 0:309da55d6e5a | 116 | // hallamos el valor de 1 a 20 que se ha ingresado |
CCastrop1012 | 0:309da55d6e5a | 117 | if(VCOMANDS[1] <= 40) periodo_Buzzer = 1.0; // Se restringe el limite inferior del valor ingresado a 40 y lo hace equivalente a un periodo de 1 segundo. |
CCastrop1012 | 0:309da55d6e5a | 118 | else if (VCOMANDS[1] > 60) periodo_Buzzer = 20.0; // Se restringe el limite Superior del valor ingresado a 60 y lo hace equivalente a un periodo de 20 segundo. |
CCastrop1012 | 0:309da55d6e5a | 119 | else periodo_Buzzer = (float)(VCOMANDS[1] - 40); |
CCastrop1012 | 0:309da55d6e5a | 120 | periodo_Buzzer = (1 / periodo_Buzzer); //Se realiza la conversión de Frequencia a Periodo |
CCastrop1012 | 0:309da55d6e5a | 121 | |
CCastrop1012 | 0:309da55d6e5a | 122 | |
CCastrop1012 | 0:309da55d6e5a | 123 | // Ahora si VCOMANDS[2] == 48 == '0'; la frecuencia estará en el orden de los Herz |
CCastrop1012 | 0:309da55d6e5a | 124 | if (VCOMANDS[2] == 48) |
CCastrop1012 | 0:309da55d6e5a | 125 | { |
CCastrop1012 | 0:309da55d6e5a | 126 | Buzzer.period(periodo_Buzzer); |
CCastrop1012 | 0:309da55d6e5a | 127 | VCOMANDS[2] = 0 ; |
CCastrop1012 | 0:309da55d6e5a | 128 | } |
CCastrop1012 | 0:309da55d6e5a | 129 | // Pero si VCOMANDS[2] == 49 == '1'; la frecuencia estará en el orden de los KiloHerz |
CCastrop1012 | 0:309da55d6e5a | 130 | else if (VCOMANDS[2] == 49) |
CCastrop1012 | 0:309da55d6e5a | 131 | { |
CCastrop1012 | 0:309da55d6e5a | 132 | Buzzer.period_us( (uint8_t)(periodo_Buzzer * 1000) ); |
CCastrop1012 | 0:309da55d6e5a | 133 | VCOMANDS[2] = 0 ; |
CCastrop1012 | 0:309da55d6e5a | 134 | } |
CCastrop1012 | 0:309da55d6e5a | 135 | // Determinamos el tiempo por el cual estará sonando el Buzeer |
CCastrop1012 | 0:309da55d6e5a | 136 | if(VCOMANDS[0] <= 40) duracion_Tono = 1; // Se restringe el limite inferior del valor ingresado a 40 y lo hace equivalente a una duración de 1 segundo. |
CCastrop1012 | 0:309da55d6e5a | 137 | else if (VCOMANDS[0] > 100) duracion_Tono = 60; // Se restringe el limite Superior del valor ingresado a 60 y lo hace equivalente a una duración de 60 segundo. |
CCastrop1012 | 0:309da55d6e5a | 138 | else duracion_Tono = (VCOMANDS[0] - 40); |
CCastrop1012 | 0:309da55d6e5a | 139 | |
CCastrop1012 | 0:309da55d6e5a | 140 | Buzzer.write(50); // Se carga el ancho de pulso a 50 ya que en la |
CCastrop1012 | 0:309da55d6e5a | 141 | // Interrupcion se pone a 0 |
CCastrop1012 | 0:309da55d6e5a | 142 | // Se habilita la interrupcion del ticker y se le asigna |
CCastrop1012 | 0:309da55d6e5a | 143 | // La función que ejecutará cuando se genere la interrupción |
CCastrop1012 | 0:309da55d6e5a | 144 | LengthTonoBuzzer.attach(&stopBuzzer,duracion_Tono); // Cargamos el Tiempo(0-60 segundos) ingresado |
CCastrop1012 | 0:309da55d6e5a | 145 | // y se lo Cargamos al Ticker del Buzzer |
CCastrop1012 | 0:309da55d6e5a | 146 | |
CCastrop1012 | 0:309da55d6e5a | 147 | VCOMANDS[9] = (uint8_t) (periodo_Buzzer * 1000); // Almacenamos El resultado del periodo en VCOMANDS[9] para visualizarlo |
CCastrop1012 | 0:309da55d6e5a | 148 | // Y confirmar que el valor sea el correcto |
CCastrop1012 | 0:309da55d6e5a | 149 | |
CCastrop1012 | 0:309da55d6e5a | 150 | |
CCastrop1012 | 0:309da55d6e5a | 151 | |
CCastrop1012 | 0:309da55d6e5a | 152 | |
CCastrop1012 | 0:309da55d6e5a | 153 | } |
CCastrop1012 | 0:309da55d6e5a | 154 | |
CCastrop1012 | 0:309da55d6e5a | 155 | |
CCastrop1012 | 0:309da55d6e5a | 156 | |
CCastrop1012 | 0:309da55d6e5a | 157 | void ReadPort() |
CCastrop1012 | 0:309da55d6e5a | 158 | { |
CCastrop1012 | 0:309da55d6e5a | 159 | |
CCastrop1012 | 0:309da55d6e5a | 160 | // if (SerialPort.readable()) |
CCastrop1012 | 0:309da55d6e5a | 161 | VCOMANDS[i] = SerialPort.getc(); |
CCastrop1012 | 0:309da55d6e5a | 162 | i++; |
CCastrop1012 | 0:309da55d6e5a | 163 | if(i > 2) i = 0; |
CCastrop1012 | 0:309da55d6e5a | 164 | |
CCastrop1012 | 0:309da55d6e5a | 165 | |
CCastrop1012 | 0:309da55d6e5a | 166 | } |