从官方提供的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支持包后,编译成功后,直接通过DEBUG USB接口下载到开发板,重启开发板后看到的现象是D3常亮,D2微亮。然后跳转到关于LED管脚宏定义的main.h文件中,再结合“5-硬件评估板(Hardware Evaulation Board)\N32G45XVL-STB V1.1(LQFP100)”的原理图,修改管脚的定义。
- /*Led1-PA8,Led2-PB4,Led3-PB5*/
- #define PORT_GROUP1 GPIOB
- #define PORT_GROUP2 GPIOA
- #define LED1_PORT PORT_GROUP2
- #define LED2_PORT PORT_GROUP1
- #define LED3_PORT PORT_GROUP1
- #define LED1_PIN GPIO_PIN_8
- #define LED2_PIN GPIO_PIN_4
- #define LED3_PIN GPIO_PIN_5
特别注意的是,管脚PB4与JTAG引脚复用,因此如果程序中没有复用到该管脚,则LED2不受控。main.c中调整代码如下:
- #include "main.h"
- #include <stdio.h>
- #include <stdint.h>
- #include "n32g45x_gpio.h"
- void Delay(uint32_t count)
- {
- for (; count > 0; count--)
- ;
- }
- void LedInit(GPIO_Module* GPIOx, uint16_t Pin)
- {
- GPIO_InitType GPIO_InitStructure;
- assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
- if (GPIOx == GPIOA)
- {
- RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOA, ENABLE);
- }
- else if (GPIOx == GPIOB)
- {
- RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOB, ENABLE);
- }
- else if (GPIOx == GPIOC)
- {
- RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOC, ENABLE);
- }
- else if (GPIOx == GPIOD)
- {
- RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOD, ENABLE);
- }
- else if (GPIOx == GPIOE)
- {
- RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOE, ENABLE);
- }
- else if (GPIOx == GPIOF)
- {
- RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOF, ENABLE);
- }
- else
- {
- if (GPIOx == GPIOG)
- {
- RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOG, ENABLE);
- }
- }
- if (Pin <= GPIO_PIN_ALL)
- {
- GPIO_InitStructure.Pin = Pin;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitPeripheral(GPIOx, &GPIO_InitStructure);
- }
- }
- void LedOn(GPIO_Module* GPIOx, uint16_t Pin)
- {
- GPIOx->PBSC = Pin;
- }
- void LedOff(GPIO_Module* GPIOx, uint16_t Pin)
- {
- GPIOx->PBC = Pin;
- }
- void LedOnOff(GPIO_Module* GPIOx, uint32_t Pin)
- {
- GPIOx->PBSC = Pin;
- }
- void LedBlink(GPIO_Module* GPIOx, uint16_t Pin)
- {
- GPIOx->POD ^= Pin;
- }
- #ifdef USE_FULL_ASSERT
- void assert_failed(const uint8_t* expr, const uint8_t* file, uint32_t line)
- {
- while (1)
- {
- }
- }
- #endif // USE_FULL_ASSERT
- int main(void)
- {
- /*SystemInit() function has been called by startup file startup_n32g45x.s*/
- int i = 0;
- GPIO_InitType GPIO_InitStructure;
- /* Initialize Led1~Led5 as output pushpull mode*/
- LedInit(PORT_GROUP2, LED1_PIN);
- LedInit(PORT_GROUP1, LED2_PIN | LED3_PIN | LED4_PIN | LED5_PIN);
- RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_AFIO | RCC_APB2_PERIPH_GPIOB, ENABLE);
- GPIO_ConfigPinRemap(GPIO_RMP_SW_JTAG_DISABLE, ENABLE);
- GPIO_InitStructure.Pin = GPIO_PIN_4;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_InitPeripheral(GPIOB, &GPIO_InitStructure);
- while (1)
- {
- /*LED1_PORT and LED2_PORT are the same port group.Enable Led2 blink and not effect Led1 by Exclusive-OR
- * operation.*/
- LedBlink(PORT_GROUP1, LED2_PIN);
- for(i=0;i<8;i++)
- {
- LedOn(PORT_GROUP2, LED1_PIN);
- LedOff(PORT_GROUP1, LED2_PIN);
- LedOff(PORT_GROUP1, LED3_PIN);
- Delay(0x88FFFF);
- LedOff(PORT_GROUP2, LED1_PIN);
- LedOn(PORT_GROUP1, LED2_PIN);
- LedOff(PORT_GROUP1, LED3_PIN);
- Delay(0x88FFFF);
- LedOff(PORT_GROUP2, LED1_PIN);
- LedOff(PORT_GROUP1, LED2_PIN);
- LedOn(PORT_GROUP1, LED3_PIN);
- Delay(0x88FFFF);
- }
- for(i=0;i<8;i++)
- {
- LedOn(PORT_GROUP2, LED1_PIN);
- LedOn(PORT_GROUP1, LED2_PIN);
- LedOn(PORT_GROUP1, LED3_PIN);
- Delay(0x38FFFF);
- LedOff(PORT_GROUP2, LED1_PIN);
- LedOff(PORT_GROUP1, LED2_PIN);
- LedOff(PORT_GROUP1, LED3_PIN);
- Delay(0x38FFFF);
- }
- }
- }
编译完成后,连接DEBUG USB数据接口,Keil中识别到下载调试器,即可将闪灯程序下载到开发板中。
LED闪灯程序部分视频剪辑如下:
另外特别提示,由于PB4复用了JTAG功能,再次进入工程中,Keil中不再识别到SW-DP下载调试器,如果还需验证、下载其它驱动程序,肿么办?需要将BOOT0引脚拉高,这里可通过跳线帽实现。
此次就MDK开发平台实现点灯实验介绍到这里,使用N32G457开发板需运用在RT-Thread系统中,当然后续会采用“RT-Thread Studio”构建工程项目,再次感谢21ic平台及国民技术&RT-Thread主办方。
|