wiringpi_btn_led_isr.c 807 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #include <wiringPi.h>
  2. #include <stdio.h>
  3. #define LED_PIN 26
  4. #define BTN_PIN 27
  5. #define DELY_MS 1000
  6. //在中断中要使用易失性变量
  7. volatile int cnt = 0;
  8. void btnInterrupt()
  9. {
  10. if(cnt%2 == 0)
  11. {
  12. //led GPIO引脚置低电平,灯亮
  13. digitalWrite(LED_PIN, LOW);
  14. }
  15. else
  16. {
  17. //led GPIO引脚置高电平,灯灭
  18. digitalWrite(LED_PIN, HIGH);
  19. }
  20. cnt++;
  21. }
  22. int main(void)
  23. {
  24. wiringPiSetup();
  25. pinMode(LED_PIN, OUTPUT);
  26. pinMode(BTN_PIN, INPUT);
  27. digitalWrite(LED_PIN, HIGH);
  28. pullUpDnControl(BTN_PIN, PUD_UP);
  29. //配置按键的边沿触发方式和中断函数
  30. wiringPiISR(BTN_PIN, INT_EDGE_RISING, &btnInterrupt);
  31. while(1)
  32. {
  33. //主线程可以做其他事情
  34. delay(DELY_MS);
  35. }
  36. return 0;
  37. }