Học liệu

Điều khiển động cơ DC bằng MSP430

  • 16/06/2019
  • Học liệu

#include  <msp430F2013.h>

#include "intrinsics.h"

//variables

#define     TANG_NGHICH      BIT1    // Button on P1.1

#define     TANG_THUAN     BIT3    // Button on P1.3

#define     GIAM_THUAN_STOP     BIT5    // Button on P1.5

#define     GIAM_NGHICH_STOP     BIT7    // Button on P1.7

#define     STOP     BIT4    // Button on P1.4

unsigned int PWM = 100;

unsigned int Freq = 0;

    void main(void)

{

  WDTCTL = WDTPW + WDTHOLD;                   // Stop watchdog timer

  P1DIR |= (BIT0) + (BIT2) + (BIT6); // Set P1.0 P1.2  AND P1.6 as Output Pins;

  P1SEL |= (BIT2);                   // Tao TA0.1 to P1.2

   

  //Set Up Button P1.1

  P1DIR &= ~TANG_NGHICH;

  P1OUT |= TANG_NGHICH;

  P1REN |= TANG_NGHICH;

  P1IES |= TANG_NGHICH;

  P1IFG &= ~TANG_NGHICH;

  P1IE |= TANG_NGHICH;

    //Set Up Button P1.3

  P1DIR &= ~TANG_THUAN;

  P1OUT |= TANG_THUAN;

  P1REN |= TANG_THUAN;

  P1IES |= TANG_THUAN;

  P1IFG &= ~TANG_THUAN;

  P1IE |= TANG_THUAN;

 

  //Set Up Button P1.5

  P1DIR &= ~GIAM_THUAN_STOP;

  P1OUT |= GIAM_THUAN_STOP;

  P1REN |= GIAM_THUAN_STOP;

  P1IES |= GIAM_THUAN_STOP;

  P1IFG &= ~GIAM_THUAN_STOP;

  P1IE |= GIAM_THUAN_STOP;

     //Set Up Button P1.7

  P1DIR &= ~GIAM_NGHICH_STOP;

  P1OUT |= GIAM_NGHICH_STOP;

  P1REN |= GIAM_NGHICH_STOP;

  P1IES |= GIAM_NGHICH_STOP;

  P1IFG &= ~GIAM_NGHICH_STOP;

  P1IE |= GIAM_NGHICH_STOP;

    // Set Up Button P1.4

  P1DIR &= ~STOP;

  P1OUT |= STOP;

  P1REN |= STOP;

  P1IES |= STOP;

  P1IFG &= ~STOP;

  P1IE |= STOP;

   //PWM Generation on P1.2

  CCR0 = 1000-1;                             // PWM Period

  CCTL1 = OUTMOD_7;                         // CCR1 reset/set

  TACTL = TASSEL_2 + MC_1;                         

  _BIS_SR(LPM0_bits + GIE);                                

}

#pragma vector=PORT1_VECTOR

__interrupt void Port1_ISR(void)

{

     //  STOP

 

 switch(P1IFG & STOP)    

    {

        case STOP:

        if (P1OUT |= BIT0)

            {

            CCR1 = 1000;

            P1OUT |= BIT2;

            P1IFG = 0;

            }

       else {

              CCR1 ==1;

              P1OUT |= BIT2 ;

              P1IFG = 0;

       } 

    } __delay_cycles(5000); 

  // TANG NGHICH

  switch(P1IFG & TANG_NGHICH)    

    {

     case TANG_NGHICH:

        if (Freq >1)

      {

            Freq --;

            CCR1 = PWM*Freq;

            P1OUT |= BIT0;

            P1OUT |= BIT2;

              P1IFG = 0;

       }

       else {

            P1OUT |= BIT0;

            P1OUT |= BIT2;

              P1IFG = 0;

           } 

     } __delay_cycles(5000);

   

// GIAM_NGHICH

   switch(P1IFG & GIAM_NGHICH_STOP)    

    {

        case GIAM_NGHICH_STOP:

        if (Freq <11)

      {

            Freq ++;

            CCR1 = PWM*Freq;

            P1OUT |= BIT0;

            P1OUT |= BIT2;

              P1IFG = 0;

       }

       else {

            P1OUT |= BIT0;

            P1OUT |= BIT2;

              P1IFG = 0;

           } 

     } __delay_cycles(5000);  

// TANG_THUAN

      switch(P1IFG & TANG_THUAN)    

    {    

        case TANG_THUAN:

        if (Freq < 11)

      {

            Freq++;

            CCR1 = PWM*Freq;

            P1OUT &= ~BIT0;

            P1OUT |= BIT2;

              P1IFG = 0;

       }

       else {

            P1OUT &= ~BIT0;

            P1OUT |= BIT2;

              P1IFG = 0;

           } 

     } __delay_cycles(5000);  

        //Decrease speed

    switch(P1IFG & GIAM_THUAN_STOP)

    {       

        case GIAM_THUAN_STOP:

        if (Freq > 0)

        {

            Freq--;

            CCR1 = PWM*Freq;

            P1OUT &= ~BIT0;

            P1OUT |= BIT2;

            BIT0 == 0;

            BIT2 == 0;

              P1IFG = 0;

        }

        else {

            P1OUT &=~ BIT0;

            P1OUT |= BIT2;

            BIT0 == 0;

            BIT2 == 0;

              P1IFG = 0;

           }   

    } 

    __delay_cycles(5000);

}

 

 

Các tin khác