jinglixixi 发表于 2022-1-23 12:37

【国民技术N32G457评测】两种N32G457开发工具及点灯程序的比较

<p style="text-align:justify">对N32G457开发板进行开发可使用MDK、IAR及RT-Thread等工具,下面仅以MDK和RT-Thread这两种工具为例,对点亮板载LED灯的程序变化加以比较。</p>

<p>若以MDK为工具,则可以直接在Nationstech.N32G45x_Library.2.0.0的压缩包中找到例程LedBlink,其程序如下:</p>

<pre>
<code class="language-cpp">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-&gt;POD&amp;=~(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-&gt;POD&amp;=~(LED3_PIN),then PORT_GROUP2-&gt;POD|=(LED4_PIN|LED5_PIN);*/
        LedOnOff(PORT_GROUP2, (LED3_PIN &lt;&lt; 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 &gt; 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 &lt;= 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, &amp;GPIO_InitStructure);
    }
}

void LedOn(GPIO_Module* GPIOx, uint16_t Pin)
{
    GPIOx-&gt;PBSC = Pin;
}

void LedOff(GPIO_Module* GPIOx, uint16_t Pin)
{
    GPIOx-&gt;PBC = Pin;
}

void LedOnOff(GPIO_Module* GPIOx, uint32_t Pin)
{
    GPIOx-&gt;PBSC = Pin;
}

void LedBlink(GPIO_Module* GPIOx, uint16_t Pin)
{
    GPIOx-&gt;POD ^= Pin;
}</code></pre>

<p>相应的引脚定义为:</p>

<p>/*Led1-PB10,Led2-PB15,Led3-PA9,Led4-PA5,Led5-PA6*/</p>

<p>#define PORT_GROUP1 GPIOB</p>

<p>#define PORT_GROUP2 GPIOA</p>

<p>#define LED1_PORT&nbsp;&nbsp; PORT_GROUP1</p>

<p>#define LED2_PORT&nbsp;&nbsp; PORT_GROUP1</p>

<p>#define LED3_PORT&nbsp;&nbsp; PORT_GROUP2</p>

<p>#define LED4_PORT&nbsp;&nbsp; PORT_GROUP2</p>

<p>#define LED5_PORT&nbsp;&nbsp; PORT_GROUP2</p>

<p>#define LED1_PIN&nbsp;&nbsp;&nbsp; GPIO_PIN_10</p>

<p>#define LED2_PIN&nbsp;&nbsp;&nbsp; GPIO_PIN_15</p>

<p>#define LED3_PIN&nbsp;&nbsp;&nbsp; GPIO_PIN_9</p>

<p>#define LED4_PIN&nbsp;&nbsp;&nbsp; GPIO_PIN_5</p>

<p>#define LED5_PIN&nbsp;&nbsp;&nbsp; GPIO_PIN_6</p>

<p>&nbsp;</p>

<p>程序看起来很臃肿,其功能就是控制 LED(D1) 常亮,PB15 控制 LED(D2) 闪烁,PA5(D3)、PA6(D4)、PA9(D5) 电平翻转。</p>

<p>归纳之后,其操作无外乎对LED相关引脚的初始化及循环体内的高低电平切换。</p>

<p>但该程序是基于N32G457QE_EVB开发板的,在编译时需改为N32G457VEL7,此外对引脚的定义也需进行修改。</p>

<p>若只是让LED(D1)产生闪烁效果,主程序则可以改为如下内容:</p>

<pre>
<code class="language-cpp">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);
    }
}
</code></pre>

<p>或是:</p>

<pre>
<code class="language-cpp">int main(void)
{
    LedInit(PORT_GROUP1, LED1_PINN);
    LedOn(PORT_GROUP1, LED1_PIN);
    while (1)
    {
        LedBlink(PORT_GROUP1, LED2_PIN);    /*link Led1*/
        Delay(0x28FFFF);
    }
}</code></pre>

<p>&nbsp;</p>

<p>若以RT-Thread为工具,则直接以创建新项目的形式即可生成,其程序如下:</p>

<pre>
<code class="language-cpp">#include &lt;stdint.h&gt;
#include &lt;rtthread.h&gt;
#include &lt;rtdevice.h&gt;
/* 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);
    }
}</code></pre>

<p>程序看上去还是比较简洁的,唯一需要指出的是尽管该程序有效,但其引脚的定义是不准确的,它使用的是芯片定义是64PIN的,而开发板实际使用的芯片是100PIN的,因此所使用的引脚编号也是需要修改的,应是67。</p>

Jacktang 发表于 2022-1-23 22:39

<p>测评的很好。</p>

<p>开发板实际使用的芯片是100PIN的,例程的引脚的定义是60PIN的,例程这也太不准确了。</p>

nmg 发表于 2022-1-24 10:26

<p>收到,论坛这边会反馈给国民技术~</p>

freebsder 发表于 2022-1-24 21:14

<p>这颗芯片也是仿STM32的吧?看函数名称太像了。</p>
页: [1]
查看完整版本: 【国民技术N32G457评测】两种N32G457开发工具及点灯程序的比较