无法在PIC 12F675中进行模拟读取

时间:2019-03-25 12:18:51

标签: c microcontroller pic

在我的代码中,我想从电池读取电压,如果电压大于3V,则它会关闭LED,但是即使电压为5v,并且在不同的GPIO中添加更多的LED并打开它们时,LED也会始终点亮0/1则GPIO2变为OFF。我正在使用PIC12F675。
AN1是我的模拟读取器引脚,它是GPIO1。
GPI02是我的LED输出引脚。

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

1 个答案:

答案 0 :(得分:1)

您的计算adcVal/1023是一个整数计算,对于adcVal,范围0 ... 1023始终为0(或1023为1)。

最好的办法是完全避免浮游钙蛋白:

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