我正在使用一个12F675 PIC,我已经编程它使3个LED闪光灯的顺序。我希望它只在没有收到特定的输入时才这样做。PIC在3v3上运行,当我测试它时,这似乎已经足够驱动芯片和LED了。输入是从另一个LED在另一个不同的PCB,基本上我想检测它是开还是关。它的灯在2v5,所以这是输入到我的输入引脚。当我测试它时,关闭2v5源或从关闭它开始并不会触发3个LED闪光灯。这是我的密码:
/*
* File: main.c
*/
// PIC12F675 Configuration Bit Settings
// 'C' source line config statements
// CONFIG
#pragma config FOSC = INTRCIO // Oscillator Selection bits (INTOSC oscillator: CLKOUT function on GP4/OSC2/CLKOUT pin, I/O function on GP5/OSC1/CLKIN)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT enabled)
#pragma config PWRTE = ON // Power-Up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = OFF // GP3/MCLR pin function select (GP3/MCLR pin function is MCLR)
#pragma config BOREN = ON // Brown-out Detect Enable bit (BOD enabled)
#pragma config CP = OFF // Code Protection bit (Program Memory code protection is disabled)
#pragma config CPD = OFF // Data Code Protection bit (Data memory code protection is disabled)
// #pragma config statements should precede project file includes.
#define LED_GRN GP0
#define LED_YLW GP1
#define LED_RED GP2
#define LED_IN GP3 // I don't think this is used but it's here to show which pin the IO is on.
#define _XTAL_FREQ 4000000
#include <xc.h>
#include <stdlib.h>
#include <htc.h>
void init_ports(void) {
ANSEL = 0X00; // Set ports as digital IO not analog input.
ADCON0 = 0x00; // Shut off ADC.
CMCON = 0x07; // Shut off comparator.
VRCON = 0x00; // Shut off the voltage reference.
TRISIO = 0x08; // All GPIO pins output except 3.
GPIO = 0x00; // Make all pins LOW.
}
void main(void) {
init_ports();
// Wait ~ 1s
__delay_ms(1000);
// Only flash LEDs if the backlight is off.
// Do nothing while the backlight is on.
while (GPIO & 0b001000);
if (GPIO & 0b000000) {
LED_GRN = 1;
__delay_ms(200);
LED_GRN = 0;
__delay_ms(5000);
LED_YLW = 1;
__delay_ms(200);
LED_YLW = 0;
__delay_ms(1000);
LED_RED = 1;
__delay_ms(200);
LED_RED = 0;
}
}我的代码有什么地方是错误的,还是2v5太低低于芯片的工作电压而被检测为接收输入?如果是后者,我是否可以以某种方式在代码中设置不同的阈值,或者使用比较器来检测超过~2v的阈值?或者我还有别的办法来解决这个问题?
更新:现在我真的很困惑,我已经在芯片上加载了一些代码来闪烁绿色的if (GPIO & 0b000000)、闪烁的黄色if (GPIO & 0b001000)和闪烁的红色else。它闪烁黄色,然后闪烁红色,无论背光是否开着。这令我感到莫名其妙,因为afaict不应该有"if“条件和”afaict“两种情况。
发布于 2020-12-10 10:03:40
您的代码中有一个问题:if (GPIO & 0b000000)。
您可以按位和在某物和0之间执行操作--这始终是0,而0总是计算为false。
你想要这样的东西:if ((GPIO & 0b001000) == 0b001000)或if ((GPIO & 0b001000) == 0b000000)
&或==是否有优先权。另一方面,你应该在电压水平上很好。在您链接的数据表第8页上,它说GP3有一个TTL输入缓冲器,在第93页上,它说TTL缓冲器的输入高压为(0.25 VDD+0.8) = 1.625V (带有3.3VDD)。
当然,你也应该测量一下,你实际上有2.5V的电压,因为二极管掉在LED上可能会把你压在下面。
发布于 2020-12-10 09:52:18
首先隔离问题:
这应该给出答案--是电压等级问题,还是软件问题,还是其他地方的硬件问题。
https://stackoverflow.com/questions/65231319
复制相似问题