UART between at90usb162 and atmega16

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

I am trying to use UART to communicate between at90usb162 and Atmega16....I can communicate one way mean half duplex when either of one is transmitter and other is receiver....

But when I try to make it both way(still half duplex because I can't send and receive simultaneously)...
I mean in a continuous loop ...first I am sending 5 bytes and then receive 5 bytes ...but its not happening...I think my code may be not proper...

For AT90usb162:

Declare Sub Usart_vsendbyte(d1 As Byte)
Declare Function Getbyte() As Byte

  Do

 C = C + 1
  A = D + C
  A = A + E


  '**** Send 5 Bytes****
  Usart_vsendbyte B
  Usart_vsendbyte D
  Usart_vsendbyte C
  Usart_vsendbyte E
  Usart_vsendbyte A


  '**** Receive 5 Bytes*******
  Sync = Getbyte()

 If Sync = 65 Then

 Addr = Getbyte()
 Data1 = Getbyte()
 Data2 = Getbyte()
  Chk = Getbyte()

 End If
 Z = Addr + Data1
 Z = Z + Data2
 If Chk = Z Then
 Set Portd.7
 Portb = Chk
 Else
 Reset Portd.7
 End If




Loop

End

Sub Usart_vsendbyte(d1 As Byte)
 Label:                                                     'wait till send a byte


If Ucsra.udre1 = 1 Then

Udr = D1
Ucsra.udre1 = 0

Else
Goto Label

End If


End Sub



Function Getbyte() As Byte

  Label1:                                                   'wait till receive a byte
  If Ucsra.7 = 1 Then
   Ucsra.7 = 0
  Getbyte = Udr

  Else
  Goto Label1
  End If
  End Function Getbyte()

For Atmega16
Same functions..but in a do loop it receives first and then send ...
It is not working ...
Any one can help??[/code]

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

Does that odd language have a facility to at least make the Rx side of things interrupt driven?

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

clawson wrote:
Does that odd language have a facility to at least make the Rx side of things interrupt driven?

can you elaborate on this? if possible can you write your logic...even in C...I will manage......

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

Well the key to breaking your half-duplex rule is to not have one end or the other block while waiting for characters to arrive. Instead you configure the code to use a UART receive interrupt so that no matter where the code is "stuck" in a loop waiting for something to happen, when a character arrives it will switch and respond to it. The usual thing to do is then put the received character into an array of bytes ("buffer") and in this buffer you maintain both read and write pointers which, when they reach the end wrap round back to the beginning (so such buffers are known as "circular" or "ring" buffers). Meanwhile, the code that actually makes use of received characters does not need to sit waiting but instead, whenever it gets the chance to process anything that's arrived "recently" it just has to compare the read and write pointers for the buffer. If they are at the same point there's nothing to be used but if the write pointer is in advance of the read pointer (which can actually be behind it because of the circular nature) then the consuming code just keeps retrieving/processing characters (and thus incrementing the read pointer) until it catches up with the write pointer.

While you could try and continue to work on your current code I have a feeling that if you made this dramatic change of architecture that any problems you are currently experiencing may evaporate - sure you may have a whole new set of problems but that's another game entirely ;-)

Cliff

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

So are you talking about using interrupts like UDRE,URXC and UTXC...
I think I would need Receiving Interrupt(URXC if I am not wrong).. for data waiting at RXD pin of uart...
is it what you are suggesting?

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

Yup, RXC interrupt.