您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 

104 行
3.0 KiB

  1. /*
  2. * pwm.c:
  3. * This tests the hardware PWM channel.
  4. *
  5. * Copyright (c) 2012-2013 Gordon Henderson.
  6. ***********************************************************************
  7. * This file is part of wiringPi:
  8. * https://github.com/WiringPi/WiringPi
  9. *
  10. * wiringPi is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Lesser General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * wiringPi is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public License
  21. * along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
  22. ***********************************************************************
  23. */
  24. #include <wiringPi.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <stdint.h>
  28. #include <signal.h>
  29. #include <unistd.h>
  30. //**********************************************************************************************************************
  31. static int terminate_process = 0;
  32. static void Signal_handler(int sig);
  33. //**********************************************************************************************************************
  34. int main(void)
  35. {
  36. int bright;
  37. printf("Raspberry Pi wiringPi PWM test program\n");
  38. if (wiringPiSetup() == -1)
  39. {
  40. printf("ERROR: wiringPi setup failed\n");
  41. return EXIT_FAILURE;
  42. }
  43. // Set the handler for SIGTERM (15)
  44. signal(SIGTERM, Signal_handler);
  45. signal(SIGHUP, Signal_handler);
  46. signal(SIGINT, Signal_handler);
  47. signal(SIGQUIT, Signal_handler);
  48. signal(SIGTRAP, Signal_handler);
  49. signal(SIGABRT, Signal_handler);
  50. signal(SIGALRM, Signal_handler);
  51. signal(SIGUSR1, Signal_handler);
  52. signal(SIGUSR2, Signal_handler);
  53. pinMode(1, PWM_OUTPUT);
  54. printf("Going into +/- loop...\n");
  55. while (!terminate_process)
  56. {
  57. for (bright = 0; !terminate_process && bright < 1024; ++bright)
  58. {
  59. printf("+"); fflush(stdout);
  60. pwmWrite(1, bright);
  61. //delay (1) ;
  62. usleep(5 * 1000);
  63. }
  64. for (bright = 1023; !terminate_process && bright >= 0; --bright)
  65. {
  66. printf("-"); fflush(stdout);
  67. pwmWrite(1, bright);
  68. //delay (1) ;
  69. usleep(5 * 1000);
  70. }
  71. }
  72. return 0;
  73. }
  74. //**********************************************************************************************************************
  75. /**
  76. * Intercepts and handles signals
  77. * This function is called when the SIGTERM signal is raised
  78. */
  79. void Signal_handler(int sig)
  80. {
  81. printf("Received signal %d\n", sig);
  82. // Signal process to exit.
  83. terminate_process = 1;
  84. }
  85. //**********************************************************************************************************************