Shop OBEX P1 Docs P2 Docs Learn Events
Determining Pulse Width — Parallax Forums

Determining Pulse Width

LightfootLightfoot Posts: 228
edited 2007-07-01 08:40 in General Discussion
How do you make an variable equal to 1 if the width of an incoming pulse on RA.0 is 58us and equal to 0 if the pulse is 100us?

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔

Comments

  • LightfootLightfoot Posts: 228
    edited 2007-06-24 00:40
    This is for a digital command control decoder I am making for my HO trains. The DCC system uses this type of serial communication. A microcontroller on the receiving end checks to see if the pulse is 58us, which is a serial 1. 100us is a zero.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
  • JonnyMacJonnyMac Posts: 8,940
    edited 2007-06-24 02:05
    You could use PULSIN to measure the pulse and a quick IF-THEN to set the value. That would not be very elegant, however, and if you can point to a description of the protocol I'm sure that something cleaner could be worked out.
  • LightfootLightfoot Posts: 228
    edited 2007-06-24 08:54
    When I pulsout like this my scope gives me negative pulses. Is it supposed to do that? It is high for about 300us and then about a 60us low pulse.

    do

    pulsout RA.0, 6

    loop

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
  • BeanBean Posts: 8,129
    edited 2007-06-24 11:58
    Please post your entire code.

    PULSOUT inverts the state of the pin. If the pin start out high, then you WILL get a negative pulse.

    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    “The United States is a nation of laws -· poorly written and randomly enforced.” - Frank Zappa

    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    www.hittconsulting.com
    ·
  • LightfootLightfoot Posts: 228
    edited 2007-06-26 04:40
    How do you program an SX to transmit the bitstream in the attached picture (taken from this www.nmra.org/standards/DCC/standards_rps/S-92-2004-07.pdf pdf)? I connected the RA.0 pin to an h bridge and used this code to experiment:

    DO

    ra.0 = ~ra.0
    pauseus 58 'Pause duration

    LOOP

    It gives me the bipolar AC necessary but it cannot use transmit any data. Also the bipolar AC starts with a positive pulse instead of the negative one. The digital command decoder interprets a logic level high as two pulses. The first pulse is a 58us -14 volt pause. The second is a +14 volt pulse at the same duration. A logic level low is read as a 100us -14 volt pulse and then a +14 volt pulse.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔


    Post Edited (Lightfoot) : 6/26/2007 4:48:40 AM GMT
    897 x 262 - 48K
  • JonnyMacJonnyMac Posts: 8,940
    edited 2007-06-26 06:26
    The subroutines in the program that's attached might get you started. Please note that I'm just a programmer, not a model railroader so I'm just going on the information that you provided; you'll probably have to tweak timing and may need to change bit polarity in the TX_DCC_BIT routine to accommodate your H-Bridge.

    Post Edited (JonnyMac) : 6/26/2007 2:01:04 PM GMT
  • LightfootLightfoot Posts: 228
    edited 2007-06-26 07:24
    Thanks for the sample but it does not work. I do not know if I am doing something wrong or if the program is a dud. Here are the electrical specs I pulled which maybe helpful
    www.nmra.org/standards/DCC/standards_rps/S-91-2004-07.pdf.

    I tried running the preamble routine on my scope and no square waves. Nothing is coming off of RA.0, just 0 volts. Same happens when I transmit a bit.

    code I ran:

    Main:

    PREAMBLE

    goto Main

    Thanks again

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔


    Post Edited (Lightfoot) : 6/26/2007 7:29:02 AM GMT
  • JonnyMacJonnyMac Posts: 8,940
    edited 2007-06-26 13:49
    I wrote that demo at the end of a very loooonnng day -- and forgot to add the word OUTPUT to the pin definition for TX. Fix it like this:

    TX              PIN     RA.0 OUTPUT
    



    Note that I can't guarantee the program will work; in fact, you should count on having to fix it as it's just intended to show you a method of transmitting bits in a byte and based on the information you provided. Since there's bit of subtlety in using the TX_DCC_BYTE sub I've fixed and re-uploaded the demo (see above for new code).

    Post Edited (JonnyMac) : 6/26/2007 2:01:59 PM GMT
  • LightfootLightfoot Posts: 228
    edited 2007-06-27 00:06
    I made a few alterations to your program and made it work. Now how do you program the chip to read the type of signal we just created. The PULSIN Statement may work but this seems to make it more sensitive to noise.

    Thanks

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔


    Post Edited (Lightfoot) : 6/27/2007 12:48:13 AM GMT
  • JonnyMacJonnyMac Posts: 8,940
    edited 2007-06-27 00:49
    Just reverse the process -- use PULSIN to measure the widths of the incoming pulses; give it a shot on your own, it will probably be an interest opportunity to learn a bit about SX programming with SX/B.
  • LightfootLightfoot Posts: 228
    edited 2007-06-29 22:14
    After much experimenting I found this is the most reliable way to transmit serial data through an HO scale railroad track.

    Transmitter Code: ******************************

    tris_a = 0
    _data var byte

    DO

    SEROUT RA.0, T2400, _data

    pauseus 100

    LOOP

    Receiver Code: ******************************

    tris_b = 0
    plp_a = 0

    DO

    SERIN RA.0, T2400, rb

    LOOP

    The only problem with the serout command is that it does not generate bipolar AC from my H Bridge. Only inverting a pin does that (the only way I know of). So the transmitter would need to transmit serial data at 2400 baud doing (ra.0 = ~ra.0) bitwise nots. I do not know how to calculate the pauses needed to send a binary 1 or 0 or have an accurate idea how to code a bitwise not serialout program. Also if there is a way (even using serout) besides bitwise not that can generate bipolar AC please say so.

    Thank you

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
  • LightfootLightfoot Posts: 228
    edited 2007-06-30 06:50
    Here is something that should work, I still do not know what values I should pause for 2400 baud. If it is True 2400 do I need a start/stop bit?

    Transmitter:

    
    ctr var byte
    data var byte
    ra = 0
    
    DO
    
    _data = 78
    
    <Any Start Bits?>
    
    FOR ctr = 1 to 8                                
    
    IF _data.7 = 1 THEN
           IF RA.0 = 0 THEN 'RA.0 may already be one from a previous one transmission.
              ra.0 = ~ra.0
           ENDIF      
    ELSE
           IF RA.0 = 1 THEN 'RA.0 may already be zero from a previous zero transmission.
              ra.0 = ~ra.0
           ENDIF
    ENDIF
    
    pauseus <how long?>
    
    _data = _data << 1
    
    NEXT
    
    LOOP
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
  • LightfootLightfoot Posts: 228
    edited 2007-07-01 08:40
    Thank You, JonnyMac, for your time. I finally found a way to make my receiver work. Here is the code for all:

    
    tris_a = 0
    tris_b = 0
    
    DO
    
    serin RA.0, T2400, rb
    
    if rb = 0 then 'If the locomotive is reverse oriented (see my picture for explanation) on the track, the polarity of the serial data is reversed.                                                             'SERIN will receive a value zero since it can not be read as true serial data.
    
    serin RA.0, N2400, rb 'Read the inverted data if the locomotive is reverse oriented on track.
    
    endif
    
    LOOP
    
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    926 x 688 - 50K
Sign In or Register to comment.