Other Parts Discussed in Thread: EK-TM4C123GXL
Thread 中讨论的其他器件:EK-TM4C123GXL
大家好、
我正在使用 EK-TM4C123GXL launchpad 板并尝试使用 Timer0作为延迟函数、但遇到了一个奇怪的问题、我不太理解我的代码为什么不起作用。 代码非常简单、如下所示:
#include <stdbool.h>
#include <stdint.h>
#include "inc/hw_ints.h"
#include "inc/hw_memmap.h"
#include "driverlib/gpio.h"
#include "driverlib/interrupt.h"
#include "driverlib/pin_map.h"
#include "driverlib/sysctl.h"
#include "driverlib/timer.h"
#include "driverlib/uart.h"
#include "utils/uartstdio.h"
//*****************************************************************************
//
// Delay ms
//
//*****************************************************************************
void Delay_ms(uint16_t ui16ms)
{
TimerEnable(TIMER0_BASE, TIMER_A);
while (ui16ms--)
{
while (!TimerIntStatus(TIMER0_BASE, TIMER_A))
{
TimerIntClear(TIMER0_BASE, TIMER_TIMA_TIMEOUT);
}
}
TimerDisable(TIMER0_BASE, TIMER_A);
}
//*****************************************************************************
//
// Configure Timer0A as a 32-bit periodic
//
//*****************************************************************************
int
main(void)
{
//
// Config system clock for 80MHz using PLL with external 16MHz oscillator
//
SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN |
SYSCTL_XTAL_16MHZ);
//
// Enable Peripheral Clocks for port PB
//
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
//
// Enable pin PB for GPIOOutput
//
GPIOPinTypeGPIOOutput(GPIO_PORTB_BASE, GPIO_PIN_0);
GPIOPinTypeGPIOOutput(GPIO_PORTB_BASE, GPIO_PIN_1);
//
// Wait for PB is ready
//
while(!SysCtlPeripheralReady(SYSCTL_PERIPH_GPIOB));
//
// Enable The Timer0 peripheral.
//
SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0);
//
// Wait for Timer0 is ready
while(!SysCtlPeripheralReady(SYSCTL_PERIPH_TIMER0));
//
// Config Timer0 32 bit periodic
//
TimerConfigure(TIMER0_BASE, TIMER_CFG_PERIODIC);
//
// Preload Timer0A for 1ms timeout
//
TimerLoadSet(TIMER0_BASE, TIMER_A, SysCtlClockGet() / 1000);
//
// Configure the Timer0A interrupt for timer timeout.
//
TimerIntEnable(TIMER0_BASE, TIMER_TIMA_TIMEOUT);
//
// Enable Timer0A.
//
TimerEnable(TIMER0_BASE, TIMER_A);
//
// Loop forever while the Timer0B runs.
//
while(1)
{
//
// Toggle port PB0 every 50ms
//
GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_0, 1);
Delay_ms(1);
GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_0, 0);
Delay_ms(1);
}
}
我遇到的问题是 Timer0溢出似乎比我预期的快得多。 我使用示波器查看端口引脚 PB0、看到它在1.5us 左右切换、而不是1ms。 系统时钟以80MHz 的频率运行。 我检查了 Timer0 TIMER_TAILR 寄存 器、预加载值正确、为0x80000十进制。 请大家仔细查看我的代码并告诉我哪里出错了吗?
提前感谢大家、祝您愉快。
此致、
TLN