View previous topic :: View next topic |
Author |
Message |
rwlee
Joined: 04 Aug 2008 Posts: 36 Location: La Porte, Texas
|
Posted: Wed Nov 29, 2023 6:05 am Post subject: USART, Data Reading isn't correct |
|
|
I'm using Atmega328p's fo a project, I have quite a few of them but the problem is they only have one UART!
But Using the software Serial port doesn'tfunction properly. The Output USART that is sending out data is working fine
Code: |
$regfile = "m328pdef.dat"
$crystal = 16000000
$baud = 19200
$hwstack = 64
$swstack = 64
$framesize = 64
Config PORTb.0 = Output
'Set Ser_out
Open "comb.0:115200,8,n,1" For Output As #1
Dim Cntr_ as Byte
Dim MyVar as Byte
Disable Interrupts
Cntr_ = 0
Do
Cntr_ = Cntr_ + 1
MyVar = Cntr_
Print #1 , MyVar
Wait 2
Loop
End
Close #1
|
Here is the recieving code with the Serial Monitor test
Code: |
$regfile = "m328pdef.dat"
$crystal = 16000000
$baud = 19200
$hwstack = 64
$swstack = 64
$framesize = 64
Config PORTb.0 = Input
Ser_in Alias Portb.0
Set Ser_In
Config PORTd.7 = Output
Open "comb.0:115200,8,n,1" For Input As #1
Open "comd.7:115200,8,n,1" For Output As #2
Dim MyBuad as Long
MyBuad = 115200
'Dim Buf_ as Byte
'Buf_ = 0
Dim MyVar as Byte
'Dim Cntr_ as Byte
MyVar = 0
Disable Interrupts
Do
MyVar = Waitkey(#1)
If MyVar <> 0 Then Print #2 , MyVar
Loop
End
|
Has anyone hadany luck with Software Serial Ports? How can I Fix this? |
|
Back to top |
|
|
MWS
Joined: 22 Aug 2009 Posts: 2262
|
Posted: Wed Nov 29, 2023 11:56 am Post subject: Re: USART, Data Reading isn't correct |
|
|
rwlee wrote: | How can I Fix this? |
You would only have to fix it, if it would be broken.
Dec 50 = ASCII '2'
Dec 51 = ASCII '3'
To return it as char:
Code: | If MyVar <> 0 Then Print #2 , Chr(MyVar) |
Btw., it has nothing to do with topic 'BASCOM-AVR XTINY/MEGAX/AVRX' |
|
Back to top |
|
|
MWS
Joined: 22 Aug 2009 Posts: 2262
|
Posted: Fri Dec 01, 2023 10:27 am Post subject: Re: USART, Data Reading isn't correct |
|
|
rwlee wrote: | I have quite a few of them but the problem is they only have one UART! |
The result would have been the same with hardware UARTs.
In anticipation my short answer makes you think and learn - which gives a better effect than to get the answer pre-chewed, it looks like my hope was in vain.
I will thus explain.
Code: | Dim MyVar as Byte
Print #1 , MyVar |
In this code internal representation of MyVar is a number of one byte, means the byte in SRam has value 1 if MyVar is 1.
If MyVar is printed = sent out and if sent out to a terminal, it's expected to be human readable as '1'.
For a terminal however a byte with value 1 is a control char SOH (start of header), 7 for example means BEL, bell = terminal rings its bell.
To display a value 1 in terminal as '1', it needs conversion into an ASCII-char, see ASCII-table, decimal 49 is '1'.
Print #1 , MyVar implicitly does this conversion, thus a byte with value 49 is sent over the line, followed by 2 bytes, one Carriage Return and one Line Feed (CR, dec 13, LF, dec 10).
It looks like this: 49, 13, 10
The receiving code misses the latter 2 bytes, while it is busy outputting to the terminal on #2, which takes longer than receiving on #1. **
The receiving controller thus reads only the first byte with value 49, it does not know that this byte had once a value of 1 on the sending controller.
In memory it is 49, as you do again a Print to a terminal it will be sent out as 49 in chars, thus 4 bytes: '4' (dec 52), '9' (dec 57), CR, LF
** This explains why the additional sent chars got missed, sending 4 bytes at 115kBaud takes longer than receiving 2 bytes at same rate.
If you want to send the value 1 as data, you need to use PrintBin().
The multiple readings '50" as shown on your second terminal occur due to a bug in WaitKey().
Edit:
Idle-level of soft-UART RX must be high as required by the specifications, then the soft UART will work ok, thus: no bug.
Went through the problem in more detail, based on this I'd expect 52, 53 as output from second terminal, instead of 50, 51. |
|
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
|
|