본문 바로가기
임베디드/Atmega328p

interrupt 2개 사용 LED 2개 제어

by sj0020 2020. 11. 6.
/*
 * 201105_interrupt.c
 *
 * Created: 2020-11-05 오전 9:37:16
 * Author : User
 */ 

#include <avr/io.h>
#include <avr/interrupt.h>
#define F_CPU 16000000UL
// #define _BV(x)	(1 << x)
// _BV(PIND2) --> 1<< PIND2


int main(void)
{
    // Configure PD2 as an input using the Data
	// Direction Register D (DDRD)
    DDRD &= ~_BV(DDD2);
	DDRD &= ~_BV(DDD3);
	
	//Enable the pull-up resistor on PD2 using the Port D
	//Data Register (PORTD)
	PORTD = (_BV(PORTD2)) | (_BV(PORTD3));
	
	// INT0 -> any logical change
	// INT1 -> any logical change
	//EICRA |= _BV(ISC10); // INT0
	//EICRA |= _BV(ISC00); // INT1
	//EICRA = (_BV(ISC00)) | (_BV(ISC00));
	//EICRA |= 5;
	//EICRA |= 0x05;
	EICRA |= 0b00000101;
	
	//Enable external interrupt 0 and interrupt 1 using the External Interrupt Mask Register
	// (EIMSK)
	EIMSK = (_BV(INT0)) | (_BV(INT1));
	
	// Configure PB5, PB4 as an output using the PortB Data Direction Register
	// (DDRB)
	DDRB = (_BV(DDB5)) | (_BV(DDB4));
	
	// Enable interrupts. SREG
	sei();
	
	//Loop forever
	while (1) 
    {
		// Nothing to do here
		// All work is done in the ISR
    }
}

ISR(INT0_vect){
	// read PD2 using the Port D Pin Input Register (PIND)
	if (PIND & _BV(PIND2)){
		// PD2 is high , so button is released
		// set PB5 low using the Port B DAta Register (PORTB)
		PORTB &= ~_BV(PORTB5);
	}
	
	else{
		// PD2 is low, so button is pressed
		// Set PB5 high using the Port B data Register (PORTB)
		PORTB |= _BV(PORTB5);
	}
}

ISR(INT1_vect){
	if (PIND & _BV(PIND3)){
		PORTB &= ~_BV(PORTB4);
	}
	
	else{
		cli();
		PORTB |= _BV(PORTB4);
	}	
	
}


 

코드 설명

https://sj0020.tistory.com/253

 

interrupt 하나 사용 ,LED 하나

/* * 201105_interrupt.c * * Created: 2020-11-05 오전 9:37:16 * Author : User */ #include #include #define F_CPU 16000000UL // #define _BV(x) (1 << x) // _BV(PIND2) --> 1<< PIND2 int main(void) { //..

sj0020.tistory.com

 

'임베디드 > Atmega328p' 카테고리의 다른 글

16x2 LCD D0, D1 -> C 포트에 꼽은 코드  (0) 2020.11.07
16x2 LCD (LC1621-SMLYH6)  (0) 2020.11.06
interrupt 하나 사용 ,LED 하나  (0) 2020.11.05
atmega 328p port ddrx  (0) 2020.11.04
button  (0) 2020.11.04