1. Soon

    Thread Starter New Member

    Jan 25, 2010
    10
    0
    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;
    }
    }
    }
     
  2. Tahmid

    Active Member

    Jul 2, 2008
    344
    25
    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:
    Code ( (Unknown Language)):
    1. void main()
    2. {
    3. int x;
    4.  
    5.  
    6. TRISA = 0xFF;
    7. TRISB = 0x00;
    8. PORTB = 0x00;
    9. ADCON0 = 0x01;
    10. ADCON1 = 0x05;
    11.  
    12. PIE1bits.ADIE = 0;
    13.   while(1)
    14. {
    15.  
    16.         ADCON0bits.GO = 1;
    17. while(ADCON0bits.GO ==0);  
    18. x = ADRESH;
    19.  
    20.  
    21. if(x>0x7B && x<0x52) // 32cm - 22cm  0x52 - 0x7B
    22.   {
    23. PORTBbits.RB0 = 1;
    24. Delay1KTCYx(1000);
    25. PORTBbits.RB0 = 0;
    26. }
    27. if(x>0x10A && x<0x7C) // 21cm - 10cm  0x7C - 0x10A
    28.   {
    29. PORTBbits.RB1 = 1;
    30. Delay1KTCYx(1000);
    31. PORTBbits.RB0 = 0;
    32. }
    33. if(x>0x214 && x<0x10B) // 9cm - 4cm  0x10B - 0x214
    34.   {
    35. PORTBbits.RB2 = 1;
    36. Delay1KTCYx(1000);
    37. PORTBbits.RB0 = 0;
    38. }
    39. }
    40. }      
    Hopefully, it'll work now.
    Try it and see.
    Tahmid.
     
  3. Soon

    Thread Starter New Member

    Jan 25, 2010
    10
    0
    Hi, if i use RA0 does it affect the program?
     
  4. Tahmid

    Active Member

    Jul 2, 2008
    344
    25
    Hi,
    It should not affect since the program written uses RA0.
     
Loading...