View previous topic :: View next topic |
Author |
Message |
toto
|
Posted: Thu Aug 18, 2016 3:29 pm Post subject: Hardware Uart to Software Uart communication |
|
|
Hello,
I have a Atmega2560 and Atmega644P conected together
On the Atmega2560 i use Hardware Uart, on the Atmega644P i use software Uart
but i not receive correct data
Can please anyone see what i do wrong?
Code: |
'Atmega 644P side
'--------------------- Software Uart use on 644P ---------------------------
'TX
Open "comc.6:9600,8,n,1" For Output As #3
Print #3 , "Test Serial Soft"
'RX
Open "comc.7:9600,8,n,1" For Input As #4
'----------------------------------------------------------------------
Enable interrupts
_begin = 254
Do
'--- Receive daa trough Soft Uart ------
B = Inkey(#4)
Reset Watchdog
If B = _begin Then 'init?
Nr = 0 : Time_out = 0 : Erro_estados = 0
Do
B = Inkey(#4)
'Print #3 , " inkeyB " ; B
Incr Time_out : Incr Nr : Waitms 1
Estad(nr) = B 'fill the array
Loop Until Nr >= 5 Or Time_out > 2000
Loop
On Atmega2560 side use Hardware Uart
Config Com3 = 9600 , Synchrone = 0 , Parity = None , Stopbits = 1 , Databits = 8 , Clockpol = 0
Open "Com3:" For Binary As #2
'ph0=rxd2 - ph.1=txd2 'TTL comunication with ATMEGA644P
'---------------------------------------------------------------------
enable interrupts
_begin = 254
Estad(1) = 127 : Estad(2) = 128 : Estad(3) = 129 : Estad(4) = 130 : Estad(5) = 131
Do
'tried this methods, but not solved
'Printbin #3 , _begin ; Estad(1) ; Estad(2) ; Estad(3) ; Estad(4) ; Estad(5)
'Print #3 , Chr(_begin) ; Chr(estad(1)) ; Chr(estad(2)) ; Chr(estad(3)) ; Chr(estad(4)) ; Chr(estad(5))
'Print #3 , _begin ; Estad(1) ; Estad(2) ; Estad(3) ; Estad(4) ; Estad(5)
Waitms 300
Print #3 , _begin
Waitms 100
Print #3 , Estad(1)
Waitms 1001
Print #3 , Estad(2)
Waitms 100
Print #3 , Estad(3)
Waitms 100
Print #3 , Estad(4)
Waitms 100
Print #3 , Estad(5)
'Print #2 , _begin ; Estad(1) ; Estad(2) ; Estad(3) ; Estad(4) ; Estad(5)
Set P_l_dbg 'led ON for Debug
Loop Until 644_init = 1 'send in loop until the Atmega644P set the pin high to indicate that received the 5 bytes
|
Thanks for help
Regards
toto
(BASCOM-AVR version : 2.0.7.8 , Latest : 2.0.7.8 ) |
|
Back to top |
|
|
luizabbadia
Joined: 11 Dec 2007 Posts: 112 Location: rio de janeiro
|
Posted: Thu Aug 18, 2016 4:39 pm Post subject: |
|
|
Hi, if you are not connecting using MAX232 may be youŽll have to use parameter "inverted"
Open "comc.6:9600,8,n,1,inverted" For Output As #3
Open "comc.7:9600,8,n,1,inverted" For Input As #4 |
|
Back to top |
|
|
toto
|
Posted: Thu Aug 18, 2016 5:13 pm Post subject: |
|
|
Hello luizabbadia,
RS232 converter is not on the PCB, it's
connected directly trough a 1k resistor,
Tomorrow i will try the inverted mode
Thank you
toto |
|
Back to top |
|
|
luizabbadia
Joined: 11 Dec 2007 Posts: 112 Location: rio de janeiro
|
Posted: Thu Aug 18, 2016 6:55 pm Post subject: |
|
|
One more reason to invert it,MAX232 would make it since you donŽt use it, you must do it yourself by declaring "inverted"
Good Luck! |
|
Back to top |
|
|
toto
|
Posted: Thu Aug 18, 2016 11:35 pm Post subject: |
|
|
Hello,
Like i said, i try it tomorrow, But i Am confuse
I send data from a hardware uart to a software uart
and must open the Com with inverted mode?
Expect the soft uart this or send the hardware uart
the data inverted?
Thanks for explanation
Toto |
|
Back to top |
|
|
luizabbadia
Joined: 11 Dec 2007 Posts: 112 Location: rio de janeiro
|
Posted: Fri Aug 19, 2016 12:11 am Post subject: |
|
|
Yes...this has some explanation at interface level.
If you connect it via MAX232, XBEE it will invert, but since you connect it via wire you should use inverted
Well, anyway I used it like this in my old projects, and it worked.
But I am talking about inverted eletrical levels, not ports.
xmit connected to rcv , normally |
|
Back to top |
|
|
luizabbadia
Joined: 11 Dec 2007 Posts: 112 Location: rio de janeiro
|
Posted: Fri Aug 19, 2016 12:14 am Post subject: |
|
|
From the Help:
"The format for the software UART is: COMpin:speed,8,N,stopbits[,INVERTED]
Where pin is the name of the PORT-pin."
Look for OPEN in the help |
|
Back to top |
|
|
AdrianJ
Joined: 16 Jan 2006 Posts: 2483 Location: Queensland
|
Posted: Fri Aug 19, 2016 12:30 am Post subject: |
|
|
If you have two processors connected directly together, IE neither has an RS232 level converter, then you DO NOT need to invert the signal. It should work correctly just connected pin-to-pin ( with maybe a resistor if you want one ).
You dont say what data to do see. Sometimes its possible to figure out what is wrong by comparing bits received vs bits sent.
I note you have the array fill commented out, so your program will not receive anything into the array as it stands.
Probably you should be using either INPUT or WAITKEY. INKEY will return a null ( binary zero ) if there is no character sent. ( I think, maybe a software UART does not do that, hardware ones certainly do ). _________________ Adrian Jansen
Computer language is a framework for creativity |
|
Back to top |
|
|
toto
|
Posted: Fri Aug 19, 2016 10:43 am Post subject: |
|
|
Hello,
The "inverted" option don't work in my case, Adrian is right
Attached the picture with the data on RX pin of the Atmega644
Code: |
Open "comc.6:9600,8,n,1" For Output As #3 'PINC.6 for Debug
Print #3 , "Test Serial Soft"
Open "comc.7:9600,8,n,1" For Input As #4 'PINC.7 receive data from Atmega2560
'Programm on Atmega644P
Do
'receive data on Soft Uart Atmega644P
If Ischarwaiting(#4) <> 0 Then
B = Inkey(#4)
Print #3 , "inkey_A " ; B 'Only for Debug, this not appears, so ischarwaiting have nothing received
If B = 254 Then 'init data?
Nr = 0
Do 'yes, get the bytes
B = Inkey(#4)
Print #3 , " inkey_B " ; B 'Only for Debug
Incr Nr : Waitms 1
Estad(nr) = B 'fill the array of 5 bytes
Loop Until Nr >= 5
Print #3 , "All arrived"
End If
End If
Loop
|
This picture shows that the Atmega2560 send the data correctly
Capture on Rx pin portC.7
Anyone idea what's wrong?
Regards
toto |
|
Back to top |
|
|
toto
|
Posted: Fri Aug 19, 2016 1:55 pm Post subject: |
|
|
Hello,
Now it works with a few limitations
The transmiter Atmega2560 i have to slow down the speed of sending, and instead sending a array, i send in separated bytes
'for my needs is not a problem
Code: |
'''Printbin #3 , _estad ; Estad(1) , 5 'works not good, not all bytes arrived
Printbin #3 , _estad 'init byte
Waitms 10
Printbin #3 , Estad1
Waitms 10
Printbin #3 , Estad2
Waitms 10
Printbin #3 , Estad3
Waitms 10
Printbin #3 , Estad4
Waitms 10
Printbin #3 , Estad5
Waitms 10
'Soft Uart receiver
Do
'If Ischarwaiting(#4) <> 0 Then 'not work
'B = Waitkey(#4) 'works
B = Inkey(#4) 'works
If B = 254 Then 'init? _estad = 254
Nr = 0
Do
B = Waitkey(#4) 'works
'B = Inkey(#4) 'not work
Incr Nr
Estad(nr) = B 'fill 5 byte array
Loop Until Nr > 5
'done, check the filled array
Print #3 , Chr(estad(1)); '= 127 is ok
Print #3 , Chr(estad(2)); '= 128 is ok
Print #3 , Chr(estad(3)); '= 129 is ok
Print #3 , Chr(estad(4)); '= 130 is ok
Print #3 , Chr(estad(5)); '= 131 is ok
end if
'end if
Loop
|
Have anyone a explanation why the Ischarwaiting(#4) and Inkey(#4) not work
Perhaps a situation for Mark to test
Best regards
toto |
|
Back to top |
|
|
laborratte
Joined: 27 Jul 2005 Posts: 299 Location: Berlin
|
Posted: Fri Aug 19, 2016 4:15 pm Post subject: |
|
|
ischarwaiting() is not supported by soft UART (see help file command open). As soft UART simulates a com port by software, there is no buffer where a received byte could be.
If you have both CPU on one board you should think about a synchronized data exchange, i.E. SPI, I2C oder clocked USART (USRT so to speak). At least a handshaking should be implemented. With a pure async COM and a lot of wait statements you're running in trouble quite easy. |
|
Back to top |
|
|
toto
|
Posted: Fri Aug 19, 2016 9:05 pm Post subject: |
|
|
Hello laborratte,
I can't change anything on the PCB
The soft Uart is in the Main-Loop and only need receive
5 bytes, not very important if one time not receive it,
but i have not experience with soft uart and don't understand
why T he fitst time it receive the 5 byte, after that can't receive
anything more.
Can it be that i must choose the chanell number lower?
I compile it with Bascom 2.0.7.8
Best regards
Toto |
|
Back to top |
|
|
EDC
Joined: 26 Mar 2014 Posts: 971
|
Posted: Fri Aug 19, 2016 9:37 pm Post subject: |
|
|
In Mega644P you have PCINT on each pin. You can detect falling edge, disable PCINT temporary, and only then input data. After receving data reenable PCINT`s. |
|
Back to top |
|
|
O-Family
Joined: 23 May 2010 Posts: 320 Location: Japan
|
Posted: Sat Aug 20, 2016 1:37 am Post subject: |
|
|
If a main loop turns around enough early.
Code: |
Do
If Pinc.7 = 0 Then 'Check the receive start.
B = Inkey(#4)
Print #3 , " inkeyB " ; B
'Next receive.
End If
'Time out processing.
Loop
|
|
|
Back to top |
|
|
toto
|
Posted: Sun Aug 21, 2016 12:47 am Post subject: |
|
|
Hello,
Thanks all that try help
On Monday i give Feedback
I will experience the Serin command
too
Regards
toto |
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You cannot download files in this forum
|
|