# [SOLVED]help on serial programming with 8051. i got black box character.. :)

Status
Not open for further replies.

guys i need help.. why I read weird characters from microcontroller? i only tried tranmitting data "r" from controller but what I read in the hyperterminal is not correct?.

the data received by pc is a block box character... what's wrong with this program?

Code C - [expand]1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void serial_init()
{
TMOD = 0x20;  //enable timer1 / mode 2 / 8-bit auto-reload
SCON = 0x50;  //Serial port mode bit 1 / 8 bit. / Receiver Enable.
TH1  = 0xFD;  // generate 9600 baudrate
TL1  = 0xFD;
TR1  = 1;     //start timer 1
}

void serial_send(unsigned char dat)
{
SBUF = dat;                        // Store data in sbuf
while(TI==0);                          // Wait till data transmit
TI=0;
}

Code ASP - [expand]1
2
3
4
5
6
void main()
{
serial_init();
serial_send('r');
while(1);
}

#### ckshivaram

TL1 should be zero or not to be written in program.

---------- Post added at 13:01 ---------- Previous post was at 12:59 ----------

try to call the send serial in a while (1) infinetely and see if you get the character on the terminal window...

check this on simulator first... also check the crystal you are using.

points: 2

#### d.javeedakthar

##### Junior Member level 1
Hello
As per your code there is must be no problem,one thing -
no neeed to initialize TL1.
check out the baud rate and port settings in the hyperterminal.If that doesnt solve the problem then post the whole code so that we can find any bugs if any.

points: 2

hi, I just simulated it in protues using virtual terminal..

and now I found out if Im going to use the serial window of keil the characters displayed correctly...
I think theres something wrong with my setup..

---------- Post added at 09:44 ---------- Previous post was at 09:42 ----------

Hello
As per your code there is must be no problem,one thing -
no neeed to initialize TL1.
check out the baud rate and port settings in the hyperterminal.If that doesnt solve the problem then post the whole code so that we can find any bugs if any.
the port setting is correct and the baudrate is the same..

that's only my code because I just started and I wrote code simply as that just to check if I can transmit data to seially..

I use the serial window of keil and i can display the character correctly but in proteus to windows hyperterminal its not displaying correctly

#### ckshivaram

check your hyperterminal settings ... or restart the system, .. Are you sure your system does not have virus??????

points: 2

check your hyperterminal settings ... or restart the system, .. Are you sure your system does not have virus??????
okay i will restart my system.. I pretty sure I dont have virus here..

---------- Post added at 09:59 ---------- Previous post was at 09:48 ----------

check your hyperterminal settings ... or restart the system, .. Are you sure your system does not have virus??????
okay i will restart my system.. I pretty sure I dont have virus here..

---------- Post added at 10:08 ---------- Previous post was at 09:59 ----------

check your hyperterminal settings ... or restart the system, .. Are you sure your system does not have virus??????
the same result..

question.. can we have hyperterminal in proteus where i can test if the controller can read/write serially ????

#### ckshivaram

please check with some external software like tera term or serial port analyser...

I think it is hyperterminal problem...

points: 2

points: 2

### samba007

points: 2

Im using now third party software named SIVterm ad hyperterminal but still I got weird characters.. hmmmm

#### ckshivaram

try with this

i got output in hyperterminal for the program.... wheck the crystal you are using and hyperterminal settings again...
9600 8-N-1 no parity..

---------- Post added at 14:22 ---------- Previous post was at 14:18 ----------

last option is replace your MAX232 IC...

samba007

### samba007

points: 2

try with this

i got output in hyperterminal for the program.... wheck the crystal you are using and hyperterminal settings again...
9600 8-N-1 no parity..
but why all hyperterminal I am using is functioning well when I connected my phone and do At commands? no problem at all..

#### ckshivaram

i am getting the output for the program on my hyperterminal......

did you disconnect the phone or remove the phone when the application was running,
Open a new connection of hyperterminal and try... dont open the shortcut you might have saves for quick opening of hyperterminal.....

okay thanks.. do you have simple serial communication program so that I can try another code?.....

---------- Post added at 11:01 ---------- Previous post was at 11:00 ----------

EDIT:

I do what you said but now I got only .... .... .... data shown in the hyperterminal instead letter "H"

---------- Post added at 11:02 ---------- Previous post was at 11:01 ----------

remember I transmitted the data virtually using proteus and show the transmitted data in hyperterminal

#### ckshivaram

Code:
#include<reg51.h>
void serial_init()
{
TMOD=0x20;
SCON=0x50;
TH1=0xfd;
TR1=1;
}
void main(void)
{
unsigned  char var;
serial_init();
while(1)
{
while(!RI);
RI=0;
ACC=SBUF;
val=ACC;
SBUF=val;
while(!TI);
TI=0;
}
}

points: 2

points: 2

### samba007

points: 2

by the way enough for that troubleshooting I will try to solved that later..

------------------------------------------------------------------------------

by the way how to simulate this in proteus? what i want to do with this program is It will transmit back to the hyperterminal of what data i entered in hyperterminal. in short it will just echo what i typed in hyperterminal

is this correct?

Code:
#include <REG51.H>

void serial_init()
{
TMOD = 0x20;  //enable timer1 / mode 2 / 8-bit auto-reload
SCON = 0x50;	//Serial port mode bit 1 / 8 bit. / Receiver Enable.
TH1  = 0xFD;	// generate 9600 baudrate
TR1  = 1;		//start timer 1
}

void serial_send(unsigned char dat)
{
SBUF = dat;       // Store data in sbuf
while(TI==0);    // Wait till data transmit
TI=0;
}

{
while(RI == 0);
RI = 0;

}

void main()
{
serial_init();
IE = 0x90;

while(1)
{

}

}

---------- Post added at 12:49 ---------- Previous post was at 11:35 ----------

I got the problem about hyperterminal.. I think the virtual serial port I am using is not communicating well.

hi this my update.. I can read and write to serially using keil UART window with the code below.. but someone can teach me how to do it in protues??

Code C - [expand]1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <REG51.H>

unsigned char recve;

void serial_init()
{
TMOD = 0x20;  //enable timer1 / mode 2 / 8-bit auto-reload
SCON = 0x50;  //Serial port mode bit 1 / 8 bit. / Receiver Enable.
TH1  = 0xFD;  // generate 9600 baudrate
TR1  = 1;     //start timer 1
}

void serial_send(unsigned char send)
{
SBUF = send;
while(TI==0);
TI = 0;
}

{
while(RI==0);
recve = SBUF;
RI=0;

}

void main()
{

P2 = 0x00;
serial_init();

while(1)
{

if(recve == 'y')
P2 = 0x03;       // turn on output when y is received
else
{
P2=0x00;
serial_send('k');   //transmit K to serial when recieved is not y
}
}

}

points: 2

points: 2

### samba007

points: 2

draaw the circuit in it as per your code and port pins, link the code you wrote and run it,, it will open a black window and show output when you run the program...........

---------- Post added at 18:44 ---------- Previous post was at 18:39 ----------

Proteus - APCircuits

---------- Post added at 18:46 ---------- Previous post was at 18:44 ----------

http://www.mindmodulations.com/resources/Proteus-createsession.doc

Im done connecting but where do i type the character I want to send?
pls check this picture

is there a component I need to include in my schematic in eqivalent to hyperterminal in proteus?

---------- Post added at 15:30 ---------- Previous post was at 15:27 ----------

wow!! nice.. its now working in hyperterminal..!!!!!

---------- Post added at 15:39 ---------- Previous post was at 15:30 ----------

hi.. i dont know what happens but when i tried to communicating again to hyperterminal with proteus, it works! heheeh thanks guys..

big thanks to you ckshivaram you always answers my query here

---------- Post added at 15:44 ---------- Previous post was at 15:39 ----------

but , question.. what is the syntax when for example i want to check a word "hi" if hi then it will reply "hello" ...

is uart communication does the reading bit by bit? I mean one letter only at a time?? ;( ;(

samba007 and MRAMA

points: 2

### samba007

points: 2

could someone explain to me the code below? i used this code to send string.

while(*p!='\0') --- > I think this line will know the end of the character send and terminate.. could someone explain this line how it works?

Code C - [expand]1
2
3
4
5
void serial_sendstr(unsigned char *p)
{
while(*p!='\0')
serial_send(*p++);
}

#### ckshivaram

*p is a pointer which has the data..
(*p!= '\0') means that till the occurance of NULL keep sending the data... whenever a NULL or an blank space is encountered stop transmission...

---------- Post added at 09:25 ---------- Previous post was at 09:23 ----------

you have a string of variable length and want to transmit it ... you dont know when to stop as the length is variable, so \0 tells that whenever a blank space is encounterd stop sending the string , and assumes that the string has been terminated or finished..

points: 2

points: 2

### samba007

points: 2

hi ckshivaram

Im doing exercise now about string how to manipulate something maybe i can use this during receiving serial data.. this project I am preparing is for sms based project I talked to you before in other thread..

i have question about the data received from serial. is it only capable of receiving only 1 character..

how can I received a string then compare then execute?
something like this:

if data receive is "romel" then P1.0 is on/off ... for now I can only received only 1 character... is ti possible to receive a string at once?

this code below is just practice purposed about string..

Code C - [expand]1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <stdio.h>
#include <string.h>

int search(char *p[], char *name);
char *names[] = {"John","Jim","Jane","James","romel",NULL};

int main()
{
char s1 [] = "ako si batman pangit";
char s2 [] = "hi Im romel gwapo";

char s3 [] = "ako ay gwapo kaayo";
char s4 [] = "gwapo kaayo";

//start from the first occurence of the string
printf ("\n1.%s", strstr (s1,"si"));
//start from the first occurence of the string

//searches string from s3 to s4 ///
if(strstr(s3, s4) != NULL)    //searches string from s3 to s4
printf("\n2.String found: %s\n \n \n \n",s4);

else
//END searches string from s3 to s4 ///

//search string inside pointer array
if(search(names, "romel") != -1)
printf("3.in list \n \n");
else
printf("not in the list \n \n");
//end search string inside pointer array

}

//search string inside pointer array function
int search(char *p[], char *name)
{
int t;

for(t=0; p[t]; ++t){
if(!strcmp(p[t], name))
{
return t;
}
}
//search string inside pointer array function