【国民技术N32G457评测】两种N32G457开发工具及点灯程序的比较
[复制链接]
对N32G457开发板进行开发可使用MDK、IAR及RT-Thread等工具,下面仅以MDK和RT-Thread这两种工具为例,对点亮板载LED灯的程序变化加以比较。
若以MDK为工具,则可以直接在Nationstech.N32G45x_Library.2.0.0的压缩包中找到例程LedBlink,其程序如下:
int main(void)
{
/*SystemInit() function has been called by startup file startup_n32g45x.s*/
/* Initialize Led1~Led5 as output pushpull mode*/
LedInit(PORT_GROUP1, LED1_PIN | LED2_PIN);
LedInit(PORT_GROUP2, LED3_PIN | LED4_PIN | LED5_PIN);
/*Turn on Led1*/
LedOn(PORT_GROUP1, LED1_PIN);
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);
/*LED3_PORT, LED4_PORT and LED5_PORT are the same port group.*/
/*Turn Led4 and Led5 off and not effect other ports by PBC register,correspond to
* PORT_GROUP2->POD&=~(LED4_PIN|LED5_PIN);*/
LedOff(PORT_GROUP2, LED4_PIN | LED5_PIN);
/* Insert delay */
Delay(0x28FFFF);
/*Turn Led4 and Led5 on,turn Led3 off and not effect other ports by PBSC register,correspond to
* PORT_GROUP2->POD&=~(LED3_PIN),then PORT_GROUP2->POD|=(LED4_PIN|LED5_PIN);*/
LedOnOff(PORT_GROUP2, (LED3_PIN << 16) | LED4_PIN | LED5_PIN);
/* Insert delay */
Delay(0x28FFFF);
/*Turn on Led3*/
LedOn(PORT_GROUP2, LED3_PIN);
/* Insert delay */
Delay(0x28FFFF);
}
}
void Delay(uint32_t count)
{
for (; count > 0; count--) ;
}
void LedInit(GPIO_Module* GPIOx, uint16_t Pin)
{
GPIO_InitType GPIO_InitStructure;
/* Check the parameters */
assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
/* Enable the GPIO Clock */
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;
}
相应的引脚定义为:
/*Led1-PB10,Led2-PB15,Led3-PA9,Led4-PA5,Led5-PA6*/
#define PORT_GROUP1 GPIOB
#define PORT_GROUP2 GPIOA
#define LED1_PORT PORT_GROUP1
#define LED2_PORT PORT_GROUP1
#define LED3_PORT PORT_GROUP2
#define LED4_PORT PORT_GROUP2
#define LED5_PORT PORT_GROUP2
#define LED1_PIN GPIO_PIN_10
#define LED2_PIN GPIO_PIN_15
#define LED3_PIN GPIO_PIN_9
#define LED4_PIN GPIO_PIN_5
#define LED5_PIN GPIO_PIN_6
程序看起来很臃肿,其功能就是控制 LED(D1) 常亮,PB15 控制 LED(D2) 闪烁,PA5(D3)、PA6(D4)、PA9(D5) 电平翻转。
归纳之后,其操作无外乎对LED相关引脚的初始化及循环体内的高低电平切换。
但该程序是基于N32G457QE_EVB开发板的,在编译时需改为N32G457VEL7,此外对引脚的定义也需进行修改。
若只是让LED(D1)产生闪烁效果,主程序则可以改为如下内容:
int main(void)
{
LedInit(PORT_GROUP1, LED1_PINN);
LedOn(PORT_GROUP1, LED1_PIN);
while (1)
{
LedOn(PORT_GROUP1, LED1_PIN); /*Turn on Led1*/
Delay(0x28FFFF);
LedOff(PORT_GROUP2, LED1); /*Turn Off Led1*/
Delay(0x28FFFF);
}
}
或是:
int main(void)
{
LedInit(PORT_GROUP1, LED1_PINN);
LedOn(PORT_GROUP1, LED1_PIN);
while (1)
{
LedBlink(PORT_GROUP1, LED2_PIN); /*link Led1*/
Delay(0x28FFFF);
}
}
若以RT-Thread为工具,则直接以创建新项目的形式即可生成,其程序如下:
#include <stdint.h>
#include <rtthread.h>
#include <rtdevice.h>
/* defined the LED1 pin: PB5 */
#define LED1_PIN 57
int main(void)
{
uint32_t Speed = 200;
/* set LED1 pin mode to output */
rt_pin_mode(LED1_PIN, PIN_MODE_OUTPUT);
while (1)
{
rt_pin_write(LED1_PIN, PIN_LOW);
rt_thread_mdelay(Speed);
rt_pin_write(LED1_PIN, PIN_HIGH);
}
}
程序看上去还是比较简洁的,唯一需要指出的是尽管该程序有效,但其引脚的定义是不准确的,它使用的是芯片定义是64PIN的,而开发板实际使用的芯片是100PIN的,因此所使用的引脚编号也是需要修改的,应是67。
|