## Recommended Posts

I can get the temperature by

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

Unfortunately, the command

UCA0TXBUF = TempC;

Thank you.

##### 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

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;

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

print digit
// wait for digit to transfer
value = value - digit * decades;
}
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 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 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 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 on other sites

Thank you very much for your help! After the Sabath is over I will implement the change.

##### Share on other sites

I was wondering what that was.

##### Share on other sites

Kinda reminds me of this short comedy about D&D players in french, with english subtitles...

off topic>

Now, what were we talking about? ##### 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 on other sites

int temp;

byte digit;

if (temp < 0) {

print '-';

temp = -temp;

}

// else print '+' if you want

print digit

// wait for digit to transfer

value = value - digit * decades;

}

print a newline or space character

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

##### 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 on other sites

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;
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...

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account. ×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×

×
• Blog

• #### Activity

×
• Create New...