【N32L43x评测】2、点灯,blink,blink,blink……
<p><span style="font-size:20px;">1、前言:</span></p><p>官方的demo用的阻塞方式点灯,即就是死等,在等待期间mcu干不了其他事情(中断除外),这种方式不太友好,本文使用非阻塞方式点灯。</p>
<p> </p>
<p><span style="font-size:20px;">2、硬件连接</span></p>
<p></p>
<p><span style="font-size:20px;">3、思路</span></p>
<p>利用定时器中断,每1ms中断一次,整个系统维护一个tick计数;</p>
<p>记录某一时刻的tick,用A表示,再获取当前的tick,用B表示,如果当前的B-A大于等于500(这里500ms闪烁一次),那么就执行一次led翻转,同时也更新A的值。</p>
<p> </p>
<p><span style="font-size:20px;">4、软件实现</span></p>
<p>(1)利用定时器中断,每1ms中断一次,整个系统维护一个tick计数</p>
<pre>
<code class="language-cpp">static uint32_t SystemTick=0;
void TimerConfig(void)
{
NVIC_InitType NVIC_InitStructure;
TIM_TimeBaseInitType TIM_TimeBaseStructure;
RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_TIM2, ENABLE);
/* Enable the TIM2 global Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
//TIMER2CLK = SystemCoreClock/(107+1) = 1MHz, the period is 1s((999+1)/1000000 = 0.001s=1ms).
/* Time base configuration */
TIM_TimeBaseStructure.Period = 107;
TIM_TimeBaseStructure.Prescaler = 999;
TIM_TimeBaseStructure.ClkDiv = TIM_CLK_DIV1;
TIM_TimeBaseStructure.CntMode = TIM_CNT_MODE_UP;
TIM_InitTimeBase(TIM2, &TIM_TimeBaseStructure);
/* TIM IT enable */
TIM_ConfigInt(TIM2, TIM_INT_UPDATE , ENABLE);
/* TIM2 enable counter */
TIM_Enable(TIM2, ENABLE);
}
void TIM2_IRQHandler(void)
{
if (TIM_GetIntStatus(TIM2, TIM_INT_UPDATE) != RESET)
{
TIM_ClrIntPendingBit(TIM2, TIM_INT_UPDATE);
SystemTick++;
}
}
uint32_t SystemGetTick(void)
{
return SystemTick;
}</code></pre>
<p> </p>
<p>(2)LED翻转</p>
<ul>
</ul>
<pre>
<code class="language-cpp">{
GPIO_InitType GPIO_InitStructure;
RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOA, ENABLE);
RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOB, ENABLE);
GPIO_InitStruct(&GPIO_InitStructure);
GPIO_InitStructure.Pin = GPIO_PIN_8;
GPIO_InitStructure.GPIO_Current = GPIO_DC_12mA;
GPIO_InitStructure.GPIO_Pull = GPIO_No_Pull;
GPIO_InitStructure.GPIO_Mode= GPIO_Mode_Out_PP;
GPIO_InitPeripheral(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.Pin = GPIO_PIN_4|GPIO_PIN_5;
GPIO_InitPeripheral(GPIOB, &GPIO_InitStructure);
}
void SystemRun(void)
{
static uint32_t curtick=0;
static uint8_t state=0;
if(SystemGetTick()-curtick>=500)
{
if(state)
{
GPIO_SetBits(GPIOA, GPIO_PIN_8);
GPIO_SetBits(GPIOB, GPIO_PIN_4|GPIO_PIN_5);
}
else
{
GPIO_ResetBits(GPIOA, GPIO_PIN_8);
GPIO_ResetBits(GPIOB, GPIO_PIN_4|GPIO_PIN_5);
}
curtick=SystemGetTick();
state=!state;
}
}</code></pre>
<p> </p>
<p><span style="font-size:20px;">5、现象</span></p>
<p>1d1700d387ef4c4f261b12cd6672ac94<br />
</p>
<p> </p>
<p> </p>
很精彩的作品哦,大家可以学习定时器,来做做状态转换! <p>三色灯为啥不直接用三色灯,,,</p>
freebsder 发表于 2022-7-5 17:37
三色灯为啥不直接用三色灯,,,
<p>这就要问官方设计人员了<img height="48" src="https://bbs.eeworld.com.cn/static/editor/plugins/hkemoji/sticker/facebook/lol.gif" width="48" /></p>
页:
[1]