Quantcast
Channel: MSP430 Technical Forums
Viewing all articles
Browse latest Browse all 2077

ADC input - how to display it on hyperterminal

$
0
0

Hi,

 

I'm using the MSP430F2012. My aim is to convert the ADC input that I get to appear as a voltage value on the hyperterminal.

For now, what i've been able to do is to obtain the ADC value and send 8 bits to PC. However, of course what I see is an ASCII representation of the 8 bits.

 

1. How can I send the ADC value (e.g. 00000100) to appear as '4' in hyperterminal?

 

2. I also want to be able to convert that ADC value (00000100) to volts by doing the conversion (4/255*5). How do I do this in C?

 

Here is the snippet of my code:

 

#include "msp430.h"

int receive();
void bittime();
void bittime15();
void send(int);

int main(void)
    {
    int c,d;
        
    WDTCTL = WDTPW + WDTHOLD;        // Stop watchdog timer

    // set clock to 8MHz
      BCSCTL1 = CALBC1_8MHZ;
    DCOCTL = CALDCO_8MHZ;

    
    P1DIR |= 0x41;                // Set P1.0,6 to output direction, the rest input
    
    P1OUT &= ~0x07;        // turn off the LED

    for (;;){
        volatile unsigned int i,j;    // volatile to prevent optimization

        // get a character
        c=receive();
        
        // clear the lower 6 bits of P1
        P1OUT &= 0xc0;
                
               
        // copy the lower 6 bits of the received char to P1
               if (c == 0x31)   //P1.1
                {
                 ADC10CTL0 = ADC10SHT_2 + SREF_0 + ADC10ON;  // select clock and ref, turn ADC10 on
                  ADC10CTL1 = INCH_1;   //choose channel 1 for ADC
                  ADC10AE0 |= 0x02;     // choose P1.1 for ADC
          ADC10CTL0 |= ENC + ADC10SC; // Sampling and conversion start
          
                  while(ADC10CTL1 & ADC10BUSY)  //wait for conversion to complete
        ;
            i = 100;                          // SW Delay
                do{
                  i--;
                      }while (i != 0);
          d = ADC10MEM;
          d = d >> 2;            

               send(d);
            send('\r');
            send('\n');

            // wait
            for(j=0; j<200; j++)
            ;
            
                }

 

 

#define TEST_BIT 0x80
#define SET_BITS 0x50    //0101 0000
#define CLEAR_BITS 0xaf  //1010 1111


int
receive()
    {
    int i=8, c=0;
    
    // wait for the start bit
    while(P1IN & TEST_BIT)
        ;
    // delay till the middle of the first data bit
    bittime15();
    
    // collect the next 8 bits
    do{
        c>>=1;
        if(P1IN & TEST_BIT)
            c |= 0x80;
        bittime();
        }while (--i != 0);
    
    return©;
    }


void send(int c)
    {
    int i=8;
    
    P1OUT &= CLEAR_BITS;    // begin the start bit
    bittime();
    do{
        if(c&1)
            P1OUT |= SET_BITS;
        else
            P1OUT &= CLEAR_BITS;
        c>>=1;                        //shift right one bit
        bittime();
        }while (--i != 0);
    P1OUT |= SET_BITS;    // begin the stop bit
    bittime();
    bittime();    // two stop bits
    }
    



    
void bittime()
    {
    volatile unsigned int i=77;
    
    do{i--;} while(i!=0);
    }

void bittime15()
    {
    volatile unsigned int i=115;
    
    do{i--;} while(i!=0);
    }
 

 

Thank you.


Viewing all articles
Browse latest Browse all 2077

Trending Articles