Advertisement
ace_ventura

termostato.c

Sep 14th, 2016
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.20 KB | None | 0 0
  1. #include <wiringPi.h>
  2. #include <stdio.h>
  3. #include <math.h>
  4.  
  5. //Declara a porta 7 como o PWM
  6. #define PWM 7
  7. #define CONT 5000000
  8.  
  9. int main (void) {
  10.     int high=0, low=0, read=0, i;
  11.     float resistance=0, temperature=0, vref = 0;
  12.     float dutyCycle;
  13.  
  14.     printf("\n");
  15.     printf("Leitura da temperatura através do RasPi\n\n");
  16.  
  17.     wiringPiSetup ();
  18.     pinMode(PWM, INPUT);//Configura a porta 7 como entrada
  19.     pinMode(1, OUTPUT);//Configura a porta 1 do wiringPi (pino 12 no header) como saída
  20.  
  21.     for(;;) { //cálculo do duty cycle
  22.         for (i=0; i<CONT; i++) {
  23.             read = digitalRead(PWM);
  24.             if(read){
  25.                 high++;
  26.             } else {
  27.                 low++;
  28.             }
  29.         }
  30.  
  31.         dutyCycle = (float)high/(high+low);
  32.         high = 0;
  33.         low = 0;
  34.         vref = 5.0*dutyCycle;
  35.         resistance = (float)(1936*vref)/(5-vref);
  36.         temperature = -log(resistance/6883)/0.03774;//conversão de dutycycle para temperatura
  37.         printf("Tensão de Referência: %0.2f V\n", vref);
  38.         printf("Duty Cycle: %0.2f %\n", 100*dutyCycle);
  39.         printf("Temperatura: %0.2f ºC\n", temperature);
  40.         printf("\n");
  41.  
  42.         if (temperature > 43) { //temperatura de chaveamento do rele
  43.             digitalWrite(1, LOW);
  44.         }else if (temperature < 42) {
  45.             digitalWrite(1, HIGH);
  46.         }
  47.     }
  48.     return 0;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement