繁体   English   中英

需要使用mplab和xc8编译器将模拟输入读取到pic16f688

[英]Need to read analog input to a pic16f688 using mplab and xc8 compiler

我正在使用PIC16f688尝试读取模拟输入,并根据读取的电压来打开或关闭灯。 使用此PIC,我已成功打开和关闭指示灯闪烁。 这是我用于此的代码。

void main() {
    ANSEL = 0b00000000; //All I/O pins are configured as digital
    CMCON0 = 0x07; // Disbale comparators
    TRISC = 0b00000000; // PORTC All Outputs
    TRISA = 0b00001000; // PORTA All Outputs, Except RA3

    do {
        RC0 = 1;
        __delay_ms(500); 
        RC0 = 0;
        __delay_ms(500);
    } while (1); // Infinite Loop
}

在阅读了不同的内容之后,我最终得到了这段代码来尝试读取模拟输入。

#pragma config FOSC = HS      // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF     // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF    // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = OFF    // Brown-out Reset Enable bit (BOR disabled)
#pragma config CPD = OFF      // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config CP = OFF       // Flash Program Memory Code Protection bit (Code protection off)


#include <xc.h>
#include <pic16f688.h>
#define _XTAL_FREQ 8000000
void main() {
    int voltage;
    ANSEL = 0b01000000; //All I/O pins are configured as digital except an6/RC2
    TRISC = 0b00000100; // PORTC All Outputs except RC2
    TRISA = 0b00001000; // PORTA All Outputs, Except RA3

    do {
        ADCON0 = 0xbb; //set to read
        GO_nDONE = 1;
        while(GO_nDONE == 1);
        voltage = (ADRESH << 8) + ADRESL; //get voltage reading
        if(voltage > 500){ //if voltage is greater than 500 out of 1023 turn light on
            RC0 = 1;
        }
        else{
            RC0 = 0;
        }
        __delay_ms(500);
    } while (1); // Infinite Loop
}

当我运行此命令时,无论输入是什么,无论输入何时接地,指示灯都会亮起。

我在MPLab中使用XC8编译器。

尝试使用CMCON0 = 0x07禁用比较器; 那么RC0应该正常工作。

我希望您使用的是外部振荡器,否则会设置错误的内部振荡器!

默认的ADC时钟为Fosc / 2,高电平时为8Mhz,因此将ADCON1设置为Fosc / 16检查数据表。

为什么删除此行?

CMCON0 = 0x07; // Disbale comparators

这是必不可少的!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM