ATmega328p Xplained Mini上有一个按键和一个LED,具体在哪个管脚如下图所示:
而要用中断来控制LED,必须了解以下寄存器:
EICRA - 外部中断控制寄存器A
比特3,2 - 外部中断1控制
ISC11 ISC10 Description
0 0 The low level of INT1 generates an interrupt request.
0 1 Any logical change on INT1 generates an interrupt request.
1 0 The falling edge of INT1 generates an interrupt request.
1 1 The rising edge of INT1 generates an interrupt request.
比特1,2 - 外部中断0控制
ISC01 ISC00 Description
0 0 The low level of INT0 generates an interrupt request.
0 1 Any logical change on INT0 generates an interrupt request.
1 0 The falling edge of INT0 generates an interrupt request.
1 1 The rising edge of INT0 generates an interrupt request.
由于开发板是按键接高电平的,所以我选下降沿触发。
EIMSK - 外部中断屏蔽寄存器
比特1:外部中断请求1允许
比特2:外部中断请求2允许
PCICR - 管脚改变中断控制寄存器
比特2:管脚改变中断允许寄存器2 23-16脚
比特1: 管脚改变中断允许寄存器1 14-8脚
比特0:管脚改变中断允许寄存器0 7-0脚
PCMSK0 - 管脚改变屏蔽寄存器0
PCINT7 到 PCINT0哪个管脚中断就设为1
具体程序如下:
/*
* GccApplication3.c
*
* Created: 2015/1/12 星期一 19:51:48
* Author: Administrator
*/
#define F_CPU 16000000UL
#include <avr/io.h>
#include <util/delay.h>
void PCINT0_vect(void)
{
PORTB |= 0x20;
_delay_ms(1000);
}
int main(void)
{
PCICR = 0x01;
PCMSK0 = 0x80;
EICRA = 0x0A;
EIMSK = 0x03;
PCICR = 0x01;
PCMSK0 = 0x80;
DDRB = 0x20;
SREG |= 0x80;
while(1)
{
//TODO:: Please write your application code
}
}
当我按下按键时,LED亮了,正明运行正常。
|