Detect digital input from 2v5 src on a chip with 3v3 power?

  8-bit, c++, microcontroller, mplab, pic

I’m using a 12F675 PIC and I have programmed it to make 3 LEDs flash in sequence. I want it to only do this when a particular input is NOT received. The PIC is running at 3v3 and that seems to be enough to power the chip and the LEDs when I test it. The input is from another LED on a different PCB, basically I want to detect whether it is on or off. It lights at 2v5 so that’s the input into my input pin. When I test it out, turning off the 2v5 source or starting with it turned off doesn’t trigger the 3 LEDs to flash. Here’s my code:

/*
 * 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;
    }
}

Is there something about my code that’s wrong, or is 2v5 too low below the operating voltage of the chip to be detected as receiving an input? If the latter, can I somehow set in code a different threshold, or use the comparator somehow to detect above a ~2v threshold? Or is there another way I should fix this problem?

UPDATE: Now I’m really confused, I have loaded some code onto the chip to blink green if (GPIO & 0b000000), blink yellow if (GPIO & 0b001000), and blink red else. It blinks yellow and then blinks red, whether the backlight is on or not. This is baffling to me because afaict it should not be possible for an "if" condition and the "else" to both fire.

Source: Windows Questions C++

LEAVE A COMMENT