123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- #include <wiringPi.h>
- #include <stdio.h>
- #define LED_PIN 26
- #define BTN_PIN 27
- #define DELY_MS 1000
- //在中断中要使用易失性变量
- volatile int cnt = 0;
- void btnInterrupt()
- {
- if(cnt%2 == 0)
- {
- //led GPIO引脚置低电平,灯亮
- digitalWrite(LED_PIN, LOW);
- }
- else
- {
- //led GPIO引脚置高电平,灯灭
- digitalWrite(LED_PIN, HIGH);
- }
- cnt++;
- }
- int main(void)
- {
- wiringPiSetup();
- pinMode(LED_PIN, OUTPUT);
- pinMode(BTN_PIN, INPUT);
- digitalWrite(LED_PIN, HIGH);
- pullUpDnControl(BTN_PIN, PUD_UP);
- //配置按键的边沿触发方式和中断函数
- wiringPiISR(BTN_PIN, INT_EDGE_RISING, &btnInterrupt);
- while(1)
- {
- //主线程可以做其他事情
- delay(DELY_MS);
- }
- return 0;
- }
|