Electronic – MSP430 instruction timing

msp430timing

I have a MSP430G2231. My ultimate goal is to communicate with a device that wants 1 bit of data each microsecond. I believe this is too fast for timers, meaning I need to handwrite assembly code that uses correct amount of cycles for timing.

ti.com says the CPU's frequency is 16MHz. The manual says that "call" takes 5 cycles. Does this mean it takes 5/16000000seconds?

To verify this, I tried to make a simple program that's supposed to toggle the LED every second by counting down from 65536 61 times. This should use ~65536*61*4 (about 16 000 000) cycles. Assuming the previous paragraph is correct, that should take 1second, but it takes 12-13 seconds.

hello:
    mov.w #62, r15
    clr.w r14
hello_next:
    dec.w r14      ; 1 cycle
    subc.w #0, r15 ; 1 cycle
    jnz hello_next ; 2 cycle

    xor.b #LED1PIN, &P1OUT
    jmp hello

Best Answer

The Family Reference Manual has timing info for various instruction starting on page 63. The number of cycles is not fixed for a certain instruction, it appears to be dependent on the mode used. Here is one of the tables from the manual, you can see most instructions take more than 1 cycle:

MSP430 Instruction timing

I haven't used an MSP4340, but I imagine there should be a tool available in the IDE to time your code and tell you how many cycles it takes to run. I'd try and find/use this first - if you can't, use the info in the manual and calculate manually.

If you tell us more about the device that needs the data (part number, format, etc) then we may be able to suggest alternatives (e.g SPI peripheral?). It sounds like a tall order to achieve this speed using this method, and not particularly efficient if a peripheral/PLD can do it for you.
With the price/capability of uCs nowadays, needing to resort to stuff like this is usually a sign for me that I need to consider moving to a more capable uC (unless the quantities are such that it makes enough of a difference)