您好!
我不熟悉 TMC4C123GH6PM 和使用 Launchpad。
我了解了 Timer0子计时器 A 用于生成所需频率和占空比的用法。
同样、我尝试使用 Timer0子计时器 B 进行频率生成、但即使花了几个小时也没能成功。
我请求就这个问题提供帮助。
/* ----------------------- Include Files --------------------- */
#include <stdint.h> // Library of Standard Integer Types
#include <stdbool.h> // Library of Standard Boolean Types
#include "inc/tm4c123gh6pm.h" // Definitions for interrupt and register assignments on Tiva C
#include "inc/hw_memmap.h" // Macros defining the memory map of the Tiva C Series device
#include "inc/hw_types.h" // Defines common types and macros
#include "driverlib/sysctl.h" // Defines and macros for System Control API of DriverLib
#include "driverlib/interrupt.h" // Defines and macros for NVIC Controller API of DriverLib
#include "driverlib/gpio.h" // Defines and macros for GPIO API of DriverLib
#include "driverlib/timer.h" // Defines and macros for Timer API of driverLib
/* ----------------------- Global Variables --------------------- */
uint32_t ui32Period; // Variable to store the period to be inputted to Timer0
uint32_t ui32IntFrequency = 0x00000009; // Variable to determine the Blinking Frequency of LEDs
/* ----------------------- Function Prototypes --------------------- */
void Timer0AIntHandler(void); // The prototype of the ISR for Timer0 Interrupt
void Timer0BIntHandler(void); // The prototype of the ISR for Timer0 Interrupt
/* ----------------------- Main Program --------------------- */
int main(void){
// Set the System clock to 80MHz and enable the clock for peripheral Port F and Timer0
SysCtlClockSet(SYSCTL_SYSDIV_2_5 | SYSCTL_USE_PLL | SYSCTL_XTAL_16MHZ | SYSCTL_OSC_MAIN);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0);
// Set the PF1, PF2, PF3 as output and configure Timer0 to run in periodic mode
GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3 | GPIO_PIN_4 );
TimerConfigure(TIMER0_BASE, TIMER_CFG_A_PERIODIC);
TimerConfigure(TIMER0_BASE, TIMER_CFG_B_PERIODIC);
//TIMER0
//TIMER0A
// Calculate the Time period for 50% duty cycle while switching at "ui32IntFreqency" Hz.
ui32Period = (SysCtlClockGet() / ui32IntFrequency) / 2;
// Load the Timer with the calculated period.
TimerLoadSet(TIMER0_BASE, TIMER_A, ui32Period - 1);
//TIMER0B
// Calculate the Time period for 50% duty cycle while switching at "ui32IntFreqency" Hz.
ui32Period = (SysCtlClockGet() / ui32IntFrequency) / 2;
// Load the Timer with the calculated period.
TimerLoadSet(TIMER0_BASE, TIMER_B, ui32Period - 1);
// Enable the Interrupt specific vector associated with Timer0A
IntEnable(INT_TIMER0A);
// Enables a specific event within the timer to generate an interrupt
TimerIntEnable(TIMER0_BASE, TIMER_TIMA_TIMEOUT);
// Enable the Interrupt specific vector associated with Timer0A
IntEnable(INT_TIMER0B);
// Enables a specific event within the timer to generate an interrupt
TimerIntEnable(TIMER0_BASE, TIMER_TIMB_TIMEOUT);
// FOR ALL TIMERS
// Master interrupt enable API for all interrupts
IntMasterEnable();
// Enable the Timer
TimerEnable(TIMER0_BASE, TIMER_A);
TimerEnable(TIMER0_BASE, TIMER_B);
while (1);
}
/* ----------------------- Function Definition --------------------- */
void Timer0AIntHandler(void)
{ // The ISR for Timer0 Interrupt Handling
// Clear the timer interrupt
TimerIntClear(TIMER0_BASE, TIMER_TIMA_TIMEOUT);
// Read the current state of the GPIO pin and write back the opposite state
if (GPIOPinRead(GPIO_PORTF_BASE, GPIO_PIN_1)){
// GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3, 0);
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1,0);
} else{
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1, GPIO_PIN_1);
}
}
void Timer0BIntHandler(void)
{ // The ISR for Timer0 Interrupt Handling
// Clear the timer interrupt
TimerIntClear(TIMER0_BASE, TIMER_TIMB_TIMEOUT);
// Read the current state of the GPIO pin and write back the opposite state
if (GPIOPinRead(GPIO_PORTF_BASE, GPIO_PIN_2)){
// GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3, 0);
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2,0);
} else{
GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, GPIO_PIN_2);
}
}