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.

[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
 

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");
 

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
 


itoa converts float to string
also sprintf is useless because take a lot of memory
 

itoa converts float to string
also sprintf is useless because take a lot of memory
details of itoa() from
https://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.
 

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.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top