This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

tm4c 通用定时器向上计数 , 启动后无法置零,TimerLoadSet()无效

#include "main.h"

void vGPIO_Config(void);
void vGPIO_PortFIntHandler(void);
void Timer0IntHander(void);
void timer_config(void);

unsigned long k;
char str[50];

int main(void)
{
SysCtlClockSet(SYSCTL_SYSDIV_2_5 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ);
/* 时钟配置,2.5分频,80Mhz,系统时钟使用PLL,使用外部主时钟源,外部晶振为16MHz */
ILI9341_init(); //液晶屏初始化
ILI9341_clear(GRAY); //清屏
BACK_COLOR=BLUE;
POINT_COLOR=BLACK;
vGPIO_Config();
timer_config();
while(1)
{
}
}

void Timer0IntHander(void)
{
TimerIntClear(TIMER0_BASE,TIMER_A);
sprintf(str,"%d",TimerValueGet(TIMER1_BASE,TIMER_A));
ILI9341_draw_string(75,75,str);

TimerLoadSet(TIMER1_BASE,TIMER_A,0);
}

void timer_config(void)
{
SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0); //使能定时器模块Timer0
SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER1); //cap
TimerConfigure(TIMER0_BASE,TIMER_CFG_PERIODIC); //配置定时器模块Timer0为Periodic周期性计数模式。
TimerConfigure(TIMER1_BASE,TIMER_CFG_SPLIT_PAIR|TIMER_CFG_A_CAP_COUNT_UP);
TimerLoadSet(TIMER0_BASE,TIMER_A,SysCtlClockGet()-1); //设定定时器模块Timer0的Load重装载值为系统时钟频率的一半再减一(为0.5秒)
TimerLoadSet(TIMER1_BASE,TIMER_A,0);
TimerControlEvent(TIMER1_BASE,TIMER_A,TIMER_EVENT_POS_EDGE);
IntRegister(INT_TIMER0A,Timer0IntHander); //注册中断函数
IntEnable(INT_TIMER0A); //使能定时器模块Timer0的定时器A的中断。
TimerIntEnable(TIMER0_BASE,TIMER_TIMA_TIMEOUT); //使能单独的定时器中断源,第二个是中断源启用的中断事件的标识码,TIMER_TIMA_TIMEOUT的意思是定时器A(TimerA)溢出(重装载),以此为标志位,当TimerA重装载时就会触发中断。
IntMasterEnable(); //使能处理器中断,使处理器能够响应中断。
IntPrioritySet(INT_TIMER0A, 0); //设置中断优先级,TM4C123G的中断优先级有8个
TimerEnable(TIMER0_BASE,TIMER_A); //使能定时器
TimerEnable(TIMER1_BASE,TIMER_A);
TimerIntClear(TIMER0_BASE,TIMER_A);
}

/* GPIOF中断服务函数 */
void vGPIO_Config(void)
{ ILI9341_clear(YELLOW);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
GPIOPinTypeTimer(GPIO_PORTB_BASE,GPIO_PIN_4);
GPIOPinConfigure(GPIO_PB4_T1CCP0);
}

  • 请您尝试一下

    TimerDisable(TIMER1_BASE,TIMER_A);
    TimerConfigure(TIMER1_BASE,TIMER_CFG_PERIODIC_UP);
    TimerEnable(TIMER1_BASE,TIMER_A);
    TimerLoadSet(TIMER1_BASE,TIMER_A,0x00FFFFFF);
  • 另外请参考下相关例程

    //*****************************************************************************
    //
    // timers.c - Timers example.
    //
    // Copyright (c) 2011-2017 Texas Instruments Incorporated.  All rights reserved.
    // Software License Agreement
    // 
    // Texas Instruments (TI) is supplying this software for use solely and
    // exclusively on TI's microcontroller products. The software is owned by
    // TI and/or its suppliers, and is protected under applicable copyright
    // laws. You may not combine this software with "viral" open-source
    // software in order to form a larger program.
    // 
    // THIS SOFTWARE IS PROVIDED "AS IS" AND WITH ALL FAULTS.
    // NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT
    // NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    // A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. TI SHALL NOT, UNDER ANY
    // CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
    // DAMAGES, FOR ANY REASON WHATSOEVER.
    // 
    // This is part of revision 2.1.4.178 of the DK-TM4C123G Firmware Package.
    //
    //*****************************************************************************
    
    #include <stdint.h>
    #include <stdbool.h>
    #include "inc/hw_ints.h"
    #include "inc/hw_memmap.h"
    #include "inc/hw_types.h"
    #include "driverlib/debug.h"
    #include "driverlib/fpu.h"
    #include "driverlib/interrupt.h"
    #include "driverlib/sysctl.h"
    #include "driverlib/timer.h"
    #include "driverlib/rom.h"
    #include "grlib/grlib.h"
    #include "drivers/cfal96x64x16.h"
    
    //*****************************************************************************
    //
    //! \addtogroup example_list
    //! <h1>Timer (timers)</h1>
    //!
    //! This example application demonstrates the use of the timers to generate
    //! periodic interrupts.  One timer is set up to interrupt once per second and
    //! the other to interrupt twice per second; each interrupt handler will toggle
    //! its own indicator on the display.
    //
    //*****************************************************************************
    
    //*****************************************************************************
    //
    // Flags that contain the current value of the interrupt indicator as displayed
    // on the CSTN display.
    //
    //*****************************************************************************
    uint32_t g_ui32Flags;
    
    //*****************************************************************************
    //
    // Graphics context used to show text on the CSTN display.
    //
    //*****************************************************************************
    tContext g_sContext;
    
    //*****************************************************************************
    //
    // The error routine that is called if the driver library encounters an error.
    //
    //*****************************************************************************
    #ifdef DEBUG
    void
    __error__(char *pcFilename, uint32_t ui32Line)
    {
    }
    #endif
    
    //*****************************************************************************
    //
    // The interrupt handler for the first timer interrupt.
    //
    //*****************************************************************************
    void
    Timer0IntHandler(void)
    {
        //
        // Clear the timer interrupt.
        //
        ROM_TimerIntClear(TIMER0_BASE, TIMER_TIMA_TIMEOUT);
    
        //
        // Toggle the flag for the first timer.
        //
        HWREGBITW(&g_ui32Flags, 0) ^= 1;
    
        //
        // Update the interrupt status on the display.
        //
        ROM_IntMasterDisable();
        GrStringDraw(&g_sContext, (HWREGBITW(&g_ui32Flags, 0) ? "1" : "0"), -1, 68,
                     26, 1);
        ROM_IntMasterEnable();
    }
    
    //*****************************************************************************
    //
    // The interrupt handler for the second timer interrupt.
    //
    //*****************************************************************************
    void
    Timer1IntHandler(void)
    {
        //
        // Clear the timer interrupt.
        //
        ROM_TimerIntClear(TIMER1_BASE, TIMER_TIMA_TIMEOUT);
    
        //
        // Toggle the flag for the second timer.
        //
        HWREGBITW(&g_ui32Flags, 1) ^= 1;
    
        //
        // Update the interrupt status on the display.
        //
        ROM_IntMasterDisable();
        GrStringDraw(&g_sContext, (HWREGBITW(&g_ui32Flags, 1) ? "1" : "0"), -1, 68,
                     36, 1);
        ROM_IntMasterEnable();
    }
    
    //*****************************************************************************
    //
    // This example application demonstrates the use of the timers to generate
    // periodic interrupts.
    //
    //*****************************************************************************
    int
    main(void)
    {
        tRectangle sRect;
    
        //
        // Enable lazy stacking for interrupt handlers.  This allows floating-point
        // instructions to be used within interrupt handlers, but at the expense of
        // extra stack usage.
        //
        ROM_FPULazyStackingEnable();
    
        //
        // Set the clocking to run directly from the crystal.
        //
        ROM_SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN |
                           SYSCTL_XTAL_16MHZ);
    
        //
        // Initialize the display driver.
        //
        CFAL96x64x16Init();
    
        //
        // Initialize the graphics context and find the middle X coordinate.
        //
        GrContextInit(&g_sContext, &g_sCFAL96x64x16);
    
        //
        // Fill the top part of the screen with blue to create the banner.
        //
        sRect.i16XMin = 0;
        sRect.i16YMin = 0;
        sRect.i16XMax = GrContextDpyWidthGet(&g_sContext) - 1;
        sRect.i16YMax = 9;
        GrContextForegroundSet(&g_sContext, ClrDarkBlue);
        GrRectFill(&g_sContext, &sRect);
    
        //
        // Change foreground for white text.
        //
        GrContextForegroundSet(&g_sContext, ClrWhite);
    
        //
        // Put the application name in the middle of the banner.
        //
        GrContextFontSet(&g_sContext, g_psFontFixed6x8);
        GrStringDrawCentered(&g_sContext, "timers", -1,
                             GrContextDpyWidthGet(&g_sContext) / 2, 4, 0);
    
        //
        // Initialize timer status display.
        //
        GrContextFontSet(&g_sContext, g_psFontFixed6x8);
        GrStringDraw(&g_sContext, "Timer 0:", -1, 16, 26, 0);
        GrStringDraw(&g_sContext, "Timer 1:", -1, 16, 36, 0);
    
        //
        // Enable the peripherals used by this example.
        //
        ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0);
        ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER1);
    
        //
        // Enable processor interrupts.
        //
        ROM_IntMasterEnable();
    
        //
        // Configure the two 32-bit periodic timers.
        //
        ROM_TimerConfigure(TIMER0_BASE, TIMER_CFG_PERIODIC);
        ROM_TimerConfigure(TIMER1_BASE, TIMER_CFG_PERIODIC);
        ROM_TimerLoadSet(TIMER0_BASE, TIMER_A, ROM_SysCtlClockGet());
        ROM_TimerLoadSet(TIMER1_BASE, TIMER_A, ROM_SysCtlClockGet() / 2);
    
        //
        // Setup the interrupts for the timer timeouts.
        //
        ROM_IntEnable(INT_TIMER0A);
        ROM_IntEnable(INT_TIMER1A);
        ROM_TimerIntEnable(TIMER0_BASE, TIMER_TIMA_TIMEOUT);
        ROM_TimerIntEnable(TIMER1_BASE, TIMER_TIMA_TIMEOUT);
    
        //
        // Enable the timers.
        //
        ROM_TimerEnable(TIMER0_BASE, TIMER_A);
        ROM_TimerEnable(TIMER1_BASE, TIMER_A);
    
        //
        // Loop forever while the timers run.
        //
        while(1)
        {
        }
    }
    

  • 按上面设置之后定时器直接从-65536递增

    开始如果第二个句改为配置TimerConfigure(TIMER1_BASE,TIMER_CFG_SPLIT_PAIR|TIMER_CFG_A_CAP_COUNT_UP);    没有效果

  • 请您看一下是不是中了勘误表里的

    GPTM#15

  • 问题好像不是这个