PIC18F4520 ADC問題

作者 ssnow1314 於 2018年02月07日 11:22:45
你好,各位大大,小弟目前使用PIC18F4520,最近在研究ADC轉換,想將input的電壓顯示在LCD上,輸入腳位為RA0,解析度算出5/1023=0.00488,在input為5V時應顯示D=1023,但顯示出的數值卻是64,想請問一下各位大大,這是為什麼,及如何解決這問題,
謝謝。

以下是我的程式:

#include "p18f4520.h"
#include "delays.h"
#include "evm_lcd.h"
#include "adc.h"
#include <stdlib.h>
#include <stdio.h>
#pragma config OSC=HS,BOREN=OFF,BORV=2,PWRT=ON,WDT=OFF,LVP=OFF
#define OSC_CLOCK 10

void main()
{
char LCD_name[] = "Chicken Test 123";
float adc0_volt;
int adc0_dec;
int adc0_volt1;
int adc0_volt2;
char Buf[16];


TRISA = 0b00000001; // Setup RA0=Input RA1=Output


// Initialise LCD
OpenLCD();
WriteCmdLCD(0*01);
LCD_Set_Cursor(0,0);
putsLCD(LCD_name);
Delay10KTCYx(21);


OpenADC(
ADC_FOSC_16 & // A/D clock source set to 32Tosc
ADC_LEFT_JUST & // ADRESH:ADRESL from roght
ADC_20_TAD, // A/D Acquisition time: 20TAD
ADC_CH0 & // Analog Channel0 AN0
ADC_INT_OFF & // ADC Interrupt off
ADC_VREFPLUS_VDD & // Vref+ = VDD
ADC_VREFMINUS_VSS, // Vref- = VSS
0b1110 //ADCON1 AN0=Analog
);

while(1)
{
ConvertADC(); // Start an A/D Conversion
while(BusyADC()); // Wait for Conversion Finished
adc0_dec=ReadADC(); // Read A/D Result

// Convert Floating Point to Decimal
adc0_volt=0.0048875855327468*adc0_dec;
adc0_volt1=(unsigned int)adc0_volt;
adc0_volt2=(unsigned int)((adc0_volt-adc0_volt1)*10);

sprintf(Buf,"D=%4u V=%u.%uV",adc0_dec,adc0_volt1,adc0_volt2);
WriteCmdLCD(0*01);
LCD_Set_Cursor(1,0);
putsLCD(Buf);


Delay10KTCYx(125);

} // End While
}

來自: http://www.microchip.com.tw/newbb/viewtopic.php?forum=1&topic_id=22208&post_id=77768