本人单片机初学者,最近在使用nrf51822,想通过中断的方法测某个引脚输入的高电平持续时间,思路是:进入上升沿中断,打开定时器开始计数,下降沿到来时,关闭计数器,读取计数值。但是具体代码实现上出了问题,如下是计数器的初始化及 中断中的处理,
static void TIMER0_init(void)
{
// Start 16 MHz crystal oscillator.
NRF_CLOCK->EVENTS_HFCLKSTARTED = 0;
NRF_CLOCK->TASKS_HFCLKSTART = 1;
// Wait for the external oscillator to start.
while (NRF_CLOCK->EVENTS_HFCLKSTARTED == 0)
{
// Do nothing.
}
NRF_TIMER0->MODE = TIMER_MODE_MODE_Counter; // Set Timer in Counter mode.
NRF_TIMER0->BITMODE = TIMER_BITMODE_BITMODE_32Bit; // 32 bit mode.
NRF_TIMER0->TASKS_CLEAR = 1; // clear the task first to be usable for later.
}
void GPIOTE_IRQHandler(void)
{
if(NRF_GPIO->IN & (0x1UL)) //B 的上升沿
{
// 计数开始
NRF_TIMER0->TASKS_START = 1; // Start the timer.
}
else //B的下降沿
{
// 停止计数,获取计数存于 Duration_Time中
NRF_TIMER0->TASKS_STOP=1; // Stop timer.
NRF_TIMER0->TASKS_CAPTURE[0] = 1; //Capture Timer value to CC[0] registers.
Duration_Time=NRF_TIMER0->CC[0]; // 持续时间
NRF_TIMER0->TASKS_CAPTURE[0] = 0; //
}
//计数清零
NRF_TIMER0->TASKS_CLEAR = 1; // clear timer
//清除B的中断
// Event causing the interrupt must be cleared. pin0
if ((NRF_GPIOTE->EVENTS_IN[0] == 1) &&
(NRF_GPIOTE->INTENSET & GPIOTE_INTENSET_IN0_Msk))
{
NRF_GPIOTE->EVENTS_IN[0] = 0;
}
}
请问,是不是计数的配置及capture出了问题? |