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_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/sysctl.h"
#include "driverlib/comp.h"
#include "driverlib/rom.h"
#include "grlib/grlib.h"

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

      //设置系统时钟为50MHz (400/2/4=50)
  ROM_SysCtlClockSet(SYSCTL_SYSDIV_1|SYSCTL_USE_OSC
                    |SYSCTL_OSC_MAIN|SYSCTL_XTAL_16MHZ);

  //使能需要的外设
  ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_COMP0);
  ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOC);
  ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOG);
  ROM_GPIOPinTypeComparator(GPIO_PORTC_BASE, GPIO_PIN_6 | 
                             GPIO_PIN_7);
  ROM_GPIOPinTypeGPIOOutput(GPIO_PORTG_BASE, GPIO_PIN_2);
  //配置比较器
  ROM_ComparatorConfigure(COMP_BASE,0,
                              
  COMP_TRIG_NONE|COMP_INT_BOTH|COMP_ASRCP_REF);
  //配置比较器参考电压为2.0625V
  ROM_ComparatorRefSet(COMP_BASE,COMP_REF_2_0625V);
  //中断使能
  ROM_ComparatorIntEnable(COMP_BASE,0);

while(1)
 {
  //获取比较结果并据此设置LED的亮灭
  if(ROM_ComparatorValueGet(COMP_BASE,0))
   ROM_GPIOPinWrite(GPIO_PORTG_BASE, GPIO_PIN_2, GPIO_PIN_2);
  else
  	ROM_GPIOPinWrite(GPIO_PORTG_BASE, GPIO_PIN_2, 0);
}
}