Need help in using adc of pic microcontroller!!

Discussion in 'Embedded Systems and Microcontrollers' started by corefinder, Mar 24, 2013.

  1. corefinder

    Thread Starter Member

    Oct 6, 2011
    55
    0
    hi, i am newbie in programming. I want to make a program in c for pic 16f887. In this program i am using port A RA0 pin as analog input and switching led. Here my intention is to apply sin wave of 5v to analog pin and comparing it with 3v and if its greater than it then led will be on. I made a program with microC Pro and its giving errors kindly help me in making this program functional.
    My program

    Code ( (Unknown Language)):
    1.  
    2. led sfr sbit porta.ra1             // declaring port A pin 1 as led
    3. unsigned char val;               // declaring 8 bit variable val
    4. void main()
    5. {
    6. porta=0x00;                         // clearing port A
    7. ansel=0x01;                        // making Port A pin 0 as analog input
    8. trisa=0x01;                       // making Port A pin 0 as input and other output
    9. while(1)
    10. {
    11. adcon1=0x00;                     // cleared all bits of adcon1
    12. adcon0=0b11000001;           // using internal rc of pic and enabling adc
    13. adresh=0x00;                     // adresh register is cleared
    14. godone=1;                        // starting conversion
    15. while(godone=1);                 // waiting till conversion is complete
    16. val=adresh;                       // putting the value of conversion into val
    17. if(val<3)                           // comparing with 3 volts
    18. led=0;
    19. else
    20. led=1;
    21. }
    22.  
     
    Last edited by a moderator: Mar 24, 2013
  2. corefinder

    Thread Starter Member

    Oct 6, 2011
    55
    0
    i have attached the image regarding errors generated by microProC
     
  3. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,016
    Please use code tags when posting code:

    http://forum.allaboutcircuits.com/showthread.php?t=37117

    I'll take a look at this when I get back from an errand (if someone does not get to it).

    But in the mean time post your whole page of code (WITH code tags). Also show which lines are causing the errors (you only need to show the first few).

    And you should be able to do a text copy of your errors. Right click on the error window. Text is easier to read than a screen shot.
     
  4. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    I'll have to get to my computer to do anything more, but your while loop's condition is not a condition.

    In C, an equality is tested with ==, as in
    Code ( (Unknown Language)):
    1. while (godone == 1);
     
  5. corefinder

    Thread Starter Member

    Oct 6, 2011
    55
    0
    Code ( (Unknown Language)):
    1.  
    2. led sfr sbit porta.ra1       // declaring port A pin 1 as led
    3. unsigned char val;            // declaring 8 bit variable val
    4. void main()
    5. {
    6. porta=0x00;                   // clearing port A
    7. ansel=0x01;                   // making Port A pin 0 as analog input
    8. trisa=0x01;                   // making Port A pin 0 as input and other output
    9. while(1)
    10. {
    11. adcon1=0x00;                  // cleared all bits of adcon1
    12. adcon0=0b11000001;            // using internal rc of pic and enabling adc
    13. adresh=0x00;                  // adresh register is cleared
    14. godone=1;                     // starting conversion
    15. while(godone==1);              // waiting till conversion is complete
    16. val=adresh;                   // putting  the value of conversion into val
    17. if(val<3)                     // comparing with 3 volts
    18. led=0;
    19. else
    20. led=1;
    21. }
    22.  
    ERRORS
    1 402 ; expected, but 'sfr' found MyProject.c
    1 402 ; expected, but '.' found MyProject.c
    1 393 'porta' Identifier redefined MyProject.c
    1 371 Specifier needed MyProject.c
    1 396 Invalid declarator expected'(' or identifier MyProject.c
    13 324 Undeclared identifier 'godone' in expression MyProject.c
    14 324 Undeclared identifier 'godone' in expression MyProject.c
    15 324 Undeclared identifier 'val' in expression MyProject.c
    16 324 Undeclared identifier 'val' in expression MyProject.c
    0 102 Finished (with errors): 24 Mar 2013, 22:25:57 MyProject.mcppi
     
  6. corefinder

    Thread Starter Member

    Oct 6, 2011
    55
    0
    Ok thanks but still errors are coming. Also I think I dont know how to assign variable to particular bit of port. In errors its not recognizing sfr and godone is also not recognised. Godone is a bit of adcon0 register. I am wondering maybe I should have assigned variable to that bit and then used it instead using it directly...
     
  7. takao21203

    Distinguished Member

    Apr 28, 2012
    3,577
    463
    I have no idea where OP found information about
    Code ( (Unknown Language)):
    1. porta.ra1
    in any kind.

    Normally all functions registers are CAPITALIZED.

    You can use small caps for your own variables, so this is why defines are capitalized.

    Also OP does not seem to know anything about C define:
    Code ( (Unknown Language)):
    1. #define
    I suggest OP examines an example C source code, and some C tutorials.
     
  8. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,016
    The purpose of using code tags is to keep the formatting of your code You should post code with indents intact. It make it easier to read.

    Which lines are causing which errors? The line numbers mean nothing to us.

    Check your compiler documentation for godone. You can also check the header file for your particular pic.

    What compiler are you using?

    In XC8 or C18 it would be:

    ADCON0bits.GO_DONE
     
  9. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,016
    This might be all wrong:

    Code ( (Unknown Language)):
    1.  
    2. porta=0x00;                   // clearing port A
    3. ansel=0x01;                   // making Port A pin 0 as analog input
    4. trisa=0x01;                   // making Port A pin 0 as input and other output
    5.  
    Should be

    Code ( (Unknown Language)):
    1.  
    2. PORTA=0x00;                   // clearing port A
    3. ANSEL=0x01;                   // making Port A pin 0 as analog input
    4. TRISA=0x01;                   // making Port A pin 0 as input and other output
    5.  
    C is case dependent. Where did you get this code? Do you know anything at all about C? If not I highly suggest that you start there.
     
  10. corefinder

    Thread Starter Member

    Oct 6, 2011
    55
    0
    I did program in microPro C.
     
  11. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,016
    Check your documentation. Something tells me all of your code is wrong. All C compilers are case sensitive. Most likely those SFR variables need to be capitals.
     
  12. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,394
    1,606
    mikroC uses a hidden dot-C file to define items specific to the PIC you are using. They are kept at ...\Program Files\Mikroelektronika\mikroC PRO for PIC\Defs and the the PIC16F887 the file is P16F877.c: note the part number in the file name.

    If you open this file you will see how all the register constants are defined. Your code should use the same names same spelling same capitalization,
     
  13. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    5,435
    1,305
    led sfr sbit porta.ra1 // declaring port A pin 1 as led

    That line is causing 90% of your problems.

    The correct form in MikroC PRO is;
    led sfr sbit PORTA.F1; // declaring port A pin 1 as led

    and NOTE that I also added the critical ; symbol at the end that was stopping the next line being recognised.

    The only other problem is you are missing a closing brace } symbol that ends your while() loop.
     
Loading...