Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><a href="http://github.com/dwelch67/msp430_samples" rel="nofollow">http://github.com/dwelch67/msp430_samples</a></p> <p>the samples show measuring periods of time using the timer, sample the timer before and after and subtract the difference, that is your execution time.</p> <p>EDIT:</p> <p>this sample uses the timers and divisors, instead of monitoring the roll over flag read the timer counter register and assuming you are counting more than the timer number of ticks, subtract one from the other to get the time. Adjust divisors to avoid the roll over and also try for the accuracy you are after.</p> <pre><code> ;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,&amp;WDTCTL ; 0x5A00|WDTHOLD ; use calibrated clock clr.b &amp;DCOCTL mov.b &amp;CALBC1_1MHZ,&amp;BCSCTL1 mov.b &amp;CALDCO_1MHZ,&amp;DCOCTL ; make p1.0 and p1.6 outputs bis.b #0x41,&amp;P1DIR bic.b #0x41,&amp;P1OUT bis.b #0x40,&amp;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,&amp;BCSCTL2 mov #0x02C4,&amp;TACTL mov #0x3D08,&amp;TACCR0 mov #0x02D0,&amp;TACTL ;mov #0x02D0,&amp;TACTL ; use this instead to blink faster loop: xor.b #0x41,&amp;P1OUT loop0: bit.w #0x0001,&amp;TACCTL0 jz loop0 bic.w #0x0001,&amp;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 </code></pre> <p>This sample uses the timer to measure a time period to transmit serial (rs232) characters, as mentioned above adjust the divisors to insure you dont count more than one cycle of the timer (the timer can roll over, that is fine 0xF000 to 0x3000 for example, not a problem, 0xF000, around once to 0xF100 that is a problem). If possible grossly over divide so that you are definitely not rolling over, the scale back the divisors until you get the best accuracy. </p> <p>Yes you can use an interrupt to deal with the roll over but that messes up the thing you are trying to measure, you dont want to do that (unless the overhead of the interrupt or whatever mechanism you use to monitor timer rollover (you dont need an interrupt for this) is acceptable to your measurement).</p> <pre><code>#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&lt;&lt;1; sa|=1&lt;&lt;9; sb=10; then=TAR; while(sb--) { if(sa&amp;1) P1OUT|=1; else P1OUT&amp;=(~1); sa&gt;&gt;=1; while(1) { now=TAR-then; if(now&gt;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&gt;&gt;rb)&amp;0xF; if(rc&gt;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 &amp;= ~0x02; BCSCTL2&amp;=~0x06; TACTL = 0x0204; TACTL = 0x0220; hexstring(0x1234,1); hexstring(0x5678,1); while(1) { //sa=0; bitin=0; while(1) if((P1IN&amp;2)==0) break; then=TAR; while(1) { if((TAR-then)&gt;=(TACCR0_VALUE&gt;&gt;1)) break; } if(P1IN&amp;2) { bitin&gt;&gt;=1; bitin|=1&lt;&lt;9; } else { bitin&gt;&gt;=1; } then+=(TACCR0_VALUE&gt;&gt;1); for(sb=0;sb&lt;9;sb++) { while(1) { if((TAR-then)&gt;=TACCR0_VALUE) break; } if(P1IN&amp;2) { bitin&gt;&gt;=1; bitin|=1&lt;&lt;9; } else { bitin&gt;&gt;=1; } then+=TACCR0_VALUE; } hexstring(bitin,0); hexstring(bitin&gt;&gt;1,1); } } //------------------------------------------------------------------- //------------------------------------------------------------------- </code></pre> <p>llvm's msp430 backend is truly experimental, read: broken, dont rely on it more than just to play with it, the gcc compiler is not trivial but not excessively painful to build either. The naken430asm assembler is very easy to use and the asm for this processor is quite simple as well, good architecture...</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload