Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

How can I transmit this variable to USART ?

Status
Not open for further replies.

bianchi77

Advanced Member level 4
Joined
Jun 11, 2009
Messages
1,313
Helped
21
Reputation
44
Reaction score
20
Trophy points
1,318
Location
California
Activity points
9,442
Guys,
Does anyone know how to transmit this variable ? I can transmit it already, but the character is not as expected...

num2str(ds1307_addr[2]));

I tried to use these functions to transmit :
Code:
void usart_transmit( unsigned char data )
{
	/* Wait for empty transmit buffer */
	while ( !( UCSR0A & (1<<UDRE0)) )
	;
	/* Put data into buffer, sends the data */
	UDR0 = data;
}



void usart_pstr(unsigned char *s) {

	// loop through entire string

	while (*s) {
		usart_transmit(*s);
		s++;
	}
}

any ideas ? thank you
 

What does num2str() does? Converts number to string? You have to send string to USART and not number. If num2str() converts number to string then you have to pass number as argument to the function. If it converts digit to character then you have to pass single digit.
 

What does num2str() does? Converts number to string? You have to send string to USART and not number. If num2str() converts number to string then you have to pass number as argument to the function. If it converts digit to character then you have to pass single digit.

Here's the function
Code:
// Implementing integer value from 00 to 99
char *num2str(char number)
{
	unsigned char digit;

	digit = '0';                       // Start with ASCII '0'
	while(number >= 10)                // Keep Looping for larger than 10
	{
		digit++;                         // Increase ASCII character
		number -= 10;                    // Subtract number with 10
	}

	sdigit[0]='0';                     // Default first Digit to '0'
	if (digit != '0')
	sdigit[0]=digit;                 // Put the Second digit

	sdigit[1]='0' + number;
	return sdigit;
}

- - - Updated - - -

Code:
usart_transmit(num2str(ds1307_addr[2])));
it's not displaying as I expected.....strange character...
 

Lets start with a value of 20 for number.

digit will be 0x30

In the first loop of while(number >= 10)

digit becomes 1 and number becomes 10

In the second loop digit becomes 2 and number becomes 0

loop is exited

sdigit[0] will be 0x30 = character '0'

Then comes

if (digit != '0')
sdigit[0]=digit;

This test passes as digit is 2, so

sdigit[0] becomes 0x02

sdigit[1] will be 0x30 + 0 = '0'

array is not terminated with null character so it is not a string.

so, you get 0x02,0x30 on terminal.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void int2str(unsigned int number);
 
 
unsigned char convstr[20];
 
 
void int2str(unsigned int number){
 
    convstr[0] = number/10000 + 0x30;
    convstr[1] = (number/1000)%10 + 0x30;
    convstr[2] = (number/100)%10 + 0x30;
    convstr[3] = (number/10)%10 + 0x30;
    convstr[4] = (number/1)%10 + 0x30;
    convstr[5] = '\0';
 
}
 
 
void main(){
 
    //Pass convstr as argument to function which prints string on UART.
        usart_pstr(&convstr);
}

 
Last edited:

So I will do :
Code:
usart_pstr(int2str(ds1307_addr[2])));
???
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top