|
本帖最后由 guwu454 于 2013-9-2 14:37 编辑
我要做一个脉冲计数器,思路是用定时器A0作为计数器,使用外部时钟,将脉冲作为外部时钟,TAR连续增模式。用定时器A1作为时间窗口,定时一秒。代码如下:
- #include <msp430.h>
- #include "string.h"
- #include "stdio.h"
- int main(void)
- {
- WDTCTL = WDTPW + WDTHOLD; // Stop WDT
- P1DIR |= 0x40; // P1.0 intput
- P1SEL |=0x01;
- TACTL = TASSEL_0 + MC_2; // SMCLK, contmode
-
- TA1CCTL0 = CCIE; // CCR0 interrupt enabled
- TA1CCR0 = 32768;
- TA1CTL = TASSEL_1 + MC_2;
-
- //usart初始化
- if (CALBC1_1MHZ==0xFF) // If calibration constant erased
- {
- while(1); // do not load, trap CPU!!
- }
- DCOCTL = 0; // Select lowest DCOx and MODx settings
- BCSCTL1 = CALBC1_1MHZ; // Set DCO
- DCOCTL = CALDCO_1MHZ;
- P1SEL = BIT1 + BIT2 ; // P1.1 = RXD, P1.2=TXD
- P1SEL2 = BIT1 + BIT2 ; // P1.1 = RXD, P1.2=TXD
- UCA0CTL1 |= UCSSEL_2; // SMCLK
- UCA0BR0 = 104; // 1MHz 9600
- UCA0BR1 = 0; // 1MHz 9600
- UCA0MCTL = UCBRS0; // Modulation UCBRSx = 1
- UCA0CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
- IE2 |= UCA0RXIE; // Enable USCI_A0 RX interrupt
-
- _BIS_SR(LPM0_bits + GIE); // Enter LPM0 w/ interrupt
- }
- // Timer A0 interrupt service routine
- #pragma vector=TIMER1_A0_VECTOR
- __interrupt void Timer_A (void)
- {
- int data=0;
- char p[5];
- int n=0;
- int i=0;
- TA1CCR0 += 32768; // Add Offset to CCR0
- P1OUT ^= 0x40; // Toggle P1.0
- data=TA0R;
- n=sprintf(p,"%05d",data);
- // n=(u8 )sprintf(buf,"%03d",(int)temp);
- for(i=0;i<n;i++)
- {
- while (!(IFG2&UCA0TXIFG)); // USCI_A0 TX buffer ready?
- UCA0TXBUF = p[i];
- }
- }
复制代码 现在的问题是读不出TA0R的值,总是为0该怎么办啊? |
阿莫论坛20周年了!感谢大家的支持与爱护!!
一只鸟敢站在脆弱的枝条上歇脚,它依仗的不是枝条不会断,而是自己有翅膀,会飞。
|