ARMVSAVR 发表于 2011-11-16 14:43:37

代码这样写有什么区别,求指点

#include<msp430x14x.h>

void main(void)
{
WDTCTL = WDTPW + WDTHOLD;               // Stop WDT
P2DIR |= 0x01;                            // P1.0 output
TBCCTL0 = CCIE;                           // CCR0 interrupt enabled
TBCCR0 = 30000 ;
TBCTL = TBSSEL_2 + MC_2;                  // SMCLK, contmode

_BIS_SR(LPM0_bits + GIE);               // Enter LPM0 w/ interrupt
}

// Timer B0 interrupt service routine
#pragma vector=TIMERB0_VECTOR
__interrupt void Timer_B (void)
{
   P2OUT ^= 0x01;                            // Toggle P1.0
//问题在此处
   TBCCR0 += 30000 ;                        // Add Offset to CCR0
}
为什么不写成: TBCCR0 = 30000 ;
我2种写法都试了,发现代码运行没有神没区别!

ele_eye 发表于 2011-11-16 14:55:23

TBCCR0 += 30000 ;等于 TBCCR0 = TBCCR0 +30000 ;

.titrwh 发表于 2011-11-16 15:27:16

因为进中断时TBCCR0=0,所以你的两种写法效果一样。

brahen 发表于 2011-11-16 15:40:22

这明显差别很大嘛,1楼正解。

JamesErik 发表于 2011-11-16 15:44:27

TBCCR0是个寄存器吧?一楼正解

zchong 发表于 2011-11-16 15:44:34

你不能只看结果,过程也很重要

zjj841011 发表于 2011-11-16 16:01:29

没有用过这款芯片,不过这两句话不一样,因为TBCCR0 = 0引起终端,但是执行到这一句的时候TBCCR0 应该不一定等于0吧。

TBCCR0 += 30000 应该是为了消除定时的误差。

猜想的,欢迎拍砖。

.titrwh 发表于 2011-11-16 16:06:10

回复【6楼】zjj841011
-----------------------------------------------------------------------

定时中断,进中断肯定是计数器溢出了。

gelite 发表于 2011-11-16 16:34:16

回复【6楼】zjj841011
-----------------------------------------------------------------------

正解,我都是这么写,两种写法运行时间长了就看出差别来了。

20061002838 发表于 2011-11-16 16:58:10

P2OUT ^= 0x01;                            // Toggle P1.0
//问题在此处
   TBCCR0 += 30000 ;                        // Add Offset to CCR0

两句话中加加点程序,弄长一点,实在不行你弄个delay进去就能看见二者的区别了

sunshulin 发表于 2011-11-16 18:19:13

回复【楼主位】ARMVSAVR
#include&lt;msp430x14x.h&gt;
void main(void)
{
wdtctl = wdtpw + wdthold;               // stop wdt
p2dir |= 0x01;                            // p1.0 output
tbcctl0 = ccie;                           // ccr0 interrupt enabled
tbccr0 = 30000 ;
tbctl = tbssel_2 + mc_2;                  // smclk, contmode
_bis_sr(lpm0_bits + gie);               // enter lpm0 w/ interrupt
}
/......
-----------------------------------------------------------------------

看看这个
点击此处下载 ourdev_695823V0AEEB.pdf(文件大小:898K) (原文件名:C语言深度解剖.pdf)

toofen 发表于 2011-11-16 18:36:39

回复【楼主位】ARMVSAVR
-----------------------------------------------------------------------

应该是为了保证每次中断之间的间隔时间相等

jing43 发表于 2011-11-16 19:22:21

顶10楼,一边看书去。。。

millwood0 发表于 2011-11-16 19:49:57

it is because of interrupt latency: when the the tmr overflows, it triggers the isr. when the execution goes to loading the tmr with the offset, the tmr has advanced a few ticks because of the latency. by adding a value to it, you have retained the ticks and making the time between each offset the same as suggested by the offset.

ARMVSAVR 发表于 2011-11-17 08:57:42

谢谢大家热心的指点!初学MSP430觉得TI的文档与例程比较好!

zzz1367 发表于 2011-11-17 11:42:39

回复【6楼】zjj841011
没有用过这款芯片,不过这两句话不一样,因为tbccr0 = 0引起终端,但是执行到这一句的时候tbccr0 应该不一定等于0吧。
tbccr0 += 30000 应该是为了消除定时的误差。
猜想的,欢迎拍砖。
-----------------------------------------------------------------------

估计是的。

duandiyinfen 发表于 2011-11-27 00:51:45

你要是有430的开发板或者是最小系统板的话,可以用仿真器进行编译,用IAR中单步运行观察寄存器中值的变化,我当初有什么搞不懂的时候就是这么慢慢看的就是一步一步看寄存器怎么变,基本理一遍之后就会懂一点了!
页: [1]
查看完整版本: 代码这样写有什么区别,求指点