Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

transmit Interger data over UART1_Write_Text

kumarr123

Junior Member level 1
Joined
Oct 18, 2023
Messages
19
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
155
Hi,
I want output of this below code like- initially it print the whatever have the initial value, it will keep on printing until I entered the new integer, again it will keep on printing(old value ) value until I entered the new value and so on. This below code works well for single digit Integer(0-9), when I read integer greater than 10, first time it get stuck, second time I read same value it print wrong value.
I am using PIC18F67K40 micro-controller.
Code:
void main(){
     char str[256] = "0";
     char st[256];
     TRISC6_bit = 0;   // Tx pin set as output
     TRISC7_bit = 1;   // Rx pin set as input

     UART1_Init(9600); // Initialize the UART with a baud rate of 9600

     Delay_ms(4);     //Wait for UART to stabilize

     while(1){
    
        if(UART1_Data_Ready() == 1){
            UART1_Read_Text(st, ";", 255);
            UART1_Write_Text(st);
             strcpy(str,st);
            UART1_Write_Text("\r\n");
            Delay_ms(1000);

        }
        else{
            UART1_Write_Text(str);
            UART1_Write_Text("\r\n");
            Delay_ms(1000);
        }
     }
 }
1) This is when I am reading single digit integer and printing the same:
screa1.png

2) This is when first time I am reading integer greater than 10 and try to print, but it get stuck:
scr2.png

3) This is when second time enter the same integer get gives wrong value:
sc3.png


Hope you understood the problem.

Regards,
Kumar
 
Hello!

Hope you understood the problem.

Not at all, and I don't understand your code either. What I understand is that
you get a string if data is ready, and copy it into str. What for?

But anyway you can operate some factorization of your code. Writing \r\n is
in bothecased (i.e. doesn't depend whether data is ready or not). And delay
does not depend on it either.
So your code here

Code:
while(1){
    if(UART1_Data_Ready() == 1){
        UART1_Read_Text(st, ";", 255);
        UART1_Write_Text(st);
        strcpy(str,st);
        UART1_Write_Text("\r\n");
        Delay_ms(1000);
    }
    else{
        UART1_Write_Text(str);
        UART1_Write_Text("\r\n");
        Delay_ms(1000);
    }
}

could be rewritten as:

Code:
while(1){
    if(UART1_Data_Ready() == 1){
        UART1_Read_Text(st, ";", 255);
        UART1_Write_Text(st);
        strcpy(str,st);
    }
    else{
        UART1_Write_Text(str);
    }
    UART1_Write_Text("\r\n");
    Delay_ms(1000);
}

Dora
 
Hello!



Not at all, and I don't understand your code either. What I understand is that
you get a string if data is ready, and copy it into str. What for?

But anyway you can operate some factorization of your code. Writing \r\n is
in bothecased (i.e. doesn't depend whether data is ready or not). And delay
does not depend on it either.
So your code here

Code:
while(1){
    if(UART1_Data_Ready() == 1){
        UART1_Read_Text(st, ";", 255);
        UART1_Write_Text(st);
        strcpy(str,st);
        UART1_Write_Text("\r\n");
        Delay_ms(1000);
    }
    else{
        UART1_Write_Text(str);
        UART1_Write_Text("\r\n");
        Delay_ms(1000);
    }
}

could be rewritten as:

Code:
while(1){
    if(UART1_Data_Ready() == 1){
        UART1_Read_Text(st, ";", 255);
        UART1_Write_Text(st);
        strcpy(str,st);
    }
    else{
        UART1_Write_Text(str);
    }
    UART1_Write_Text("\r\n");
    Delay_ms(1000);
}

Dora
Hi Dora,
I am using mikroC pro for pic compiler to develop this code.
And the micro controller, I am using PIC18F67K40.
yes, we can write code that way also.

my problem is, from the code Initially it keep on print the number over UART, untill you will not entered new number, when you entered new number it will keep print new number and so on. This code is working fine for single digit integer. But When I entered number greater than 10. It will not work.

Kumar
 
Hello!
It depends on the implementation of UARTReadText. Is it a standard function or something you wrote yourself.
Your UART might generate an interrupt each time it receives a byte. In this case, you have to create a buffered UART
which puts data in an array until you have some character which triggers an interrupt. I can't tell you without
having a look at the code. If somebody else wrote the code, you should check that code or its documentation.
Dora.
 
Hello!
It depends on the implementation of UARTReadText. Is it a standard function or something you wrote yourself.
Your UART might generate an interrupt each time it receives a byte. In this case, you have to create a buffered UART
which puts data in an array until you have some character which triggers an interrupt. I can't tell you without
having a look at the code. If somebody else wrote the code, you should check that code or its documentation.
Dora.
Hi Dora,
Yes, UART1_Read_Text(input,delimiter, size), it is a standard library function, it reads a string till delimiter.

Kumar
 
Hi,

from mikroE doc:

UART1_Read_Text. As mentioned it reads data until delimiter.


UART Library:
This is a blocking call: the delimiter sequence is expected, otherwise the procedure exits (if the delimiter is not found).

Unclear how long it waits (blocking)
Unclear what happens when there is data after the delimiter. My guess: it stays in the buffer.

Klaus
 

LaTeX Commands Quick-Menu:

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top