RT:
长话短说,PIC新手,高手请轻拍。
代码如下,运行的时候发现AD没有数据出来,也就是AD值一直是0,请帮忙分析下!多谢!#include <xc.h> // include standard header file
// set Config bits
#pragma config CP = OFF, FOSC = INTOSC // Code Protection bit (Code protection off).
#pragma config MCLRE = OFF
#pragma config WDTE = OFF //Watch dog timer off
// Definitions
#define _XTAL_FREQ 4000000 //this is used by the __delay_ms(xx) and __delay_us(xx) functions
//**********************************************************************************
void init( )
{
// PORT A Assignments
TRISAbits.TRISA0 = 1; //RA0 = Analog Voltage In
TRISAbits.TRISA1 = 1; //RA1 = input
IOCANbits.IOCAN1 = 1; //RA1 NEGATIVE EDGE REGISTER IOC
TRISAbits.TRISA2 = 0; //RA2 = output
TRISAbits.TRISA3 = 0; //RA3 = nc (MCLR)
TRISAbits.TRISA4 = 0; //RA4 = output
TRISAbits.TRISA5 = 0; //RA5 = output
// set up oscillator control register.
OSCCONbits.IRCF = 0x0D; //set OSCCON IRCF bits 1101 frequency=4Mhz
OSCCONbits.SCS = 0x02; //set the SCS bits internal oscillator block
// set up INTCON register.
INTCONbits.IOCIE = 1; //set INTCON IOCIE bits Enabled
// set up FVRCON register.
//FVRCONbits.FVREN = 1; //Fixed Voltage Reference Enabled 1.024V
//FVRCONbits.ADFVR = 0x02; //ADC FVR Buffer Gain is 2x, 2 x VFVR = 2.048V
// set up ADC control register.
ANSELAbits.ANSA0 = 1; //Select A0 as analog input pin
ADCON0bits.CHS = 0x00; //Set ADC conversion Channel AN0.
ADCON0bits.ADON = 1; //ADC is on
ADCON1bits.ADCS = 0x04; //Set ADC clock FRC x111. 0x04 = Fosc/4
ADCON1bits.ADFM = 0x01; //Set Data Right justified.
ADCON1bits.ADPREF = 0x00; //Set Vref connected to VDD.
}
//**********************************************************************************
unsigned int Read_ADC_Value(void)
{
unsigned int ADCValue;
__delay_ms(10);
ADCON0bits.GO = 1; //start conversion
while (ADCON0bits.GO) continue;//wait for conversion to finish
ADCValue = ADRESH << 8; //get the 2 msbs of the result and rotate 8 bits to the left
ADCValue = ADCValue + ADRESL; //now add the low 8 bits of the resut into our return variable
return (ADCValue); //return the 10bit result in a single variable
}
//***********************************************************************************
//***********************************************************************************
void main( )
{
unsigned int AnalogValue = 0;
unsigned char i = 0;
init();
while(1)
{
AnalogValue = Read_ADC_Value();
__delay_ms(400);
if(AnalogValue <= 200)
LATAbits.LATA5 ^= 1;
else
LATAbits.LATA5 = 0;
}
}
|