This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Right shift operator for transmission to PC

Hello,

Can some one please help me understand the highlighted line of code in the timer A interrupt routine. Does the shift mean that the right most bit is tranmitted to the PC. If so how does that happen( is put out on the p1.1 pin?)

__interrupt void Timer_A (void)
{
  CCR0 += Bitime;     // Add Offset to CCR0 
  if ( BitCnt == 0)     // If all bits TXed, disable interrupt
    CCTL0 &= ~ CCIE ;
  else
  {
    CCTL0 |=  OUTMOD2;                    // TX Space
    if (TXByte & 0x01)
      CCTL0 &= ~ OUTMOD2;                   // TX Mark
    TXByte = TXByte >> 1;
    BitCnt --;
  }
}

 

Thanks in advance

  • Hi Venu,

    That shift operation puts the right-most bit (the least-significant bit) into the garbage.  The code just above the shift sets CCTL0 to control the output pin according the right-most bit.  However, it appears not to be using the correct OUTMODx.

    Jeff

  • Hi,

    I would like to add one note to the shift operation (maybe it will be useful).
    The most significant bit (here most left)  after shift operation depends on variable type:
    a) if it is unsigned, then zero is inserted,
    b) if it is signed then the bit is copied from it previous state

    Regards,
    Piotr Romaniuk, Ph.D.
    ELESOFTROM

  • Jeff Tenney said:
    However, it appears not to be using the correct OUTMODx.

    Depends on the initialization.

    Actually it adds/subtracts 4 from the current Output mode. So RESET becomes SET, Reset/set becomes Set/reset. etc.

    Note that it isn't OURMOD_2 (the enumerated  mode) but rather OUTMOD2 (the bit 2 of the outmode bitfield)

  • Nice catch JMG.  Makes sense now.  Unfortunately it's another example of using "|=" and "&=" when a simple "=" would suffice, would be more self documenting, and would even be 1 cycle faster on CPUX.

    Jeff

  • Thanks a lot for taking the time to reply.  Here is my understanding now

    Everytime the routine is called ( i.e when the counter is down to zero from "Bittime" value ), The service inspects the right most bit of the TXByte and does the following

    If it is a "0", The outmod bits are set to 101 which inturn drives the output line  to 0 (  OUT pin of CCTL0 )

    If it is a "1", The outmod bits are set to 001 which inturn drives the output line  to 1 (  OUT pin of CCTL0 )

    Instead of controlling OUTPUT by using OUTMODx pins, would it work if the value of OUTx is set to the value of the right most bit of TXByte directly ( since it is an rw port) i.e

    CCTL0 &=  ~BIT1;                    // TX Space
        if (TXByte & 0x01)
          CCTL0 |=  BIT1;                   // TX Mark
        TXByte = TXByte >> 1;
        BitCnt --;

    And not mess with OUTMODx at all. Is there a reason to control the output pin with OUTMODx pins?

    Also, How is this pin ( OUT of CCTL0 connected to the serial port of the PC? )

    Thanks

    venu

  • venu surampudi said:

    Is there a reason to control the output pin with OUTMODx pins?

    Keep in mind that the OUTMODx bits merely represent a configuration setting in the timer channel; they are not "pins".  The actual output signal from each timer channel is called "OUTn", as in "OUT0" or "OUT1".  If you want that OUTn signal to go to an MCU pin, then you must set the appropriate MCU pin for timer control (PxSEL register) and set its direction to output (PxDIR register).

    Setting OUTMODx to "SET" does not change the OUT signal right away.  It instructs the timer channel to wait until the counter register reaches the CCRn value and then set the OUT signal.

    In this code, using OUTMODx "SET" and "RESET" allows the timer to change the OUT signal instead of your software.  The benefit is timing.  Your software cannot reasonably generate these edges with the precise timing normally required for serial I/O.  The timer can generate them at exact intervals.

    With that in mind you can see that the ISR runs after a bit has just started.  The ISR sets up the next output bit and tells the timer when to make the transition (if any).

    venu surampudi said:

    How is this pin ( OUT of CCTL0 connected to the serial port of the PC? )

    The OUTn signal is allowed to drive the pin if PxSEL and PxDIR are set correctly.  The signal from the pin is carried over traces, connectors, cables, and level translators and finally to the PC UART RX pin.

    Jeff

  • Jeff,

    Excellent elaboration. Cant thank you enough. You are my nominee for authoring launchpad 2 user guide :)

    Venu

  • venu surampudi said:
    Excellent elaboration. Cant thank you enough. You are my nominee for authoring launchpad 2 user guide :)

    Jeff is on its way to become the next Guru, if he keeps up his good work.

    Your problem, however, isn't LaunchPad related. All MSPs and their timers work this way.

    However, the documentation of the sample codes (almost all of them, not only hte LaunchPad) and the used 'tricks' to make things work in them, is really a field thah higly needs improvement.

**Attention** This is a public forum