Electronic Projects, forums and more.

Go Back   Electronic Circuits Projects Diagrams Free > Electronics Categories > Micro Controllers


Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc.

Reply
 
LinkBack Thread Tools Display Modes
Old 15th August 2008, 02:05 AM   (permalink)
Default Adc

Ad1con1

--------------------------------------------------------------------------------

HI i am new in c programming,i have one doubt in the below coding.in this coding already set AD1CON2bits.BUFS=0(AD1CON2 = 0x041Fthen why we write code if (AD1CON2bits.BUFS==1),else if(AD1CON2bits.BUFS==0)
pls explain me, i am using P24FJ64GA002


#
void ADCInit()
{
AD1CON1 = 0x80E4; AD1CON2 = 0x041F;
AD1CON3 = 0x1102;
.
.
}
void _ISR _ADC1Interrupt(void)
{
unsigned int Temp;

if (AD1CON2bits.BUFS==1)
{
Temp = ADC1BUF1;
Temp += ADC1BUF3;
Temp += ADC1BUF5;
Temp += ADC1BUF7;
Temp = Temp/4;
Adc_Vo += Temp;
Adc_Vo = Adc_Vo/2;


}
else if(AD1CON2bits.BUFS==0)
{
Temp = ADC1BUF9;
Temp += ADC1BUFB;
Temp += ADC1BUFD;
Temp += ADC1BUFF;
Temp = Temp/4;
Adc_Vo += Temp;
Adc_Vo = Adc_Vo/2;
}
sahanaashwin is offline  
Old 15th August 2008, 03:00 PM   (permalink)
Default

Quote:
Originally Posted by sahanaashwin View Post
Ad1con1

in this coding already set AD1CON2bits.BUFS=0(AD1CON2 = 0x041F
then why we write code if (AD1CON2bits.BUFS==1),
else if(AD1CON2bits.BUFS==0)

pls explain me
The important thing to note here is the equality operator: ==, which differs from the assignment operator: =

The = is used to assign, as you have done with

AD1CON1 = 0x80E4; AD1CON2 = 0x041F;
AD1CON3 = 0x1102;

And this one == is used to test.

this line - if (AD1CON2bits.BUFS==1) - tests the bit BUFS in the AD1CON register, and if it is set (equal to 1,) executes everything between the { and } directly after that line.

IE: if the bit is set then
{
Temp = ADC1BUF1;
Temp += ADC1BUF3;
Temp += ADC1BUF5;
Temp += ADC1BUF7;
Temp = Temp/4;
Adc_Vo += Temp;
Adc_Vo = Adc_Vo/2;
}
will execute.

Or ELSE if the bit is clear (equal to 0) everything after that statement -
else if(AD1CON2bits.BUFS==0) -
and between the {} brackets will execute.

Last edited by BeeBop; 15th August 2008 at 03:01 PM.
BeeBop is offline  
Old 16th August 2008, 02:37 AM   (permalink)
Default

I've messed that up a time or two =O
__________________
"Because I be what I be. I would tell you what you want to know if I
could, mum, but I be a cat, and no cat anywhere ever gave anyone a
straight answer, har har."
Sceadwian is offline  
Old 18th August 2008, 12:09 PM   (permalink)
Default

thank you beebop
sahanaashwin is offline  
Reply

Bookmarks

Thread Tools
Display Modes





All times are GMT. The time now is 04:00 AM.


Electronic Circuits  |  Learning Electronics
Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.

eXTReMe Tracker