本帖最后由 比神乐 于 2022-12-2 17:49 编辑
今天捣鼓了一下按键控制灯,用microchip studio自动生成代码,然后自己再增加一部分代码。
代码如下:
#include <atmel_start.h>
int main(void)
{
/* Initializes MCU, drivers and middleware */
atmel_start_init();
_gpio_set_level(GPIO_PORTC,0x00040000,1);
/* Replace with your application code */
while (1) {
if((gpio_get_port_level(GPIO_PORTB)&0x80000000)==0x00000000)
_gpio_set_level(GPIO_PORTC,0x00040000,0);
else
_gpio_set_level(GPIO_PORTC,0x00040000,1);
}
}
初始化部分代码:
#include <atmel_start.h>
/**
* Initializes MCU, drivers and middleware in the project
**/
void atmel_start_init(void)
{
system_init();
}
/*
* 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 "driver_init.h"
#include <peripheral_clk_config.h>
#include <utils.h>
#include <hal_init.h>
void system_init(void)
{
init_mcu();
// GPIO on PB31
// Set pin direction to input
gpio_set_pin_direction(KEY, GPIO_DIRECTION_IN);
gpio_set_pin_pull_mode(KEY,
// <y> Pull configuration
// <id> pad_pull_config
// <GPIO_PULL_OFF"> Off
// <GPIO_PULL_UP"> Pull-up
// <GPIO_PULL_DOWN"> Pull-down
GPIO_PULL_UP);
gpio_set_pin_function(KEY, GPIO_PIN_FUNCTION_OFF);
// GPIO on PC18
gpio_set_pin_level(LED,
// <y> Initial level
// <id> pad_initial_level
// <false"> Low
// <true"> High
true);
// Set pin direction to output
gpio_set_pin_direction(LED, GPIO_DIRECTION_OUT);
gpio_set_pin_function(LED, GPIO_PIN_FUNCTION_OFF);
}
效果图:
|