Send data to PC through UART, data not the same

Discussion about projects that used PIC Microcontroller, Hardware Interface, Programming Algorithm and etc......

Send data to PC through UART, data not the same

Postby khmok » Fri Mar 22, 2013 12:39 am

Hi,

I am currently using PIC16F873A, MPLAB 8.46, UIC00B programmer/debugger and HI TECH compiler.
I am trying to send data to PC through UART, but the data display on Putty is different from the one that I sent.
As you can see from attachment, there are 3 characters (in fact, the data that I have sent are 173, 174 and 175 respectively).

Can anyone help on this? Thanks!

while(1) //Infinite loop
{
unsigned char data = 175;
UART_init();
uart_send(data);
}

void uart_send(unsigned char data)
{
while(TXIF==0); //only send the new data after
TXREG=data; //the previous data finish sent
}

void UART_init(void)
{
#define BAUDRATE 115200 //bps
#ifndef _XTAL_FREQ
#define _XTAL_FREQ 4000000 //MHz
#endif

TRISC6 = 1; //TX pin
TRISC7 = 1; //RX pin

SPBRG = ((_XTAL_FREQ/16)/BAUDRATE) - 1;
BRGH = 1; //Fast Baudrate
SYNC = 0; //Asynchronous
TXEN = 1;
TX9 = 0; // 8-bit transmission
CREN = 1; // Enable reception
SPEN = 1; // Enable serial port pins
RX9 = 0; // 8-bit reception

}
Attachments
UART_TX_1.PNG
ASCII code is not the same as the data being sent
khmok
Newbie
 
Posts: 13
Joined: Fri Mar 08, 2013 6:08 pm

Re: Send data to PC through UART, data not the same

Postby khmok » Fri Mar 22, 2013 9:34 pm

Just found out the problem. From the data that is not tally, that showed Baud rate is not accurate.

I am using 4MHz crystal and desired to choose 115 200 baudrate. Calculation for SPBRG value = 1.17. I think the 0.17 that cause the baudrate error.
khmok
Newbie
 
Posts: 13
Joined: Fri Mar 08, 2013 6:08 pm


Return to PIC Microcontroller

Who is online

Users browsing this forum: No registered users and 24 guests