Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic 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.

Register Log in

[PIC] convert int to ASCII and send it to UART for PIC

Status
Not open for further replies.

tinumanikuttan

Newbie level 2
Joined
May 21, 2015
Messages
2
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
11
I am trying to transmit an integer by converting it to ascii. I am getting no output when I send it. can someone tell me where i am wrong.? My code :

Code:
void main()
{
 inUART();
 int n = 123;
 char a[10];

 inttostring(a,n);
 senddata(a);
}

void inttostring(char a[],int num)
{
 int i,rem,len=0,n;
 n=num;
 while(n!=0){
	len++;
	n=n/10;
	}
for(i=0;i<len;i++){
	rem = num%10;
	num = num/10;
	a[len-(i+1)] = rem+'0';
	}
 a[len] = '\0';
}

void senddata(char buf[])
{
 int i;
 for(i=0;buf[i]!='\0';i++)
	sendsing(buf[i]);
}

void sendsing(char sing)
{
 while(!TRMT);
 TXREG = sing;
}
- - - Updated - - -

I am using PIC16F877a and Hi-Tech C
 

horace1

Advanced Member level 5
Joined
Nov 18, 2008
Messages
2,123
Helped
596
Reputation
1,188
Reaction score
573
Trophy points
1,393
Location
Norwich, UK
Activity points
13,071
your functions inttostring() and senddata() look OK
could your initalisation of the UART be incorrect? e.g. wrong baud rate?
try sending a string, e.g.
Code:
senddata("hello");
 

KlausST

Super Moderator
Staff member
Joined
Apr 17, 2014
Messages
18,016
Helped
4,043
Reputation
8,086
Reaction score
3,955
Trophy points
113
Activity points
118,955
Hi,

The routines may be ok.
But i'd avoid divisions in controllers, because they are slow.
I'd rather subtract 100 as long as value >= 100, count the loops for hundreds
Then subtract 10 as long as value >= 10, count the loops for tens,
Rest is ones

This makes max 12 integer 8 bit subtractions..

Klaus
 

horace1

Advanced Member level 5
Joined
Nov 18, 2008
Messages
2,123
Helped
596
Reputation
1,188
Reaction score
573
Trophy points
1,393
Location
Norwich, UK
Activity points
13,071

Easyrider83

Advanced Member level 5
Joined
Oct 11, 2011
Messages
1,608
Helped
373
Reputation
746
Reaction score
361
Trophy points
1,363
Location
Tallinn, Estonia
Activity points
8,577
itoa converts float to string
also sprintf is useless because take a lot of memory
 

horace1

Advanced Member level 5
Joined
Nov 18, 2008
Messages
2,123
Helped
596
Reputation
1,188
Reaction score
573
Trophy points
1,393
Location
Norwich, UK
Activity points
13,071
itoa converts float to string
also sprintf is useless because take a lot of memory
details of itoa() from
http://www.cplusplus.com/reference/cstdlib/itoa/

char * itoa ( int value, char * str, int base );

Convert integer to string (non-standard function)
Converts an integer value to a null-terminated string using the specified base and stores the result in the array given by str parameter.


- - - Updated - - -

I have never had problems using functions from <stdio.h> such as sprintf() with modern microcontrollers such as PIC24, dsPIC, PIC32, ARM7, ARM9, etc. It is often cheaper to move to a microcontroller with more program memory than paying an expensive programmer to write project specific code where a solution already exists.
 

xenos

Full Member level 4
Joined
May 9, 2015
Messages
212
Helped
82
Reputation
164
Reaction score
81
Trophy points
28
Location
127.0.0.1
Activity points
1,182
Your code has a bug when number is 0. Will not return "0" but an empty string.
Of course this is not your problem. You have uart init error or hardware error and uart does not communicate properly.

The code you wrote do too many divisions, I suggest using this function in stead:
Code:
void inttostring(char a[],int num)
{
	signed char i,len=0;
	do{
		a[len++] = num%10 + '0';
		num=num/10;
	}while(num);
           a[len]='\0';
	//reverse order
	for(i=len/2;i>=0;i--){
 		char tmp = a[i];
		a[i] = a[len-i-1];
		a[len-i-1] = tmp;
	}
}
There are a few other ways to convert, the fastest and smallest should be to to convert the bytes to hex and transmit.
Code:
char hex(unsigned char x){
        x &= 0x0f;
        return (x<10) ? x : x-10+'a';
}
void uart_send_i(int x){
       char i;
#define _P(i)      (((unsigned char *)&x)[i])
       for(i=0;i<sizeof(x);i++){
            uart_send_char(hex(_P(i) >> 4));
            uart_send_char(hex(_P(i)) );
       }
#undef _P
}
 
Last edited:

Status
Not open for further replies.
Toggle Sidebar

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top