Help for UART initialization

General discussion on mikroC PRO for 8051.
Post Reply
Author
Message
elijah981
Posts: 2
Joined: 20 Nov 2015 05:50

Help for UART initialization

#1 Post by elijah981 » 20 Nov 2015 06:00

Hello,

I am a professor at a University and here in the microprocessors laboratory we use the Easy8051 v6 board.

I wanted to teach UART communication and the examples work fine.
But I wanted to write a piece of code to initialize the UART so that students get a feel of how the library files may be written.

The piece of code I wrote looks like this..

//*****************************************************//
/*
Task 1: Write a program to send a string from microcontroller to hyperterminal of PC using serial communication.
*/

#include<reg51.h>
#include<stdio.h>

#define BAUD 2400
#define SYSCLK 10000000
#define CLK_MODE 12

#define CNT -SYSCLK/BAUD/CLK_MODE/32

void main()
{
TMOD = 0x20;
TH1 = CNT;
SCON = 0x40;
TR1 = 1;

printf("Hello world!!! This is working.");
while(1);

}

char putchar(char ch)
{
SBUF = ch;
while(TI == 0);
TI = 0;
return ch;
}
// *****************************************************//

When I check this out in the hyperterminal I get gibberish. The reason I suspect is that, the count calculated by the code doesn't match for 2400 baud rate since the clock is of 10MHz. Hence, usually crsytals of numbers like 11.0592MHz or 18.432MHz is used. But the examples given just work fine.

I'm wondering can you help a bit in fine tuning this simple code to get the desired result.

Thank you for your time.

Regards,
Elijah Toppo
Lecturer, School of Technology,
Pandit Deendayal Petroleum University,
Gandhinagar, Gujarat.

User avatar
Aleksandar.Mitrovic
mikroElektronika team
Posts: 1697
Joined: 11 Mar 2015 12:48

Re: Help for UART initialization

#2 Post by Aleksandar.Mitrovic » 20 Nov 2015 16:07

Hi,

Can you please tell me which microcontroller did you use for this example?

I suggest you to use PCON and TCON registers as well.
And set correct values for them.

Best regards,
Aleksandar

elijah981
Posts: 2
Joined: 20 Nov 2015 05:50

Re: Help for UART initialization

#3 Post by elijah981 » 24 Nov 2015 08:19

Hi,

I used AT89S8253.

I'm using the bits in TCON like TR1. Are you suggesting to set the value of TCON as an 8-bit register?
Also, should I use the PCON for doubling the UART speed?

Thank you for your reply.

User avatar
Aleksandar.Mitrovic
mikroElektronika team
Posts: 1697
Joined: 11 Mar 2015 12:48

Re: Help for UART initialization

#4 Post by Aleksandar.Mitrovic » 24 Nov 2015 14:40

Hi,

Try to set registers like this:
SCON = 0x50;
PCON = 0x80;
TCON = 0x40;
TMOD = 0x20;

Best regards,
Aleksandar

Post Reply

Return to “mikroC PRO for 8051 General”