Rakibul Hasan
Rakibul Hasan

Reputation: 108

Can not take Analog read in PIC 12F675

In my code I want to read voltage from a battery and if the voltage greater than 3V then it off the LEDs.But the LED always on even when the voltage is 5v and when I add more LED in different GPIO and turn them 0/1 then the GPIO2 become off. I am using PIC12F675.
AN1 is my analog reader pin which is GPIO1.
GPI02 is my LED output pin.

// CONFIG
#pragma config FOSC = INTRCIO   // Oscillator Selection bits (INTOSC oscillator: I/O function on GP4/OSC2/CLKOUT pin, I/O function on GP5/OSC1/CLKIN)
#pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF      // Power-Up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = ON     // MCLR
#pragma config BOREN = OFF      // Brown-out Detect Enable bit (BOD disabled)
#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.
// Use project enums instead of #define for ON and OFF.

#include <xc.h>

#define _XTAL_FREQ 4000000  // 4MHZ crystal

void main(void) {
    unsigned int adcVal;
    double voltage;
    int i=0;
    TRISIObits.TRISIO1 = 1;
    TRISIObits.TRISIO2 = 0;
    TRISIObits.TRISIO3 = 0;

    ANSELbits.ADCS0 = 1;
    ANSELbits.ADCS1 = 0;
    ANSELbits.ADCS2 = 1; //FOSC/16
    ANSELbits.ANS1 = 1; //channel 2
    ADCON0bits.CHS0 = 1;
    ADCON0bits.CHS1 = 0; //AN1
    ADCON0bits.ADON = 1; //Turn it on
    ADCON0bits.GO = 1;
    ADCON0bits.ADFM = 1;
    while (1) {
        __delay_us(5);
        ADCON0bits.ADON = 1;
        GO_nDONE = 1;
        while (GO_nDONE); //Wait for ADC to complete
        adcVal = (((unsigned int) ADRESH << 8) + ADRESL);
        ADCON0bits.ADON = 0;
        voltage = ((double) (adcVal / 1023)*5.0);

        if (voltage >= 3.0) {
            GPIObits.GP2 = 0; //LED off      

        } else {
            GPIObits.GP2 = 1; // LED On/                      
        }
    }
}

Upvotes: 1

Views: 675

Answers (1)

Mike
Mike

Reputation: 4288

Your calculation adcVal/1023 is an integer calculation and for a range 0 ... 1023 for adcVal always 0 (or 1 for 1023).

Best thing is to avoid the float calculatin at all:

// CONFIG
#pragma config FOSC = INTRCIO   // Oscillator Selection bits (INTOSC oscillator: I/O function on GP4/OSC2/CLKOUT pin, I/O function on GP5/OSC1/CLKIN)
#pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF      // Power-Up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = ON     // MCLR
#pragma config BOREN = OFF      // Brown-out Detect Enable bit (BOD disabled)
#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.
// Use project enums instead of #define for ON and OFF.

#include <xc.h>

#define _XTAL_FREQ 4000000  // 4MHZ crystal

void main(void) {
    unsigned int adcVal;
    double voltage;
    int i=0;
    TRISIObits.TRISIO1 = 1;
    TRISIObits.TRISIO2 = 0;
    TRISIObits.TRISIO3 = 0;

    ANSELbits.ADCS0 = 1;
    ANSELbits.ADCS1 = 0;
    ANSELbits.ADCS2 = 1; //FOSC/16
    ANSELbits.ANS1 = 1; //channel 2
    ADCON0bits.CHS0 = 1;
    ADCON0bits.CHS1 = 0; //AN1
    ADCON0bits.ADON = 1; //Turn it on
    ADCON0bits.GO = 1;
    ADCON0bits.ADFM = 1;
    while (1) {
        __delay_us(5);
        ADCON0bits.ADON = 1;
        GO_nDONE = 1;
        while (GO_nDONE); //Wait for ADC to complete
        adcVal = (((unsigned int) ADRESH << 8) + ADRESL);
        ADCON0bits.ADON = 0;

        if (adcVal  >= 614) {            //value for 3.0V
            GPIObits.GP2 = 0; //LED off      

        } else {
            GPIObits.GP2 = 1; // LED On/                      
        }
    }
}

Upvotes: 1

Related Questions