Forum Index
HomeZBasic Home   Forum RulesForum Rules   Forum FAQForum FAQ   MemberlistMemberlist   UsergroupsUsergroups   RSS FeedRSS Feed
Site SearchSite Search   LinksLinks   DownloadDownload   Digests and SubscriptionsDigests and Subscriptions
ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in   RegisterRegister
How to read RS232 9600 9 bit data stream? ShiftInEx?

 
Post new topic   Reply to topic    Forum Index -> ZBasic Language
Author Message
mscheo



Joined: 04 Feb 2006
Posts: 3

Posted: 04 February 2006, 15:55 PM    Post subject: How to read RS232 9600 9 bit data stream? ShiftInEx? Reply with quote

I am a BX-24 user, but having a requirement which I think ZX-24 can fulfill. I need to read in a RS232 stream at 9600 baud rate but it sometimes has 8 data bits and sometimes 9 data bits beside the start and stop bits. I think the ShiftInEx function of ZX-24. Is my assumption correct? If yes, what bitTime should I use to emulate the 9600 baud rate? Or any more clever method of reading such unconventional data stream?
Back to top
dkinzer
Site Admin


Joined: 03 Sep 2005
Posts: 2499
Location: Portland, OR

Posted: 04 February 2006, 17:13 PM    Post subject: Reply with quote

Quote:
...it sometimes has 8 data bits and sometimes 9 data bits beside the start and stop bits


The variability will likely cause problems. How can the receiver know when 9 bits are being sent versus 8?

I don't think that ShiftInEx() will be useful. The problem is that you don't know when the start bit will arrive so you first have to detect the start bit and then begin sampling near the middle of the bit window and storing the bits received. For low baud rates, you might be able to detect the start bit and then begin ShiftInEx() so that it samples near the middle of the bit window. That will be difficult for 9600 baud where the bit window is only 145uS wide.

In another message, http://www.zbasic.net/forum/post-560.html#560, it was shown how to use ShiftOutEx() to transmit serial data, an idea that could be adapted to 9-bit transmission. Transmission is easier than reception, however.

One other possibility is to use the hardware UART (Com1) which has 9-bit capability. To do so, you'd need to read the USART section in the mega32 manual (see the Download Page.
Back to top
mscheo



Joined: 04 Feb 2006
Posts: 3

Posted: 05 February 2006, 15:11 PM    Post subject: Reply with quote

Thanks, Don, for your reply.
If bit window is 145us for 9600 baud rate (or should it be 104uS), and min bit time is 4us, shouldn't there be enough time to do executed the below?
If dataPin = 1 then ' Start bit detected
uInt = ShiftInEx (dataPin, clkPin, 9, &H04, bitTime) ' get in 9 bits
end if
(Am I correct to set bitTime = 145/0.0687?)
Any additional pointer is very much appreciated. Guess I will go ahead and order a few units to play around (as I still have a dozen of BX24 to utilise).
Back to top
dkinzer
Site Admin


Joined: 03 Sep 2005
Posts: 2499
Location: Portland, OR

Posted: 05 February 2006, 21:22 PM    Post subject: Reply with quote

Quote:
or should it be 104uS

You are correct. I don't know where I got 145. Sorry.
Quote:
shouldn't there be enough time to do executed the below?

Perhaps. You'll have to try it and see. The issue will be how accurately (and repeatably) you get ShiftInEx() to sample near the middle of the bit window. If you can get it to sample reliably within the 25% to 75% range you'll probably be OK. The narrower you can get the range, the better.
Quote:
Am I correct to set bitTime = 145/0.0687?

As you noted, the 145 should be 104. The other way of looking at it is since the default Register.TimerSpeed1 setting clocks Timer1 at the CPU clock rate, you want a bitTime parameter that reflects the ratio of the CPU clock to the baud rate, e.g. 14.7457MHz / 9600 = 1536.

You might be able to use a separate task that executes a WaitForInterrupt() that is triggered on the leading edge of the start bit. Once the task gets control, you may need to delay up to one half a bit time before invoking ShiftInEx() to get it centered in the bit window. Getting this code to work will be much easier if you can observe the ShiftInEx clock waveform along with the serial data. If the data is read pre-clock, the read will occur about 5 CPU cycles before the clock edge. If the data is read post-clock, the read will occur about 3 CPU cycles after the clock edge. After observing the relationship between the clock edge and the center of the bit window you can adjust the initial delay get get close to the center. Note that you should use PulseOut() with a zero pin value to generate the delay (as opposed to Delay() or Sleep()) to avoid having a task switch occur.
Back to top
mscheo



Joined: 04 Feb 2006
Posts: 3

Posted: 06 February 2006, 6:36 AM    Post subject: Reply with quote

Don, thanks for your valuable advice and info. Appreciate it.
Have already place order for the ZX24s and will test it out. Cheers!
Back to top
Display posts from previous:   
Post new topic   Reply to topic    Forum Index -> ZBasic Language Time synchro. with the server - Timezone/DST with your computer
Page 1 of 1

 


All content Copyright © 2005-2012 Elba Corp. All Rights Reserved.
Opinions expressed in posts are those of the author and not necessarily those of Elba Corp.
Powered by phpBB © 2001, 2005 phpBB Group