【发布时间】:2018-10-25 04:02:20
【问题描述】:
我正在使用带有 Keil uVision IDE 的 Tiva C 系列开发板。我基本上是在尝试使用中断执行以下操作:
如果未按下开关 1 和开关 2:红色 LED 闪烁。 如果按下开关 1,则进入中断程序并闪烁绿色 LED。
一旦释放开关 1,重新进入主电源以继续闪烁红色 LED。
在下降沿触发时进入中断服务程序。我知道我需要在上升沿触发时以某种方式返回 main,但是我不确定如何执行此操作。
我试图尽我所能发表评论,所以这是我到目前为止的评论:
#include <stdint.h>
#include <stdbool.h>
#include "Final Project.h"
#include "inc/hw_types.h"
#include "inc/hw_memmap.h"
#include "inc/hw_gpio.h"
#include "driverlib/sysctl.h"
#include "driverlib/pin_map.h"
#include "driverlib/gpio.h"
#include "inc/tm4c123gh6pm.h" // manually added by the programmer
#include "driverlib/interrupt.h" // manually added by the programmer
// Interrupt handler
void GPIOPortF_Handler(void)
{
// acknowledge flag for PF0
GPIO_PORTF_ICR_R |= 0x01;
// Switch 1 is pressed
if(GPIOPinRead(GPIO_PORTF_BASE, GPIO_PIN_4)==0x00)
{
// turn off red LED
GPIO_PORTF_DATA_R &= ~0x02;
// turn off blue LED
GPIO_PORTF_DATA_R &= ~0x04;
// while switch 1 is pressed
while(GPIOPinRead(GPIO_PORTF_BASE, GPIO_PIN_4)==0x00)
{
// Turn on green LED
GPIO_PORTF_DATA_R |= 0x08;
// Delay
SysCtlDelay(16000000/3/2);
// Turn off green LED
GPIO_PORTF_DATA_R &= ~0x08;
// Delay
SysCtlDelay(16000000/3/2);
}
}
}
void Interrupt_Init(void)
{
NVIC_EN0_R |= 0x40000000; // enable interrupt 30 in NVIC (GPIOF)
NVIC_PRI7_R &= ~0x00E00000; // configure GPIOF interru
GPIO_PORTF_IM_R |= 0x01; // arm interrupt on PF0
GPIO_PORTF_IS_R &= ~0x01; // PF0 is edge-sensitive
GPIO_PORTF_IBE_R |= 0x01; // PF0 both edges trigger
//GPIO_PORTF_IEV_R &= ~0x01; // PF0 falling edge event
//IntGlobalEnable(); // Globally enable interrupt (without PinMux)
IntMasterEnable(); // Globally enable interrupt (with PinMux)
}
void
PortFunctionInit(void)
{
// Enable Peripheral Clocks
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
// Enable pin PD1 for GPIOInput
GPIOPinTypeGPIOInput(GPIO_PORTD_BASE, GPIO_PIN_1);
// Enable pin PD0 for GPIOOutput
GPIOPinTypeGPIOOutput(GPIO_PORTD_BASE, GPIO_PIN_0);
// Enable pin PF2 for GPIOOutput
GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_2);
// Enable pin PF3 for GPIOOutput
GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_3);
// Enable pin PF0 for GPIOInput
GPIO_PORTF_PUR_R |= 0x01;
// Enable pin PF4 for GPIOInput
GPIO_PORTF_PUR_R |= 0x10;
//First open the lock and select the bits we want to modify in the GPIO commit register.
HWREG(GPIO_PORTF_BASE + GPIO_O_LOCK) = GPIO_LOCK_KEY;
HWREG(GPIO_PORTF_BASE + GPIO_O_CR) = 0x1;
//Now modify the configuration of the pins that we unlocked.
GPIOPinTypeGPIOInput(GPIO_PORTF_BASE, GPIO_PIN_0);
// Enable pin PF1 for GPIOOutput
GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_1);
// Enable pin PF4 for GPIOInput
GPIOPinTypeGPIOInput(GPIO_PORTF_BASE, GPIO_PIN_4);
}
int main(void)
{
// Iinitialize the GPIO ports
PortFunctionInit();
// Configure the GPIOF interrupt
Interrupt_Init();
// Loop forever.
while(1)
{
// Turn on red lED
GPIO_PORTF_DATA_R |= 0x02;
// Delay
SysCtlDelay(16000000/3/2);
// Turn off red lED
GPIO_PORTF_DATA_R &= ~0x02;
// Delay
SysCtlDelay(16000000/3/2);
}
}
【问题讨论】:
标签: c