Jump to content
Sign in to follow this  
mvs

Temperature reading every second.

Recommended Posts

My device is MSP430F2274. Could you please help me write a program that transmits a temperature reading once a second.

 

I can get the temperature by

 

P1OUT ^= 0x01; // Toggle P1.0

ADC10CTL0 |= ENC + ADC10SC; // Sampling and conversion start

FirstADCVal = ADC10MEM; // Read out 1st ADC value

TempC=.6877*FirstADCVal-277.7; // Temperature

 

Unfortunately, the command

 

UCA0TXBUF = TempC;

 

transmits abracadabra.

 

Thank you.

Share this post


Link to post
Share on other sites

Yup, that's right!

'a' = 97

'b' = 98

'c' = 99

'd' = 100

 

'A' = 65

'B' = 66

'C' = 67

 

Putting an integer into the USART register will make it transmit the CHARACTER

represented by that value. Now how to fix that will depend on how fancy you want

to get. You will need to convert the resulting temperature from an int, digit by digit,

to a string of characters that represent your temperature.

 

Say your temperature reading is 65. To display that you need to convert that value

to a '6' followed by a '5', maybe add a new line, a space, whatever to separate each

reading.

 

Remember that a '6' is 48+6 or 54. A zero is just 48+0. These are the ASCII values

for the digits: 48 + digit. Digit needs to stay less than 10.

/

Basically the conversion goes like this in "pseudo-C":

int decades = 100;
int temp;
byte digit;

temp = temperature reading;
if (temp < 0) {
   print '-';
   temp = -temp;
}
// else print '+' if you want

while (decade > 0) {
   digit = value / decades;
   print digit
   // wait for digit to transfer
   value = value - digit * decades;
   decades /= 10;
}
print a newline or space character

 

This is not the most efficient way but you can see what has

to be done to break down and display a number.

 

An alternative method would be to convert the number to a

string, say using sprintf(), and then spit the string out the

serial port.

 

-Rusty-

Share this post


Link to post
Share on other sites
An alternative method would be to convert the number to a

string, say using sprintf(), and then spit the string out the

serial port.

BWAAAHAAAhhaaaaaahaaaaaaaa...... The seven headed hydra of printf() rears its resource killing head.

 

See this thread if you dare go this way ;)

Share this post


Link to post
Share on other sites

For example:

void UartSend(char *data, int length)
{
int i = 0;
char charCommand = 0;
for(i=0;i	{
	while (!(IFG2 & UCA0TXIFG));              // Ensure USCI_A0 is ready
	if(charCommand)
	{
		charCommand ^= 1;
		if(data[i] == 'r')
			UCA0TXBUF = 15;
		if(data[i] == 'n')
			UCA0TXBUF = 12;
	}		      	  			
	else
	{
		if(data[i] == '\\')
			charCommand ^= 1;
		else UCA0TXBUF = data[i];
	}
} 
}

 

That is a function I'm using in some test code for the time being... It could be used in conjunction with itoa() ? :)

Share this post


Link to post
Share on other sites
An alternative method would be to convert the number to a

string, say using sprintf(), and then spit the string out the

serial port.

BWAAAHAAAhhaaaaaahaaaaaaaa...... The seven headed hydra of printf() rears its resource killing head.

 

See this thread if you dare go this way ;)

 

I roll a d20. I get hit for 4d6 save d2. I die.

 

On the other hand, just do computer sided processing on the string.

Share this post


Link to post
Share on other sites
Putting an integer into the USART register will make it transmit the CHARACTER

represented by that value. Now how to fix that will depend on how fancy you want

to get. You will need to convert the resulting temperature from an int, digit by digit,

to a string of characters that represent your temperature.

 

 

Thank you very much. Unfortunately, that is too complicated for a beginner. Is there a command which can send a real number to th Serial Port? Temp is a real, not an integer.

Share this post


Link to post
Share on other sites
int decades = 100;

int temp;

byte digit;

 

temp = temperature reading;

if (temp < 0) {

print '-';

temp = -temp;

}

// else print '+' if you want

 

while (decade > 0) {

digit = value / decades;

print digit

// wait for digit to transfer

value = value - digit * decades;

decades /= 10;

}

print a newline or space character

 

I am sorry, but this seems to be a code for some other device.

Share this post


Link to post
Share on other sites
void UartSend(char *data, int length)

{

int i = 0;

char charCommand = 0;

for(i=0;i

{

while (!(IFG2 & UCA0TXIFG)); // Ensure USCI_A0 is ready

if(charCommand)

{

charCommand ^= 1;

if(data == 'r')

UCA0TXBUF = 15;

if(data == 'n')

UCA0TXBUF = 12;

}

else

{

if(data == '\\')

charCommand ^= 1;

else UCA0TXBUF = data;

}

}

}

 

Thank you very much. Where should I input this part of the program?

 

It gave me an error:

 

Error[Pe159]: declaration is incompatible with previous "UartSend" (declared at line 42) C:\Documents and Settings\...

Error while running C/C++ compiler

Share this post


Link to post
Share on other sites

itoa function from http://www.strudel.org.uk/itoa/#newest

 

Place this at the top of your source file

void UartSend(char *data, int length);
char* itoa(int value, char* result, int base);

 

Place something like this anywhere in your code

char mychars[5];
itoa(23,mychars, 10);
UartSend((char *)mychars,strlen(mychars));

 

And place this at the end of your source file

void UartSend(char *data, int length)
{
int i = 0;
char charCommand = 0;
for(i=0;i	{
	while (!(IFG2 & UCA0TXIFG));              // Ensure USCI_A0 is ready
	if(charCommand)							// Working with command char
	{
		charCommand ^= 1;
		if(data[i] == 'r')					// Carriage return 
			UCA0TXBUF = 13;
		if(data[i] == 'n')					// New line
			UCA0TXBUF = 10;
		if(data[i] == '\\')					// Send backslash
			UCA0TXBUF = '\\';
		if(data[i] == '"')					// Send quote
			UCA0TXBUF = '"';
	}		      	  			
	else
	{
		if(data[i] == '\\')					// Is next char a command?
			charCommand ^= 1;
		else UCA0TXBUF = data[i];			// Send this char to console
	}
} 
}

char* itoa(int value, char* result, int base) {
// check that the base if valid
if (base < 2 || base > 36) { *result = '\0'; return result; }
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;
}
return result;
}

 

I've tested this and it works...

Share this post


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.

Sign in to follow this  

×
×
  • Create New...