performance - PIC 16f1827 ADC 转换太慢 XC8

标签 performance microcontroller pic adc xc8

我关注了this Tutorial并更改了我的微 Controller 16f1827 的代码。我还改变了代码的功能。如果 ADC 值超过最大值的一半,则 LED 应亮起。 ADC 值,如果低于一半则关闭 LED。

// CONFIG
#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 LVP = OFF      // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF      // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF      // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF       // Flash Program Memory Code Protection bit (Code protection off)

#include <xc.h>
#include <pic16f1827.h>

#define _XTAL_FREQ 8000000

void ADC_Init()
{
  ADCON0 = 0x81;               //Turn ON ADC and Clock Selection
  ADCON1 = 0x00;               //All pins as Analog Input and setting Reference Voltages
}

unsigned int ADC_Read(unsigned char channel)
{
  if(channel > 7)              //Channel range is 0 ~ 7
    return 0;

  ADCON0 &= 0xC5;              //Clearing channel selection bits
  ADCON0 |= channel<<3;        //Setting channel selection bits
  __delay_ms(2);               //Acquisition time to charge hold capacitor
  GO_nDONE = 1;                //Initializes A/D conversion
  while(GO_nDONE);             //Waiting for conversion to complete
  return ((ADRESH<<8)+ADRESL); //Return result
}

void main()
{
  unsigned int a;
  TRISA = 0xFF;                 //Analog pins as Input
  TRISB = 0x00;                 //Port B as Output
  //TRISC = 0x00;                 //Port C as Output
  ADC_Init();                   //Initialize ADC

  do
  {
    a = ADC_Read(0);            //Read Analog Channel 0
    //PORTB = a;                  //Write Lower bits to PORTB
    //PORTC = a>>8;               //Write Higher 2 bits to PORTC


if(a > 512){
PORTBbits.RB7 = 1;
}else{
PORTBbits.RB7 = 0;
}


    __delay_ms(100);            //Delay
  }while(1);                    //Infinite Loop
}

代码在 XC8 中编译没有错误。问题是 PIC 检测 ADC 变化的速度太慢。如果我将输入引脚连接到正引用值,它会在大约 2 秒的延迟后打开 LED。当我将 ADC 输入更改为 0v 时,也会发生同样的情况。所有变化的检测速度都非常慢。为什么 ADC 工作这么慢?

最佳答案

您链接到的教程使用带有 8MHz 晶体振荡器的 PIC16F877A,而您似乎尝试使用带有内部振荡器的 PIC16F1827,而不是外部振荡器。像您所做的那样仅更改 PIC 头文件是不够的。您还必须设置所需的振荡器模式,并注意两个部分之间不同的任何其他配置选项。我不确定,但我认为16F1827的默认内部振荡器频率是1MHz,而不是8MHz。这可能在某种程度上解释您遇到的问题。

顺便说一句:不要试图伪造你的代码。确保正确配置微 Controller ,否则它们迟早会咬你的屁股。

关于performance - PIC 16f1827 ADC 转换太慢 XC8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39236086/

相关文章:

javascript - 我应该将 jQuery 或 DOM 对象作为参数传递吗? (性能问题)

c++ - 试除法代码在 Windows 上运行 32 位比在 Linux 上运行 64 位快 2 倍

c - 由脉冲决定的 PIC 单片机的输出长度

assembly - 创建一个 ASM 文件,该文件将返回位于 8 个连续寄存器中的字符

c++ - 动态分配的数组或 std::vector

mysql - 索引字段+限制1与唯一字段在性能方面的比较| MySQL

c++ - C++ 函数 WriteFile 和 ReadFile 中参数 LPVOID lpBuffer 使用什么变量类型

linux - 使用 UART 进行流量控制

embedded - PIC I2C 从设备确认数据