Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

MSP430 c language programming doubt

Status
Not open for further replies.

AnuHV

Member level 2
Joined
Nov 4, 2011
Messages
45
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,571
Code:
#include  <msp430x44x.h>
// These need to be global in this example. Otherwise, the
// compiler removes them because they are not used
static unsigned int A0results;
static unsigned int A5results;
static unsigned int A2results;
static unsigned int A3results;
static unsigned int A4results;
int ADC();
void main(void)
{
  WDTCTL = WDTPW+WDTHOLD;  // Stop watchdog timer
  ADC();
}
 ADC() 
{
  P6SEL=0x3D;  
  P5SEL=0X00;
  P5DIR=0XFF;// Enable A/D channel inputs
  ADC12CTL0 = ADC12ON+MSC+SHT0_8;           // ADC12 on, extend sampling time
                                            // to avoid overflow of results
  ADC12CTL1 = SHP+CONSEQ_3;                 // Use sampling timer, repeated seq
  ADC12MCTL0 = INCH_0;                      // ref+=AVcc, channel = A0
  ADC12MCTL2 = INCH_2;                      // ref+=AVcc, channel = A2
   ADC12MCTL3 = INCH_3;                     //ref+=AVcc, channel = A3
  ADC12MCTL4 = INCH_4;
  ADC12MCTL5 = INCH_5+EOS;// ref+=AVcc, channel = A4, end seq.
  ADC12IE = 0x08;                           // Enable ADC12IFG.3
  ADC12CTL0 |= ENC;                         // Enable conversions
  ADC12CTL0 |= ADC12SC;                     // Start conversion
  _BIS_SR(LPM0_bits+GIE);                  // Enter LPM0, enable interrupts
}
#pragma vector=ADC_VECTOR
__interrupt void ADC12ISR (void)
{
  //static unsigned int index = 0;

  A0results = ADC12MEM0;             // Move A0 results, IFG is cleared
  A2results = ADC12MEM2;             // Move A2 results, IFG is cleared
  A3results = ADC12MEM3;             // Move A3 results, IFG is cleared
  A4results = ADC12MEM4;             // Move A4 results, IFG is cleared
   A5results = ADC12MEM5;
  If((0x0A00<=A0results<=0x0Aff)&&(0x0A00<=A2results<=0x0B50)&&(0x0A00<=A3results<=0x0BFF)&&(0x0850<=A4results<=0x09FF)&&(0x0C00<=A5results<=0x0CFF))
     P5OUT=0XFF;
     else 
    P5OUT=0X00;
}
In the above code whenever i execute the if condition the loop is always entering the true condition though the condition does not satisfies.
could someone rectify my mistake please?
 

With this: (0x0A00<=A0results<=0x0Aff) are you trying to check if A0results is between 0x0A00 and 0x0Aff?

C does not do that, unless you have a very 'clever' compiler. Mine doesn't do it.

It does this:

First, it does 0x0A00<=A0results ... this gives false (0) or true (1)
then, the answer from above is compared by <=0x0Aff ... this will always give true because 0 and 1 are less than 0x0Aff.

Does that make sense?

The same of course for all the other variations in your code.

You need to do:

(0x0A00<=A0results && A0results<=0x0Aff)
 

Re: ADC programming in MSP430F449

Hello!

I would suspect the following line:
Code:
If((0x0A00<=A0results<=0x0Aff)&&(0x0A00<=A2results<=0x0B50)&&(0x0A00<=A3results<=0x0BFF)&&(0x0850<=A4results<=0x09FF)&&(0x0C00<=A5results<=0x0CFF))

It's very compact, but t's simply unreadable.
By the way, judging from the first (), are you sure of the result of if(a<= b <= c)?
I would opt for if((a <= b) && (b <= c)) rather than doing it at once. It may work, but I have
never tried and I think I will never try.

By the way, I'm not sure of this, that might be on purpose, but are you sure of ADC12IE = 0x08?
Usually you trigger an interrupt when you got the last sample. In this case, your interrupt will be
triggered when you get data in the ADC12MEM3 register, which might not be what you want.

Dora.
 

Hello!

AnuHV, could you please avoid to post the same message in 2 different threads?
You got twice the same kind of reply, one from FoxyRick and one from me, which
means that one of us lost time sending a reply that was already on the forum.

Thanks,

Dora.
 

AnuHV, could you please avoid to post the same message in 2 different threads?
You got twice the same kind of reply, one from FoxyRick and one from me, which
means that one of us lost time sending a reply that was already on the forum.

Duplicate threads merged into single thread.

BigDog
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top