工具与软件:
您好!
Im 尝试创建一个计时器、以定期触发 TM4C1230E6PM 上的中断。
下面是我提出的最小示例:
// --- Includes #include <stdio.h> #include <stdbool.h> #include <stdint.h> #include "inc/hw_memmap.h" #include "driverlib/timer.h" #include "driverlib/sysctl.h" #include "driverlib/interrupt.h" // --- Typedefs typedef void (*pFctHandler)(void); typedef const struct { uint8_t TIMER_NR; uint32_t SYSCTL_TIMER; uint32_t TIMER_BASE; uint32_t TIMER_CFG; uint32_t TIMER_A_OR_B; uint32_t TIMER_INTERVAL_US; uint32_t INT_FLAGS; pFctHandler callbackFct; } timerHW_t; // --- Local Function Prototypes void test_tick(void); // --- Variables timerHW_t tmr = { .TIMER_NR = 0, .SYSCTL_TIMER = SYSCTL_PERIPH_TIMER2, .TIMER_BASE = TIMER2_BASE, .TIMER_CFG = TIMER_CFG_PERIODIC, .TIMER_A_OR_B = TIMER_BOTH, .TIMER_INTERVAL_US = 1000, // not implemented .callbackFct = test_tick, .INT_FLAGS = TIMER_TIMA_TIMEOUT, }; int main(void) { // Set Clock SysCtlClockSet(SYSCTL_SYSDIV_2_5 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ); //80MHz SysClk // Enable Interrupts IntMasterEnable(); // Enable the peripheral SysCtlPeripheralEnable(tmr.SYSCTL_TIMER); // Wait for the module to be ready. while(!SysCtlPeripheralReady(tmr.SYSCTL_TIMER)) { } // configure Timer TimerConfigure(tmr.TIMER_BASE, tmr.TIMER_CFG); TimerLoadSet(tmr.TIMER_BASE, tmr.TIMER_A_OR_B, SysCtlClockGet()); // Register the interrupt handler TimerIntRegister(tmr.TIMER_BASE, tmr.TIMER_A_OR_B, tmr.callbackFct); // Start the timer TimerEnable(tmr.TIMER_BASE, tmr.TIMER_A_OR_B); TimerIntEnable(tmr.TIMER_BASE, tmr.INT_FLAGS); while(1) { // do nothing ; } } void test_tick(void) { TimerIntClear(tmr.TIMER_BASE, tmr.INT_FLAGS); // Why is this line needed? TimerLoadSet(tmr.TIMER_BASE, tmr.TIMER_A_OR_B, SysCtlClockGet()); }
与此相关的问题如下:
- 为什么需要81号线? 当达到负载0时、带有 TIMER_CFG_PERIODICENT 的计时器是否应该不会自行复位? 没有这一行,我基本上卡在 test_tick ()
- 当第81行处于活动状态时、我注意到第78行之间的时钟计数不一致。 它通常在预期的80 000 000 (80 000 192)左右、但在两者之间偏离显示[0 (???)、70 000 000、68 000、79 000 ]。 计时器不应该非常精确地确定调用之间发生的时钟周期数吗?
提前感谢您的任何帮助!