Need help in using adc of pic microcontroller!!

Thread Starter

corefinder

Joined Oct 6, 2011
55
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

Rich (BB code):
led sfr sbit porta.ra1             // declaring port A pin 1 as led
unsigned char val;               // declaring 8 bit variable val
void main()
{
porta=0x00;                         // clearing port A
ansel=0x01;                        // making Port A pin 0 as analog input
trisa=0x01;                       // making Port A pin 0 as input and other output
while(1)
{
adcon1=0x00;                     // cleared all bits of adcon1
adcon0=0b11000001;           // using internal rc of pic and enabling adc
adresh=0x00;                     // adresh register is cleared
godone=1;                        // starting conversion
while(godone=1);                 // waiting till conversion is complete
val=adresh;                       // putting the value of conversion into val
if(val<3)                           // comparing with 3 volts
led=0;
else
led=1;
}
 
Last edited by a moderator:

spinnaker

Joined Oct 29, 2009
7,830
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.
 

tshuck

Joined Oct 18, 2012
3,534
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
Rich (BB code):
while (godone == 1);
 

Thread Starter

corefinder

Joined Oct 6, 2011
55
Rich (BB code):
led sfr sbit porta.ra1       // declaring port A pin 1 as led
unsigned char val;            // declaring 8 bit variable val
void main() 
{
porta=0x00;                   // clearing port A
ansel=0x01;                   // making Port A pin 0 as analog input
trisa=0x01;                   // making Port A pin 0 as input and other output
while(1)
{
adcon1=0x00;                  // cleared all bits of adcon1
adcon0=0b11000001;            // using internal rc of pic and enabling adc
adresh=0x00;                  // adresh register is cleared
godone=1;                     // starting conversion
while(godone==1);              // waiting till conversion is complete
val=adresh;                   // putting  the value of conversion into val
if(val<3)                     // comparing with 3 volts
led=0;
else
led=1;
}
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
 

Thread Starter

corefinder

Joined Oct 6, 2011
55
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
Rich (BB code):
while (godone == 1);
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...
 

takao21203

Joined Apr 28, 2012
3,702
I have no idea where OP found information about
Rich (BB code):
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:
Rich (BB code):
#define
I suggest OP examines an example C source code, and some C tutorials.
 

spinnaker

Joined Oct 29, 2009
7,830
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
 

spinnaker

Joined Oct 29, 2009
7,830
This might be all wrong:

Rich (BB code):
porta=0x00;                   // clearing port A
ansel=0x01;                   // making Port A pin 0 as analog input
trisa=0x01;                   // making Port A pin 0 as input and other output
Should be

Rich (BB code):
PORTA=0x00;                   // clearing port A
ANSEL=0x01;                   // making Port A pin 0 as analog input
TRISA=0x01;                   // making Port A pin 0 as input and other output
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.
 

Thread Starter

corefinder

Joined Oct 6, 2011
55
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
I did program in microPro C.
 

spinnaker

Joined Oct 29, 2009
7,830
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.
 

ErnieM

Joined Apr 24, 2011
8,377
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,
 

THE_RB

Joined Feb 11, 2008
5,438
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.
 
Top