簡體   English   中英

如何使用MSP430獲得執行時間?

[英]How to get execution time using msp430?

我想知道C代碼的執行時間(以毫秒為單位),我使用msp430f16

任何幫助將不勝感激。

謝謝。

http://github.com/dwelch67/msp430_samples

樣本顯示使用計時器的測量時間段,在之前和之后對計時器進行采樣,然后減去差值,即執行時間。

編輯:

此示例使用計時器和除數,而不是監視翻轉標志,而是讀取計時器計數器寄存器,並假設您計數的數量超過計時器的滴答數,請從另一個計數中減去一個來獲得時間。 調整除數以避免翻滾,並嘗試獲得想要的精度。

    ;This version is written for naken430asm.
    ;http://www.mikekohn.net/micro/naken430asm_msp430_assembler.php
    ;naken430asm -o filename.hex filename.s
    ;mspdebug takes hex files as well as elfs.

WDTCTL equ 0x0120


CALBC1_1MHZ equ 0x10FF
CALDCO_1MHZ equ 0x10FE

DCOCTL  equ 0x56
BCSCTL1 equ 0x57
BCSCTL2 equ 0x58

TACTL   equ 0x0160
TAR     equ 0x0170
TACCR0  equ 0x0172
TACCTL0 equ 0x0162

P1OUT   equ 0x0021
P1DIR   equ 0x0022


    org 0xFC00

reset:
    mov #0x0280,r1

    mov #0x5A80,&WDTCTL ; 0x5A00|WDTHOLD

    ; use calibrated clock
    clr.b &DCOCTL
    mov.b &CALBC1_1MHZ,&BCSCTL1
    mov.b &CALDCO_1MHZ,&DCOCTL

    ; make p1.0 and p1.6 outputs
    bis.b #0x41,&P1DIR
    bic.b #0x41,&P1OUT
    bis.b #0x40,&P1OUT

    ; 1MHz is 1000000 clocks per second
    ; 1000000 = 0xF4240
    ; The timers are 16 bit
    ; Using a divide by 8 in BCSCTL2 gives
    ; 125000 (0x1E848) clocks in a second
    ; Using a divide by 8 in the timer gives
    ; 15625 (0x3D09) timer ticks per second.

    ; If both divisors are by 8, and we set
    ; TACCR0 to 0x3D08 and set for count up mode
    ; then, theory, we can measure seconds.

    bis.b #0x06,&BCSCTL2
    mov #0x02C4,&TACTL
    mov #0x3D08,&TACCR0
    mov #0x02D0,&TACTL
    ;mov #0x02D0,&TACTL ; use this instead to blink faster

loop:
    xor.b #0x41,&P1OUT
loop0:
    bit.w #0x0001,&TACCTL0
    jz loop0
    bic.w #0x0001,&TACCTL0

    jmp loop


hang:
    jmp hang

    org 0xFFE0
    dw hang
    dw hang
    dw hang
    dw hang
    dw hang
    dw hang
    dw hang
    dw hang
    dw hang
    dw hang
    dw hang
    dw hang
    dw hang
    dw hang
    dw hang
    dw reset

此示例使用計時器來測量傳輸串行(rs232)字符的時間段,如上所述,調整除數以確保您不計數計時器的多個周期(計時器可以翻轉,對於0xF000可以為0xF000到0x3000例如,不是問題,0xF000,一次到0xF100就是一個問題)。 如果可能的話,可能會出現嚴重的過度除法,以致您絕對不會翻轉,請縮小除數,直到獲得最佳精度為止。

是的,您可以使用中斷來處理翻轉,但是這樣會使您要測量的東西弄亂了,您不想這樣做(除非中斷的開銷或用於監視計時器翻轉的任何機制(您不需要您的測量可接受)。

#define WDTCTL     (*((volatile unsigned short *)0x0120))

#define CALBC1_1MHZ (*((volatile unsigned char *)0x10FF))
#define CALDCO_1MHZ (*((volatile unsigned char *)0x10FE))
#define CALBC1_8MHZ (*((volatile unsigned char *)0x10FD))
#define CALDCO_8MHZ (*((volatile unsigned char *)0x10FC))
#define CALBC1_12MHZ (*((volatile unsigned char *)0x10FB))
#define CALDCO_12MHZ (*((volatile unsigned char *)0x10FA))
#define CALBC1_16MHZ (*((volatile unsigned char *)0x10F9))
#define CALDCO_16MHZ (*((volatile unsigned char *)0x10F8))

#define DCOCTL  (*((volatile unsigned char *)0x56))
#define BCSCTL1 (*((volatile unsigned char *)0x57))
#define BCSCTL2 (*((volatile unsigned char *)0x58))

#define TACTL   (*((volatile unsigned short *)0x0160))
#define TAR     (*((volatile unsigned short *)0x0170))
#define TACCR0  (*((volatile unsigned short *)0x0172))
#define TACCTL0 (*((volatile unsigned short *)0x0162))


#define P1IN  (*((volatile unsigned char *)0x0020))
#define P1OUT (*((volatile unsigned char *)0x0021))
#define P1DIR (*((volatile unsigned char *)0x0022))

// 16MHz clock
// The timer is 16 bit
// set to divide by 1
// 16,000,000 / 155200 = 138.88889
#define TACCR0_VALUE 138

//-------------------------------------------------------------------
void uart_putc ( unsigned short c )
{
    unsigned short sa;
    unsigned short sb;
    unsigned short then,now;

    sa=c<<1;
    sa|=1<<9;
    sb=10;
    then=TAR;
    while(sb--)
    {
        if(sa&1) P1OUT|=1; else P1OUT&=(~1);
        sa>>=1;
        while(1)
        {
            now=TAR-then;
            if(now>TACCR0_VALUE) break;
        }
        then+=TACCR0_VALUE;
    }
}
//-------------------------------------------------------------------
void hexstring ( unsigned short d, unsigned short cr )
{
    //unsigned short ra;
    unsigned short rb;
    unsigned short rc;

    rb=16;
    while(1)
    {
        rb-=4;
        rc=(d>>rb)&0xF;
        if(rc>9) rc+=0x37; else rc+=0x30;
        uart_putc(rc);
        if(rb==0) break;
    }
    if(cr)
    {
        uart_putc(0x0D);
        uart_putc(0x0A);
    }
    else
    {
        uart_putc(0x20);
    }
}
//-------------------------------------------------------------------
void notmain ( void )
{
    unsigned short /*sa,*/sb;
    //unsigned short start;
    unsigned short then; //,now;
    unsigned short bitin;
    //unsigned short log[32];

    WDTCTL = 0x5A80;

    // use calibrated clock
    DCOCTL = 0x00;
    BCSCTL1 = CALBC1_16MHZ;
    DCOCTL = CALDCO_16MHZ;

    // make p1.0 an output
    P1DIR |= 0x01;
    P1OUT |= 0x01;

    P1DIR &= ~0x02;


    BCSCTL2&=~0x06;
    TACTL = 0x0204;
    TACTL = 0x0220;

    hexstring(0x1234,1);
    hexstring(0x5678,1);

    while(1)
    {
        //sa=0;
        bitin=0;
        while(1) if((P1IN&2)==0) break;
        then=TAR;
        while(1)
        {
            if((TAR-then)>=(TACCR0_VALUE>>1)) break;
        }
        if(P1IN&2)
        {
            bitin>>=1;
            bitin|=1<<9;
        }
        else
        {
            bitin>>=1;
        }
        then+=(TACCR0_VALUE>>1);
            for(sb=0;sb<9;sb++)
        {
            while(1)
            {
                if((TAR-then)>=TACCR0_VALUE) break;
            }
            if(P1IN&2)
            {
                bitin>>=1;
                bitin|=1<<9;
            }
            else
            {
                bitin>>=1;
            }
            then+=TACCR0_VALUE;
        }
        hexstring(bitin,0);  hexstring(bitin>>1,1);
    }
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------

llvm的msp430后端確實是實驗性的,讀起來:壞了,不要僅僅依靠它來玩,gcc編譯器也不是瑣碎的事,但也不難構建。 naken430asm匯編器非常易於使用,該處理器的asm也非常簡單,良好的體系結構...

沒有通用的方法可以執行此操作,您可以使用可用的硬件計時器資源並將其配置為提供適當的時基。 我建議對於定時代碼執行,毫秒級的計時器可能會有所幫助。 微秒可能更合適。

沒有開銷或額外的代碼(甚至硬件),並且可能具有更高的准確性的更簡單的方法是在模擬器中執行和分析代碼。 我相信Code Composer Studio包含性能分析和模擬工具。 其他工具鏈也很希望包含它們。 如果要測試的代碼具有硬件時序/延遲依賴性,則此方法可能不合適。

另一種簡單的方法是在執行前后切換可用的GPIO,並使用示波器或外部定時器/計數器監視引腳。 這種方法將包括硬件延遲/抖動,以及與在執行被測代碼期間可能發生的中斷相關的所有開銷。 當沒有可用的硬件計時器資源時,也可以實現它。

一些MSP430器件具有板載周期計數器,使用調試器時可以使用該計數器。 我發現比較代碼序列時這非常准確。

我不知道您的設備是否有該設備。 實際上,我還沒有找到一個命名為MSP430f16的文件,它們通常在“ f”之后有三到四位數字。

如果您在不更改現有軟件的情況下快速查找內容,但不是非常准確。 您可以在要分析的代碼之前和之后使用日志記錄斷點。

如果您使用的是IAR,則此選項會被隱藏。 您必須右鍵單擊要添加斷點的行,然后選擇日志記錄斷點。

當然,觸發日志會有一些延遲,這個延遲應該是恆定的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM