Announcement

Collapse
No announcement yet.

Calculating Serial Out timing

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Calculating Serial Out timing

    How does one calculate the time it takes for a serial out command, with a 4 Mhz internal oscillator at 9600 baud with 8 bytes of data?

  • #2
    ((1/9600) * 10) * 8 ;8 Bytes take 10 bits when you consider Start & Stop bits. 1 Second divided by Baudrate (9600) gives you bits per second, which takes 0.000 104 167 seconds per bit. Times 10 = 0.001 041 667 seconds per byte. Times 8 bytes = 8.333 milliseconds. You might want to add ((Fosc/4) * 3) for reloading the TXREG; 0.000 003 seconds between bytes. Tally = 0.008 333 + (0.000 003 * 7) = 8.354 milliseconds total (or pretty close).

    To rehash:
    - 1 / Baud = bits per second, regardless of Fosc
    - 1 byte of data = 10 bits sent (Start bit + Byte + Stop bit)
    - It takes approximately 3 cycles to reload the TXREG; at 4 MHz Osc, Fosc/4 = 1 MHz; (1/1 MHz) * 3 = 0.000 003 seconds
    - There are only 7 reloads to send 8 bytes
    We can crack this cotton PIC'n thang!

    Comment


    • #3
      Thanks Mike. Excellent tutorial.

      Comment


      • #4
        Is there a list somewhere of other command times?

        Comment


        • #5
          Execution times vary too much to create a meaningful list. Variable type and variable placement in RAM are the big factors, but there are more. The math operators alone require over six thousand different macros, each with a different execution time. That doesn't take into account the varying time for bank-switching that's determined by variable placement.
          Charles Leo
          ME Labs, Inc.
          http://melabs.com

          Comment


          • #6
            Can I assume that a 'serial in' calculates the same as serout above?
            I am involving a servo motor in this code. Within the servo portion which is exacting within a 20ms frame, I need to insert a 'serial in' (9600 baud) and an: 'if b1=0 then switch'
            The portion of code is as follows:

            for p0=210 to 125 step-1
            pulsout porta.2,p0
            pause 20-p0/100
            pulsout porta.2.p0 'this repeat is to get the timing I want the servo to take
            pause 20-p0/100
            'here I need serial in time it takes
            'here I need the 'if' statement time it takes
            next p0

            If indeed the serial in timing is the same as serout, what I need to know is the timing for the 'if' statement
            mpgmike, if you know this and it's no problem for you, would you mind? Would be appreciated and this topic is related to the other topic I posted today.
            Last edited by Keith55555; 09-06-2018, 03:31 PM.

            Comment


            • #7
              kEITH55555, Attached is a program I wrote some time ago using interrupts to operate a analog type servo in the back ground. HTH.
              Attached Files
              Dave Purola,
              N8NTA
              EN82fn

              Comment


              • #8
                1/9600 = 0.000 104 166 667 seconds per bit transmitted/received. 1 Start + 8 data + 1 stop = 10 bits; 0.001 041 667 seconds for a single byte sent/received.
                We can crack this cotton PIC'n thang!

                Comment


                • #9
                  Thanks Mike. And the 'if /then' statement? Sorry to be a pain and counting on you. If I knew how to calculate these I certainly wouldn't bother you.I was about to do this empirically and lose my marbles.

                  Comment

                  Working...
                  X