can I read two digit number using UART1_Read()?

General discussion on mikroC PRO for PIC.
Post Reply
Author
Message
kumar123
Posts: 68
Joined: 17 Oct 2023 07:32

can I read two digit number using UART1_Read()?

#1 Post by kumar123 » 27 Mar 2024 13:55

Hi,
I have a doubt, can I read two digit number using UART1_Read() function, number can be any two digit number.
If yes then how ?

Code: Select all

UART1_Read(digit);
Any suggestions will be appreciated!

Reagrds,
kumar

User avatar
IvanJeremic
mikroElektronika team
Posts: 316
Joined: 05 Sep 2022 14:32

Re: can I read two digit number using UART1_Read()?

#2 Post by IvanJeremic » 07 Apr 2024 09:30

Hi,

Uart1_read can read two digit numbers, you can see it used in multiple examples for click boards where it reads a lot larger numbers.

You can also find examples for using UART without click boards where you can read larger numbers.

Regards,

Ivan.

paulfjujo
Posts: 1544
Joined: 24 Jun 2007 19:27
Location: 01800 St Maurice de Gourdans France
Contact:

Re: can I read two digit number using UART1_Read()?

#3 Post by paulfjujo » 07 Apr 2024 19:35

hello,


what is your definition of a digit ?

Uart1_Read ( a_byte);

a value on 2 digits : ex: 65
MSB=6 LSB=5
BCD coded as
0110 00101

On other side : how are send theses 2 digits ?
in binary format 0100 0001
ascii format "65"
BCD format 0110 00101

kumar123
Posts: 68
Joined: 17 Oct 2023 07:32

Re: can I read two digit number using UART1_Read()?

#4 Post by kumar123 » 08 Apr 2024 05:15

Hello IvanJeremic,
Could you provide any example or link of that query from where I can get something

Regards,
Kumar

kumar123
Posts: 68
Joined: 17 Oct 2023 07:32

Re: can I read two digit number using UART1_Read()?

#5 Post by kumar123 » 08 Apr 2024 05:20

Hello paulfjujo,
I am sending these number in ascii formate.
for example:

Code: Select all

unsigned int number;
number = UART1_Read();  //input = 65
UART1_Write(number);  //output = 6
Lets say if I want to read 65 from the above code It will only read 6

Regards,
Kumar

paulfjujo
Posts: 1544
Joined: 24 Jun 2007 19:27
Location: 01800 St Maurice de Gourdans France
Contact:

Re: can I read two digit number using UART1_Read()?

#6 Post by paulfjujo » 08 Apr 2024 19:34

try this





char c:
int value;
int i=0;



.. somme init here....

do
{
i=0;
if ( (UART1_Data_Ready())&& (i==0))
{
c=UART1_Read() ;
UART1_Write (c); // echo '6' en ascii
value=( c-48)*10; // value=6*10=60
i++;
}

if ((UART1_Data_Ready()) && (i==1))
{
c=UART1_Read() ;
UART1_Write (c); // echo '5' ascii
value= value +( c-48); // value = 60 + 5 = 65
i++;
}
}
while(i<2);

at exit , value=65 ..if you send "65" by keyboard terminal !

kumar123
Posts: 68
Joined: 17 Oct 2023 07:32

Re: can I read two digit number using UART1_Read()?

#7 Post by kumar123 » 16 Apr 2024 10:59

Hello paulfjujo,
Your code is working fine. but if print number in next line it will not work.

Code: Select all

char c:
int value;
int i=0;



.. somme init here....

do
{
i=0;
if ( (UART1_Data_Ready())&& (i==0))
{
c=UART1_Read() ;
UART1_Write (c); // echo '6' en ascii
value=( c-48)*10; // value=6*10=60
i++;
}

if ((UART1_Data_Ready()) && (i==1))
{
c=UART1_Read() ;
UART1_Write (c); // echo '5' ascii
value= value +( c-48); // value = 60 + 5 = 65
i++;
}
}
while(i<2);

/*
* The output of the above code shoud be like this :
*   65
*   23
*   78
*   90
* but your code is giving output in one line 65 23 78 90 etc...
*/

Regards,
Kumar

paulfjujo
Posts: 1544
Joined: 24 Jun 2007 19:27
Location: 01800 St Maurice de Gourdans France
Contact:

Re: can I read two digit number using UART1_Read()?

#8 Post by paulfjujo » 17 Apr 2024 15:04

hello,

kumar123 wrote:
16 Apr 2024 10:59
.....Your code is working fine. but if print number in next line it will not work.

Code: Select all

char c:
int value;
int i=0;
.. somme init here....

do
{
i=0;
if ( (UART1_Data_Ready())&& (i==0))
{
c=UART1_Read() ;
UART1_Write (c); // echo '6' en ascii
value=( c-48)*10; // value=6*10=60
i++;
}

if ((UART1_Data_Ready()) && (i==1))
{
c=UART1_Read() ;
UART1_Write (c); // echo '5' ascii
value= value +( c-48); // value = 60 + 5 = 65
i++;
}
//   add here LF +CR  => to  change to the begining of next line
UART1_Write (10);
UART1_Write (13);

}
while(i<2);


Post Reply

Return to “mikroC PRO for PIC General”