[N32G45x] 快速搭建环境,闪光灯点起

[复制链接]
1267|2
 楼主| yinwuqing110 发表于 2022-2-20 17:43 | 显示全部楼层 |阅读模式
   从官方提供的SDK中可获知“7-软件开发套件(Software Development Kit)”文件夹下包含了所有基本工程示例,工程中有提供基于IAR与MDK平台的参考工程源码,本人习惯使用Keil5开发平时的项目,因此侧重MDK平台。话不多说,直接在软件开发套件的文件夹下,解压“Nationstech.N32G45x_Library.2.0.0”压缩包,进到“Nationstech.N32G45x_Library.2.0.0\projects\n32g45x_EVAL\examples\GPIO\LedBlink”路径中,然后打开“MDK-ARM”文件夹下的LedBlink工程,打开后弹出需要安装好pack支持包的对话框,开发者可通过在线或离线安装pack包。 Nationstech.N32G45x_DFP.1.0.4.zip (111.67 KB, 下载次数: 15)
导入pack包.png
      安装好pack支持包后,编译成功后,直接通过DEBUG USB接口下载到开发板,重启开发板后看到的现象是D3常亮,D2微亮。然后跳转到关于LED管脚宏定义的main.h文件中,再结合“5-硬件评估板(Hardware Evaulation Board)\N32G45XVL-STB V1.1(LQFP100)”的原理图,修改管脚的定义。
  1. /*Led1-PA8,Led2-PB4,Led3-PB5*/
  2. #define PORT_GROUP1 GPIOB
  3. #define PORT_GROUP2 GPIOA
  4. #define LED1_PORT   PORT_GROUP2
  5. #define LED2_PORT   PORT_GROUP1
  6. #define LED3_PORT   PORT_GROUP1
  7. #define LED1_PIN    GPIO_PIN_8
  8. #define LED2_PIN    GPIO_PIN_4
  9. #define LED3_PIN    GPIO_PIN_5
LED管脚连线.png
      特别注意的是,管脚PB4与JTAG引脚复用,因此如果程序中没有复用到该管脚,则LED2不受控。main.c中调整代码如下:
  1. #include "main.h"
  2. #include <stdio.h>
  3. #include <stdint.h>
  4. #include "n32g45x_gpio.h"

  5. void Delay(uint32_t count)
  6. {
  7.     for (; count > 0; count--)
  8.         ;
  9. }

  10. void LedInit(GPIO_Module* GPIOx, uint16_t Pin)
  11. {
  12.     GPIO_InitType GPIO_InitStructure;

  13.     assert_param(IS_GPIO_ALL_PERIPH(GPIOx));

  14.     if (GPIOx == GPIOA)
  15.     {
  16.         RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOA, ENABLE);
  17.     }
  18.     else if (GPIOx == GPIOB)
  19.     {
  20.         RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOB, ENABLE);
  21.     }
  22.     else if (GPIOx == GPIOC)
  23.     {
  24.         RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOC, ENABLE);
  25.     }
  26.     else if (GPIOx == GPIOD)
  27.     {
  28.         RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOD, ENABLE);
  29.     }
  30.     else if (GPIOx == GPIOE)
  31.     {
  32.         RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOE, ENABLE);
  33.     }
  34.     else if (GPIOx == GPIOF)
  35.     {
  36.         RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOF, ENABLE);
  37.     }
  38.     else
  39.     {
  40.         if (GPIOx == GPIOG)
  41.         {
  42.             RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOG, ENABLE);
  43.         }
  44.     }

  45.     if (Pin <= GPIO_PIN_ALL)
  46.     {
  47.         GPIO_InitStructure.Pin        = Pin;
  48.         GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_Out_PP;
  49.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  50.         GPIO_InitPeripheral(GPIOx, &GPIO_InitStructure);
  51.     }
  52. }

  53. void LedOn(GPIO_Module* GPIOx, uint16_t Pin)
  54. {
  55.     GPIOx->PBSC = Pin;
  56. }

  57. void LedOff(GPIO_Module* GPIOx, uint16_t Pin)
  58. {
  59.     GPIOx->PBC = Pin;
  60. }

  61. void LedOnOff(GPIO_Module* GPIOx, uint32_t Pin)
  62. {
  63.     GPIOx->PBSC = Pin;
  64. }

  65. void LedBlink(GPIO_Module* GPIOx, uint16_t Pin)
  66. {
  67.     GPIOx->POD ^= Pin;
  68. }

  69. #ifdef USE_FULL_ASSERT
  70. void assert_failed(const uint8_t* expr, const uint8_t* file, uint32_t line)
  71. {
  72.     while (1)
  73.     {
  74.     }
  75. }
  76. #endif // USE_FULL_ASSERT

  77. int main(void)
  78. {
  79.     /*SystemInit() function has been called by startup file startup_n32g45x.s*/
  80.                 int i = 0;
  81.           GPIO_InitType GPIO_InitStructure;
  82.     /* Initialize Led1~Led5 as output pushpull mode*/
  83.     LedInit(PORT_GROUP2, LED1_PIN);
  84.     LedInit(PORT_GROUP1, LED2_PIN | LED3_PIN | LED4_PIN | LED5_PIN);

  85.                 RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_AFIO | RCC_APB2_PERIPH_GPIOB, ENABLE);
  86.                 GPIO_ConfigPinRemap(GPIO_RMP_SW_JTAG_DISABLE, ENABLE);
  87.                 GPIO_InitStructure.Pin = GPIO_PIN_4;
  88.                 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  89.                 GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_Out_PP;
  90.     GPIO_InitPeripheral(GPIOB, &GPIO_InitStructure);

  91.     while (1)
  92.     {
  93.         /*LED1_PORT and LED2_PORT are the same port group.Enable Led2 blink and not effect Led1 by Exclusive-OR
  94.          * operation.*/
  95.         LedBlink(PORT_GROUP1, LED2_PIN);

  96.                         for(i=0;i<8;i++)
  97.                         {
  98.                           LedOn(PORT_GROUP2, LED1_PIN);
  99.         LedOff(PORT_GROUP1, LED2_PIN);
  100.                                 LedOff(PORT_GROUP1, LED3_PIN);
  101.         Delay(0x88FFFF);
  102.                                 LedOff(PORT_GROUP2, LED1_PIN);
  103.         LedOn(PORT_GROUP1, LED2_PIN);
  104.                                 LedOff(PORT_GROUP1, LED3_PIN);
  105.                                 Delay(0x88FFFF);
  106.                                 LedOff(PORT_GROUP2, LED1_PIN);
  107.         LedOff(PORT_GROUP1, LED2_PIN);
  108.                                 LedOn(PORT_GROUP1, LED3_PIN);
  109.                                 Delay(0x88FFFF);
  110.                         }
  111.                         for(i=0;i<8;i++)
  112.                         {
  113.                         LedOn(PORT_GROUP2, LED1_PIN);
  114.       LedOn(PORT_GROUP1, LED2_PIN);
  115.                         LedOn(PORT_GROUP1, LED3_PIN);
  116.                         Delay(0x38FFFF);
  117.                         LedOff(PORT_GROUP2, LED1_PIN);
  118.       LedOff(PORT_GROUP1, LED2_PIN);
  119.                         LedOff(PORT_GROUP1, LED3_PIN);
  120.                         Delay(0x38FFFF);
  121.                         }
  122.     }
  123. }
     编译完成后,连接DEBUG USB数据接口,Keil中识别到下载调试器,即可将闪灯程序下载到开发板中。
识别下载调试器.jpg
      LED闪灯程序部分视频剪辑如下:
LED闪灯剪辑.gif
      另外特别提示,由于PB4复用了JTAG功能,再次进入工程中,Keil中不再识别到SW-DP下载调试器,如果还需验证、下载其它驱动程序,肿么办?需要将BOOT0引脚拉高,这里可通过跳线帽实现。
将BOOT0引脚拉高.png
BOOT0拉高.jpg
       此次就MDK开发平台实现点灯实验介绍到这里,使用N32G457开发板需运用在RT-Thread系统中,当然后续会采用“RT-Thread Studio”构建工程项目,再次感谢21ic平台及国民技术&RT-Thread主办方。
王栋春 发表于 2022-2-20 22:22 | 显示全部楼层
围观一下。                                                     
daichaodai 发表于 2022-2-21 08:00 来自手机 | 显示全部楼层
快速上手,点灯跑起来。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

107

主题

1102

帖子

7

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