binary.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * binary.c:
  3. * Using the Quick 2 wire 16-bit GPIO expansion board to output
  4. * a binary counter.
  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 <mcp23017.h>
  28. #define Q2W_BASE 100
  29. int main (void)
  30. {
  31. int i, bit ;
  32. // Enable the on-goard GPIO
  33. wiringPiSetup () ;
  34. // Add in the mcp23017 on the q2w board
  35. mcp23017Setup (Q2W_BASE, 0x20) ;
  36. printf ("Raspberry Pi - quite2Wire MCP23017 Test\n") ;
  37. // On-board button Input:
  38. pinMode (0, INPUT) ;
  39. // First 10 pins on q2w board as outputs:
  40. for (i = 0 ; i < 10 ; ++i)
  41. pinMode (Q2W_BASE + i, OUTPUT) ;
  42. // Last pin as an input with the internal pull-up enabled
  43. pinMode (Q2W_BASE + 15, INPUT) ;
  44. pullUpDnControl (Q2W_BASE + 15, PUD_UP) ;
  45. // Loop, outputting a binary number,
  46. // Go faster with the button, or stop if the
  47. // on-board button is pushed
  48. for (;;)
  49. {
  50. for (i = 0 ; i < 1024 ; ++i)
  51. {
  52. for (bit = 0 ; bit < 10 ; ++bit)
  53. digitalWrite (Q2W_BASE + bit, i & (1 << bit)) ;
  54. while (digitalRead (0) == HIGH) // While pushed
  55. delay (1) ;
  56. if (digitalRead (Q2W_BASE + 15) == HIGH) // Not Pushed
  57. delay (100) ;
  58. }
  59. }
  60. return 0 ;
  61. }