Jump to content
43oh

Simple ADC LED control program MSP430G2553


Recommended Posts

I am working a simple code to allow the "brightness" of the onboard LED P1 to
follow a varying input voltage from 0-3.6v. I have the code to the the point
that I can see the ADC10MEM register change state, I am hung up on translating
the input to the output for the LED. any help would be greatly appreciated. My
code below...
 

#include <msp430.h>

int main(void)

{
volatile unsigned int ADCval, pw, pi;
WDTCTL = WDTPW + WDTHOLD; // Stop Watch Dog Timer
P1DIR |= 0x01;
ADC10CTL0 = ADC10SHT_3 + ADC10ON; // Set sampling time, turn
on ADC10
ADC10CTL1 = INCH_4 + ADC10DIV_7;
//ADC10CTL0 |= ENC ; // Conversion enabled
ADC10AE0 |= 0x04; // Enable interrupt


for (;
{

ADC10CTL0 |= ENC + ADC10SC; // Sampling and conversion start
while (ADC10CTL1 & ADC10BUSY); // ADC10BUSY?
while (!(ADC10CTL0 & ADC10IFG));
ADCval = ADC10MEM;

pw = ADCval * 0x08;
pi = 0x2200 - pw;

TACCTL0 = pw;
TACCTL1 = pi;

}

}

 

Link to post
Share on other sites

Rob,

Thanks for your response. I have added to my code and seem to have moved forward a bit. I am unable to get the P1.0 to operate as expected, for some reason the P1.6 (green onboard LED) seems to work (unfortunately with a lot of flicker). I am not sure why the red LED (P1.0) is not responding. Any thoughts?

 

#include <msp430g2553.h>

/*
 * main.c
 */
int main(void)

{
	unsigned int ADCval, pw, pi;  //unsigned char is type of unsigned 8 bit variable.

	WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer.

	ADC10CTL1 = INCH_4 + ADC10DIV_7;

	ADC10CTL0 = ADC10SHT_3 + ADC10ON;    //Set sample and hold time, turn ADC On

	ADC10CTL0 |= ENC;
	ADC10AE0 |= 0X04;                       //  Enable ADC for P1.4
	while(1)                                // Infinite Loop
	{

		ADC10CTL0 |= ADC10SC;              //Start conversion
		while (ADC10CTL1 & ADC10BUSY);
		while (!(ADC10CTL0 & ADC10IFG));

        ADC10CTL0 &= ~(ADC10BUSY + ADC10IFG);  //clear flags?

        ADCval = ADC10MEM;
        pw = ADCval * 8;
        pi = 0x2200 - pw;

        P1DIR |= BIT6;
        P1SEL |= BIT6;

        TACCR0 = pw;

        TACCTL1 = OUTMOD_3;

        TACCR1 = pi;

        TACTL = TASSEL_2 + MC_1;

        }
	}

Link to post
Share on other sites

 

 I am not sure why the red LED (P1.0) is not responding. Any thoughts?

 

        P1DIR |= BIT6;
        P1SEL |= BIT6;

 

 

Because in your code you only set P1.6 to output and activate the CCR output(in your case PWM signal) for it.

Unfortunately P1.0 is not connected to a TimerA CCR, so you will have to toggle it manually using an interrupt routine to generate a PWM signal.

Link to post
Share on other sites

I had started with P1.0, with no luck so i went to P1.6 to see if that would work...I was trying to get confirmation that the main body of my code was ok. It appears that it is at least close as I can get close to the expected output on P1.6. It makes sense that i would not be getting the expected output on P1.0 if there is no TimerA connection to it. I am thinking a loop with the pw and pi variables turning on and off the P1.0 LED.

Link to post
Share on other sites

If you really want PWM on P1.0 I would rather generate the signal using interrupts generated by TimerA than a loop with delays. This is a much more elegant solution and allows you to use the low power modes of the MSP430. I am pretty sure you can find examples for software PWM on the forum :)

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...