[PIC32/SAM] ATSAMD51 EVK评估板评测2按键控制灯

[复制链接]
501|0
 楼主| 比神乐 发表于 2022-12-2 18:54 | 显示全部楼层 |阅读模式
今天捣鼓了一下按键控制灯,用microchip studio自动生成代码,然后自己再增加一部分代码。
代码如下:
  1. #include <atmel_start.h>

  2. int main(void)
  3. {
  4.         /* Initializes MCU, drivers and middleware */
  5.         atmel_start_init();
  6.         _gpio_set_level(GPIO_PORTC,0x00040000,1);
  7.         /* Replace with your application code */
  8.         while (1) {
  9.                
  10.                 if((gpio_get_port_level(GPIO_PORTB)&0x80000000)==0x00000000)
  11.                         _gpio_set_level(GPIO_PORTC,0x00040000,0);
  12.                 else
  13.                         _gpio_set_level(GPIO_PORTC,0x00040000,1);

  14.         }
  15. }

初始化代码:
  1. #include <atmel_start.h>

  2. /**
  3. * Initializes MCU, drivers and middleware in the project
  4. **/
  5. void atmel_start_init(void)
  6. {
  7.         system_init();
  8. }

  9. /*
  10. * Code generated from Atmel Start.
  11. *
  12. * This file will be overwritten when reconfiguring your Atmel Start project.
  13. * Please copy examples or other code you want to keep to a separate file
  14. * to avoid losing it when reconfiguring.
  15. */

  16. #include "driver_init.h"
  17. #include <peripheral_clk_config.h>
  18. #include <utils.h>
  19. #include <hal_init.h>

  20. void system_init(void)
  21. {
  22.         init_mcu();

  23.         // GPIO on PB31

  24.         // Set pin direction to input
  25.         gpio_set_pin_direction(KEY, GPIO_DIRECTION_IN);

  26.         gpio_set_pin_pull_mode(KEY,
  27.                                // <y> Pull configuration
  28.                                // <id> pad_pull_config
  29.                                // <GPIO_PULL_OFF"> Off
  30.                                // <GPIO_PULL_UP"> Pull-up
  31.                                // <GPIO_PULL_DOWN"> Pull-down
  32.                                GPIO_PULL_UP);

  33.         gpio_set_pin_function(KEY, GPIO_PIN_FUNCTION_OFF);

  34.         // GPIO on PC18

  35.         gpio_set_pin_level(LED,
  36.                            // <y> Initial level
  37.                            // <id> pad_initial_level
  38.                            // <false"> Low
  39.                            // <true"> High
  40.                            true);

  41.         // Set pin direction to output
  42.         gpio_set_pin_direction(LED, GPIO_DIRECTION_OUT);

  43.         gpio_set_pin_function(LED, GPIO_PIN_FUNCTION_OFF);
  44. }
效果图:






本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×
您需要登录后才可以回帖 登录 | 注册

本版积分规则

470

主题

3537

帖子

7

粉丝
快速回复 在线客服 返回列表 返回顶部