Hello!
i am giving sine wave from function generator to msp430 launchpad. and i am getting value of sine wave on PC using UART. I got exact sine wave for 10 hz. but for 50 hz, sine wave distorted. i have uploaded my code here.can you give me solution?
#include <msp430.h>
#include <math.h>
#define UART_TXD 0x02 // TXD on P1.1 (Timer0_A.OUT0)
#define UART_TBIT (1000000 / 9600) // 9600 Baud, SMCLK = 1MHz
// Globals for transmit UART communication
unsigned int txData; // UART internal variable for TX
void TimerA_UART_tx(unsigned char byte); // Function prototypes
void TimerA_UART_print(char *string);
int temp1,adc,adc1;
float supply = 3.70;
char disp_res;
unsigned char temp;
int a,b,c;
int v;
void main(void){
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
DCOCTL = 0x00; // Set DCOCLK to 1MHz
BCSCTL1 = CALBC1_1MHZ;
DCOCTL = CALDCO_1MHZ;
// DCOCTL = CALDCO_16MHZ; // set internal oscillator at 16MHz
// BCSCTL1 = CALBC1_16MHZ; // set internal oscillator at 16MHz
// ADC10CTL1 |= CONSEQ1; //continuous sample mode, MUST BE SET FIRST!
// ADC10CTL0 |= ADC10SHT_2 + ADC10ON + MSC; //sample and hold time, adc on, cont. sample
ADC10CTL0 |= ADC10ON + ADC10SHT_3 + MSC;
ADC10CTL1 |= ADC10SSEL_3 + ADC10DIV_7 + CONSEQ_2;
ADC10AE0 |= 0x01; // select channel A0
ADC10CTL0 |= ADC10SC + ENC; // start conversions
P1OUT = UART_TXD; // Initialize P1.1
P1SEL = UART_TXD; // Timer function for TXD pin
P1DIR = UART_TXD; // Set TXD pin to output
// Timer_A for transmit UART operation
TA0CCTL0 = OUT; // Set TXD Idle as Mark = '1'
TA0CCTL1 = SCS + CM1 + CAP; // Sync, Neg Edge, Capture
TA0CTL = TASSEL_2 + MC_2; // SMCLK, start in continuous mode
_BIS_SR(GIE); // Enable CPU interrupts
while(1)
{
//TimerA_UART_print("\n");
//__delay_cycles(500000);
// __delay_cycles(500000);
//__delay_cycles(500000);
v = ADC10MEM;
v=v/3;
// v=v+6;
// v = (v*3.3)/1024;
a=v/100;
b=v/10;
b=b%10;
c=v%10;
TimerA_UART_tx(a+0x30);
TimerA_UART_tx('.');
TimerA_UART_tx(b+0x30);
TimerA_UART_tx(c+0x30);
TimerA_UART_tx(0x0D);
TimerA_UART_tx(0x0A);
//disp_res =( v*supply)/1024;
//TimerA_UART_print("Volt:");
//TimerA_UART_print(disp_res);
//TimerA_UART_tx(disp_res);
//TimerA_UART_tx(b);
}
}
void TimerA_UART_tx(unsigned char byte) { // Outputs one byte using the Timer_A UART
while (TACCTL0 & CCIE); // Ensure last char got TX'd
TA0CCR0 = TAR; // Current state of TA counter
TA0CCR0 += UART_TBIT; // One bit time till first bit
txData = byte; // Load transmit data, e.g. 'A'=01000001
txData |= 0x100; // Add mark stop bit, e.g. 101000001
txData <<= 1; // Add space start bit, e.g. 1010000010
TA0CCTL0 = OUTMOD0 + CCIE; // Set TXD on, enable counter interrupt
}
void TimerA_UART_print(char *string) { // Prints a string using the Timer_A UART
while (*string)
TimerA_UART_tx(*string++);
}
#pragma vector = TIMER0_A0_VECTOR // Timer_A UART - Transmit ISR
__interrupt void Timer_A0_ISR(void) {
static unsigned char txBitCnt = 10;
TA0CCR0 += UART_TBIT; // Add Offset to CCRx
if (txBitCnt == 0) { // All bits TXed?
TA0CCTL0 &= ~CCIE; // All bits TXed, disable interrupt
txBitCnt = 10; // Re-load bit counter
}
else {
if (txData & 0x01)
TA0CCTL0 &= ~OUTMOD2; // TX Mark '1'
else
TA0CCTL0 |= OUTMOD2; // TX Space '0'
}
txData >>= 1; // Shift right 1 bit (low bits TX'ed first)
txBitCnt--;
}

