lugl4313820 发表于 2023-4-28 08:09

【国民技术N32WB031_STB开发板评测】GPIO之按键与LED灯

<p><a href="https://bbs.eeworld.com.cn/thread-1241678-1-1.html">【国民技术N32WB031_STB开发板评测】创建MDK模版&nbsp;</a></p>

<p>1、在这篇创建的基础上,复制这个模版,重命名为N32WB_KEY。并在bsp文件夹下新建key、led两个文件夹,添加好key.c、key.h、led.c、led.h。目录如下:</p>

<p>├─key<br />
│ &nbsp; &nbsp; &nbsp;key.c<br />
│ &nbsp; &nbsp; &nbsp;key.h<br />
├─led<br />
│ &nbsp; &nbsp; &nbsp;led.c<br />
│ &nbsp; &nbsp; &nbsp;led.h</p>

<p>2、用mdk打开工程,把创建bsp/LED\bsp/key分组,并把.c\.h添加进工程:</p>

<p></p>

<p> &nbsp; &nbsp;3、led.c程序如下:</p>

<pre>
<code>#include "led.h"


/**
* @brief Configures LED GPIO.
* @param GPIOx x can be A to G to select the GPIO port.
* @param Pin This parameter can be GPIO_PIN_0~GPIO_PIN_15.
*/
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
    {
      return;
    }

    /* Configure the GPIO pin */
    if (Pin &lt;= GPIO_PIN_ALL)
    {
      GPIO_InitStruct(&amp;GPIO_InitStructure);
      GPIO_InitStructure.Pin = Pin;
      GPIO_InitStructure.GPIO_Mode = GPIO_MODE_OUTPUT_PP;
      GPIO_InitPeripheral(GPIOx, &amp;GPIO_InitStructure);
    }
}

/**
* @briefTurns selected Led on.
* @param GPIOx x can be A to G to select the GPIO port.
* @param Pin This parameter can be GPIO_PIN_0~GPIO_PIN_15.
*/
void LedOn(GPIO_Module *GPIOx, uint16_t Pin)
{
    GPIO_SetBits(GPIOx, Pin);
}

/**
* @briefTurns selected Led Off.
* @param GPIOx x can be A to G to select the GPIO port.
* @param Pin This parameter can be GPIO_PIN_0~GPIO_PIN_15.
*/
void LedOff(GPIO_Module* GPIOx, uint16_t Pin)
{
    GPIO_ResetBits(GPIOx, Pin);
}

/**
* @briefToggles the selected Led.
* @param GPIOx x can be A to G to select the GPIO port.
* @param Pin This parameter can be GPIO_PIN_0~GPIO_PIN_15.
*/
void LedBlink(GPIO_Module* GPIOx, uint16_t Pin)
{
    GPIO_TogglePin(GPIOx, Pin);
}

</code></pre>

<p>led.h:</p>

<pre>
<code>#ifndef __LED_H__
#define __LED_H__
#include "n32wb03x.h"
void LedInit(GPIO_Module* GPIOx, uint16_t Pin);
void LedOn(GPIO_Module *GPIOx, uint16_t Pin);
void LedOff(GPIO_Module* GPIOx, uint16_t Pin);
void LedBlink(GPIO_Module* GPIOx, uint16_t Pin);

#endif
</code></pre>

<p>key.c:</p>

<pre>
<code>#include "key.h"
#include "led.h"
#include "main.h"

/**
* @briefConfigures key port.
* @param GPIOx x can be A to G to select the GPIO port.
* @param Pin This parameter can be GPIO_PIN_0~GPIO_PIN_15.
*/
void KeyInputExtiInit(GPIO_Module* GPIOx, uint16_t Pin)
{
    GPIO_InitType GPIO_InitStructure;
    EXTI_InitType EXTI_InitStructure;
    NVIC_InitType NVIC_InitStructure;

    /* Check the parameters */
    assert_param(IS_GPIO_ALL_PERIPH(GPIOx));

    /* Enable the GPIO Clock */
    if (GPIOx == GPIOA)
    {
      RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOA | RCC_APB2_PERIPH_AFIO, ENABLE);
    }
    else if (GPIOx == GPIOB)
    {
      RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOB | RCC_APB2_PERIPH_AFIO, ENABLE);
    }
    else
    {
      return;
    }

    /*Configure the GPIO pin as input floating*/
    if (Pin &lt;= GPIO_PIN_ALL)
    {
      GPIO_InitStruct(&amp;GPIO_InitStructure);
      GPIO_InitStructure.Pin          = Pin;
      GPIO_InitStructure.GPIO_Pull    = GPIO_PULL_UP;
      GPIO_InitPeripheral(GPIOx, &amp;GPIO_InitStructure);
    }

    /*Configure key EXTI Line to key input Pin*/
    GPIO_ConfigEXTILine(KEY_INPUT_PORT_SOURCE, KEY_INPUT_PIN_SOURCE);

    /*Configure key EXTI line*/
    EXTI_InitStructure.EXTI_Line    = KEY_INPUT_EXTI_LINE;
    EXTI_InitStructure.EXTI_Mode    = EXTI_Mode_Interrupt;
    EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; // EXTI_Trigger_Rising;
    EXTI_InitStructure.EXTI_LineCmd = ENABLE;
    EXTI_InitPeripheral(&amp;EXTI_InitStructure);

    /*Set key input interrupt priority*/
    NVIC_InitStructure.NVIC_IRQChannel                   = KEY_INPUT_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPriority         = 1;
    NVIC_InitStructure.NVIC_IRQChannelCmd                = ENABLE;
    NVIC_Init(&amp;NVIC_InitStructure);
}


</code></pre>

<p>ke.y:</p>

<pre>
<code>#ifndef __KEY_H__
#define __KEY_H__
#include "n32wb03x.h"


void KeyInputExtiInit(GPIO_Module* GPIOx, uint16_t Pin);

#endif
</code></pre>

<p>4、在n32wb03_it.c中添加按键中断程序:</p>

<pre>
<code>void EXTI2_3_IRQHandler(void)
{
    if (RESET != EXTI_GetITStatus(KEY_INPUT_EXTI_LINE))
    {
      if(GPIO_ReadInputDataBit(KEY_INPUT_PORT, KEY_INPUT_PIN) == RESET)
      {
            Delay(10);
            if(GPIO_ReadInputDataBit(KEY_INPUT_PORT, KEY_INPUT_PIN) == RESET)
            {
                LedBlink(LED2_PORT, LED2_PIN);
            }
      }
      EXTI_ClrITPendBit(KEY_INPUT_EXTI_LINE);
    }
}</code></pre>

<p>【注】这里只是演示所用,不能在实际项目中运行</p>

<p>然后在主程序里初始化key\led</p>

<pre>
<code>#include "led.h"
#include "key.h"

int main(void)
{

    LedInit(LED1_PORT, LED1_PIN);
    LedInit(LED2_PORT, LED2_PIN);
   

    LedOn(LED1_PORT, LED1_PIN);
    LedOn(LED2_PORT, LED2_PIN);
                KeyInputExtiInit(KEY_INPUT_PORT, KEY_INPUT_PIN);
    while (1)
    {
      LedBlink(LED1_PORT, LED1_PIN);


      Delay(0x28FFFF);


    }
}</code></pre>

<p>这样就可以实现LED1的周期闪烁,当按下Button1时翻转LED2【注】由于在中断中用了延时,所以效果不是很好,等讲了定时器,会进行优化。</p>

lugl4313820 发表于 2023-4-28 08:11

<div>附上工程,大家可以取用,如果有不足之处,还请指出。多谢!</div>

wangerxian 发表于 2023-4-29 14:33

<p>后面是不是准备测评蓝牙了~</p>

lugl4313820 发表于 2023-4-29 17:03

wangerxian 发表于 2023-4-29 14:33
后面是不是准备测评蓝牙了~

<p>蓝牙是作业之一,我还是想能做出什么样的功耗的东西出来。</p>

led2015 发表于 2023-4-29 20:01

<p>期待一些基本作业,有空再有一点小实验就更好了</p>

lugl4313820 发表于 2023-4-29 20:58

led2015 发表于 2023-4-29 20:01
期待一些基本作业,有空再有一点小实验就更好了

<p>主要是水平有限呀,还得继续操练。</p>

J1anbean 发表于 2023-5-5 11:56

<p><font _msthash="737" _mstmutation="1" _msttexthash="549356743">你好 我想问一下为啥我用国民的LED实例 在里面添加按键控制LED 按键没反应 只能在上电一瞬间才监测按键是否按下</font></p>

J1anbean 发表于 2023-5-5 11:56

<p>能加个WX探讨下 帮我看下吗 我小白</p>

lugl4313820 发表于 2023-5-5 13:34

J1anbean 发表于 2023-5-5 11:56
你好 我想问一下为啥我用国民的LED实例 在里面添加按键控制LED 按键没反应 只能在上电一瞬间才监测按键是否 ...

<p>你可以把代码帖到我下面,我帮看看怎么回事。</p>

lugl4313820 发表于 2023-5-5 15:19

J1anbean 发表于 2023-5-5 11:56
能加个WX探讨下 帮我看下吗 我小白

<p>可以的,你发私信我wx吧。</p>

J1anbean 发表于 2023-5-5 15:33

<p _msthash="786" _msttexthash="910663">#ifndef __MAIN_H__<br />
#define __MAIN_H__</p>

<p _msthash="785" _msttexthash="7549672">#ifdef __cplusplus<br _istranslated="1" />
extern &ldquo;C&rdquo; {<br _istranslated="1" />
#endif</p>

<p _msthash="784" _msttexthash="4516915">#include &ldquo;N32G031.H&rdquo;</p>

<p _msthash="783" _msttexthash="59995468">/*LED1-PB1,Led2-PB6,Led3-PB7*/<br _istranslated="1" />
#define PORT_GROUP GPIOB<br _istranslated="1" />
#define LED1_PORT PORT_GROUP<br _istranslated="1" />
#define LED2_PORT PORT_GROUP<br _istranslated="1" />
#define LED3_PORT PORT_GROUP#define LED1_PIN GPIO_PIN_1<br _istranslated="1" />
<br _istranslated="1" />
#define LED2_PIN GPIO_PIN_6#define LED3_PIN GPIO_PIN_7</p>

<p _msthash="782" _msttexthash="25882545">#define KEY_INPUT_PORT GPIOA<br _istranslated="1" />
#define KEY_INPUT_PIN GPIO_PIN_5<br _istranslated="1" />
#define KEY_INPUT_EXTI_LINE EXTI_LINE5#define KEY_INPUT_PORT_SOURCE GPIOA_PORT_SOURCE<br _istranslated="1" />
<br _istranslated="1" />
#define KEY_INPUT_PIN_SOURCE GPIO_PIN_SOURCE5#define KEY_INPUT_IRQn EXTI4_15_IRQn</p>

<p _msthash="781" _msttexthash="3384467359">无效延迟(uint32_t计数);<br _istranslated="1" />
void LedInit(GPIO_Module* GPIOx, uint16_t Pin);<br _istranslated="1" />
void LedOn(GPIO_Module *GPIOx, uint16_t Pin);<br _istranslated="1" />
void LedOff(GPIO_Module* GPIOx, uint16_t Pin);<br _istranslated="1" />
void LedBlink(GPIO_Module* GPIOx, uint16_t Pin);<br _istranslated="1" />
void LedBreath(GPIO_Module* GPIOx, uint16_t Pin);<br _istranslated="1" />
void KeyInputExtiInit(GPIO_Module* GPIOx, uint16_t Pin);<br _istranslated="1" />
//uint8_t Key_Scan(GPIO_Module *GPIOx,uint16_t pin);<br _istranslated="1" />
#ifdef __cplusplus<br _istranslated="1" />
}#endif</p>

<p _msthash="780" _msttexthash="329004">#endif /* __MAIN_H__ */</p>

J1anbean 发表于 2023-5-5 15:36

<p _msthidden="1"> &nbsp;</p>

J1anbean 发表于 2023-5-5 15:37

<p> &nbsp;</p>

J1anbean 发表于 2023-5-5 15:39

<p _msthash="882" _msttexthash="14803581">void KeyInputExtiInit(GPIO_Module* GPIOx, uint16_t Pin)<br />
{<br />
&nbsp; &nbsp; GPIO_InitType GPIO_InitStructure;<br />
&nbsp; &nbsp; EXTI_InitType EXTI_InitStructure;<br />
&nbsp; &nbsp; NVIC_InitType NVIC_InitStructure;</p>

<p _msthash="881" _msttexthash="2500186">&nbsp; &nbsp; /* Check the parameters */<br />
&nbsp; &nbsp; assert_param(IS_GPIO_ALL_PERIPH(GPIOx));</p>

<p><font _msthash="880" _mstmutation="1" _msttexthash="94670121">&nbsp; &nbsp; /* Enable the GPIO Clock */<br _mstmutation="1" />
&nbsp; &nbsp; if (GPIOx == GPIOA)<br _mstmutation="1" />
&nbsp; &nbsp; {<br _mstmutation="1" />
&nbsp; &nbsp; &nbsp; &nbsp; RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOA | RCC_APB2_PERIPH_AFIO, ENABLE);<br _mstmutation="1" />
&nbsp; &nbsp; }<br _mstmutation="1" />
&nbsp; &nbsp; else if (GPIOx == GPIOB)<br _mstmutation="1" />
&nbsp; &nbsp; {<br _mstmutation="1" />
&nbsp; &nbsp; &nbsp; &nbsp; RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOB | RCC_APB2_PERIPH_AFIO, ENABLE);<br _mstmutation="1" />
&nbsp; &nbsp; }<br _mstmutation="1" />
&nbsp; &nbsp; else if (GPIOx == GPIOC)<br _mstmutation="1" />
&nbsp; &nbsp; {<br _mstmutation="1" />
&nbsp; &nbsp; &nbsp; &nbsp; RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOC | RCC_APB2_PERIPH_AFIO, ENABLE);<br _mstmutation="1" />
&nbsp; &nbsp; }<br _mstmutation="1" />
&nbsp; &nbsp; else if (GPIOx == GPIOF)<br _mstmutation="1" />
&nbsp; &nbsp; {<br _mstmutation="1" />
&nbsp; &nbsp; &nbsp; &nbsp; RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOF | RCC_APB2_PERIPH_AFIO, ENABLE);<br _mstmutation="1" />
&nbsp; &nbsp; }<br _mstmutation="1" />
&nbsp; &nbsp; else<br _mstmutation="1" />
&nbsp; &nbsp; {<br _mstmutation="1" />
&nbsp; &nbsp; &nbsp; &nbsp; return;</font><br />
&nbsp; &nbsp; }</p>

<p><font _msthash="879" _mstmutation="1" _msttexthash="28323880">&nbsp; &nbsp; /*Configure the GPIO pin as input floating*/<br _mstmutation="1" />
&nbsp; &nbsp; if (Pin &lt;= GPIO_PIN_ALL)<br _mstmutation="1" />
&nbsp; &nbsp; {<br _mstmutation="1" />
&nbsp; &nbsp; &nbsp; &nbsp; GPIO_InitStruct(&amp;GPIO_InitStructure);<br _mstmutation="1" />
&nbsp; &nbsp; &nbsp; &nbsp; GPIO_InitStructure.Pin &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;= Pin;<br _mstmutation="1" />
&nbsp; &nbsp; &nbsp; &nbsp; GPIO_InitStructure.GPIO_Pull &nbsp; &nbsp;= GPIO_PULL_UP;<br _mstmutation="1" />
&nbsp; &nbsp; &nbsp; &nbsp; GPIO_InitPeripheral(GPIOx, &amp;GPIO_InitStructure);</font><br />
&nbsp; &nbsp; }</p>

<p _msthash="878" _msttexthash="6209632">&nbsp; &nbsp; /*Configure key EXTI Line to key input Pin*/<br />
&nbsp; &nbsp; GPIO_ConfigEXTILine(KEY_INPUT_PORT_SOURCE, KEY_INPUT_PIN_SOURCE);</p>

<p _msthash="877" _msttexthash="47978840">&nbsp; &nbsp; /*Configure key EXTI line*/<br />
&nbsp; &nbsp; EXTI_InitStructure.EXTI_Line &nbsp; &nbsp;= KEY_INPUT_EXTI_LINE;<br />
&nbsp; &nbsp; EXTI_InitStructure.EXTI_Mode &nbsp; &nbsp;= EXTI_Mode_Interrupt;<br />
&nbsp; &nbsp; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; // EXTI_Trigger_Rising;<br />
&nbsp; &nbsp; EXTI_InitStructure.EXTI_LineCmd = ENABLE;<br />
&nbsp; &nbsp; EXTI_InitPeripheral(&amp;EXTI_InitStructure);</p>

<p><font _msthash="876" _mstmutation="1" _msttexthash="25658230">&nbsp; &nbsp; /*Set key input interrupt priority*/<br _mstmutation="1" />
&nbsp; &nbsp; NVIC_InitStructure.NVIC_IRQChannel &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; = KEY_INPUT_IRQn;<br _mstmutation="1" />
&nbsp; &nbsp; NVIC_InitStructure.NVIC_IRQChannelPriority &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; = 1;<br _mstmutation="1" />
&nbsp; &nbsp; NVIC_InitStructure.NVIC_IRQChannelCmd &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;= ENABLE;<br _mstmutation="1" />
&nbsp; &nbsp; NVIC_Init(&amp;NVIC_InitStructure);</font><br />
}</p>

<p>/**<br />
<font _msthash="875" _mstmutation="1" _msttexthash="8780603">&nbsp;* @brief &nbsp;Inserts a delay time.<br _mstmutation="1" />
&nbsp;* @param count specifies the delay time length.<br _mstmutation="1" />
&nbsp;*/<br _mstmutation="1" />
void Delay(uint32_t count)<br _mstmutation="1" />
{<br _mstmutation="1" />
&nbsp; &nbsp; for (; count &gt; 0; count--)</font><br />
&nbsp; &nbsp; &nbsp; &nbsp; ;<br />
}</p>

<p>/**<br />
<font _msthash="874" _mstmutation="1" _msttexthash="24134292">&nbsp;* @brief &nbsp;Configures LED GPIO.<br _mstmutation="1" />
&nbsp;* @param GPIOx x can be A to G to select the GPIO port.<br _mstmutation="1" />
&nbsp;* @param Pin This parameter can be GPIO_PIN_0~GPIO_PIN_15.<br _mstmutation="1" />
&nbsp;*/<br _mstmutation="1" />
void LedInit(GPIO_Module* GPIOx, uint16_t Pin)<br _mstmutation="1" />
{<br _mstmutation="1" />
&nbsp; &nbsp; GPIO_InitType GPIO_InitStructure;</font></p>

<p _msthash="873" _msttexthash="2500186">&nbsp; &nbsp; /* Check the parameters */<br />
&nbsp; &nbsp; assert_param(IS_GPIO_ALL_PERIPH(GPIOx));</p>

<p><font _msthash="872" _mstmutation="1" _msttexthash="61623991">&nbsp; &nbsp; /* Enable the GPIO Clock */<br _mstmutation="1" />
&nbsp; &nbsp; if (GPIOx == GPIOA)<br _mstmutation="1" />
&nbsp; &nbsp; {<br _mstmutation="1" />
&nbsp; &nbsp; &nbsp; &nbsp; RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOA, ENABLE);<br _mstmutation="1" />
&nbsp; &nbsp; }<br _mstmutation="1" />
&nbsp; &nbsp; else if (GPIOx == GPIOB)<br _mstmutation="1" />
&nbsp; &nbsp; {<br _mstmutation="1" />
&nbsp; &nbsp; &nbsp; &nbsp; RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOB, ENABLE);<br _mstmutation="1" />
&nbsp; &nbsp; }<br _mstmutation="1" />
&nbsp; &nbsp; else if (GPIOx == GPIOC)<br _mstmutation="1" />
&nbsp; &nbsp; {<br _mstmutation="1" />
&nbsp; &nbsp; &nbsp; &nbsp; RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOC, ENABLE);<br _mstmutation="1" />
&nbsp; &nbsp; }<br _mstmutation="1" />
&nbsp; &nbsp; else if (GPIOx == GPIOF)<br _mstmutation="1" />
&nbsp; &nbsp; {<br _mstmutation="1" />
&nbsp; &nbsp; &nbsp; &nbsp; RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOF, ENABLE);<br _mstmutation="1" />
&nbsp; &nbsp; }<br _mstmutation="1" />
&nbsp; &nbsp; else<br _mstmutation="1" />
&nbsp; &nbsp; {<br _mstmutation="1" />
&nbsp; &nbsp; &nbsp; &nbsp; return;</font><br />
&nbsp; &nbsp; }</p>

<p><font _msthash="871" _mstmutation="1" _msttexthash="26029159">&nbsp; &nbsp; /* Configure the GPIO pin */<br _mstmutation="1" />
&nbsp; &nbsp; if (Pin &lt;= GPIO_PIN_ALL)<br _mstmutation="1" />
&nbsp; &nbsp; {<br _mstmutation="1" />
&nbsp; &nbsp; &nbsp; &nbsp; GPIO_InitStruct(&amp;GPIO_InitStructure);<br _mstmutation="1" />
&nbsp; &nbsp; &nbsp; &nbsp; GPIO_InitStructure.Pin = Pin;<br _mstmutation="1" />
&nbsp; &nbsp; &nbsp; &nbsp; GPIO_InitStructure.GPIO_Mode = GPIO_MODE_OUTPUT_PP;<br _mstmutation="1" />
&nbsp; &nbsp; &nbsp; &nbsp; GPIO_InitPeripheral(GPIOx, &amp;GPIO_InitStructure);</font><br />
&nbsp; &nbsp; }<br />
}</p>

<p>/**<br />
<font _msthash="870" _mstmutation="1" _msttexthash="21246212">&nbsp;* @brief &nbsp;Turns selected Led on.<br _mstmutation="1" />
&nbsp;* @param GPIOx x can be A to G to select the GPIO port.<br _mstmutation="1" />
&nbsp;* @param Pin This parameter can be GPIO_PIN_0~GPIO_PIN_15.<br _mstmutation="1" />
&nbsp;*/<br _mstmutation="1" />
void LedOn(GPIO_Module *GPIOx, uint16_t Pin)<br _mstmutation="1" />
{<br _mstmutation="1" />
&nbsp; &nbsp; GPIO_SetBits(GPIOx, Pin);</font><br />
}</p>

<p>/**<br />
<font _msthash="869" _mstmutation="1" _msttexthash="22227218">&nbsp;* @brief &nbsp;Turns selected Led Off.<br _mstmutation="1" />
&nbsp;* @param GPIOx x can be A to G to select the GPIO port.<br _mstmutation="1" />
&nbsp;* @param Pin This parameter can be GPIO_PIN_0~GPIO_PIN_15.<br _mstmutation="1" />
&nbsp;*/<br _mstmutation="1" />
void LedOff(GPIO_Module* GPIOx, uint16_t Pin)<br _mstmutation="1" />
{<br _mstmutation="1" />
&nbsp; &nbsp; GPIO_ResetBits(GPIOx, Pin);</font><br />
}</p>

<p>/**<br />
<font _msthash="868" _mstmutation="1" _msttexthash="23200853">&nbsp;* @brief &nbsp;Toggles the selected Led.<br _mstmutation="1" />
&nbsp;* @param GPIOx x can be A to G to select the GPIO port.<br _mstmutation="1" />
&nbsp;* @param Pin This parameter can be GPIO_PIN_0~GPIO_PIN_15.<br _mstmutation="1" />
&nbsp;*/<br _mstmutation="1" />
void LedBlink(GPIO_Module* GPIOx, uint16_t Pin)<br _mstmutation="1" />
{<br _mstmutation="1" />
&nbsp; &nbsp; GPIO_TogglePin(GPIOx, Pin);</font><br />
}</p>

<p><font _msthash="867" _mstmutation="1" _msttexthash="132487511">void LedBreath(GPIO_Module* GPIOx, uint16_t Pin)<br _mstmutation="1" />
{<br _mstmutation="1" />
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;unsigned char i = 0;<br _mstmutation="1" />
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;unsigned int t = 1;<br _mstmutation="1" />
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;unsigned char flag = 1;<br _mstmutation="1" />
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; while(1)<br _mstmutation="1" />
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;{&nbsp;<br _mstmutation="1" />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; if(flag == 1) //LED??<br _mstmutation="1" />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;{<br _mstmutation="1" />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;for(i=0;i&lt;10;i++)<br _mstmutation="1" />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;{<br _mstmutation="1" />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;GPIO_ResetBits(PORT_GROUP, LED1_PIN | LED2_PIN | LED3_PIN);&nbsp;<br _mstmutation="1" />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;Delay(t);<br _mstmutation="1" />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;GPIO_SetBits(PORT_GROUP,LED1_PIN | LED2_PIN | LED3_PIN);&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;//LED??<br _mstmutation="1" />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;Delay(1001-t);<br _mstmutation="1" />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}<br _mstmutation="1" />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;t++;&nbsp;&nbsp; &nbsp;<br _mstmutation="1" />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if(t == 1000)<br _mstmutation="1" />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;{<br _mstmutation="1" />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;flag = 0;<br _mstmutation="1" />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}<br _mstmutation="1" />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}<br _mstmutation="1" />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if(flag == 0) //LED????<br _mstmutation="1" />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;{<br _mstmutation="1" />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;for(i=0;i&lt;10;i++)<br _mstmutation="1" />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;{&nbsp;&nbsp; &nbsp;<br _mstmutation="1" />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;GPIO_ResetBits(PORT_GROUP, LED1_PIN | LED2_PIN | LED3_PIN); &nbsp;&nbsp; &nbsp;//LED??<br _mstmutation="1" />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;Delay(t);<br _mstmutation="1" />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;GPIO_SetBits(PORT_GROUP,LED1_PIN | LED2_PIN | LED3_PIN);&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;//LED??<br _mstmutation="1" />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;Delay(1001-t);<br _mstmutation="1" />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}<br _mstmutation="1" />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;t--;<br _mstmutation="1" />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if(t == 1)<br _mstmutation="1" />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;{<br _mstmutation="1" />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;flag = 1;</font><br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}<br />
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}<br />
&nbsp;&nbsp; &nbsp;}<br />
}</p>

<p>/**<br />
<font _msthash="866" _mstmutation="1" _msttexthash="1146381002">* @brief 用户断言失败的功能。<br _istranslated="1" _mstmutation="1" />
* @param文件 失败的调用的名称。<br _istranslated="1" _mstmutation="1" />
* @param行 失败的呼叫的源线路号。<br _istranslated="1" _mstmutation="1" />
*/<br _istranslated="1" _mstmutation="1" />
#ifdef USE_FULL_ASSERT<br _istranslated="1" _mstmutation="1" />
void assert_failed(const uint8_t* expr, const uint8_t* file, uint32_t line){<br _istranslated="1" _mstmutation="1" />
while (1)</font><br _istranslated="1" _mstmutation="1" />
<br />
&nbsp; &nbsp; {<br />
&nbsp; &nbsp; }<br />
}</p>

J1anbean 发表于 2023-5-5 15:40

<p>帮忙看看 楼主 下面是LED KEY初始化 和LED 点亮熄灭的函数 最上面是头文件和宏定义&nbsp; 中间是main函数</p>

lugl4313820 发表于 2023-5-5 15:59

<p>你如果要想检测按键,你这样写阻塞式的delay是不行的,要用状态机来写才行。</p>

J1anbean 发表于 2023-5-5 16:06

<p><font _msthash="1018" _mstmutation="1" _msttexthash="185880812">就算我不这么写延迟按键按下去也没反应 不知道哪里有问题</font></p>

J1anbean 发表于 2023-5-5 16:10

<p>可以看看代码打包发你了</p>

J1anbean 发表于 2023-5-5 16:11

<div><br />
<br />
&nbsp;</div>

lugl4313820 发表于 2023-5-5 16:17

J1anbean 发表于 2023-5-5 16:10
可以看看代码打包发你了

<p>好的,晚上回去修改发给你。你要去学习一下状态机的知识,这样才行。</p>
页: [1] 2
查看完整版本: 【国民技术N32WB031_STB开发板评测】GPIO之按键与LED灯