temperature.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * temperature.c:
  3. * Demonstrate use of the Gertboard A to D converter to make
  4. * a simple thermometer using the LM35.
  5. *
  6. * Copyright (c) 2012-2013 Gordon Henderson. <projects@drogon.net>
  7. ***********************************************************************
  8. * This file is part of wiringPi:
  9. * https://projects.drogon.net/raspberry-pi/wiringpi/
  10. *
  11. * wiringPi is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Lesser General Public License as published by
  13. * the Free Software Foundation, either version 3 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * wiringPi is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public License
  22. * along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
  23. ***********************************************************************
  24. */
  25. #include <stdio.h>
  26. #include <wiringPi.h>
  27. #include <gertboard.h>
  28. int main ()
  29. {
  30. int x1, x2 ;
  31. double v1, v2 ;
  32. printf ("\n") ;
  33. printf ("Gertboard demo: Simple Thermemeter\n") ;
  34. printf ("==================================\n") ;
  35. // Always initialise wiringPi. Use wiringPiSys() if you don't need
  36. // (or want) to run as root
  37. wiringPiSetupSys () ;
  38. // Initialise the Gertboard analog hardware at pin 100
  39. gertboardAnalogSetup (100) ;
  40. printf ("\n") ;
  41. printf ("| Channel 0 | Channel 1 | Temperature 1 | Temperature 2 |\n") ;
  42. for (;;)
  43. {
  44. // Read the 2 channels:
  45. x1 = analogRead (100) ;
  46. x2 = analogRead (101) ;
  47. // Convert to a voltage:
  48. v1 = (double)x1 / 1023.0 * 3.3 ;
  49. v2 = (double)x2 / 1023.0 * 3.3 ;
  50. // Print
  51. printf ("| %6.3f | %6.3f |", v1, v2) ;
  52. // Print Temperature of both channels by converting the LM35 reading
  53. // to a temperature. Fortunately these are easy: 0.01 volts per C.
  54. printf (" %4.1f | %4.1f |\r", v1 * 100.0, v2 * 100.0) ;
  55. fflush (stdout) ;
  56. }
  57. return 0 ;
  58. }