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.

清华大学TM4C123微处理器原理与实践例程-按键实验程序解析



//按键实验程序解析
//头文件
#include <stdint.h>
#include <stdbool.h>
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "driverlib/sysctl.h"
#include "driverlib/rom.h"
#include "driverlib/gpio.h"
#include "driverlib/fpu.h"
#include "driverlib/pin_map.h"
#include "inc/tm4c123gh6pge.h"

int main (void)
{
  //使能FPU
  FPUEnable();
  FPULazyStackingEnable();

  //配置系统时钟为50MHz
  SysCtlClockSet(SYSCTL_SYSDIV_4 |SYSCTL_USE_PLL
                |SYSCTL_OSC_MAIN |SYSCTL_XTAL_16MHZ);
  //使能外设PORTM
  ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOG);
  ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOM);
  //配置PG2为输出口,PM4为输入口,且有弱上拉。
  ROM_GPIOPinTypeGPIOOutput(GPIO_PORTG_BASE,GPIO_PIN_2);
  ROM_GPIOPinTypeGPIOInput(GPIO_PORTM_BASE,GPIO_PIN_4);
  ROM_GPIOPadConfigSet(GPIO_PORTM_BASE, GPIO_PIN_4,
                         GPIO_STRENGTH_2MA,     
                         GPIO_PIN_TYPE_STD_WPU);
while(1)
{
  //如果不按下按键(PM4输入为高), User LED灭
if(ROM_GPIOPinRead(GPIO_PORTM_BASE,GPIO_PIN_4)&&GPIO_PIN_4)
ROM_GPIOPinWrite(GPIO_PORTG_BASE,GPIO_PIN_2,0); 
  //如果按下按键(PM4输入为低), User LED亮
else
  ROM_GPIOPinWrite(GPIO_PORTG_BASE,GPIO_PIN_2,GPIO_PIN_2);
}
}