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.

Number of clock cycles in this program and the frequency of the blinking

Other Parts Discussed in Thread: MSP430G2553

I am studying Electrical Engineering and I just bought the Launchpad MSP-EX430G2. My example program blinks the red led in the PCB. I am asked to calculate the number of clock cycles that the delay in the below program corresponds to and finding the frequency of the blinking.

#include "msp430g2553.h"

 

;-------------------------------------------------------------------------------

;;; ---- gcc doesn't know about PC,SP,SR,CG1,CG2 ----

 

#define PC r0                   

#define SP r1

#define SR r2

#define CG1 r2

#define CG2 r3

;-------------------------------------------------------------------------------

        .text

;-------------------------------------------------------------------------------

RESET_ISR:

        ;; disable watchdog and set stack to highest RAM addr

        mov.w   #WDTPW+WDTHOLD,&WDTCTL

        mov.w   #__stack,SP                     ; gcc ldscripts compute __stack based on mmcu

 

        ;; initialize clock,gpio,timer

       BIS.B #0x01,&P1DIR ; Set bit0 in port1 as output

       BIC.B #0x01,&P1OUT ; clear bit0

start: MOV.W #10000, R15  ; Load 50000 into R15

loop1: DEC.W R15          ; Decrement value stored in R15

       JNZ loop1          ; Jump to loop1 if value in R15 is non zero

       BIS.B #0x01,&P1OUT ; set bit0

       MOV.W #10000, R15  ; Load 50000 into R15

loop2: DEC.W R15          ; Decrement value stored in R15

       JNZ loop2          ; Jump to loop2 if value in R15 is non zero

       BIC.B #0x01,&P1OUT ; Clear bit0

       JMP start          ; Jump to start

       ;------------------------------------------------------------------------------

; UNEXPECTED_ISR - default handler for unhandled interrupt

;-------------------------------------------------------------------------------

UNEXPECTED_ISR:

        reti                    ; cycles: 5

       ;------------------------------------------------------------------------------

; Interrupt Vectors - see the datasheet for your chip

;                    *msp430g2553 vectors described below

;------------------------------------------------------------------------------

        .section ".vectors", "ax", @progbits

        .word UNEXPECTED_ISR    ;0xffe0 slot  0  0

        .word UNEXPECTED_ISR    ;0xffe2 slot  1  2

        .word UNEXPECTED_ISR    ;0xffe4 slot  2  4 (PORT1_VECTOR)

        .word UNEXPECTED_ISR    ;0xffe6 slot  3  6 (PORT2_VECTOR)

        .word UNEXPECTED_ISR    ;0xffe8 slot  4  8 

        .word UNEXPECTED_ISR    ;0xffea slot  5  A (ADC10_VECTOR)

        .word UNEXPECTED_ISR    ;0xffec slot  6  C (USCIAB0TX_VECTOR) 

        .word UNEXPECTED_ISR    ;0xffee slot  7  E (USCIAB0RX_VECTOR)

        .word UNEXPECTED_ISR    ;0xfff0 slot  8 10 (TIMER0_A1_VECTOR)

        .word UNEXPECTED_ISR     ;0xfff2 slot  9 12 (TIMER0_A0_VECTOR)

        .word UNEXPECTED_ISR    ;0xfff4 slot 10 14 (WDT_VECTOR)

        .word UNEXPECTED_ISR    ;0xfff6 slot 11 16 (COMPARATORA_VECTOR)

        .word UNEXPECTED_ISR    ;0xfff8 slot 12 18 (TIMER1_A1_VECTOR)

        .word UNEXPECTED_ISR    ;0xfffa slot 13 1a (TIMER1_A0_VECTOR)

        .word UNEXPECTED_ISR    ;0xfffc slot 14 1c (NMI_VECTOR)

        .word RESET_ISR         ;0xfffe slot 15 1e (RESET_VECTOR)

        .end

 

  • There is a hard way to find the answer.

    Read the MSP430G2553 data-sheet and the F2xx User's Guide about the MSP430 CPU. It is hard work. But not that hard.

    And there is a easy way -- you are using that.

    There was an old fashioned story about Phys-101 students. They were given a barometer and sent to measure the height of the Empire Building. Some of the students came back with the right answer but without the barometer. They gave the barometer away in exchange of the answer.

    I think that story needs to be updated. Every student came back with the right answer and the barometer. They got the answer from Internet for free.

  • I checked the MSP430G2553 data sheet. It says that the RETI instruction has 5 cycles. I used "MOV.W #10000, R15  ;" which loads 10000*5=50000 into R15. How do I find the frequency of the blinking. Is it 1/50000=2E-5 Hz ? Please help me out here.

  • Yes, RETI takes 5 cycles to execute. But you did not use RETI in your code at all.

    MOV.W #10000,R15 takes 2 cycles to execute. Not 10000*5 cycles and has nothing to do with RETI.

    DEC.W R15 takes 1 cycle. JNZ somewhere takes 2 cycles. etc. etc.

    You need to add all the number of cycles of each instruction between the "blink"

    You also need to find out the frequency of the clock. Divide that by the total number of cycles to get the frequency of the "blink".

  • start: MOV.W #10000, R15  ; Load 50000 into R15                        2 CYCLES

    loop1: DEC.W R15          ; Decrement value stored in R15              1 CYCLE

           JNZ loop1          ; Jump to loop1 if value in R15 is non zero  2 CYCLES

           BIS.B #0x01,&P1OUT ; set bit0                                   5 CYCLES

           MOV.W #10000, R15  ; Load 50000 into R15                        2 CYCLES

    loop2: DEC.W R15          ; Decrement value stored in R15              1 CYCLE

           JNZ loop2                 ; Jump to loop2 if value in R15 is non zero  2 CYCLES

           BIC.B #0x01,&P1OUT ; Clear bit0                                 5 CYCLES

           JMP start          ; Jump to start                              2 CYCLES

     


    So it take 1+2+5+2= 10 cycles between the blink?

    And how do I measure the clock frequency using the cycles?

  • So close yet so far away ;-)

    When I said between the "blink", I did not mean on paper or the list. I mean what the CPU executes.

    start: MOV.W #10000, R15  ; Load 50000 into R15                        2 CYCLES

    loop1: DEC.W R15          ; Decrement value stored in R15              1 CYCLE

           JNZ loop1          ; Jump to loop1 if value in R15 is non zero  2 CYCLES

    The CPU will go back to loop1 again and again until R15 becomes Z. This means those 1+2=3 cycles will be repeated 10000 times.

           BIS.B #0x01,&P1OUT ; set bit0                                   5 CYCLES

           MOV.W #10000, R15  ; Load 50000 into R15                        2 CYCLES

    loop2: DEC.W R15          ; Decrement value stored in R15              1 CYCLE

           JNZ loop2                 ; Jump to loop2 if value in R15 is non zero  2 CYCLES

    The CPU will go back to loop2 again and again until R15 becomes Z. This means those 1+2=3 cycles will be repeated 10000 times.

           BIC.B #0x01,&P1OUT ; Clear bit0                                 5 CYCLES

           JMP start          ; Jump to start                              2 CYCLES

    So it takes 2+10000*(1+2)+5+2+10000*(1+2)+5+2=60016 cycles

    The data sheet will tell you that because you did not change the BCS registers, the clock for the CPU is approximately at 1.1 MHz

  • I have used RETI in the UNEXPECTED_ISR. Does it have any impact? The example code contains

    start: MOV.W #10000, R15  ; Load 50000 into R15 

  • It is prudent to expect the unexpected. If the unexpected happens, then the CPU will execute those code. Otherwise, there is no impact.

**Attention** This is a public forum