①按键计数实验代码:

int count = 0;

void setup() { Serial.begin(9600); pinMode(2, INPUT_PULLUP); attachInterrupt(digitalPinToInterrupt(2), buttonPressed, FALLING); }

void loop() { }

void buttonPressed() { count++; Serial.print("Button pressed "); Serial.print(count); Serial.println(" times."); }

②控制 LED 实验代码:

int ledPins[] = {3, 4, 5, 6, 7};

int button2Count = 0; int button3Count = 0;

void setup() { Serial.begin(9600); pinMode(2, INPUT_PULLUP); pinMode(3, INPUT_PULLUP); attachInterrupt(digitalPinToInterrupt(2), button2Pressed, FALLING); attachInterrupt(digitalPinToInterrupt(3), button3Pressed, FALLING); for(int i = 0; i < 5; i++) { pinMode(ledPins[i], OUTPUT); } }

void loop() { int binary = button2Count * 10 + button3Count; Serial.print("Binary: "); Serial.println(binary, BIN); for(int i = 0; i < 5; i++) { if(binary & (1 << i)) { digitalWrite(ledPins[i], HIGH); } else { digitalWrite(ledPins[i], LOW); } } }

void button2Pressed() { button2Count++; }

void button3Pressed() { button3Count++;

给出下列外部中断与串口通行实验的C语言代码目的:掌握 ARDUINO 的外部中断引脚有哪些串行通行的接口是哪几个引脚 这在硬件层面上的应用很重要。掌握外部中断的触发与回调函数串口通行的启动等。内容:①连接 1 个按键至外部中断引脚 2 上ARDUINO 记录按键次数采用中断方式非查询方式并通过串口打印按键次数;②连接 2 个按键至外部中断引脚 23 上同时自己选取其它的 5 个端口接 5 个 LE

原文地址: https://www.cveoy.top/t/topic/eh6x 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录