Analog to Digital Conversion in PIC18F4520

Status
Not open for further replies.

meijunfun

New Member
HI,
I need help in my programming. I am using accelerometer which gives analog signal. I need to convert this signal into digital in order for my microcontroller to read. I am having trouble in my programming part, not sure to write the conversion part.


this is the example of my programming:


Is there anything wrong with my programming?


Thanks
 
You have the result right justified and only read ADRESH so you will only get 2 bits of information. You also add 256 to it and test if greater than 255!

Try changing it to,
Code:
void main(){
int t;
    ADCON0 = 0b00000001;//READ ANALOG VOLTAGE FROM RA0
    ADCON1 = 0b00001110;//CONFIGURE ADC, RA0
    ADCON2 = 0b[COLOR="Red"]0[/COLOR]0000001;
    TRISA = 0b00000001;// RA0 INPUT

    while(1){

        Delay10TCYx(12);
        ADCON0bits.GO = 1;
        while(ADCON0bits.DONE);
        t=ADRESH[COLOR="red"]*256+ADRESL[/COLOR];

        if(t>255){

            int x=t;
        }
    }
}

Your variable will now contain 0 to 1023.

Mike.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…