How to Get string using uart interrupt

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

hello everybody . I have two board avr using atmega2560: board master and board slave . when board slave doing something, board master send a string. I use  uart interrupt but the string I received was error . this is my code of slave . thanks for reading.

String str = "";
UseString()
{
    //do something;
}
int main()
{

  UBRR1H = 0;
  UBRR1L = 0;
  UCSR1A = 0x00;
  UCSR1C = (30 << UMSEL11) | (1 << UCSZ11) | (1 << UCSZ10) ;
  UCSR1B = (1 << RXEN1) | (1 << TXEN1) | ( 1 << RXCIE1); //enable Tx and Rx
  sei();
}
while()
{
  if (check  ==  1)
  {
    UseString();
    check  = 0 ;
  }
  else
  {
    // do something
  }
}
ISR(USART1_RX_vect)
{
  char c = UDR1;
  str += c ;
  check = 1 ;
}

 

Divide

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

1:
UCSR1C = (30 << UMSEL11) | (1 << UCSZ11) | (1 << UCSZ10) ; ????
(30 << UMSEL11) will (effectively) set UMSEL11 = 0 and UMSEL10 = 0
so the end-result is that you will have Asynchronous mode, no-parity, 1 stop-bit, 8 data-bits.
Is that what the other device is set to ?


2:
What baudrate do you want ?
To determine that we need to know what the frequency of your processor clock is.

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

When does "str" ever stop growing?

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

How can this work with your WHILE() loop on the outside of MAIN?

 

Your ISR sets CHECK every time it fires, so your code will constantly only read one character at a time, not a 'String' unless the USESTRING() has some sort of magic code in it.

 

Post a complete code otherwise this is looking like a 100 poster.

 

JIm

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

Please Read: Code-of-Conduct

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

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

you_mylove wrote:
this is my code of slave

No, it most definitively is not

 

Post exactly the code you have in your editor and that you compile. Do not do any editing on the way here.

 

Simply mark everything in your editor/IDE, copy and paste into a post here.

 

Posting some edited "close-to-wghat-I-have" code snippet here will create nothing but confusion, frustration and bad or no help to you.

 

Whats wrong with the code in your post? At least these:

 

1. #include directives missing (<avr/io.h> and <string> at least..)

2. As Jim noted, code outside function (the while-loop)

 

It would help clarify for the users here if you where explicit that this is C++ code (not C).

 

you_mylove wrote:
he string I received was error

It would help if you where clear about in what way the received string was wrong (empty? garbage chars? or what...?)

"He used to carry his guitar in a gunny sack, or sit beneath the tree by the railroad track. Oh the engineers would see him sitting in the shade, Strumming with the rhythm that the drivers made. People passing by, they would stop and say, "Oh, my, what that little country boy could play!" [Chuck Berry]

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

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

hi all .thanks for reply. I have fixed it .

 

Divide

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

thank you_mylove.

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

you_mylove wrote:
 I have fixed it .

That's great.

 

So how about posting the code - for the benefit of future readers who may come here with the same problem ... ?

 

And please mark the thread as solved: http://www.avrfreaks.net/comment...