Jump to content
43oh

A simple ADC test


Recommended Posts

I'm trying to get a simple ADC program to work but it's not going so well.

Right now I just want the LED to go off lol.

 

#include 
#include 


void main(void)
{
WDTCTL = WDTPW + WDTHOLD;

P1DIR = BIT1;				//LED
P1OUT = BIT1;

ADC10CTL0 &= ~SREF_7;
ADC10CTL0 |= SREF_0;

ADC10CTL0 |= ADC10SHT_0;
ADC10CTL1 |= INCH_0;
ADC10AE0 |= 0x01;

while(1)
{

	ADC10CTL1 |= INCH_0;
	ADC10AE0 |= 0x01;
	ADC10CTL0 |= ADC10ON + ENC;
	ADC10CTL0 |= ADC10SC;

	while ((ADC10CTL1 & ADC10BUSY) != 1)
	{
	}

	if(ADC10MEM > 1)	//read adc on P1.0
	{
		P1OUT = 0;
	}
}
}

 

A lot of the code is taken from the EasyMSP ADC10.h file.

 

post-7895-135135530691_thumb.jpg

Link to post
Share on other sites

turd,

 

Try this. I found out that the ADC10 had to be turned (using ADC10ON) on BEFORE setting the analog channel select bits. Also ENC should be cleared after each conversion.

 

void main(void)
{
  WDTCTL = WDTPW + WDTHOLD; /* Put the Watchdog on hold */

  P1DIR = BIT0;            //LED
  P1OUT = BIT0;

  ADC10AE0 = BIT3; /* Enable the analog channel on P1.3 (Button) */
  ADC10CTL0 = ADC10ON; /* Turn on the ADC10 logic */
  ADC10CTL1 =  INCH_3; /* Set the sampling channel to A3 or P1.3 (Button) */


  while(1) 
  {
     ADC10CTL0 |= ADC10SC | ENC; /* This starts the conversion */

     while ((ADC10CTL1 & ADC10BUSY) > 0)
     {
	/* Loop here while the ADC10 is busy converting or even better sleep here while waiting for the ADC10 interrupt */
     }

     ADC10CTL0 &= ~ENC; /* Clear ENC. This must be done after doing a conversion */

     if(ADC10MEM > 10)   //read adc on P1.0
     {
        P1OUT = 0;
     }
     else
     {
   	  P1OUT = BIT0;
     }
  }
}

 

Also take note that these lines have no effect.

 

ADC10CTL0 |= SREF_0;
ADC10CTL0 |= ADC10SHT_0;
ADC10CTL1 |= INCH_0;

 

Doing a OR bitwise assignment operation (|=) with the input of zero will have no effect, for example:

    0110
OR   0000
----------
    0110

It is only useful for setting bits.

 

However, a AND bitwise assignment operation (&=) however, would work.

      0110
AND    0000 
-----------
      0000

 

I will be updating the (very old) ADC10 source code today. Thanks.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...