请问一下,想用cc2538的定时器记一段时间内的脉冲数,应该用Input Edge-Count Mode 这个,还是Input Edge-Timing Mode ?按照文档里的初始化配置方法初始化好了好了,但是不知道合不合适,后面也不知道该咋办了,定时器的定时时间又是怎么计算的?
Viki Shi:
看你是否需要timer继续运行
When counting down in Input Edge-Count Mode, the timer stops after the programmed number of edge events has been detected. To re-enable the timer, ensure that the TnEN bit is cleared and repeat #4 through #9.
In Input Edge Timing mode, the timer continues running after an edge event has been detected, but the timer interval can be changed at any time by writing the GPTMTnILR register. The change takes effect at the next cycle after the write.
Fei Chen:
回复 Viki Shi:
你好,我想用定时器计PC6接的按键按了几次,结果串口啥都没打印出来,下面是我的代码,能帮我看看哪些地方不对吗,谢谢
#include "contiki.h"
#include "dev/gptimer.h"
#include "dev/ioc.h"
#include "dev/sys-ctrl.h"
#include "dev/uart.h"
#include "reg.h"
#include <stdio.h>void timer0_init(uint16_t psc, uint16_t arr)
{/*Clock for GPT0 is enabled*/REG(SYS_CTRL_RCGCGPT) = 1;REG(IOC_PC4_SEL) = 0xD;/*Timer A is disabled. Positive edge. Negative edge*/REG(GPT_0_BASE + GPTIMER_CTL) = 0x4;/*16-bit timer configuration*/REG(GPT_0_BASE + GPTIMER_CFG) = 0x4;
/*The timer counts up. When counting up, the timer starts from a value of 0x0.Edge-time mode.Capture mode*/REF(GPT_0_BASE + GPTIMER_TAMR) = 0x10 | 0x4 | GPTIMER_TAMR_TAMR_CAPTURE;/*GPTM Timer A prescale*/REG(GPT_0_BASE + GPTIMER_TAPR) = psc;
/*GPTM A interval load register*/REG(GPT_0_BASE + GPTIMER_TAILR) = arr; /* Timer A is enabled and begins counting or the capture logic is
enabled based on the GPTMCFG register.*/REG(GPT_0_BASE + GPTIMER_CTL) |= 1;
}PROCESS(timer0_process, "spi");
AUTOSTART_PROCESSES(&timer0_process);
PROCESS_THREAD(timer0_process, ev, data)
{uint16_t count;PROCESS_BEGIN();uart_init();timer0_init(0, 0xFFFF);while(1){if (count != REG(GPT_0_BASE + GPTIMER_TAR)){count = REG(GPT_0_BASE + GPTIMER_TAR);printf("cnt: %d\n", count);}}PROCESS_END();
}
TI中文支持网