1. 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.
    Dismiss Notice

urgent help in Code!!!!

Discussion in 'Microcontrollers' started by engkhlaif, Mar 15, 2008.

  1. engkhlaif

    engkhlaif New Member

    Joined:
    Nov 26, 2007
    Messages:
    67
    Likes:
    1
    i want to write a c code that configure and gp2d12 IR sensor that will be used to detect the distance of the moving object and then convert the analog voltage into digital and then into distance
    iam using the MPLAB and C language
    when i use the folowing code the reading always gives
    voltage=0;
    i dont know why
    pls ny one can help me

    #include <pic.h>
    #include "lcd.h"
    #include "delay.h"
    #include<stdio.h>

    //setup the configuration word for use with ICD2

    __CONFIG(DEBUGEN & WDTDIS & LVPDIS ) ;


    void init_a2d(void){
    ADCON0=0; // select Fosc/2
    ADCON1=0; // select left justify result. A/D port configuration 0
    ADON=1; // turn on the A2D conversion module
    }

    /* Return an 8 bit result */
    unsigned char read_a2d(unsigned char channel){
    channel&=0x07; // truncate channel to 3 bits
    ADCON0&=0xC5; // clear current channel select
    ADCON0|=(channel<<3); // apply the new channel select
    ADGO=1; // initiate conversion on the selected channel
    while(ADGO)continue;
    return(ADRESH); // return 8 MSB of the result
    }


    void main (void)
    {

    unsigned char outString[20];
    float voltage;
    unsigned char x;
    init_a2d(); // initialise the A2D module
    GIE=0;
    TRISE = 0 ;
    TRISD = 0 ;
    TRISB = 0 ;
    TRISA = 0 ;
    ADCON1 = 7 ;
    RE2 = 0 ;
    RA0=1;
    DelayMs(500) ;
    lcd_init() ;

    while(1)
    {
    lcd_clear() ;
    lcd_goto(0) ;
    lcd_puts("welcome " ) ;
    x=read_a2d(1);
    voltage=(x/255.0)*(5.0);
    sprintf(outString,"A2D = %d.%d volts",voltage);
    lcd_puts(outString);
    //lcd_puts("Real time ") ;
    DelayMs(1500);

    }

    }
     
  2. engkhlaif

    engkhlaif New Member

    Joined:
    Nov 26, 2007
    Messages:
    67
    Likes:
    1
    no one can help me in the problem
     
  3. futz

    futz Active Member

    Joined:
    Sep 15, 2007
    Messages:
    2,043
    Likes:
    24
    Location:
    Vancouver, B.C.
    Please post code with Code tags. Simply click on # in the Advanced Posting menu before pasting your code.

    Like this:
    Code (text):
    #include <pic.h>
    #include "lcd.h"
    #include "delay.h"
    #include <stdio.h>

    //setup the configuration word for use with ICD2

    __CONFIG(DEBUGEN & WDTDIS & LVPDIS ) ;

    void init_a2d(void)
    {
        ADCON0=0;           // select Fosc/2
        ADCON1=0;           // select left justify result. A/D port configuration 0
        ADON=1;             // turn on the A2D conversion module
    }

    /* Return an 8 bit result */
    unsigned char read_a2d(unsigned char channel)
    {
        channel&=0x07;          // truncate channel to 3 bits
        ADCON0&=0xC5;           // clear current channel select
        ADCON0|=(channel<<3);       // apply the new channel select
        ADGO=1;             // initiate conversion on the selected channel
        while(ADGO)continue;
        return(ADRESH);         // return 8 MSB of the result
    }

    void main (void)
    {
        unsigned char outString[20];
        float voltage;
        unsigned char x;
        init_a2d();         // initialise the A2D module
        GIE=0;
        TRISA = TRISB = TRISD = TRISE = 0;
        ADCON1 = 7;
        RE2 = 0;
        RA0=1;
        DelayMs(500);
        lcd_init();

        while(1)
        {
            lcd_clear();
            lcd_goto(0);
            lcd_puts("welcome ");
            x=read_a2d(1);
            voltage=(x/255.0)*(5.0);
            sprintf(outString,"A2D = %d.%d volts",voltage);
            lcd_puts(outString);
            //lcd_puts("Real time ");
            DelayMs(1500);
        }
    }
    Unfortunately with C code the 8-space tabs used by this site will usually mess up your 4-space (or 2 or 3 or whatever you use) C tabs somewhat. But it's better than no indentation at all. :D
     
    Last edited: Mar 15, 2008
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. engkhlaif

    engkhlaif New Member

    Joined:
    Nov 26, 2007
    Messages:
    67
    Likes:
    1

    Unfortunately with C code the 8-space tabs used by this site will usually mess up your 4-space (or 2 or 3 or whatever you use) C tabs somewhat. But it's better than no indentation at all. :D[/QUOTE]

    i dont understand ur openion!!!!!!!!!!!!
     
  6. futz

    futz Active Member

    Joined:
    Sep 15, 2007
    Messages:
    2,043
    Likes:
    24
    Location:
    Vancouver, B.C.
    Scroll up and look at your original post and then the indented C code in mine. Think about it for a bit. It'll come to you. :p

    I have a GP2D12 here and was thinking of writing some code for it anyway. In C even. But that doesn't help you right now. I may have it working by tonight. Maybe...
     
  7. engkhlaif

    engkhlaif New Member

    Joined:
    Nov 26, 2007
    Messages:
    67
    Likes:
    1
     
  8. engkhlaif

    engkhlaif New Member

    Joined:
    Nov 26, 2007
    Messages:
    67
    Likes:
    1
    i have attached all thee needed file to compile my project
    iam using MPLAB v7.31 with c language
     

    Attached Files:

    • ADC.c
      File size:
      1.4 KB
      Views:
      160
    • delay.c
      File size:
      266 bytes
      Views:
      104
    • delay.h
      File size:
      543 bytes
      Views:
      114
    • lcd.c
      File size:
      1.6 KB
      Views:
      127
    • lcd.h
      File size:
      309 bytes
      Views:
      116
  9. futz

    futz Active Member

    Joined:
    Sep 15, 2007
    Messages:
    2,043
    Likes:
    24
    Location:
    Vancouver, B.C.
    Well you've procrastinated maybe a bit too much. :D

    Which PIC are you using? I don't see the def anywhere in the code.
     
  10. engkhlaif

    engkhlaif New Member

    Joined:
    Nov 26, 2007
    Messages:
    67
    Likes:
    1
    u know what i have worked for it since 3 days or more and i cant took the result i need
    so pls if u can help me !!!!!! i will appreciate that for u man
     
  11. engkhlaif

    engkhlaif New Member

    Joined:
    Nov 26, 2007
    Messages:
    67
    Likes:
    1
    PIC16F877A
     
  12. futz

    futz Active Member

    Joined:
    Sep 15, 2007
    Messages:
    2,043
    Likes:
    24
    Location:
    Vancouver, B.C.
    The only C I have is for 18F and 24F/30F. The only one I've actually used at all is C30 for 24F/30F. I do all my 16F stuff in assembler. So I'll work at that and see if I can come up with something you can sort of port to 16F. No promises. I might get interrupted in an hour or two and nothing more will happen today.
     
  13. engkhlaif

    engkhlaif New Member

    Joined:
    Nov 26, 2007
    Messages:
    67
    Likes:
    1
    thanx for every thing man
     
  14. atferrari

    atferrari Well-Known Member

    Joined:
    Oct 8, 2003
    Messages:
    2,812
    Likes:
    121
    Location:
    Buenos Aires - Argentina
  15. engkhlaif

    engkhlaif New Member

    Joined:
    Nov 26, 2007
    Messages:
    67
    Likes:
    1
    • Like Like x 1
  16. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,016
    Likes:
    317
    Location:
    Brisbane Australia
    You chose a bad title for your thread. Some people get funny about it. Personally, your question was valid and I have had a quick look at your code.

    The thing that sticks out is ADCON1=7; this sets all analogue pins to digital.

    Mike.
     
  17. blueroomelectronics

    blueroomelectronics Well-Known Member

    Joined:
    Jan 21, 2007
    Messages:
    12,536
    Likes:
    168
    Location:
    Toronto, Canada
    I'm one of those "funny" people, I find "urgent" = school assignment. The OP should spend a moment figuring out how to use the debugger, it's a powerful tool when used properly.
     
  18. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,016
    Likes:
    317
    Location:
    Brisbane Australia
    But, the guy has written code and asked for help debugging it. What is wrong with that? As I said, he picked the wrong title, the content of his post was fine.

    I'm starting to see less and less tolerance of newbies and this is not good. Everyone started somewhere. The guys that post the "PLLLEEEEEZZZZZ" posts never get anywhere, but it now seems to be spreading to what I consider reasonable posts for a newbie. Lighten up guys.

    Mike.
     
  19. futz

    futz Active Member

    Joined:
    Sep 15, 2007
    Messages:
    2,043
    Likes:
    24
    Location:
    Vancouver, B.C.
    I've got my 30F4013 C30 code for GP2D12 partly working if you want a look. The 16-bit PIC is quite different from the 8-bit one you're using though.

    View attachment gp2d12_test.c

    I did find a link to some A/D code for 16F877 though. Only difference between 877 and 877A is that the A model has some new comparators (I think).

    Also, Nigel's tutorials have info on doing this.
     
    Last edited: Mar 16, 2008
  20. engkhlaif

    engkhlaif New Member

    Joined:
    Nov 26, 2007
    Messages:
    67
    Likes:
    1
    first i want to thank u for ur hint about the tittle
    u mean that i have to change ADCON1=7 to ADCON1=0 or what
    u know i dont know what to do !!!
    i have headache from this problem and tomoro i have to submit it to my instructor
     
  21. blueroomelectronics

    blueroomelectronics Well-Known Member

    Joined:
    Jan 21, 2007
    Messages:
    12,536
    Likes:
    168
    Location:
    Toronto, Canada
    The answer is on page 128 of the 16F877A datasheet.
     

Share This Page