首先加入外部中断模块:
然后编辑这个模埠,如下设置:
我设置按下和抬起都中断。
再添加个管脚LED0 PB30
最后调试运行,LED0在按键时反转一下,抬起时反转一下:
原程序如下:
main.c:
#include "atmel_start.h"
#include "atmel_start_pins.h"
int main(void)
{
system_init();
/* Replace with your application code */
while(1) {
}
}
atmel_start.c:
/*
* Code generated from Atmel Start.
*
* This file will be overwritten when reconfiguring your Atmel Start project.
* Please copy examples or other code you want to keep to a separate file
* to avoid losing it when reconfiguring.
*/
#include "atmel_start.h"
#include <utils.h>
#include <hal_init.h>
#include <hpl_irq.h>
#include <hpl_pm1_v201_base.h>
#include <hpl_gclk1_v210_base.h>
#include <peripheral_gclk_config.h>
extern struct _irq_descriptor *_irq_table[PERIPH_COUNT_IRQn];
extern void Default_Handler(void);
void EXTERNAL_IRQ_0_init(void)
{
_gclk_enable_channel(EIC_GCLK_ID, CONF_GCLK_EIC_SRC);
ext_irq_init();
// Set pin direction to input
gpio_set_pin_direction(PA15, GPIO_DIRECTION_IN);
gpio_set_pin_pull_mode(PA15,
// <y> Pull configuration
// <id> pad_pull_config
// <GPIO_PULL_OFF"> Off
// <GPIO_PULL_UP"> Pull-up
// <GPIO_PULL_DOWN"> Pull-down
GPIO_PULL_OFF);
gpio_set_pin_mux(PA15, GPIO_MUX_A);
}
void EIC_Handler(void)
{
if (_irq_table[ EIC_IRQn + 0 ]) {
_irq_table[ EIC_IRQn + 0 ]->handler(
_irq_table[ EIC_IRQn + 0 ]->parameter);
} else {
Default_Handler();
}
}
void GCLK_Handler(void)
{
if (_irq_table[ +0 ]) {
_irq_table[ +0 ]->handler(_irq_table[ +0 ]->parameter);
} else {
Default_Handler();
}
}
void SYSCTRL_Handler(void)
{
if (_irq_table[ SYSCTRL_IRQn + 0 ]) {
_irq_table[ SYSCTRL_IRQn + 0 ]->handler(
_irq_table[ SYSCTRL_IRQn + 0 ]->parameter);
} else {
Default_Handler();
}
}
void PM_Handler(void)
{
if (_irq_table[ PM_IRQn + 0 ]) {
_irq_table[ PM_IRQn + 0 ]->handler(_irq_table[ PM_IRQn + 0 ]->parameter);
} else {
Default_Handler();
}
}
static void button_on_PA15_pressed(void)
{
gpio_toggle_port_level(GPIO_PORTB,PORT_PB30 );
}
/**
* Example of using EXTERNAL_IRQ_0
*/
void EXTERNAL_IRQ_0_example(void)
{
ext_irq_register(PIN_PA15, button_on_PA15_pressed);
}
void system_init(void)
{
init_mcu();
EXTERNAL_IRQ_0_init();
EXTERNAL_IRQ_0_example();
// GPIO on PB30
// Set pin direction to output
gpio_set_pin_direction(PIN_PB30, GPIO_DIRECTION_OUT);
gpio_set_pin_level(PIN_PB30,
// <y> Initial level
// <id> pad_initial_level
// <false"> Low
// <true"> High
false);
gpio_set_pin_mux(PIN_PB30, GPIO_MUX_OFF);
}
|