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.
工具与软件:
您好!
我正在尝试编写一个代码来测量通过 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) { } }
您好!
您知道它中断的基准电压是多少吗? 您可以尝试使用等于0V 的基准电压吗? 我想知道是否已经进入中断 ISR。
ref = 0V 时的行为相同。 只要直流电源的+端子连接到 PC6、LED 就会熄灭、当断开时、无论引脚的电压为 I SUPPLY、它都会亮起。 但在 ref = 0.275V 或更高时、无论电源电压如何、LED 都保持导通状态。
此外、这里是我用于提供直流电压的 virtualbench 设置。 我使用 DMM、看到它正在提供正确的电压。 我的电源有什么问题吗?
我的代码 是否应该包括 ComparatorIntRegister 函数?
你可以尝试一下、但我认为这不是问题、因为你已经静态地插入了 ISR。 ComparatorIntRegister 是插入 ISR 矢量的动态方法。
您好!
"我要你帮我舔一舔。"
头奖。 我应该对 C0-使用 PC7、而不是 PC6。 此外、ComparatorConfigure 的索引应为0、而不是 COMP 0的1。 谢谢!