找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 5377|回复: 0
收起左侧

用MSP430F5438A检测频率有问题

[复制链接]
ID:66032 发表于 2014-10-18 15:47 | 显示全部楼层 |阅读模式
我看了官方的USER's Guide几遍, 查了资料都没办法弄出来, 问题是数码管显示“255” 不变, 请哪位高手帮我指出错误, 非常谢谢!
//*********************Capture the frequency test **************************
//  ACLK = TACLK = 32kHz, MCLK = SMCLK = default DCO ~1.045MHz
//
//  As coded and with TACLK = 1.045MHz, toggle rates are:
//  P2.1= CCR0 = 1045000/260 = 4.19kHz
//  P2.2= CCR1 =65   50% dutycycle;
//  P2.3= CCR2 =26   80% dutycycle;
//
//               MSP430F5438A
//            -------------------
//        /|\|                   |
//         --|RST                |
//           |                   |
//           |         P2.1/TA1.0|--> CCR0     
//           |         P2.2/TA1.1|--> CCR1-----|
//           |         P2.3/TA1.2|--> CCR2     | connected as this
//           |               P1.2|--> TA0.1----|
//  
//  MQ.Wu   Eastar-HK.COM
//******************************************************************************
#include <msp430.h>
#include <stdlib.h>
#include <stdio.h>
#include "PIN_DEF.H"
#define uchar unsigned char
#define uint unsigned int
uchar Count=0, First_Time=0x01;
uint REdge1, REdge2, FEdge;
uchar num1, num2, num3, num4;

uchar const table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
uchar const table1[]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10};

void Init_Port(void)
{
  P5DIR  |= POWER   ; //define the direction for O/P;
  MAIN_POWER_ON    ;  
  P7DIR  |= LED_PWR  ;
  P7OUT  |= LED_PWR   ;// turn off the LED;
  //INTERNAL_PULL_UP       ;
// ROW_IN_COL_OUT         ;
  P1OUT   = 0xF0           ;
  P1DIR   = 0xF0          ;
  P9OUT   = 0x0F         ;
  P9DIR   = 0x0F     ;
  P8DIR = 0xFF;
  P8OUT = 0xFF;

}
void Display(num1,num2,num3,num4)
{

    P8OUT = table[num1];     
    P9OUT &= ~BIT0;
    __delay_cycles(100);
    P9OUT |= BIT0;

    P8OUT = table[num2];
    P9OUT &= ~BIT1;
    __delay_cycles(100);
    P9OUT |= BIT1;

    P8OUT = table[num3];
    P9OUT &= ~BIT2;
    __delay_cycles(100);
    P9OUT |= BIT2;

    P8OUT = table[num4];
    P9OUT &= ~BIT3;
    __delay_cycles(100);
    P9OUT |= BIT3;

}                                


int main(void)
{
  uint Period, ON_Period;
  uchar DutyCycle;
  WDTCTL = WDTPW + WDTHOLD;                 // Stop WDT
  Init_Port();
  P1DIR &= ~BIT2;                           // P1.2/TA0.1 Input Capture
  P1SEL |= BIT2;                            // TA0.1 option select
  P2DIR |=0x0C;
  P2SEL |=0x0C;
  TA0CCTL1 = CAP + CM_3 + CCIE + SCS + CCIS_0;
  TA0CTL = TASSEL_2 + MC_2 + TAIE + TACLR;         // SMCLK, continous mode, clear TAR


  TA1CCR0 = 130;                            // PWM Period/2
  TA1CCTL1 = OUTMOD_6;                      // CCR1 toggle/set
  TA1CCR1 = 65;                             // CCR1 PWM duty cycle
  TA1CCTL2 = OUTMOD_6;                      // CCR2 toggle/set
  TA1CCR2 = 26;                             // CCR2 PWM duty cycle
  TA1CTL = TASSEL_2 + MC_3 + TACLR;         // SMCLK, up-down mode, clear TAR
  //__bis_SR_register(LPM0_bits);             // Enter LPM0

  //__bis_SR_register( GIE);

  while(1)
  {

     __bis_SR_register(GIE);  
      if (TA0CCTL1 & COV)                   // Check for Capture Overflow
      {  
         TA0CCTL1 &=~COV;
          while(1);                         // Loop Forever
      }
      Period = REdge2 - REdge1;             // Calculate Period
      ON_Period = FEdge-REdge1;             // On period
      DutyCycle = ((unsigned long)ON_Period*100/Period);
      num1=DutyCycle/1000;
      num2=DutyCycle%1000/100;
      num3=DutyCycle%100/10;
      num4=DutyCycle%10;
      Display(num1,num2,num3,num4);
  }
}

#pragma vector=TIMER0_A1_VECTOR
__interrupt void TIMER0_A1_ISR(void)
{

  switch(__even_in_range(TA0IV,14))
  {
    case 0: break;                  
    case 2:   if (TA0CCTL1 & CCI)                 // Capture Input Pin Status
          {
            // Rising Edge was captured
            if (!Count)
            {
                REdge1 = TA0CCR1;
                Count++;
            }
            else
            {
                REdge2 = TA0CCR1;
                Count=0x0;
                __bic_SR_register_on_exit(LPM0+ GIE);  // Exit LPM0 on return to main
            }

            if (First_Time)
                First_Time = 0x0;
        }
        else
        {
            // Falling Edge was captured
            if(!First_Time)
            {
                FEdge = TA0CCR1;
            }
        }

             break;
    case 4:  break;
    case 6:  break;                         // CCR3 not used
    case 8:  break;                         // CCR4 not used
    case 10: break;                         // CCR5 not used
    case 12: break;                         // Reserved not used
    case 14: break;
    default: break;
}
}


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表