Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

Software UART missing characters

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    www.mcselec.com Forum Index -> BASCOM-AVR Archive
View previous topic :: View next topic  
Author Message
hal50

Bascom Member



Joined: 16 Feb 2014
Posts: 3

germany.gif
PostPosted: Sun Feb 16, 2014 2:35 pm    Post subject: Software UART missing characters Reply with quote

Hi Folks,

I have a curios problem with software UART. I like to connect a sensor (with 2400 Baud) to ATMEGA168 software UART.
The regular (hardware UART) COM is connected to a PC.

Both UARTs are interrupt driven. For software UART, I enabled PCINT on receive pin. The interrupts are working well and I got something on both UARTs.

My software interrupt routine does the following:

- disable interrupts
- get (read) byte into variable (Get #1....)
- give this immediately to hardware UART (UDR=Recbyte)
- and also echoing it back (Put #2....)
- enable all interrupts
.... and go back

Normally, this code is not very complicated BUT in reality I do have a problem:

Whenever I try to echo the received character, I loose every 2nd character. When I eliminate this line, I got every character on hardware UART.
To make it clearer: I send for example: "ASDFGHJKL" and get "ADGJL" back es echo.

What I am doing wrong????

Any help is appreciated.

Thanks in advance,

Robert
Code:
'------------------------------------------------------------------------------
$regfile = "m168def.dat"                                    'ATmega168-Deklarationen
$crystal = 11059200                                         'Quarz: 11059200 Hz

Dim S As String * 10                                        '10+1 Byte langer String
Dim B(11) As Byte At S Overlay                              'Auf Adresse von s setzen!

Dim N As Byte                                               'Für aktuelle Stringlänge
Dim Recbyte As Byte

'------------------------------------------------------------------------------
' Hardware UART konfigurieren; 2400,8N1
Config Com1 = 2400 , Synchrone = 0 , Parity = None , Stopbits = 1 , Databits = 8 , Clockpol = 0

'Öffne Kommunikation mit Sonde
Open "comb.0:2400,8,n,1" For Input As #1                    'Port B.0 ist RxD für Opto (E-Feld)
Open "comb.1:2400,8,n,1" For Output As #2                   'Port B.1 ist TxD für Opto (E-Feld)
On Pcint0 Serielle_lesen                                    'Sprung bei Interrupt (Port B.0 = PCINT0)

On Urxc Onrxd
Enable Urxc

Pcmsk0.0 = 1                                                ' PCINT für Port B.0 freigeben
Pcicr.0 = 1                                                 ' PCints aktivieren (PCIE0=1)
Enable Interrupts                                           ' Interrupts gernerell freigeben

'*******************************************************************************
'*********************** H A U P T P R O G R A M *******************************
'*******************************************************************************
Main:
N = 0
Do
   'mach was
Loop
Close #1
Close #2
'*******************************************************************************

'*******************************************************************************
'*************************** Interrupt Routinen ********************************
'*******************************************************************************
Onrxd:                                                      ' wird bei Eingabe ein serieller Interrupt ausgelöst
   Pcmsk0.0 = 0                                             ' erstmal PCINT0 Soft-UART sperren
   Incr N                                                   ' zähle empfangene Zeichen
   B(n) = Udr                                               ' empfangenes Zeichen in String einfügen
   Udr = B(n)                                               ' und als Echo zurücksenden
   Pcmsk0.0 = 1                                             ' PCINT0 Soft-UART wieder freigeben
Return
'*******************************************************************************
Serielle_lesen:
   Disable Urxc                                             ' serieller Interrupt sperren
   Pcmsk0.0 = 0                                             ' Solange Bits reinkommen, PCINT0 sperren
   Get #1 , Recbyte                                         ' Byte lesen
   Udr = Recbyte                                            ' und gleich weiter auf UART senden

'   Put #2 , Recbyte                                         ' Byte als Echo zurücksenden

   Pcmsk0.0 = 1                                             ' PCINT0 wieder freigeben
   Enable Urxc                                              ' serieller Interrupt wieder freigeben
Return
'*******************************************************************************


(BASCOM-AVR version : 2.0.7.7 )
Back to top
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 2262

blank.gif
PostPosted: Sun Feb 16, 2014 3:54 pm    Post subject: Reply with quote

Why do you post the same stuff in more than one forum, aka crosspost?
Simply have one's forum members do the thinking or inform them all about the fact of your crossposting.

For your problem: You simply have not understood, how interrupts work on your type of controller. In an ISR interrupts are globally locked, that's why already locking of URXC in Serielle_lesen is meaningless.
Code:
Serielle_lesen:
   Disable Urxc
   Pcmsk0.0 = 0
   Get #1 , Recbyte
   Udr = Recbyte    
   Put #2 , Recbyte
   Pcmsk0.0 = 1    
   Enable Urxc        
Return

PCINT is locked the same, so clearing the PCINT-mask is meaningless for this purpose either, it only has another (in your case important) side-effect. One would solve this better by clearing the PCINT-flag before leaving the ISR.
The next two lines may work as supposed, the hardware UART handles one byte without further software needs to take care of.
Fifth line, returning the received byte as echo will take up the same time in a soft-uart as receiving a byte therein. As there's no handshake to tell the sender to the soft-uarts to wait, the sender sends one more byte, while the time of returning it, means it is missed. As still in the ISR, no other interrupt may be executed, while the soft-uart is bitbanging the echo out.
For the rest of the ISR code same applies as said above.

Ergo: the whole concept is flawed.
Back to top
View user's profile
hal50

Bascom Member



Joined: 16 Feb 2014
Posts: 3

germany.gif
PostPosted: Sun Feb 16, 2014 5:33 pm    Post subject: Reply with quote

Thank you or your answer. I don't know, that all Bascom Forums are connected.

Thank you also for your explanation. I think, it is now clearer to me and more logically. If I understand you right, sending character after receiving it takes same time and therefore every second character ist lost?

Thank you very much, I will find other solution.

Robert
Back to top
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 2262

blank.gif
PostPosted: Sun Feb 16, 2014 5:39 pm    Post subject: Reply with quote

hal50 wrote:
it takes same time and therefore every second character ist lost?

A soft-uart uses waits for the timing and blocks this way the code as long it takes to send the byte out. If this code is executed in an ISR, everything else is blocked, including other interrupts.
Quote:
I don't know, that all Bascom Forums are connected.

They are not connected, it is simply unkind to post the same question in several forums the same time.
If it is a very special question, which likely finds not much helpers, simply state in the opening post which forums you have addressed, so others may learn from the result.
Your question however was not difficult and could have been answered by several members of each addressed forum.


Last edited by MWS on Sun Feb 16, 2014 5:49 pm; edited 1 time in total
Back to top
View user's profile
hal50

Bascom Member



Joined: 16 Feb 2014
Posts: 3

germany.gif
PostPosted: Sun Feb 16, 2014 5:46 pm    Post subject: Reply with quote

ok, sorry, it is the first time, I searched for help.

Next time I will respect this. Smile
Back to top
View user's profile
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    www.mcselec.com Forum Index -> BASCOM-AVR Archive All times are GMT + 1 Hour
Page 1 of 1

 
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