我正在使用 Code Composer Studio 和 C 语言开发 Tiva TM4C1294 我想创建一个计时器函数并使用它,而不是在下面的代码块中使用延迟函数。例如,当我在 main 中编写 Timer(5) 代码时,我希望它在运行后 5 秒停止进程。你能帮助我吗? 我是一个菜鸟,因为这是我第一次编写此类代码。如有遗漏请询问。
//*****************************************************************************
//
// gpio_jtag.c - Example to demonstrate recovering the JTAG interface.
//
// Copyright (c) 2013-2020 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.2.0.295 of the EK-TM4C1294XL Firmware Package.
//
//*****************************************************************************
#include <stdint.h>
#include <stdbool.h>
#include "inc/hw_gpio.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/gpio.h"
#include "driverlib/interrupt.h"
#include "driverlib/pin_map.h"
#include "driverlib/rom.h"
#include "driverlib/rom_map.h"
#include "driverlib/sysctl.h"
#include "driverlib/systick.h"
#include "driverlib/uart.h"
#include "utils/uartstdio.h"
#include "drivers/buttons.h"
#include <inc/tm4c1294ncpdt.h>
//*****************************************************************************
//
//! \addtogroup example_list
//! <h1>GPIO JTAG Recovery (gpio_jtag)</h1>
//!
//! This example demonstrates changing the JTAG pins into GPIOs, a with a
//! mechanism to revert them to JTAG pins. When first run, the pins remain in
//! JTAG mode. Pressing the USR_SW1 button will toggle the pins between JTAG
//! mode and GPIO mode. Because there is no debouncing of the push button
//! (either in hardware or software), a button press will occasionally result
//! in more than one mode change.
//!
//! In this example, four pins (PC0, PC1, PC2, and PC3) are switched.
//!
//! UART0, connected to the ICDI virtual COM port and running at 115,200,
//! 8-N-1, is used to display messages from this application.
//
//*****************************************************************************
//*****************************************************************************
//
// System clock rate in Hz.
//
//*****************************************************************************
uint32_t g_ui32SysClock;
//*****************************************************************************
//
// The current mode of pins PC0, PC1, PC2, and PC3. When zero, the pins
// are in JTAG mode; when non-zero, the pins are in GPIO mode.
//
//*****************************************************************************
volatile uint32_t g_ui32Mode;
//*****************************************************************************
//
// 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 PB4 pin interrupt. When triggered, this will
// toggle the JTAG pins between JTAG and GPIO mode.
//
//*****************************************************************************
void
SysTickIntHandler(void)
{
uint8_t ui8Buttons;
uint8_t ui8ButtonsChanged;
//
// Grab the current, debounced state of the buttons.
//
ui8Buttons = ButtonsPoll(&ui8ButtonsChanged, 0);
//
// If the USR_SW1 button has been pressed, and was previously not pressed,
// start the process of changing the behavior of the JTAG pins.
//
if(BUTTON_PRESSED(USR_SW1, ui8Buttons, ui8ButtonsChanged))
{
//
// Toggle the pin mode.
//
g_ui32Mode ^= 1;
//
// See if the pins should be in JTAG or GPIO mode.
//
if(g_ui32Mode == 0)
{
//
// Change PC0-3 into hardware (i.e. JTAG) pins.
//
HWREG(GPIO_PORTC_BASE + GPIO_O_LOCK) = GPIO_LOCK_KEY;
HWREG(GPIO_PORTC_BASE + GPIO_O_CR) = 0x01;
HWREG(GPIO_PORTC_BASE + GPIO_O_AFSEL) |= 0x01;
HWREG(GPIO_PORTC_BASE + GPIO_O_LOCK) = GPIO_LOCK_KEY;
HWREG(GPIO_PORTC_BASE + GPIO_O_CR) = 0x02;
HWREG(GPIO_PORTC_BASE + GPIO_O_AFSEL) |= 0x02;
HWREG(GPIO_PORTC_BASE + GPIO_O_LOCK) = GPIO_LOCK_KEY;
HWREG(GPIO_PORTC_BASE + GPIO_O_CR) = 0x04;
HWREG(GPIO_PORTC_BASE + GPIO_O_AFSEL) |= 0x04;
HWREG(GPIO_PORTC_BASE + GPIO_O_LOCK) = GPIO_LOCK_KEY;
HWREG(GPIO_PORTC_BASE + GPIO_O_CR) = 0x08;
HWREG(GPIO_PORTC_BASE + GPIO_O_AFSEL) |= 0x08;
HWREG(GPIO_PORTC_BASE + GPIO_O_LOCK) = GPIO_LOCK_KEY;
HWREG(GPIO_PORTC_BASE + GPIO_O_CR) = 0x00;
HWREG(GPIO_PORTC_BASE + GPIO_O_LOCK) = 0;
//
// Turn on the LED to indicate that the pins are in JTAG mode.
//
MAP_GPIOPinWrite(GPIO_PORTN_BASE, GPIO_PIN_0 | GPIO_PIN_1,
GPIO_PIN_0);
}
else
{
//
// Change PC0-3 into GPIO inputs.
//
HWREG(GPIO_PORTC_BASE + GPIO_O_LOCK) = GPIO_LOCK_KEY;
HWREG(GPIO_PORTC_BASE + GPIO_O_CR) = 0x01;
HWREG(GPIO_PORTC_BASE + GPIO_O_AFSEL) &= 0xfe;
HWREG(GPIO_PORTC_BASE + GPIO_O_LOCK) = GPIO_LOCK_KEY;
HWREG(GPIO_PORTC_BASE + GPIO_O_CR) = 0x02;
HWREG(GPIO_PORTC_BASE + GPIO_O_AFSEL) &= 0xfd;
HWREG(GPIO_PORTC_BASE + GPIO_O_LOCK) = GPIO_LOCK_KEY;
HWREG(GPIO_PORTC_BASE + GPIO_O_CR) = 0x04;
HWREG(GPIO_PORTC_BASE + GPIO_O_AFSEL) &= 0xfb;
HWREG(GPIO_PORTC_BASE + GPIO_O_LOCK) = GPIO_LOCK_KEY;
HWREG(GPIO_PORTC_BASE + GPIO_O_CR) = 0x08;
HWREG(GPIO_PORTC_BASE + GPIO_O_AFSEL) &= 0xf7;
HWREG(GPIO_PORTC_BASE + GPIO_O_LOCK) = GPIO_LOCK_KEY;
HWREG(GPIO_PORTC_BASE + GPIO_O_CR) = 0x00;
HWREG(GPIO_PORTC_BASE + GPIO_O_LOCK) = 0;
MAP_GPIOPinTypeGPIOInput(GPIO_PORTC_BASE, (GPIO_PIN_0 | GPIO_PIN_1 |
GPIO_PIN_2 |
GPIO_PIN_3));
//
// Turn off the LED to indicate that the pins are in GPIO mode.
//
MAP_GPIOPinWrite(GPIO_PORTN_BASE, GPIO_PIN_0 | GPIO_PIN_1,
GPIO_PIN_1);
}
}
}
//*****************************************************************************
//
// Configure the UART and its pins. This must be called before UARTprintf().
//
//*****************************************************************************
void
ConfigureUART(void)
{
//
// Enable the GPIO Peripheral used by the UART.
//
MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
//
// Enable UART0
//
MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
//
// Configure GPIO Pins for UART mode.
//
MAP_GPIOPinConfigure(GPIO_PA0_U0RX);
MAP_GPIOPinConfigure(GPIO_PA1_U0TX);
MAP_GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);
//
// Initialize the UART for console I/O.
//
UARTStdioConfig(0, 115200, g_ui32SysClock);
}
//*****************************************************************************
//
// Toggle the JTAG pins between JTAG and GPIO mode with a push button selecting
// between the two.
//
//*****************************************************************************
//*****************************************************************************
//
// Timer fonksiyonu, saniye cinsinden bir değer alır ve bu sürenin sonunda kesme oluşturur.
//
//*****************************************************************************
void Timer(uint32_t ui32Seconds)
{
MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0);
MAP_IntEnable(INT_TIMER0A);
MAP_TimerConfigure(TIMER0_BASE, TIMER_CFG_PERIODIC);
MAP_TimerLoadSet(TIMER0_BASE, TIMER_0, g_ui32SysClock * ui32Seconds);
MAP_IntRegister(INT_TIMER0A, TimerHandler);
MAP_IntMasterEnable();
MAP_TimerIntEnable(TIMER0_BASE, TIMER_TIMA_TIMEOUT);
MAP_TimerEnable(TIMER0_BASE, TIMER_A);
}
uint32_t i; //int 1
int main(void) {
SYSCTL_RCGCGPIO_R=0X1100; // set clock portn
i=SYSCTL_RCGCGPIO_R; // delay (more than 3 cycles)
GPIO_PORTN_DIR_R=0X03; //enable the GPIO pin for the LED-PN0, set the direction as output, and
GPIO_PORTN_DEN_R=0X03; //enable the GPIO pin for digital function
GPIO_PORTJ_AHB_DIR_R=0;
GPIO_PORTJ_AHB_DEN_R=0X03;
GPIO_PORTJ_AHB_PUR_R=0X01;
while(1){
GPIO_PORTN_DATA_R &=~0X02; //turn led off
while (GPIO_PORTJ_AHB_DATA_R & 0X01){
GPIO_PORTN_DATA_R |=0X01; //turn led on
Timer(10);
// SysCtlDelay(2866666);
GPIO_PORTN_DATA_R &=~0X01; //turn led off again
Timer(10);
// SysCtlDelay(2866666);
}
GPIO_PORTN_DATA_R |=0X02;
}
}
为微控制器编写软件并与硬件交互时,有必要阅读参考手册(针对您的情况)Tiva C 系列 TM4C1294NCPDT 微控制器数据表 (Rev. B)。本手册介绍了硬件的工作原理以及如何配置它。
如果您正在寻找的是使用计时器的阻塞延迟功能,那么您需要执行以下操作:
x
秒后触发中断。但是,我不建议为此使用计时器。同时浪费了硬件和软件资源。如果您需要阻塞延迟,请在
SysCtlDelay
周围编写一个包装函数,以使其更易于使用。例如,此包装函数可以将以秒为单位的延迟转换为 SysCtlDelay
所需的单位。如果您需要的是非阻塞延迟,以便处理器可以在延迟期间执行其他操作,那么硬件定时器就是最佳选择。