【国民技术N32WB031_STB开发板评测】GPIO之按键与LED灯
<p><a href="https://bbs.eeworld.com.cn/thread-1241678-1-1.html">【国民技术N32WB031_STB开发板评测】创建MDK模版 </a></p><p>1、在这篇创建的基础上,复制这个模版,重命名为N32WB_KEY。并在bsp文件夹下新建key、led两个文件夹,添加好key.c、key.h、led.c、led.h。目录如下:</p>
<p>├─key<br />
│ key.c<br />
│ key.h<br />
├─led<br />
│ led.c<br />
│ led.h</p>
<p>2、用mdk打开工程,把创建bsp/LED\bsp/key分组,并把.c\.h添加进工程:</p>
<p></p>
<p> 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 <= GPIO_PIN_ALL)
{
GPIO_InitStruct(&GPIO_InitStructure);
GPIO_InitStructure.Pin = Pin;
GPIO_InitStructure.GPIO_Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitPeripheral(GPIOx, &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 <= GPIO_PIN_ALL)
{
GPIO_InitStruct(&GPIO_InitStructure);
GPIO_InitStructure.Pin = Pin;
GPIO_InitStructure.GPIO_Pull = GPIO_PULL_UP;
GPIO_InitPeripheral(GPIOx, &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(&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(&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>
<div>附上工程,大家可以取用,如果有不足之处,还请指出。多谢!</div>
<p>后面是不是准备测评蓝牙了~</p>
wangerxian 发表于 2023-4-29 14:33
后面是不是准备测评蓝牙了~
<p>蓝牙是作业之一,我还是想能做出什么样的功耗的东西出来。</p>
<p>期待一些基本作业,有空再有一点小实验就更好了</p>
led2015 发表于 2023-4-29 20:01
期待一些基本作业,有空再有一点小实验就更好了
<p>主要是水平有限呀,还得继续操练。</p>
<p><font _msthash="737" _mstmutation="1" _msttexthash="549356743">你好 我想问一下为啥我用国民的LED实例 在里面添加按键控制LED 按键没反应 只能在上电一瞬间才监测按键是否按下</font></p>
<p>能加个WX探讨下 帮我看下吗 我小白</p>
J1anbean 发表于 2023-5-5 11:56
你好 我想问一下为啥我用国民的LED实例 在里面添加按键控制LED 按键没反应 只能在上电一瞬间才监测按键是否 ...
<p>你可以把代码帖到我下面,我帮看看怎么回事。</p>
J1anbean 发表于 2023-5-5 11:56
能加个WX探讨下 帮我看下吗 我小白
<p>可以的,你发私信我wx吧。</p>
<p _msthash="786" _msttexthash="910663">#ifndef __MAIN_H__<br />
#define __MAIN_H__</p>
<p _msthash="785" _msttexthash="7549672">#ifdef __cplusplus<br _istranslated="1" />
extern “C” {<br _istranslated="1" />
#endif</p>
<p _msthash="784" _msttexthash="4516915">#include “N32G031.H”</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>
<p _msthidden="1"> </p>
<p> </p>
<p _msthash="882" _msttexthash="14803581">void KeyInputExtiInit(GPIO_Module* GPIOx, uint16_t Pin)<br />
{<br />
GPIO_InitType GPIO_InitStructure;<br />
EXTI_InitType EXTI_InitStructure;<br />
NVIC_InitType NVIC_InitStructure;</p>
<p _msthash="881" _msttexthash="2500186"> /* Check the parameters */<br />
assert_param(IS_GPIO_ALL_PERIPH(GPIOx));</p>
<p><font _msthash="880" _mstmutation="1" _msttexthash="94670121"> /* Enable the GPIO Clock */<br _mstmutation="1" />
if (GPIOx == GPIOA)<br _mstmutation="1" />
{<br _mstmutation="1" />
RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOA | RCC_APB2_PERIPH_AFIO, ENABLE);<br _mstmutation="1" />
}<br _mstmutation="1" />
else if (GPIOx == GPIOB)<br _mstmutation="1" />
{<br _mstmutation="1" />
RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOB | RCC_APB2_PERIPH_AFIO, ENABLE);<br _mstmutation="1" />
}<br _mstmutation="1" />
else if (GPIOx == GPIOC)<br _mstmutation="1" />
{<br _mstmutation="1" />
RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOC | RCC_APB2_PERIPH_AFIO, ENABLE);<br _mstmutation="1" />
}<br _mstmutation="1" />
else if (GPIOx == GPIOF)<br _mstmutation="1" />
{<br _mstmutation="1" />
RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOF | RCC_APB2_PERIPH_AFIO, ENABLE);<br _mstmutation="1" />
}<br _mstmutation="1" />
else<br _mstmutation="1" />
{<br _mstmutation="1" />
return;</font><br />
}</p>
<p><font _msthash="879" _mstmutation="1" _msttexthash="28323880"> /*Configure the GPIO pin as input floating*/<br _mstmutation="1" />
if (Pin <= GPIO_PIN_ALL)<br _mstmutation="1" />
{<br _mstmutation="1" />
GPIO_InitStruct(&GPIO_InitStructure);<br _mstmutation="1" />
GPIO_InitStructure.Pin = Pin;<br _mstmutation="1" />
GPIO_InitStructure.GPIO_Pull = GPIO_PULL_UP;<br _mstmutation="1" />
GPIO_InitPeripheral(GPIOx, &GPIO_InitStructure);</font><br />
}</p>
<p _msthash="878" _msttexthash="6209632"> /*Configure key EXTI Line to key input Pin*/<br />
GPIO_ConfigEXTILine(KEY_INPUT_PORT_SOURCE, KEY_INPUT_PIN_SOURCE);</p>
<p _msthash="877" _msttexthash="47978840"> /*Configure key EXTI line*/<br />
EXTI_InitStructure.EXTI_Line = KEY_INPUT_EXTI_LINE;<br />
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;<br />
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; // EXTI_Trigger_Rising;<br />
EXTI_InitStructure.EXTI_LineCmd = ENABLE;<br />
EXTI_InitPeripheral(&EXTI_InitStructure);</p>
<p><font _msthash="876" _mstmutation="1" _msttexthash="25658230"> /*Set key input interrupt priority*/<br _mstmutation="1" />
NVIC_InitStructure.NVIC_IRQChannel = KEY_INPUT_IRQn;<br _mstmutation="1" />
NVIC_InitStructure.NVIC_IRQChannelPriority = 1;<br _mstmutation="1" />
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;<br _mstmutation="1" />
NVIC_Init(&NVIC_InitStructure);</font><br />
}</p>
<p>/**<br />
<font _msthash="875" _mstmutation="1" _msttexthash="8780603"> * @brief Inserts a delay time.<br _mstmutation="1" />
* @param count specifies the delay time length.<br _mstmutation="1" />
*/<br _mstmutation="1" />
void Delay(uint32_t count)<br _mstmutation="1" />
{<br _mstmutation="1" />
for (; count > 0; count--)</font><br />
;<br />
}</p>
<p>/**<br />
<font _msthash="874" _mstmutation="1" _msttexthash="24134292"> * @brief Configures LED GPIO.<br _mstmutation="1" />
* @param GPIOx x can be A to G to select the GPIO port.<br _mstmutation="1" />
* @param Pin This parameter can be GPIO_PIN_0~GPIO_PIN_15.<br _mstmutation="1" />
*/<br _mstmutation="1" />
void LedInit(GPIO_Module* GPIOx, uint16_t Pin)<br _mstmutation="1" />
{<br _mstmutation="1" />
GPIO_InitType GPIO_InitStructure;</font></p>
<p _msthash="873" _msttexthash="2500186"> /* Check the parameters */<br />
assert_param(IS_GPIO_ALL_PERIPH(GPIOx));</p>
<p><font _msthash="872" _mstmutation="1" _msttexthash="61623991"> /* Enable the GPIO Clock */<br _mstmutation="1" />
if (GPIOx == GPIOA)<br _mstmutation="1" />
{<br _mstmutation="1" />
RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOA, ENABLE);<br _mstmutation="1" />
}<br _mstmutation="1" />
else if (GPIOx == GPIOB)<br _mstmutation="1" />
{<br _mstmutation="1" />
RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOB, ENABLE);<br _mstmutation="1" />
}<br _mstmutation="1" />
else if (GPIOx == GPIOC)<br _mstmutation="1" />
{<br _mstmutation="1" />
RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOC, ENABLE);<br _mstmutation="1" />
}<br _mstmutation="1" />
else if (GPIOx == GPIOF)<br _mstmutation="1" />
{<br _mstmutation="1" />
RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOF, ENABLE);<br _mstmutation="1" />
}<br _mstmutation="1" />
else<br _mstmutation="1" />
{<br _mstmutation="1" />
return;</font><br />
}</p>
<p><font _msthash="871" _mstmutation="1" _msttexthash="26029159"> /* Configure the GPIO pin */<br _mstmutation="1" />
if (Pin <= GPIO_PIN_ALL)<br _mstmutation="1" />
{<br _mstmutation="1" />
GPIO_InitStruct(&GPIO_InitStructure);<br _mstmutation="1" />
GPIO_InitStructure.Pin = Pin;<br _mstmutation="1" />
GPIO_InitStructure.GPIO_Mode = GPIO_MODE_OUTPUT_PP;<br _mstmutation="1" />
GPIO_InitPeripheral(GPIOx, &GPIO_InitStructure);</font><br />
}<br />
}</p>
<p>/**<br />
<font _msthash="870" _mstmutation="1" _msttexthash="21246212"> * @brief Turns selected Led on.<br _mstmutation="1" />
* @param GPIOx x can be A to G to select the GPIO port.<br _mstmutation="1" />
* @param Pin This parameter can be GPIO_PIN_0~GPIO_PIN_15.<br _mstmutation="1" />
*/<br _mstmutation="1" />
void LedOn(GPIO_Module *GPIOx, uint16_t Pin)<br _mstmutation="1" />
{<br _mstmutation="1" />
GPIO_SetBits(GPIOx, Pin);</font><br />
}</p>
<p>/**<br />
<font _msthash="869" _mstmutation="1" _msttexthash="22227218"> * @brief Turns selected Led Off.<br _mstmutation="1" />
* @param GPIOx x can be A to G to select the GPIO port.<br _mstmutation="1" />
* @param Pin This parameter can be GPIO_PIN_0~GPIO_PIN_15.<br _mstmutation="1" />
*/<br _mstmutation="1" />
void LedOff(GPIO_Module* GPIOx, uint16_t Pin)<br _mstmutation="1" />
{<br _mstmutation="1" />
GPIO_ResetBits(GPIOx, Pin);</font><br />
}</p>
<p>/**<br />
<font _msthash="868" _mstmutation="1" _msttexthash="23200853"> * @brief Toggles the selected Led.<br _mstmutation="1" />
* @param GPIOx x can be A to G to select the GPIO port.<br _mstmutation="1" />
* @param Pin This parameter can be GPIO_PIN_0~GPIO_PIN_15.<br _mstmutation="1" />
*/<br _mstmutation="1" />
void LedBlink(GPIO_Module* GPIOx, uint16_t Pin)<br _mstmutation="1" />
{<br _mstmutation="1" />
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" />
unsigned char i = 0;<br _mstmutation="1" />
unsigned int t = 1;<br _mstmutation="1" />
unsigned char flag = 1;<br _mstmutation="1" />
while(1)<br _mstmutation="1" />
{ <br _mstmutation="1" />
if(flag == 1) //LED??<br _mstmutation="1" />
{<br _mstmutation="1" />
for(i=0;i<10;i++)<br _mstmutation="1" />
{<br _mstmutation="1" />
GPIO_ResetBits(PORT_GROUP, LED1_PIN | LED2_PIN | LED3_PIN); <br _mstmutation="1" />
Delay(t);<br _mstmutation="1" />
GPIO_SetBits(PORT_GROUP,LED1_PIN | LED2_PIN | LED3_PIN); //LED??<br _mstmutation="1" />
Delay(1001-t);<br _mstmutation="1" />
}<br _mstmutation="1" />
t++; <br _mstmutation="1" />
if(t == 1000)<br _mstmutation="1" />
{<br _mstmutation="1" />
flag = 0;<br _mstmutation="1" />
}<br _mstmutation="1" />
}<br _mstmutation="1" />
if(flag == 0) //LED????<br _mstmutation="1" />
{<br _mstmutation="1" />
for(i=0;i<10;i++)<br _mstmutation="1" />
{ <br _mstmutation="1" />
GPIO_ResetBits(PORT_GROUP, LED1_PIN | LED2_PIN | LED3_PIN); //LED??<br _mstmutation="1" />
Delay(t);<br _mstmutation="1" />
GPIO_SetBits(PORT_GROUP,LED1_PIN | LED2_PIN | LED3_PIN); //LED??<br _mstmutation="1" />
Delay(1001-t);<br _mstmutation="1" />
}<br _mstmutation="1" />
t--;<br _mstmutation="1" />
if(t == 1)<br _mstmutation="1" />
{<br _mstmutation="1" />
flag = 1;</font><br />
}<br />
}<br />
}<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 />
{<br />
}<br />
}</p>
<p>帮忙看看 楼主 下面是LED KEY初始化 和LED 点亮熄灭的函数 最上面是头文件和宏定义 中间是main函数</p>
<p>你如果要想检测按键,你这样写阻塞式的delay是不行的,要用状态机来写才行。</p>
<p><font _msthash="1018" _mstmutation="1" _msttexthash="185880812">就算我不这么写延迟按键按下去也没反应 不知道哪里有问题</font></p>
<p>可以看看代码打包发你了</p>
<div><br />
<br />
</div>
J1anbean 发表于 2023-5-5 16:10
可以看看代码打包发你了
<p>好的,晚上回去修改发给你。你要去学习一下状态机的知识,这样才行。</p>
页:
[1]
2