MSP430 : utilisation du TimerB en mode Capture
Répondre à la discussion
Affichage des résultats 1 à 1 sur 1

MSP430 : utilisation du TimerB en mode Capture



  1. #1
    arbtec

    MSP430 : utilisation du TimerB en mode Capture


    ------

    Bonjour, j'ai une petite carte basée sur un MSP430F247, avec laquelle je souhaite mesurer la période de deux signaux arrivant sur P4.0 et P4.1 (entre 5 et 15kHz).
    Je souhaite configurer le TimerB en mode capture, stocker la valeur du compteur sur les fronts et en déduire rapidement la période de mes signaux.
    Les signaux arrivent bien sur le micro, par contre je n'arrive pas à entrer dans l'interruption (flag est à 1 : IT Pending)

    Auriez-vous déjà utilisé le mode capture sur le timerB, auriez-vous un exemple de code, ou voyez vous une erreur dans mon code ci-dessous?

    D'avance merci
    Code:
    //***************************************************************************************
    TimerA configuré pour IT toutes les ms
    Led Toggle toutes les 100ms
    TimerB en mode capteur sur P4.0 et P4.1
    //***************************************************************************************
    
    #include <msp430.h>                
    unsigned int NbCycles100ms=0;
    
    /** \brief Used to count 100ms periods */
        volatile unsigned int  tick_100ms;
        /** \brief Used to count 1ms periods and to generate other timings*/
        volatile unsigned int  tick_1ms;
    
        unsigned int Count_P40=0, R1Edge1=0, R1Edge2=0;
        unsigned int Count_P41=0, R2Edge1=0, R2Edge2=0;
        unsigned int overflow_count=0;
        unsigned int PeriodeP40=0, PeriodeP41=0;
    
    int main(void) {
        unsigned int temp_u16, NbCycles100ms;
    
        WDTCTL = WDTPW | WDTHOLD;        // Stop watchdog timer
    
        //Clock configuration : External 8MHz Crystal on XT2 and ACLK = VLO
        BCSCTL1 = 0x0A; //XT2 = ON + XTS = LF + DIVA = 0 + RSELx = A
        BCSCTL2 = 0x88; //SELMx = 10 + DIVMx = 0 + SELS = 1 + DIVSx = 0 + DCOR = 0
        BCSCTL3 = 0xA0; //XT2Sx = 10 (3 to 16 MHz) + LFXT1S = 10 (VLOCK) + XCAPx = 00 + XT2OF = 0 + LFXT1OF = 0
    
        // stop everything
        TBCTL = 0;
        TBCCTL0 = 0; TBCCTL1 = 0; TBCCTL2 = 0; TBCCTL3 = 0; TBCCTL4 = 0; TBCCTL5 = 0; TBCCTL6 = 0;
        TBCCR0 = 0; TBCCR1 = 0; TBCCR2 = 0; TBCCR3 = 0; TBCCR4 = 0; TBCCR5 = 0; TBCCR6 = 0;
    
        // TimerA Init (1kHz base time)
        TACTL  = TASSEL_2 + TAIE;     // SMCLK, upmode, interrupt
        TACCR0 = 0x1F40;
    
        P3DIR |= 0x06;                    // Set P3.1 & P3.2 to output direction
    
        P4DIR = 0x0;  //As input
        P4SEL = 0x03; //Pin 4.1 et 4.0s special function enable
    
        // TimerB : input frequency measurement
        TBCTL = 0x0200;  // SMCLK + Continuous Mode + Interrupt Enable
        TBCCTL0 = 0x4000 + 0x1000 + 0x800 + 0x100 + 0x10; //rising edge + CCIxB + SCS + CAP + CCIE
        TBCCTL1 = 0x4000 + 0x1000 + 0x800 + 0x100 + 0x10; //rising edge + CCIxB + SCS + CAP + CCIE
        TBCTL &= ~0x1;    //Clear Interrupt
        TBCTL += 0x24; // TBCLR + MC_2 (continuous Mode)
        TACTL += 0x10; // MC_1 : up to TACCR0 value
    
        _BIS_SR(GIE); //Enable interrupts
    
        // Main loop
        while(1) {
            temp_u16 = tick_100ms;
            NbCycles100ms++;
            if (NbCycles100ms%2){
                P3OUT &= ~0x06;}
            else{
                P3OUT |= 0x02;}
            PeriodeP40 = R1Edge2 - R1Edge1;
            PeriodeP41 = R2Edge2 - R2Edge1;
            while (temp_u16 == tick_100ms){}; //Wait until end of current cycle
        }
    }
    
    #pragma vector = TIMERA1_VECTOR
    __interrupt void Timer_A(void)
    {
        switch( TAIV ){
            case  2: break;    //CCR1 not used
            case  4: break;    //CCR2 not used
            case 10: {
    
                tick_1ms += 1;
    
                if (tick_1ms == 99){
                    tick_1ms    = 0;
                    tick_100ms += 1;
                }
            } break;
    
            default: break;
        }
    }
    #pragma vector=TIMERB0_VECTOR
    __interrupt void Timer_B0 (void)
    {
          TBCCTL0 &= ~CCIFG;
          TBCTL &= ~0x1;
        //Capture detected
          if (!Count_P40)
              {
              // TBCCR0 = TBR; // Done automatically at ISR
              R1Edge1 = TBCCR0;
              Count_P40++;
              }
          else
              {
              R1Edge2 = TBCCR0;
              Count_P40=0x0;
              }
    }
    #pragma vector=TIMERB1_VECTOR
    __interrupt void Timer_B1 (void)
    {
      switch (TBIV)
      {
        //Capture detected
        case  TBIV_TBCCR1:
            if (!Count_P41)
                      {
                      // TBCCR1 = TBR; // Done automatically at ISR
                      R2Edge1 = TBCCR1;
                      Count_P41++;
                      }
                  else
                      {
                      R2Edge2 = TBCCR1;
                      Count_P41=0x0;
                      }
            TBCCTL1 &= ~CCIFG;
          break;
        case  TBIV_TBCCR2:
            TBCCTL2 &= ~CCIFG;
            break;
        case  TBIV_TBCCR3:
                TBCCTL3 &= ~CCIFG;
                break;
        case  TBIV_TBCCR4:
                TBCCTL4 &= ~CCIFG;
                break;
        case  TBIV_TBCCR5:
                TBCCTL5 &= ~CCIFG;
                break;
        case  TBIV_TBCCR6:
                TBCCTL6 &= ~CCIFG;
                break;
        //Overflow in Timer B detected
        case TB0IV_TBIFG:
           overflow_count++; //Increment a 16 bit variable to get a 32bit timer
           TBCCTL0 &= ~CCIFG;
           TBCTL &= ~0x1;
           break;
        default : break;
      }
      TBCTL &= ~0x1;
    }

    -----
    Dernière modification par Antoane ; 19/03/2015 à 11h52. Motif: Ajout balises [code]

Discussions similaires

  1. Mode capture PIC
    Par krimo-30 dans le forum Électronique
    Réponses: 18
    Dernier message: 20/11/2013, 22h33
  2. mode capture sur dsPIC
    Par invitef3ff390d dans le forum Électronique
    Réponses: 2
    Dernier message: 07/05/2012, 15h04
  3. [PIC] Utilisation du mode Capture
    Par Tripic dans le forum Électronique
    Réponses: 5
    Dernier message: 23/08/2009, 21h34
  4. Mode capture sur 18F
    Par invited6358b87 dans le forum Électronique
    Réponses: 0
    Dernier message: 24/02/2007, 15h54
  5. mode capture du PIC16F627 ?
    Par invite164b8116 dans le forum Électronique
    Réponses: 3
    Dernier message: 03/10/2006, 17h39
Dans la rubrique Tech de Futura, découvrez nos comparatifs produits sur l'informatique et les technologies : imprimantes laser couleur, casques audio, chaises gamer...