Serial communication discussion

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

this is receiver code

//////////////////////////////////////

#ifndef F_CPU
#define F_CPU 8000000UL
#endif
#include <avr/io.h>
#include <util/delay.h>
#define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) -1)

void UART_init(long USART_BAUDRATE)
{
    UCSRB |=(1<<RXEN)|(1<<TXEN);
    UCSRC |=(1<<URSEL)|(1<<UCSZ0)|(1<<UCSZ1);
    UBRRL |=(BAUD_PRESCALE);
    UBRRH |=(BAUD_PRESCALE>>8);
}
unsigned char UART_Rxchar()
{
    while((UCSRA & (1<<RXC))==0);
    return(UDR);
}
// void UART_Txchar(char ch)
// {
//     while (!(UCSRA & (1<<UDRE)));
//     UDR=ch;
//
// }

int main()
{
    DDRC=0xff;
    int a=0;

    UART_init(9600);
    while(1)
    {
        a=UART_Rxchar();
        if (a==1)
        {
           PORTC=0xff;

        }
        else if (a==2)
        {
            PORTC=0x00;
            PORTB=0xff;
            _delay_ms(1000);
            PORTB=0x00;
            _delay_ms(1000);

        }

    }
}

///////////////////////////////////////////////////////////////////////////////////

this is transmitter code

#ifndef F_CPU
#define F_CPU 8000000UL
#endif
#include <avr/io.h>
#include <util/delay.h>
#define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) -1)

void UART_init(long USART_BAUDRATE)
{
    UCSRB |=(1<<RXEN)|(1<<TXEN);
    UCSRC |=(1<<URSEL)|(1<<UCSZ0)|(1<<UCSZ1)|(1<<USBS);
    UBRRL |=(BAUD_PRESCALE);
    UBRRH |=(BAUD_PRESCALE>>8);

}
// unsigned char UART_Rxchar()
// {
//     while((UCSRA & (1<<RXC))==0);
//     return(UDR);
// }
void UART_Txchar(char ch)
{
    while (!(UCSRA & (1<<UDRE)));
    UDR=ch;

}

int main()
{
    DDRD =(1<<PIND1);
    DDRC &=~(1<<PINC0);
    PORTC|=1<<PINC0;

    UART_init(9600);
    while(1)
    {
        if (bit_is_clear(PINC,0))
        {
            UART_Txchar(1);
            _delay_ms(220);
        }
        else
        {
            UART_Txchar(2);
        }

    }
}

when receiver recevies 2 then it is performing the PORTB on and off but it is very slow like it is taking 4 or 5 sec and sometimes it stops toggle(not working any more).

and when it is receiving 1 it doesnot performing anything and the uart which i have made is very slow 

.kindly if anyone can help me to find why my uart is very slow what i have made wrong.

hoping for your valuable response.

 

Rjchoudhary

Last Edited: Sat. Apr 25, 2020 - 09:37 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

State your controller chip.

 

Moved to ATMega forum in the meantime.

 

Ross McKenzie ValuSoft Melbourne Australia

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

My transmitter chip is atmega16 and receiver is atmega32

Rjchoudhary

Last Edited: Sat. Apr 25, 2020 - 08:34 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Rename your topic to something that better represents your problem. 'avr discussion' is not very useful - this is an AVR specific forum, so most of the discussion is about AVRs.

 

AS for your problem - you have two devices. There is three possibilities - either unit has a defect or they both have a defect. You need to determine which of the units does not behave as you expect. What tools do you have at you disposal?

 

If I were to guess, I'd suggest that the delays whilst flashing the led are part of the cause. Whilst that is happening, the receiver is not paying attention to the transmitter. You can prove this simply - decrease the delays and see if that affects the outcome.

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

See #4 in my signature.

#1 Hardware Problem? https://www.avrfreaks.net/forum/...

#2 Hardware Problem? Read AVR042.

#3 All grounds are not created equal

#4 Have you proved your chip is running at xxMHz?

#5 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand."

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

Yes i did the same you suggest me but doesn't working

Rjchoudhary

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

Kartman wrote:
you have two devices. There is three possibilities - either unit has a defect or they both have a defect

indeed. [1]

 

It is a classic mistake to try to implement both ends of a link at once - don't do that!

 

Only a couple of weeks ago:

 

https://www.avrfreaks.net/commen...

 

Tips on debugging serial comms:

 

https://www.avrfreaks.net/commen... - and follow the links

 

 

Tips on debugging in general:

 

https://www.avrfreaks.net/commen...

 

Brian Fairchild wrote:
See #4 in my signature.

Always good to pay attention to what people put in their signatures ...

 

wink

 

EDIT

 

[1] Actually, there is a 4th possibility - that the connections between the 2 units are wrong and/or faulty

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Sat. Apr 25, 2020 - 09:50 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

This is what i am getting when pressing the switch and when I don't puch the switch it should transmit
2 but it doesn't

Attachment(s): 

Rjchoudhary

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

What tools do you have at your disposal? Seems you have proteus. Why didn't you tell us that up front?

 

The virtual terminal uses ASCII codes. The value 2 is unprintable. To print a an ASCII '2', you would need to send 0x32

 

Also, how do you tell Proteus your AVR is working at 8MHz? It might use the default value of 1MHz which means all your delays are 8 times longer. Might that explain your observations?

Last Edited: Sat. Apr 25, 2020 - 10:06 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Somebody mentioned about paying attention to  the information in people's signatures ...

 

Please see Tip #1 in mine for how to get a proper screen shot, and put it in your post.

 

Kartman wrote:
Seems you have proteus.

So you have the ability to step through your code and see what it's doing.

 

And you have the ability to apply virtual instruments to the circuit to see what the hardware is doing.

 

You need to learn to use your tools!

 

https://www.avrfreaks.net/commen...

 

https://www.avrfreaks.net/commen...

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

And you are still making the mistake of trying to do it all an once!

 

Develop and debug just one at a time

 

https://www.avrfreaks.net/commen...

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

its my fault that i gave you a wrong information.

what i want to show you is my connection and the thing is i am transmitting 1 when switch pressed(if pressed will low the pin) and when it is not it will transmit 2. But when i upload this to my controller (practically not in Proteus) it response time is too slow and it keep transmitting 2 whether i'm pressing the switch or not. when receiver will receive 2 it will turn all b port high and low but instead of taking a delay of 1 sec it is taking a lot.this is my problem.please find me a solution.

yes i send you the pic of Proteus it was my fault but the real problem exists when i upload the program in my hardware.

Rjchoudhary

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

Does it work correctly in proteus?

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

Rajat choudhary wrote:
please find me a solution.

No: it is your project - so it is your job to debug it!

 

Sure, we can help - but the primary job is yours.

 

Again, see: https://www.avrfreaks.net/commen...

 

Also #10

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Sat. Apr 25, 2020 - 10:55 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Rajat choudhary wrote:

...but instead of taking a delay of 1 sec it is taking a lot.this is my problem.please find me a solution.

 

I refer you back to post #5 which says " #4 Have you proved your chip is running at xxMHz? "

#1 Hardware Problem? https://www.avrfreaks.net/forum/...

#2 Hardware Problem? Read AVR042.

#3 All grounds are not created equal

#4 Have you proved your chip is running at xxMHz?

#5 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand."

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

You are using an external 8 MHz crystal on each processor to ensure you have accurate & error-free communications, correct?

 

change these    ...you are stuck waiting for 2 seconds for these.

 _delay_ms(1000);

to 

 _delay_ms(40);

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

Last Edited: Sat. Apr 25, 2020 - 04:12 PM