This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

adc and uart in msp430g2253

hello there here it is my code for adc and uart.  in code variable a,b,c make digit which is i am transmitting through UART. now i want to set threshold like if value is less than 1.9 then i want to turn on LED .but it doesn't work for me.

i am asking about this part

  if(a << 1 && b << 9 )
					             {
					            	 P1OUT |= BIT6;
					            	 serial_charTX('t');
					             }
					             else
					             {
					            	 P1OUT &= ~BIT6;
					             }

complete code is below

#include <msp430.h>
unsigned char name[] = "RUSHIN\n";
//unsigned char surname[] = "THAKKAR\n";
int a,b,c,v;
void serial_init(void);
void serial_charTX(char c);
void UART0_Write_Text(unsigned char msg[]);
char serial_charRX(void);
void clock_init(void);
void adc_init(void);
void main(void) {
	WDTCTL = WDTHOLD + WDTPW; //

	  P1DIR |= BIT6;
   clock_init(); // Set clock frequency to 1 MHz
   serial_init();
	//serial_charTX('t');
	//UART0_Write_Text(name);
	//UART0_Write_Text(surname);
	//serial_charTX('r');
	while (1) {
		//__delay_cycles(500000);
		//	__delay_cycles(500000);
		//	__delay_cycles(500000);
		  //int a,b,c,v =0;
		 // ADC10MEM= 0;
		  // Initialize UART module

					adc_init();
					             v = ADC10MEM;
					                v=v/3;
					                v=v+6;

					               a=v/100;
					               b=v/10;
								 b=b%10;
					             c=v%10;
					             if(a << 1 && b << 9 )
					             {
					            	 P1OUT |= BIT6;
					            	 serial_charTX('t');
					             }
					             else
					             {
					            	 P1OUT &= ~BIT6;
					             }

					 serial_charTX(a+0x30);
					 serial_charTX('.');
					             serial_charTX(b+0x30);
					             serial_charTX(c+0x30);
					             serial_charTX(0x0D);
					             serial_charTX(0x0A);

					//TimerA_UART_print("\n");
				}




	}

void adc_init(void)
{

	 ADC10CTL1 |= ADC10SSEL_3  + ADC10DIV_7 + CONSEQ_2; 							//continuous sample mode, MUST BE SET FIRST!
		  ADC10CTL0 |= ADC10ON + ADC10SHT_3 + MSC + REFON + REF2_5V; 			//sample and hold time, adc on, cont. sample
		  ADC10AE0 |= 0x01; 								// select channel A0
		  ADC10CTL0 |= ADC10SC + ENC;
}
void serial_init(void) //Initialize TX
{

    P1SEL = BIT1 + BIT2;// ****** mask based on pin (same process we used in Lab 6) ****** //
    P1SEL2 =BIT1 + BIT2; // ****** mask based on pin (same process we used in Lab 6) ****** //
    UCA0CTL1 |= UCSWRST; // Disable UART module for configuration
    UCA0CTL1 |=UCSSEL_2; // ****** SMCLK source, keep in reset state p. 440 User's Guide ****** //
    UCA0BR0 =104; // ****** 9600 Baud rate	- Assumes 1 MHz clock pg 441 & 435 User's Guide ****** //
    UCA0MCTL =UCBRS0; // ****** 2nd Stage modulation = 1, Oversampling off p. 442 & 435 User's Guide ****** //
    IE2 = 0x00; // Interrupts disabled
    UCA0CTL1 &= ~UCSWRST; // Enable UART module
}


void serial_charTX(char c)
{
    while( !(IFG2 & UCA0TXIFG) ); // Wait until the transmit buffer is empty
    UCA0TXBUF = c;			// Send the character through the Xmit buffer
}
void UART0_Write_Text(unsigned char msg[])
{
	while(*msg)
	{
		serial_charTX(*msg);
		msg++;
	}
}

char serial_charRX(void)
{
    while( !(IFG2 & UCA0RXIFG) ); // Wait until a character has been received
    return UCA0RXBUF; // Return received character
}

void clock_init(void)
{
    DCOCTL = 0x00;
    BCSCTL1 = CALBC1_1MHZ; // Calibrate to 1 MHz
    DCOCTL = CALDCO_1MHZ;
}















  • >now i want to set threshold like if value is less than 1.9 then i want to turn on LED .but it doesn't work for me.
    How exactly it does no work for you? What are you expecting and what results you are getting instead? What you tried to "fix" ir and how did it go?
  • from code you can see that i am transmitting digit "a.bc". which is value of adc output. my aim is to control device using adc output. so that i am comparing it with some threshold like 1.9. if adc output is less than 1.9. uart will transmitt data 't'. so my problem is how to compare entire digit 'a.bc'??. here i have to compare separately like comparing a,b and then c.
  • Rushin,

    your code isn't that good to understand. Could you please make an example with a value from the ADC10. As Ilmars said: What are you expecting from your program?

    Let's say you have 1017 from the ADC:

    v = ADC10MEM;           // 1017
    
    v = v / 3;              // v = 1017 / 3   = 339
    v = v + 6;              // v = 339  + 6   = 345
    a = v / 100;            // a = 345  / 100 = 3
    b = v / 10;             // b = 345  / 10  = 34
    b = b % 10;             // b = 34   % 10  = 4
    c = v % 10;             // c = 339  % 10  = 9
                            // RESULT: a = 3, b = 4, c = 9
    
    if( a << 1 && b << 9 )  // SAME AS: if( 0000 0000 0000 0011 << 1 && 0000 0000 0000 1001 << 9 )
                            // SAME AS: if( 0000 0000 0000 0110 && 0001 0010 0000 0000 )
                            // SAME AS: if( 1 ) <- EXECUTED
    {
      P1OUT |= BIT6;
      serial_charTX( 't' );
    }
    else
    {
      P1OUT &= ~BIT6;
    }

    Is that what you want to do? Why not compare the number before you split it into the single characters.

    And do you want to transmit the ASCII-numbers over UART? Then you have to add +48 to each number to get the ASCII-value. What is your 0x30 for?

    Edit: 0x30h is 48d - that is OK! And had a mistake in the example - your a, b and c is 16 bit int, not char.

    Dennis

  • yes i can compare the number before i split it into single characters!. thank you! solved
  • That's it?
    Just for interest: Why dividing the number by 3 and then adding 6 to it? What is that for a value?

    Dennis
  • in my system, i am getting small error in adc value. so i add 6 and get exact value!
  • Have a look at page 15 of the datasheet for the controller. The device has calibration data stored in the info section of the flash. There are correction factors for the ADC that were determined during production. Maybe interesting for you.

    Dennis
  • hello! i got some problem.  i made some changes in my code

    #include <msp430.h>
    unsigned char name[] = "RUSHIN\n";
    //unsigned char surname[] = "THAKKAR\n";
    int a,b,c,v,x;
    void serial_init(void);
    void serial_charTX(char c);
    void UART0_Write_Text(unsigned char msg[]);
    char serial_charRX(void);
    void clock_init(void);
    void adc_init(void);
    void main(void) {
    	WDTCTL = WDTHOLD + WDTPW; //
    
    	  P1DIR |= BIT6;
       clock_init(); // Set clock frequency to 1 MHz
       serial_init();
    	//serial_charTX('t');
    	//UART0_Write_Text(name);
    	//UART0_Write_Text(surname);
    	//serial_charTX('r');
    	while (1) {
    		//__delay_cycles(500000);
    		//	__delay_cycles(500000);
    		//	__delay_cycles(500000);
    		  //int a,b,c,v =0;
    		 // ADC10MEM= 0;
    		  // Initialize UART module
    
    					adc_init();
    					             v = ADC10MEM;
    
    					             if(200<= v <=540)
    					             {
    					            	 P1OUT |= BIT6;
    					             serial_charTX('t');
    					             }
    
    					             else
    					             {
    					            	 P1OUT &= ~BIT6;
    					             }
    					             x=v/3;
    					          					                x=x+6;
    
    					          					               a=x/100;
    					          					               b=x/10;
    					          								 b=b%10;
    					          					             c=x%10;
    					 serial_charTX(a+0x30);
    					 serial_charTX('.');
    					             serial_charTX(b+0x30);
    					             serial_charTX(c+0x30);
    					             serial_charTX(0x0D);
    					             serial_charTX(0x0A);
    
    					//TimerA_UART_print("\n");
    				}
    
    
    
    
    	}
    
    void adc_init(void)
    {
    
    	 ADC10CTL1 |= ADC10SSEL_3  + ADC10DIV_7 + CONSEQ_2; 							//continuous sample mode, MUST BE SET FIRST!
    		  ADC10CTL0 |= ADC10ON + ADC10SHT_3 + MSC + REFON + REF2_5V; 			//sample and hold time, adc on, cont. sample
    		  ADC10AE0 |= 0x01; 								// select channel A0
    		  ADC10CTL0 |= ADC10SC + ENC;
    }
    void serial_init(void) //Initialize TX
    {
    
        P1SEL = BIT1 + BIT2;// ****** mask based on pin (same process we used in Lab 6) ****** //
        P1SEL2 =BIT1 + BIT2; // ****** mask based on pin (same process we used in Lab 6) ****** //
        UCA0CTL1 |= UCSWRST; // Disable UART module for configuration
        UCA0CTL1 |=UCSSEL_2; // ****** SMCLK source, keep in reset state p. 440 User's Guide ****** //
        UCA0BR0 =104; // ****** 9600 Baud rate	- Assumes 1 MHz clock pg 441 & 435 User's Guide ****** //
        UCA0MCTL =UCBRS0; // ****** 2nd Stage modulation = 1, Oversampling off p. 442 & 435 User's Guide ****** //
        IE2 = 0x00; // Interrupts disabled
        UCA0CTL1 &= ~UCSWRST; // Enable UART module
    }
    
    
    void serial_charTX(char c)
    {
        while( !(IFG2 & UCA0TXIFG) ); // Wait until the transmit buffer is empty
        UCA0TXBUF = c;			// Send the character through the Xmit buffer
    }
    void UART0_Write_Text(unsigned char msg[])
    {
    	while(*msg)
    	{
    		serial_charTX(*msg);
    		msg++;
    	}
    }
    
    char serial_charRX(void)
    {
        while( !(IFG2 & UCA0RXIFG) ); // Wait until a character has been received
        return UCA0RXBUF; // Return received character
    }
    
    void clock_init(void)
    {
        DCOCTL = 0x00;
        BCSCTL1 = CALBC1_1MHZ; // Calibrate to 1 MHz
        DCOCTL = CALDCO_1MHZ;
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

    here 

    200<= v <=540,

    but  in terminal i am getting value like

    t2.07
    t2.32
    t2.24
    t2.27
    t2.50
    t2.28
    t2.55
    t2.54
    t2.14
    t2.30
    t2.25
    t2.12
    t2.35
    t2.26.

    can you tell me what is problem here?

  • Rushin,

    split this

    if( 200 <= v <= 540 )

    into

    if( (v >= 200) && (v <= 540) )

    Dennis

**Attention** This is a public forum