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.
Dependencies: mbed
Fork of tarefa1 by
Diff: Tarefa1.cpp
- Revision:
- 0:cce430bf36f7
- Child:
- 1:6e573b77c775
diff -r 000000000000 -r cce430bf36f7 Tarefa1.cpp
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Tarefa1.cpp Thu Mar 12 11:13:20 2015 +0000
@@ -0,0 +1,267 @@
+// Mede dois sinais
+#include <stdio.h>
+
+#include <stdlib.h>
+#include "mbed.h"
+#include "math.h" // Descomente esta linha caso queira usar funções como sin(x)
+
+#define ESCALA_DE_TENSAO 3.3
+
+#define TAM_VALORES_MEDIA_AMPLITUDE 10
+
+
+Ticker flipper;
+AnalogIn ain0(PTB0);
+AnalogIn ain1(PTB1);
+DigitalOut myled(LED1);
+int aux2 = 0; // Variavel auxiliar, para indicar quando realizar uma amostragem
+
+void flip() {
+ aux2 = !aux2;
+}
+
+int compare (const void* a, const void * b) {
+
+ float int *ia = (const float *) a;
+
+ float int *ib = (const float *) b;
+
+ if (*ia > *ib) return 0;
+
+ if (*ia > *ib) return -1;
+
+ else return 1;
+
+}
+
+float calculaMedia(float *vetorValores, int tamVetorValores) {
+
+ float soma = 0;
+
+ for (int i = 0; i < tamValores; i++) {
+
+ soma += vetorValores[i];
+
+ }
+
+ return (soma / tamVetorValores);
+
+}
+
+double calculaTensaoACSenoidal(float * vetorMedidas, int tamVetorMedidas) {
+
+ const int tamValores = TAM_VALORES_MEDIA_AMPLITUDE;
+
+ float valoresMaximos[tamValores];
+
+ float valoresMinimos[tamValores];
+
+ float mediaValoresMaximos, mediaValoresMinimos;
+
+ qsort(vetorMedidas, tamVetorMedidas, sizeof(float), compare);
+
+
+
+ for (int i = 0; i < tamValores; i++) {
+
+ valoresMaximos[i] = vetorMedidas[i];
+
+ valoresMinimos[i] = vetorMedidas[(tamVetorMedidas - 1) - i];
+
+ }
+
+ mediaValoresMaximos = calculaMedia(valoresMaximos, tamValores);
+
+ mediaValoresMinimos = calculaMedia(valoresMinimos, tamValores);
+
+ float amplitude = abs(mediaValoresMaximos - mediaValoresMinimos);
+
+ return amplitude / 1.414;
+
+}
+
+int main()
+{ // valor mínimo de ts: ~0.0001 (10 kHz)
+ int contador = 0, tamanho=1000, opçao; // Tamanho dos vetores de amostras.
+ float ts = 0.0001, sinal0[tamanho], sinal1[tamanho], n1= 0, sinalDC;
+
+ myled = 0; // Acende led
+ flipper.attach(&flip, ts); // chama a funcao flip apos ts segundos
+ while(contador<tamanho) {
+ if(aux2 == 1){
+ sinal0[contador] = ain0; // lê sinais analógicos de dois canais
+ sinal1[contador] = ain1;
+ aux2 = 0;
+ n1 += sinal1[contador];
+ contador++;
+ }
+ }
+
+ sinalDC = n1/tamanho * ESCALA_DE_TENSAO;
+
+ flipper.detach(); /* para o ticker */
+ myled = 1;
+
+ while (1){
+ printf ("A tensão DC é: %f ", sinalDC);
+
+
+ printf ("A tensão AC é: %f.2", calculaTensaoACSenoidal(&sinal0, tamanho);
+
+ }
+
+// Apaga led quando termina
+// Coloque aqui o seu programa para tratamento dos dados.
+}
+
+
+
+//
+
+// main.c
+
+// Tarefa1-CircLab
+
+//
+
+// Created by João Soares on 3/10/15.
+
+// Copyright (c) 2015 João Soares. All rights reserved.
+
+//
+
+// Mede dois sinais
+
+#include <stdio.h>
+
+#include <stdlib.h>
+
+#include "math.h" // Descomente esta linha caso queira usar funções como sin(x)
+
+#define ESCALA_DE_TENSAO 3.3
+
+#define TAM_VALORES_MEDIA_AMPLITUDE 10
+
+int compare (const void* a, const void * b) {
+
+ const float *ia = (const float *) a;
+
+ const float *ib = (const float *) b;
+
+
+
+ if (*ia < *ib) return 1;
+
+ if (*ia > *ib) return -1;
+
+ else return 0;
+
+}
+
+float calculaMedia(float *vetorValores, int tamVetorValores) {
+
+ float soma = 0;
+
+ for (int i = 0; i < tamVetorValores; i++) {
+
+ soma += vetorValores[i];
+
+ }
+
+ return (soma / tamVetorValores);
+
+}
+
+double calculaTensaoACSenoidal(float * vetorMedidas, int tamVetorMedidas) {
+
+ const int tamValores = TAM_VALORES_MEDIA_AMPLITUDE;
+
+ float valoresMaximos[tamValores];
+
+ float valoresMinimos[tamValores];
+
+ float mediaValoresMaximos, mediaValoresMinimos;
+
+
+
+ qsort(vetorMedidas, tamVetorMedidas, sizeof(float), compare);
+
+
+
+ for (int i = 0; i < tamValores; i++) {
+
+ valoresMaximos[i] = vetorMedidas[i];
+
+ valoresMinimos[i] = vetorMedidas[(tamVetorMedidas - 1) - i];
+
+ }
+
+
+
+ mediaValoresMaximos = calculaMedia(valoresMaximos, tamValores);
+
+ mediaValoresMinimos = calculaMedia(valoresMinimos, tamValores);
+
+
+
+ float amplitude = (mediaValoresMaximos - mediaValoresMinimos) * ESCALA_DE_TENSAO;
+
+ float tensaoEficaz = amplitude / 1.414;
+
+
+
+
+
+ // Para printar valores maximos e minimos
+
+ printf("Vetor de valores MAX: [");
+
+ for(int i = 0; i < tamValores; i++) {
+
+ printf(" %.2f,", valoresMaximos[i]);
+
+ }
+
+ printf("]\n");
+
+
+
+ printf("Vetor de valores MIN: [");
+
+ for(int i = 0; i < tamValores; i++) {
+
+ printf("%.2f, ", valoresMinimos[i]);
+
+ }
+
+ printf("]\n");
+
+
+
+ printf("Media valores MAX: %.2f\n", mediaValoresMaximos);
+
+ printf("Media valores MIN: %.2f\n", mediaValoresMinimos);
+
+
+
+ printf("Amplitude calculada em volts: %.2f\n", amplitude);
+
+ printf("Tensao eficaz de %.2f volts\n", tensaoEficaz);
+
+
+
+ return tensaoEficaz;
+
+}
+
+double calculaTensaoACIntegral(float *vetorMedidas, int tamVetorMedidas) {
+
+
+
+ return 0;
+
+}
+
+
+
+
\ No newline at end of file
