2, avrstudio提供了很好的ide能力.尽量的便捷用户,基本可以做到0代码编程,比如我们这个led程序,因为led的位置和引脚固定.我们就可以大胆放心的坐享其成,官方为我们写好这样一个例程,让我们能够迅速通过不同的方式来掌握io引脚的输入输出,,本例程就是关于板载的按键和一个黑色led( yellow led), 点击快速例程模版: new example project, 能够迅速建立起一个示例代码工程,
/** If \true, interrupts are used to alter the board state, when \false polling
* is used.
*/
#define USE_INTERRUPTS true
/** If \true, the External Interrupt Controller module is used to check when the
* button state changes, when \false the PORT module is used.
*/
#define USE_EIC true
/** Updates the board LED to the current button state. */
static void update_led_state(void)
{
bool pin_state = port_pin_get_input_level(BUTTON_0_PIN);
port_pin_set_output_level(LED_0_PIN, pin_state);
}
#if USE_INTERRUPTS == true
# if USE_EIC == true
/** Callback function for the EXTINT driver, called when an external interrupt
* detection occurs.
*/
static void extint_callback(void)
{
update_led_state();
}
/** Configures and registers the External Interrupt callback function with the
* driver.
*/
static void configure_eic_callback(void)
{
extint_register_callback(extint_callback,
BUTTON_0_EIC_LINE,
EXTINT_CALLBACK_TYPE_DETECT);
extint_chan_enable_callback(BUTTON_0_EIC_LINE,
EXTINT_CALLBACK_TYPE_DETECT);
}
# else
/** Handler for the device SysTick module, called when the SysTick counter
* reaches the set period.
*
* \note As this is a raw device interrupt, the function name is significant
* and must not be altered to ensure it is hooked into the device's
* vector table.
*/
void SysTick_Handler(void)
{
update_led_state();
}
/** Configures the SysTick module to fire a SysTick interrupt every 999 system
* clock source cycles.
*/
static void configure_systick_handler(void)
{
SysTick->CTRL = 0;
SysTick->LOAD = 999;
SysTick->VAL = 0;
SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk | SysTick_CTRL_TICKINT_Msk | SysTick_CTRL_ENABLE_Msk;
}
# endif
#endif
#if USE_EIC == true
/** Configures the External Interrupt Controller to detect changes in the board
* button state.
*/
static void configure_extint(void)
{
struct extint_chan_conf eint_chan_conf;
extint_chan_get_config_defaults(&eint_chan_conf);
while (true) {
/* Do nothing - use interrupts */
}
#else
# if USE_EIC == false
while (true) {
update_led_state();
}
# else
while (true) {
if (extint_chan_is_detected(BUTTON_0_EIC_LINE)) {
extint_chan_clear_detected(BUTTON_0_EIC_LINE);