不能在 PIC 12F675 中进行模拟读取
Can not take Analog read in PIC 12F675
在我的代码中,我想从电池读取电压,如果电压大于 3V,那么它会关闭 LEDs.But 即使电压为 5v,当我在不同的位置添加更多 LED 时,LED 也始终亮着GPIO 并将它们变为 0/1,然后 GPIO2 变为关闭。我正在使用 PIC12F675。
AN1 是我的模拟 reader 引脚,它是 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/
}
}
}
您的计算 adcVal/1023
是一个整数计算,对于范围为 0 ... 1023 的 adcVal
始终为 0(或 1 为 1023)。
最好的办法是完全避免浮点计算:
// 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/
}
}
}
在我的代码中,我想从电池读取电压,如果电压大于 3V,那么它会关闭 LEDs.But 即使电压为 5v,当我在不同的位置添加更多 LED 时,LED 也始终亮着GPIO 并将它们变为 0/1,然后 GPIO2 变为关闭。我正在使用 PIC12F675。
AN1 是我的模拟 reader 引脚,它是 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/
}
}
}
您的计算 adcVal/1023
是一个整数计算,对于范围为 0 ... 1023 的 adcVal
始终为 0(或 1 为 1023)。
最好的办法是完全避免浮点计算:
// 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/
}
}
}