18f452

Thread Starter

Soon

Joined Jan 25, 2010
10
Hi,
I have program a code using C18 by using the function Adcon. I am connecting GP2D120 connecting to 18F452, the code is not working, what is wrong with it? i have set the range to active different led

Here is the code
void main()
{
int x;


TRISA = 0xFF;
TRISB = 0x00;

PIE1bits.ADIE = 1;
while(1)
{

ADCON1 = 0b00000101;
ADCON0bits.GO = 1;
while(PIR1bits.ADIF ==0);
ADCON0bits.GO = 0;
PIR1bits.ADIF =0;
x = ADRESH;




if(x>0x7B && x<0x52) // 32cm - 22cm 0x52 - 0x7B
{
PORTBbits.RB0 = 1;
Delay1KTCYx(1000);
PORTBbits.RB0 = 0;
}
if(x>0x10A && x<0x7C) // 21cm - 10cm 0x7C - 0x10A
{
PORTBbits.RB1 = 1;
Delay1KTCYx(1000);
PORTBbits.RB0 = 0;
}
if(x>0x214 && x<0x10B) // 9cm - 4cm 0x10B - 0x214
{
PORTBbits.RB2 = 1;
Delay1KTCYx(1000);
PORTBbits.RB0 = 0;
}
}
}
 

Tahmid

Joined Jul 2, 2008
343
Hi,
You enabled the interrupt but don't have an interrupt service routine.
Instead, use polling.
You did not set ADCON0 properly.
Change your code to:
Rich (BB code):
void main()
{
int x;


TRISA = 0xFF;
TRISB = 0x00;
PORTB = 0x00;
ADCON0 = 0x01;
ADCON1 = 0x05;

PIE1bits.ADIE = 0;
  while(1)
{

        ADCON0bits.GO = 1;
while(ADCON0bits.GO ==0);  
x = ADRESH;


if(x>0x7B && x<0x52) // 32cm - 22cm  0x52 - 0x7B
  {
PORTBbits.RB0 = 1;
Delay1KTCYx(1000);
PORTBbits.RB0 = 0;
}
if(x>0x10A && x<0x7C) // 21cm - 10cm  0x7C - 0x10A
  {
PORTBbits.RB1 = 1;
Delay1KTCYx(1000);
PORTBbits.RB0 = 0;
}
if(x>0x214 && x<0x10B) // 9cm - 4cm  0x10B - 0x214
  {
PORTBbits.RB2 = 1;
Delay1KTCYx(1000);
PORTBbits.RB0 = 0;
}
}
}
Hopefully, it'll work now.
Try it and see.
Tahmid.
 
Top