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 "string" and transmit using uart

Status
Not open for further replies.

helmi_mjd

Member level 2
Joined
Feb 20, 2011
Messages
45
Helped
2
Reputation
4
Reaction score
2
Trophy points
1,288
Activity points
1,668
Hi! I have one question regarding converting integer to string and transmit the data through uart as given in the code. The output are:1,2,3 instead of 1000,2000,3000. Where am I doing wrong?



Code:
#include "Compiler.h"
#include "delay.h"
#include "system.h"
#include "uart.h"

//DEVICE CONFIGURATION
_FOSC(CSW_FSCM_OFF & XT_PLL8); 
//Watchdog Timer = Off.                                
_FWDT(WDT_OFF);	
//Brown Out Reset = Off. POR Timer Value = 64ms. 	
//Master Clear = Enabled.					
_FBORPOR(PBOR_OFF & PWRT_64 & MCLR_EN); 
// Code Protection = On.										 
_FGS(CODE_PROT_OFF); 

//MAIN FUNCTION
int main()
{
	//Clear all ports.
	LATA = 0; LATB = 0; 
	LATC = 0; LATD = 0; 
	LATF = 0;
	
	//Initialize port/pin as Output
	TRISA = 0; TRISB = 0; 
	TRISC = 0; TRISD = 0; 
	TRISF = 0;

	uart2_initialize();

	while(1){

		char string[4];

		int data[3] = {1000,2000,3000};

		itoa(&string[0],data[0],10);
		itoa(&string[1],data[1],10);
		itoa(&string[2],data[2],10);;
		string[3] = '\0';
		uart2_putstr(string);
		delay_ms(100);		
	}
}

void uart2_initialize(void)
{
   // Set the Baud Rate.
   U2BRG = 129;                        // ((Fosc/4)/(16 * Baud rate)) - 1
                                    // 129 = 9600bps baudrate for 10MHz crystal with 8xPLL
                                    // 20 = 57600bps baudrate for 10MHz crystal with 8xPLL
                                    // 10 = 115200bps baudrate for 10MHz crystal with 8xPLL
   
   U2STAbits.URXISEL = 0;                   // Interrupt flag bit is set for every character received.
   IPC6bits.U2RXIP   = 5;                   // UART2 Receive Interrupt Priority = 4.
   U2STAbits.OERR    = 0;                    // Clear the Receive Overflow Flag.
   IFS1bits.U2RXIF   = 0;                   // Clear the UART2 Receive Interrupt flag.
   IEC1bits.U2RXIE   = 0;                   // Disable UART2 Receive Interrupt.
   U2MODEbits.UARTEN = 1;                  // Enable UART2.
   U2STAbits.UTXEN = 1;                       // Enable UART2 Transmit.
}

void uart2_putstr(const char* csz_string)
{
   // Loop until the end of string.
   while (*csz_string != '\0') {
      uart2_transmit(*csz_string);
         
      // Point to next character.
      csz_string++;
   }
}

void uart2_transmit(unsigned char uc_data)
{
   // Wait until the transmit buffer is ready for new data.
   while (U2STAbits.UTXBF == 1);
   
   // Transmit the data.
   U2TXREG = uc_data;
}
 

How do you want to put three 4 digit numbers into a string that has room for three characters?

itoa() is also writing "out of bounds" (behind the end of string[4]), possibly causing all kinds of unexpected. processor action.

This could e.g. work, but it's sending the numbers without any separator "100020003000"

Consider what you exactly want to achieve.
Code:
  char string[5];

   int data[3] = {1000,2000,3000};

   itoa(string,data[0]);
   uart2_putstr(string);
   itoa(string,data[1]);
   uart2_putstr(string);
   itoa(string,data[2]);
   uart2_putstr(string);
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top