Send Interger data over UART1_Write_Text

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

Send Interger data over UART1_Write_Text

#1 Post by kumar123 » 11 Dec 2023 11:31

Hi,
I am trying to send integer data over uart from 1 to 65535, for every integer taking delay of 1 second. I am getting data from 1 to 260 without given any delay, if i introduce delay of 1 second, I only received 1,2,3 integer as output.


Code: Select all

unsigned Uart1_Intout_ReturnInt(unsigned long int i) {
   char puf[8]; //for max 5 digits and the end-sign

   WordToStr(i, puf); // in "Conversions" library
   UART1_Write_Text(puf);
   Delay_ms(1000);
   UART1_Write(10);
   UART1_Write(13);
   return i;
}



void main() {
    unsigned y, result;
    unsigned long int counter=1;;

    UART1_Init(9600);  //9600
    Delay_ms(10);
    y = 1;
    
    result = Uart1_Intout_ReturnInt(y);   // "  999"
    while(counter <=65535){
      Uart1_Intout_ReturnInt(1 + counter);   // " 1000"
      counter++;
      if(counter == 65536){
            counter = 1;
       }
       
    }

}





Regards,
Kumar

hexreader
Posts: 1784
Joined: 27 Jun 2010 12:07
Location: England

Re: Send Interger data over UART1_Write_Text

#2 Post by hexreader » 12 Dec 2023 00:36

You need to turn off watchdog timer in project configuration bits
Start every day with a smile...... (get it over with) :)

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

Re: Send Interger data over UART1_Write_Text

#3 Post by kumar123 » 01 Feb 2024 10:04

Hi hexreader,

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.

Code: Select all

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);
        }
     }
 }
This is when I am reading single digit integer and printing the same:
screa1.png
screa1.png (207.33 KiB) Viewed 313 times
This is when first time I am reading integer greater than 10 and try to print, but it get stuck
scr2.png
scr2.png (204.46 KiB) Viewed 313 times
This is when second time enter the same integer get gives wrong value:
sc3.png
sc3.png (204.52 KiB) Viewed 313 times
Hope you understood the problem.

Regards,
Kumar

Thomas.Pahl@t-online.de
Posts: 158
Joined: 24 May 2008 15:55
Location: Germany

Re: Send Interger data over UART1_Write_Text

#4 Post by Thomas.Pahl@t-online.de » 04 Feb 2024 18:03

You should decide if you want to send the number as text or as number.

If you want to send as text (you use uart_write_text) then you have to convert the integer into a string with IntToStr function.
And on the receiver side should convert it back (with StrToInt function).

If you want to send the integer as number. split it into two bytes with hi(integer) and lo(integer) and send them one after the other (with uart_write) over the interface.
On the receiver side receive the two bytes and put them together.

greetings

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

Re: Send Interger data over UART1_Write_Text

#5 Post by kumar123 » 09 Feb 2024 06:37

Hi,
I had tried whatever you suggest, still it was not working .

Is there any suggestions?

Regards,
Kumar

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

Re: Send Interger data over UART1_Write_Text

#6 Post by kumar123 » 14 Feb 2024 06:02

Hi,

Does anyone have suggestions for the above query?

Regards,
Kumar

Post Reply

Return to “mikroC PRO for PIC General”