coding query in serial communication

Go To Last Post
11 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

void rxstring()                     ////function which receives the string and then print it on serial monitor 
{  
char myValue;
while((UCSR0A & (1<<RXC0))==0);    
{    
    for(int i=0;i<5;i++)             ////here i<5  ....5 is the length of the string which i am sending 
    {      bit(UCSR0A);                           //// if i change i<10 .....then string of length 5
    myValue = UART_RxChar();
    str[i]=myValue;
    UART_TxChar(str[i]);
    printString("\n");
    //bit(UCSR0A);                     //function to print the value of register in binary.
    }

 

 

 void bit(uint8_t val)
 {
         int8_t ptr;
         for(ptr=8;ptr>0;--ptr) 
         {
             if ((val >> (ptr-1)) & 0x01) 
             {
                 UART_TxChar('1');
                 } 
                 else 
                 {
                 UART_TxChar('0');
             }
         }
     }

i have made the function rxstring() for printing the string on the serial monitor (the string is received by atmega328 from laptop and then wants to print that string on serial monitor).

now

1)  when i dont write function bit() in rxstring() i get the output like in screenshot (119)( which is correct and prints all the characters of the string and stores the string in str).

but

2)when i write the function bit() in rxstring() i get the output like in sctreenshot(121)( which is not correct as characters of the string which i send are missing and it stores the incorrect string in str ...like the characters which are missed are not read or gets overwrite....i write the bit() to read the value of UCSRA register in binary so that can make out how the bits of it gets changed character by character and to see the value of it when the last character of the string is send).

but i cant make out the error and didnt understand the values which are shown by the register UCSRA according to the characters received. 

also what is reason for characters not printing??

 

main function is this

int main(void)
{  
    UART_Init();

    
    while (1)
    {
        rxstring();
      printString(str);    
      printString("\n");
     if(strcmp("kunal",str)==0)
     printString("string found\n");
     
     }
}

 

Attachment(s): 

This topic has a solution.

Kunal Gupta

Last Edited: Tue. Jan 7, 2020 - 06:02 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You bit() function takes 8 character times to do its work. Whilst it is doing this, your code is not reading the incoming characters.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Kartman wrote:
You bit() function takes 8 character times to do its work. 

 what does it mean by 8 characters times ...like is this the length of the string ...

 

 

Kunal Gupta

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Depending on the baud rate set for the usart, it takes a finite amount of time for the usart to send a character. This is one character time. In actual time this is 1 / baud rate times 10. So at 9600 baud this is around 1ms.
Your bit() function sends 8 characters.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Kartman wrote:
Depending on the baud rate set for the usart, it takes a finite amount of time for the usart to send a character. This is one character time. In actual time this is 1 / baud rate times 10. So at 9600 baud this is around 1ms.

So 1 ms for sending one character so if i am sending 8 characters as (length of the register in atmega 328 is 8) it will take 8ms for the uart to send one byte.

Also can u tell more about character time ..like what is its use and how can it effect my serial communication

Kunal Gupta

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You AVR can only do one thing at a time. The way you have written your code is that the AVR is busy sending characters and is not trying to read characters, so they get missed. 

 

 

Also can u tell more about character time ..like what is its use and how can it effect my serial communication

 

character time is simple - the time the usart takes to send a character - like how long does it take you to brush your teeth. It is a fundamental principle of serial communication - not an effect!

 

If you are brushing your teeth, who is going to answer the door? 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Kartman wrote:

 The way you have written your code is that the AVR is busy sending characters and is not trying to read characters, so they get missed. 

 

the bit function i found on the bet i did not write it by my own ...rxstring function is writen by me

so if possible can u tell me how is bit function working??if possible

Kunal Gupta

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Why don't you simply use interrupts and save your sanity - and a long thread post count her as well?

 

Which AVR are you using?

 

Set up a simple receive buffer that is loaded by the RX interrupt routine each time a character is received.

 

For transmitting, all you do is load the UDR and off it goes.

 

Jim

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

void bit(uint8_t val)

 

Why would you use such a ridiculous name??? That is about the worst possible choice anyone could make!  Use dog, frog, anything that won't be confused with the design of the chip or process.  

At least make the name either completely independent from any possible confusion with anything in the software package or system,  or make it a descriptive name. 

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

Last Edited: Tue. Dec 24, 2019 - 12:06 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

jgmdesign wrote:

Why don't you simply use interrupts and save your sanity - and a long thread post count her as well?

i want to try without  interreputs

and i am using atmega328

 

jgmdesign wrote:

Set up a simple receive buffer that is loaded by the RX interrupt routine each time a character is received.

For transmitting, all you do is load the UDR and off it goes.

  

 yaa i know this thing ..i want to know the logic behind the bit function.

Kunal Gupta

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The fundamental problem is your bit() function is like you walking to the shops to buy milk. In the meantime the postman has knocked at your door (ie a receive character) but you're not at home to answer the door.

 

You need to consider how you can send characters out whilst still looking for incoming characters. Think in terms of the logistics. Once you've figured out the logistics, then writing the code is simple.

 

If you want example code, then the Arduino core code has a general solution to the problem using 'circular' buffers. Yours is a common problem - there's common solutions.