Microcontroller › 8051 › Cannot Receive 4digit Integer value from serial port
- This topic has 10 replies, 2 voices, and was last updated 10 years, 3 months ago by
Bakhtiar.
-
AuthorPosts
-
December 6, 2014 at 10:17 am #3339
Bakhtiar
ParticipantDear Concern,
I have send 1 digit value from serial port and it received and work but when i am sending 4 digit integer value at a time from serial port it cannot reveive the value it receive only 1 digit value. Can any one help me.
Thanks in advanced.
Bakhtiar
December 7, 2014 at 7:24 pm #12400Ashutosh Bhatt
Participantfrom serial port each and every letter or digit is received as ASCII value that is also saparately
that means if you want to send 4 digit value as 3425 then serial port will send 4 ASCII values
33h, 34h, 32h and 35h
you have to store all four values in an array (or table)
then you have to write a program to convert all 4 ASCII values into digits and finally combine them into single value
December 10, 2014 at 9:17 am #12408Bakhtiar
ParticipantThanks a lot about your reply.
Ok if i use array then my program is receive using serial first time data here is my code.
void compare()
{if(strcmp(msg,”1234″)==0)
{
lcdcmd(0x01);
lcddata(‘B’);
}
if (strcmp(msg,”4321″)==0)
{
lcdcmd(0x01);
lcddata(‘C’);
}
}void recieve() interrupt 4
{int i;
while (RI != 1) {;}msg[i++] = SBUF;
RI = 0;compare();
}for example: after running the code it take any one of the input
if input “1234”
then it worked.
but at the same time when i input
“4321”
it will not take.
please help me
December 12, 2014 at 11:11 am #12416Ashutosh Bhatt
Participantyou have not initialized i as i=0
also after getting all four codes you have to again reset i to 0
December 12, 2014 at 1:23 pm #12419Bakhtiar
ParticipantThanks a lot sir,
Your instruction is finally give me solution.I have appled and its done successfull.
I reset the “i” and it worked.
Thanks again Sir.
December 17, 2014 at 5:57 pm #12422Ashutosh Bhatt
Participantgood
now post your working code your project details
so that other can get ideas
December 18, 2014 at 4:11 pm #12431Bakhtiar
ParticipantIt work fine but lettle problem i faceing, this is
the define string compare is ok for string compare but if i again press the same keys the
it will take only first digit for example#include<reg51.h>
#include<string.h>
#define dataport P2
int i=0;
unsigned char msg[20];
void lcdcmd(unsigned char);
void lcddata(unsigned char);void compare()
{if(strcmp(msg,”1234″)==0)
{
lcdcmd(0x01);
lcddata(‘B’);
i=0;
}
if (strcmp(msg,”4321″)==0)
{
lcdcmd(0x01);
lcddata(‘C’);
i=0;
}
}void recieve() interrupt 4
{while (RI != 1) {;}
msg[i++] = SBUF;
RI = 0;compare();
}void main()
{
while(1);
}
void lcdcmd(unsigned char value)
{ dataport=value;
rs=0;
rw=0;
en=0;
delay(1);
en=1;
}void lcddata(unsigned char value2)
{ dataport=value2;
rs=1;
rw=0;
en=0;
delay(1);
en=1;
}if again press “1” then the first compare block will display ‘B’ it will
not take “234”
but if i press another digit block that is “4321” it will take that one and display ‘C’
again if i press first 4 digit then
it will display ‘B’it will still not empty the fist digit block
but take second digit block again it will take first digit block so on.December 21, 2014 at 6:29 pm #12442Ashutosh Bhatt
Participantin your compare() function
after first condition if(….)
next condition must be else if (….)
not just if
December 22, 2014 at 5:36 pm #12445Bakhtiar
Participantstill it will take the first block value ,i mean the array value is not empty.
if i press another block of value then previous array value will empty.
for example
first time
when press “1234” then lcd display
if i again press only “1” of that block that time also lcd display.But i want if i again press “1234” second time then lcd will display.
after your advice i change my code in below way.
void compare()
{if(strcmp(msg,”1234″)==0)
{
lcdcmd(0x01);
lcddata(‘B’);
i=0;
}
else if (strcmp(msg,”4321″)==0)
{
lcdcmd(0x01);
lcddata(‘C’);
i=0;
}
}void recieve() interrupt 4
{while (RI != 1) {;}
msg[i++] = SBUF;
RI = 0;compare();
}but still face problem
December 24, 2014 at 10:17 am #12454Ashutosh Bhatt
Participantyour compare function should be only called after you receive all digits like 4 or 5 etc,
you are calling compare function everytime when you receive any byte
December 24, 2014 at 11:13 am #12455Bakhtiar
ParticipantThanks your valuable answer.
Please ,
Can you tell how can i do this or any example. Then it will be very helpfull for me.
Thanks
-
AuthorPosts
- You must be logged in to reply to this topic.