๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’ป Microprocessor/์บก์Šคํ†ค ์ฑŒ๋ฆฐ์ง€

[2์ฃผ์ฐจ] (External Interrupt) Caston Challenge 4

by GroovyArea 2022. 10. 5.

โ€ป Caston Challenge 2๋ฒˆ, 3๋ฒˆ ๋ฌธ์ œ๋Š” ํ•˜๋“œ์›จ์–ด์  ๊ตฌ์„ฑ์ด๊ธฐ ๋•Œ๋ฌธ์— ์ƒ๋žตํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

 

๋ฌธ์ œ

- ์Šค์œ„์น˜ 1์€ Falling Edge๋กœ

- ์Šค์œ„์น˜ 2๋Š” Rising Edge๋กœ ์„ค์ •

- AVR ๋ช…๋ น์–ด(PORT ์ž…์ถœ๋ ฅ)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ Interrupt ์‹คํ—˜

 

- ์Šค์œ„์น˜ 1 Debounce ํšŒ๋กœ๋ฅผ ์„ค๊ณ„

- ์Šค์œ„์น˜ 2 ์ผ๋ฐ˜ ํšŒ๋กœ๋กœ ์„ค๊ณ„

 

- ์Šค์œ„์น˜ 1 LED 4๊ฐœ๊ฐ€ LSB ๋ถ€ํ„ฐ ํ•˜๋‚˜์”ฉ ๋ˆ„์ ํ•˜์—ฌ On, LCD์—๋Š” 1ํ–‰์—๋Š” “Falling Edge” Displayํ•˜๊ณ , 2ํ–‰์—๋Š” ํ˜„์žฌ LED๊ฐ€ ๋ช‡๊ฐœ On ๋˜์—ˆ๋Š”์ง€ ๊ฐœ์ˆ˜๋ฅผ Display

 

- ์Šค์œ„์น˜ 2 LED 4๊ฐœ๊ฐ€ MSB ๋ถ€ํ„ฐ ํ•˜๋‚˜์”ฉ ์ด๋™ํ•˜๋ฉด์„œ On, LCD์—๋Š” 1ํ–‰์—๋Š” “Rising

Edge”๋ผ๊ณ  Display, 2ํ–‰์—๋Š” MSB ๊ธฐ์ค€์œผ๋กœ ์ฒซ๋ฒˆ์งธ LED๋ฅผ 1์ด๋ผ๊ณ  ํ–ˆ์„ ๋•Œ, ํ˜„์žฌ ๋ช‡ ๋ฒˆ ์งธ LED๊ฐ€ On ๋˜์–ด ์žˆ๋Š”์ง€ ์œ„์น˜๋ฅผ Display

 

๋ฌธ์ œ ์ดํ•ด

๋ฌธ์ œ์— ๋‚˜์˜จ ์ˆœ์„œ ๊ทธ๋Œ€๋กœ ํšŒ๋กœ๋ฅผ ๊ตฌ์„ฑํ•˜์—ฌ ์ง„ํ–‰ํ•˜๋ฉด ๋  ๊ฒƒ ๊ฐ™๋‹ค.

LSB, MSB๊ฐ€ ์˜ค๋žœ๋งŒ์— ๋“ฃ๋Š” ๊ฐœ๋…์ด๋ผ led ํฌํŠธ์˜ ๋น„ํŠธ๋ฅผ ์ž˜ ์กฐ์ž‘ํ•ด์•ผ ๋  ๊ฒƒ ๊ฐ™๋‹ค.

 

 

์ฝ”๋“œ

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);

#define led1 22
#define led2 23
#define led3 24
#define led4 25

#define sw1 2
#define sw2 3

volatile int state = HIGH;
volatile int count_sw1 = 0;
volatile int count_sw2 = 4;
const byte interruptPin1 = 2;
const byte interruptPin2 = 3;

void setup() {

  // put your setup code here, to run once:
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(sw2, INPUT_PULLUP);

  lcd.init();
  lcd.backlight();

  noInterrupts();
  interrupts();
  attachInterrupt(digitalPinToInterrupt(interruptPin1), leastToMost, FALLING);
  attachInterrupt(digitalPinToInterrupt(interruptPin2), mostToLeast, RISING);
}

void loop() {

  if (count_sw1 > 0 && count_sw1 < 5) {
    lcd.clear();
    lcd.print("Falling Edge");
    lcd.setCursor(0, 2);
    lcd.print(count_sw1);
  }

  if (count_sw1 >= 5) {
    count_sw1 = 0;
    PORTA = 0x01;
  }

  if (count_sw2 < 4 && count_sw2 >= 0) {
    lcd.clear();
    lcd.print("Rising Edge");
    lcd.setCursor(0, 1);
    lcd.print(count_sw1);
  }

  if (count_sw2 < 0) {
    count_sw2 = 0;
    PORTA = 0x04;
  }
}

void leastToMost() {
  count_sw1++;
  PORTA = 0x01 << count_sw1;
}

void mostToLeast() {
  count_sw2--;
  PORTA = 0x04 >> count_sw2;
}
  • LCD๋Š” I2C ํ†ต์‹ ์ด ๊ฐ€๋Šฅํ•œ LCD๋ฅผ ์ด์šฉ
  • ์ธํ„ฐ๋ŸฝํŠธ ํ•€ 2๊ฐœ๋ฅผ ์ง€์ •ํ•˜์—ฌ Falling, Rising ์ธํ„ฐ๋ŸฝํŠธ ์„ค์ •
  • ์ธํ„ฐ๋ŸฝํŠธ ๋ฐœ์ƒ ์‹œ count ๋ณ€์ˆ˜๋ฅผ ์ด์šฉํ•˜์—ฌ ๋น„ํŠธ๋ฅผ ์กฐ์ž‘ํ–ˆ๋‹ค.

 

I2C LCD๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด์„œ ๋””์Šคํ”Œ๋ ˆ์ด๊ฐ€ ๊ณ„์† ๊นจ์ง€๋Š” ์ผ์ด ๋ฐœ์ƒํ–ˆ๋‹ค. 

์•Œ์•„๋ณด๋‹ˆ, I2C๋„ ์ธํ„ฐ๋ŸฝํŠธ๊ฐ€ ํ•„์š”ํ•œ๋ฐ, ๊ทธ ๊ณผ์ •์—์„œ ๋‚ด๊ฐ€ ์„ค์ •ํ•œ ์ธํ„ฐ๋ŸฝํŠธ์™€ ๋™์‹œ์— ๊ผฌ์ธ ์ด์œ ๊ฐ€ ์žˆ์„ ์ˆ˜ ์žˆ๋‹ค๊ณ  ๋“ค์—ˆ๋‹ค.

์ธํ„ฐ๋ŸฝํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์‹คํ—˜์—์„œ๋Š” ์ผ๋ฐ˜ LCD๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹๊ฒ ๋‹ค.

๋ฐ˜์‘ํ˜•