Advertisement
RuiViana

Untitled

Apr 30th, 2015
592
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1.  
  2. // frequencimetro de 60Hz
  3. // Voltimetro
  4.  
  5. #include <LiquidCrystal.h>
  6. LiquidCrystal lcd(13,12,11,10,9,8);
  7. long freq, tempo, TOld, TNew;
  8.  
  9. #define resistance_R500 1000000
  10. #define resistance_V2 10000
  11. #define caliberation_V2 1.05
  12. #define range500_mul (resistance_R500 / resistance_V2) * caliberation_V2
  13. #define resistance_R2 1000
  14.  
  15. int adc_value = 0;
  16. int voltage_peak_value = 0;
  17. float voltage_average_value = 0;
  18. float dc_voltage_V0 = 0;
  19. int ac_voltage_V0 = 0;
  20. unsigned long sample_count = 0;
  21.  
  22. void setup()
  23. {
  24. Serial.begin(9600);
  25. pinMode(2,INPUT);
  26. lcd.begin(20, 2);
  27. lcd.setCursor(3,0);
  28. lcd.print("FREQUENCIMETRO");
  29. Serial.print("FREQUENCIMETRO");
  30. }
  31.  
  32. void loop()
  33. {
  34.  
  35. voltagem();
  36.  
  37. attachInterrupt(0, frequencia, RISING);
  38. // delay(50);
  39. // noInterrupts();
  40. }
  41.  
  42. //=============================== VOLTAGE ========================================//
  43. void voltagem()
  44. {
  45. voltage_peak_value = 0;
  46. for(sample_count = 0; sample_count < 500; sample_count ++)
  47. {
  48. adc_value = analogRead(A1);
  49. if(voltage_peak_value < adc_value)
  50. voltage_peak_value = adc_value;
  51. else;
  52. delayMicroseconds(1);
  53. }
  54. dc_voltage_V0 = voltage_peak_value * 0.00488;
  55. ac_voltage_V0 = (dc_voltage_V0 / 1.414) * range500_mul;
  56. lcd.setCursor(0,0);
  57. Serial.print("V");
  58. lcd.setCursor(3,0);
  59. Serial.println(ac_voltage_V0);
  60. }
  61.  
  62. //=============================== FREQUENCY ========================================//
  63. void frequencia()
  64. {
  65. tempo = micros();
  66. TOld = tempo - TNew;
  67. TNew = tempo;
  68. freq = (10000000/TOld)+5; // +5 correção de erro de divisão
  69.  
  70. lcd.setCursor(7,1);
  71. Serial.print(freq/10);
  72. lcd.setCursor(13,1);
  73. Serial.println("Hz");
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement