Other Parts Discussed in Thread: MSP430G2553
Tool/software: Code Composer Studio
Hello everyone.
i am using the MSP430G2553 launchpad's ADC10 to convert internal temperature sensor and send conversion results via UART.
I use TeraTerm to read UART data on PC, and i switched jumpers to HW position.
The problem is that there is no data displayed on TeraTerm.
Here is my code, and thank you for your valuable support.
//***************************************************************************************
// this program uses MSP430 internal temperature sensor with ADC10 module
//
// built with Code Composer Studio v6.2
//****************************************************************************************
#include <msp430.h>
#include <stdio.h>
#include <string.h>
long ADCDATA; //ADCDATA variable to store ADC data from ADC10MEM
long tempInDeg;
char stringTemp[] = {};
// function prototypes
void itoa(long unsigned int value, char* result, int base);
//void itoa(); // function prototype
void UART_TX( char * tx_data);
void clock_init(){
//clock calibration to 1 MHz---------------------------------------------------------------------------------
DCOCTL = 0; // Select lowest DCOx and MODx settings
BCSCTL1 = CALBC1_1MHZ; // Set DCO
DCOCTL = CALDCO_1MHZ;
}
void GPIO_init(){
// although P1.0 and P1.6 are initialized, they're not used
P1DIR |=0x41;
P1OUT &=~ 0x41;
}
void ADC10_init(){
// ADC initialization
ADC10CTL0 = SREF_1 + ADC10SHT_3 + REFON + ADC10ON + ADC10IE; // Internal voltage (VR+ = VREF+ AND VR- = Vss); SHT= 64*ADC10CLK; ref ON; ADC10 ON; enable interrupts
ADC10CTL1 = INCH_10 + ADC10SSEL_2; // input channel 10--> internal temperature sensor; MCLK
_delay_cycles(50);
}
void UART_init(){
/*
// even when i tried to use the following 2 lines of code, it didn't work
P1DIR |= 0x02 + 0x04; // set P1.1 + P1.2 UART Pins as output direction
P1OUT &=~ 0x02 + 0x04; // clear P1.1 + P1.2 UART Pins
*/
//--------- Setting the UART function for P1.1 & P1.2 --------//
P1SEL |= BIT1 + BIT2; // P1.1 UCA0RXD input
P1SEL2 |= BIT1 + BIT2; // P1.2 UCA0TXD output
//------------ Configuring the UART(USCI_A0) ----------------//
UCA0CTL1 = UCSSEL_2 + UCSWRST; // SMCLK ; software reset before making any changes (USCI_A0 disabled)
// selecting 9600 baud rate
UCA0BR0 = 104; // 104 From datasheet table-
UCA0BR1 = 0; // -selects baudrate =9600,clk = SMCLK
UCA0MCTL = UCBRS_1; // Modulation value = 1 from datasheet
UCA0CTL1 &= ~UCSWRST; // Clear UCSWRST to enable USCI_A0
//---------------- Enabling the interrupts ------------------//
IE2 |= UCA0TXIE; // Enable the Transmit interrupt
// IE2 |= UCA0RXIE; // Enable the Receive interrupt // we don't need this because we only send
_BIS_SR(GIE); // Enable the global interrupt
}
//Main function-----------------------------
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop Watch Dog Timer
// functions call---------------------
clock_init();
GPIO_init();
ADC10_init();
UART_init();
__bis_SR_register(GIE); // global interrupt enable
while(1)
{
ADC10CTL0 |= ENC + ADC10SC; // enable conversion; Sampling and conversion start
__bis_SR_register(CPUOFF); // go to LPM_0 while ADC is converting
ADCDATA = ADC10MEM; // Read ADC value
// Temperature in degrees conversion by using formula ADCDATA = (1024*(Vtemp/Vref))
tempInDeg = ((ADCDATA - 673) * 423) / 1024;
//tempInDeg = ((ADCDATA * 1500)/1024 - 986.6)/3.55;
itoa(tempInDeg,stringTemp,10); // convert tempInDeg into string
// the following data is not displayed on TeraTerm
UART_TX("msp430 is starting\n\r");
UART_TX("configuration clock ok\n\r");
UART_TX("temperature is\n\r");
UART_TX(stringTemp);
__delay_cycles(10000); //delay
ADC10CTL0 &= ~ENC; // stop conversion
}
}
// Interrupt Service Routines--------------------------------------------
#pragma vector= ADC10_VECTOR
__interrupt void ADC10_ISR(){
__bic_SR_register_on_exit(CPUOFF); // exit LPM_0
}
#pragma vector = USCIAB0TX_VECTOR
__interrupt void TransmitInterrupt(void)
{
}
// function to send data through UART
void UART_TX( char * tx_data) // Define a function which accepts a character pointer to an array
{
unsigned int i=0;
while(tx_data[i]) // Increment through array, look for null pointer (0) at end of string
{
while ((UCA0STAT & UCBUSY)); // Wait if line TX/RX module is busy with data
UCA0TXBUF = tx_data[i]; // Send out element i of tx_data array on UART bus
i++; // Increment variable for array address
}
}
// C language function to convert integers to strings
void itoa(long unsigned int value, char* result, int base)
{
// check that the base if valid
if (base < 2 || base > 36) { *result = '\0';}
char* ptr = result, *ptr1 = result, tmp_char;
int tmp_value;
do {
tmp_value = value;
value /= base;
*ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz" [35 + (tmp_value - value * base)];
} while ( value );
// Apply negative sign
if (tmp_value < 0) *ptr++ = '-';
*ptr-- = '\0';
while(ptr1 < ptr) {
tmp_char = *ptr;
*ptr--= *ptr1;
*ptr1++ = tmp_char;
}
}