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