Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Problems with AD converter

Status
Not open for further replies.

yohanevindra

New Member
Im trying to make a simple program light the LEDSs based on the AD conversion value, which will be the position of the variable resistor which is connected to Port A0 on the PICDEM2PLUS board.

This is my code
Code:
#include <p18f4520.h>

#define LED1 0x01
#define LED2 0x02
#define LED3 0x03
#define LED4 0x04

char table[4];

//#pragma config

#pragma interrupt adisr
void adisr ()
{
	if (PIR1bits.ADIF = 1)
	{
		PORTB = table[ADRESH];
		PIR1bits.ADIF = 0;
	}
	

}

#pragma code ad_high_pri = 0x0008
void high_prio_int()
{
	_asm
	GOTO adisr 
	_endasm
}

	

#pragma code
void init_config(void)
{
    TRISA  = 0x01; //Make A0 an input
	TRISB  = 0x00;
    ADCON1 = 0x00;
    ADCON2 = 0x00;
	ADCON0 = 0x01; //Select A/D Channel 0 PortA0 and enable A/D module
    IPR1   = 0x00; //Reset ADIF flag
    PIR1   = 0x00;
    RCON   = 0x80;
    PIE1   = 0x40;
    INTCON = 0xA0; //Enabble global interrupts
	T0CON  = 0b11000000;
	ADRESH = 0x00;
	ADRESL = 0x00;


}

void main(void)
{
	table[0] = LED1;
	table[1] = LED2;
	table[2] = LED3;
	table[3] = LED4;
    init_config();
	while (INTCONbits.TMR0IF = 0)
	{
	
	}
	
	while(1);
}

When I insert breakpoints and run it, the ADRESH/ADRESL registers are always 0. What am I doing wrong here?
 
I suspect that you need to revisit your initialisation of the A/D control registers. For a start, the Port B pins 0 and 1 are also mapped to the AN12 and AN10 functions (respectively). This means (as a minimum) that you need to set ADCON1 PCFG bits to a value of at least 5 to make sure that these pins are operating as digital port pins and not A/D inputs. (Generally I like to only enable the A/D ports that I'm actually using so I would use "ADCON1bits.PCFG = 0x0e").

Next I would really doubt if the zero acquisition time select value (ADCON2 ACQT) is correct, especially as you are not selecting the FRC as your source which (according to Note #1 in this section) will add in a single cycle for the acquisition time. I suspect that you need to go back to read up on how to configure the digital and analogue ports.

Is your value for interrupt setup correct? You are enabling the high priority interrupt but also the Timer 0 overflow interrupt; however you are not setting up the timer and not handling the interrupt within the ISR. You are also not setting the A/D interrupt to be high priority in IPR1 (your comment would be more appropriate for the PIR1 register).

Finally, you really don't need to pre-set the output result registers (ADRESH and ADRESL)

Susan
 
I dont think there is a problem with the ADCON1 PCFG value, as the PORTB LEDs light properly when I insert them at points to check that the program has reached that point. For instance, to check that the interrupt ISR was reached, i inserted a command to PORTB = 0x03, and the LEDs light up correctly.

My main problem seems to be that the ADRESH and ADRESL registers dont seem to be getting the values.

About the acquisition values, I initially had it at 20Tad, and reduced it to 0. will try it though.

I have setup the timer, and start it..the timer is jus to give the AD converter time to start up. I dont think you have to do that each time the AD converter performs a conversion. I dont have an ISR for the timer, as all that it does is stay in the loop until the interrupt flag is set...

will try setting the AD interrupt to high prioriy, but since the LEDs light up, I know that it goes into the ISR, so i'm not doing anything wrong there...its just that the values arent getting written to the ADRESH and ADRESL registers..
 
PROBLEM SOLVED!!!!!

you have to restart the AD converter everytime after you finish a conversion. That's why the registers were never updating.
 
Status
Not open for further replies.

Latest threads

Back
Top