Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

Hardware Uart to Software Uart communication
Goto page 1, 2  Next
 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR
View previous topic :: View next topic  
Author Message
toto







PostPosted: Thu Aug 18, 2016 3:29 pm    Post subject: Hardware Uart to Software Uart communication Reply with quote

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
Code:
 


(BASCOM-AVR version : 2.0.7.8 , Latest : 2.0.7.8 )
Back to top
luizabbadia

Bascom Member



Joined: 11 Dec 2007
Posts: 112
Location: rio de janeiro

brazil.gif
PostPosted: Thu Aug 18, 2016 4:39 pm    Post subject: Reply with quote

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
View user's profile MSN Messenger
toto







PostPosted: Thu Aug 18, 2016 5:13 pm    Post subject: Reply with quote

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

Bascom Member



Joined: 11 Dec 2007
Posts: 112
Location: rio de janeiro

brazil.gif
PostPosted: Thu Aug 18, 2016 6:55 pm    Post subject: Reply with quote

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
View user's profile MSN Messenger
toto







PostPosted: Thu Aug 18, 2016 11:35 pm    Post subject: Reply with quote

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

Bascom Member



Joined: 11 Dec 2007
Posts: 112
Location: rio de janeiro

brazil.gif
PostPosted: Fri Aug 19, 2016 12:11 am    Post subject: Reply with quote

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
View user's profile MSN Messenger
luizabbadia

Bascom Member



Joined: 11 Dec 2007
Posts: 112
Location: rio de janeiro

brazil.gif
PostPosted: Fri Aug 19, 2016 12:14 am    Post subject: Reply with quote

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
View user's profile MSN Messenger
AdrianJ

Bascom Expert



Joined: 16 Jan 2006
Posts: 2472
Location: Queensland

australia.gif
PostPosted: Fri Aug 19, 2016 12:30 am    Post subject: Reply with quote

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
View user's profile Visit poster's website
toto







PostPosted: Fri Aug 19, 2016 10:43 am    Post subject: Reply with quote

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







PostPosted: Fri Aug 19, 2016 1:55 pm    Post subject: Reply with quote

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

Bascom Expert



Joined: 27 Jul 2005
Posts: 262
Location: Berlin

germany.gif
PostPosted: Fri Aug 19, 2016 4:15 pm    Post subject: Reply with quote

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
View user's profile
toto







PostPosted: Fri Aug 19, 2016 9:05 pm    Post subject: Reply with quote

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

Bascom Expert



Joined: 26 Mar 2014
Posts: 692

poland.gif
PostPosted: Fri Aug 19, 2016 9:37 pm    Post subject: Reply with quote

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
View user's profile Visit poster's website
O-Family

Bascom Expert



Joined: 23 May 2010
Posts: 182
Location: Japan

japan.gif
PostPosted: Sat Aug 20, 2016 1:37 am    Post subject: Reply with quote

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
View user's profile Visit poster's website
toto







PostPosted: Sun Aug 21, 2016 12:47 am    Post subject: Reply with quote

Hello,
Thanks all that try help
On Monday i give Feedback
I will experience the Serin command
too

Regards
toto
Back to top
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR All times are GMT + 1 Hour
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
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