Advertisement
RuiViana

Untitled

Apr 30th, 2015
381
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 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. attachInterrupt(0, frequencia, RISING);
  31. }
  32.  
  33. void loop()
  34. {
  35. voltagem();
  36. delay(20);
  37. }
  38.  
  39. //=============================== VOLTAGE ========================================//
  40. void voltagem()
  41. {
  42. voltage_peak_value = 0;
  43. for(sample_count = 0; sample_count < 500; sample_count ++)
  44. {
  45. adc_value = analogRead(A1);
  46. if(voltage_peak_value < adc_value)
  47. voltage_peak_value = adc_value;
  48. else;
  49. delayMicroseconds(1);
  50. }
  51. dc_voltage_V0 = voltage_peak_value * 0.00488;
  52. ac_voltage_V0 = (dc_voltage_V0 / 1.414) * range500_mul;
  53. lcd.setCursor(0,0);
  54. Serial.print("V");
  55. lcd.setCursor(3,0);
  56. Serial.println(ac_voltage_V0);
  57. }
  58.  
  59. //=============================== FREQUENCY ========================================//
  60. void frequencia()
  61. {
  62. tempo = micros();
  63. TOld = tempo - TNew;
  64. TNew = tempo;
  65. freq = (10000000/TOld)+5; // +5 correção de erro de divisão
  66.  
  67. lcd.setCursor(7,1);
  68. Serial.print(freq/10);
  69. lcd.setCursor(13,1);
  70. Serial.println("Hz");
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement