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.

[参考译文] TM4C123GH6PM:模拟比较器不工作

Guru**** 2510095 points


请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

https://e2e.ti.com/support/microcontrollers/arm-based-microcontrollers-group/arm-based-microcontrollers/f/arm-based-microcontrollers-forum/1380651/tm4c123gh6pm-analog-comparator-not-working

器件型号:TM4C123GH6PM

工具与软件:

您好!

我正在尝试编写一个代码来测量通过 PC6的电压、看看它是否超过了我在代码中设置的基准电压。 我的代码如下所示、基准电压为0.1375V。 当我通过直流电源调节电压(+端子连接到 PC6、接地端子连接到 TM4C 的接地)时、初始状态实际上没有发生任何变化。 我应该如何修复我的代码、以便每当电压低于基准电压时 、红色 LED 熄灭、当电压低于基准电压时、红色 LED 点亮? 此外、我认为我已经正确地更新了中断的矢量表。

#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include "inc/tm4c123gh6pm.h"
#include "inc/hw_memmap.h"
#include "inc/hw_gpio.h"
#include "driverlib/pwm.h"
#include "driverlib/gpio.h"
#include "driverlib/pin_map.h"
#include "driverlib/ssi.h"
#include "driverlib/adc.h"
#include "driverlib/sysctl.h"
#include "driverlib/uart.h"
#include "utils/uartstdio.h"
#include "driverlib/interrupt.h"
#include "driverlib/comp.h"


void Comparator0IntHandler(void);
void Comparator_Init(void);

void Comparator_Init(void)
{
    // Enable the peripherals for the comparator and GPIO
    SysCtlPeripheralEnable(SYSCTL_PERIPH_COMP0);
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOC);

    // Configure the comparator input pin (e.g., C0+ on PC6)
    GPIOPinTypeComparator(GPIO_PORTC_BASE, GPIO_PIN_6);

    // Configure the internal reference for the comparator
    ComparatorRefSet(COMP_BASE, COMP_REF_0_1375V); // Use the closest available reference voltage

    // Configure the comparator
    ComparatorConfigure(COMP_BASE, 0, COMP_TRIG_NONE | COMP_INT_BOTH | COMP_ASRCP_REF);

    SysCtlDelay(100);


    // Clear any pending comparator interrupts
    ComparatorIntClear(COMP_BASE, 0);

    // Enable comparator interrupt
    ComparatorIntEnable(COMP_BASE, 0);

    // Enable the interrupt in the NVIC (Nested Vectored Interrupt Controller)
    IntEnable(INT_COMP0);
    IntMasterEnable();
}

void Comparator0IntHandler(void)
{
    // Clear the comparator interrupt
    ComparatorIntClear(COMP_BASE, 0);

    // Handle the comparator output (you can add your logic here)
    if (ComparatorValueGet(COMP_BASE, 0))
    {
        // Voltage is above the threshold
        GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1, GPIO_PIN_1);  // Turn on the RED LED
       // UARTprintf("high");
    }
    else
    {
        // Voltage is below the threshold
        GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_1, 0);  // Turn off the RED LED
      //  UARTprintf("low");
    }
}


void main(void)
{
    // Set the system clock to 80 MHz
    SysCtlClockSet(SYSCTL_SYSDIV_2_5 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ);

    // Enable the GPIO port for an LED (for demonstration purposes)
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
    GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_1);

    // Initialize the comparator
    Comparator_Init();

    while (1)
    {

    }
}