MSP430 MCU key interrupt program

Publisher:EnigmaticSoulLatest update time:2018-01-29 Source: eefocusKeywords:MSP430 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

#include "IRRemote.h" 

void main(void) 

    //初始化 
    Initialize();                            


    //Infinite loopwhile 
    (1) 
    { 
        unsigned int transmit; 
        //Initialize variable 
        transmit = ENDTRANSMIT; 
        //Wait for a key to be pressed 
        SetForPress(); 
        //Enter LPM4 mode 
        LPM4; 
         
        Debounce();  
        //Scan keyboard 
        KeyS CAN ();  
        //Keyboard processing 
        KeyLookup();  
        if (Error_Flags == 0)  
        {                  
            //If a key is pressed 
            SetupForRelease(); 
            do  
            {  
                //Transmit data (RC5 code) 
                Transmit(); 
                 
                transmit = TestRetransmit(); 
                //Delay 
                DelayToNextTransmit();  
            } while (transmit == RETRANSMIT); 
        } 
    } 


void Initialize(void) 

    //Stop watchdog 
    WDTCTL = WDTPW+WDTHOLD; 
    //P2 port is output 
    P2DIR = 0xFF; 
    //P2.3 is TA1 function pin 
    P2SEL = 0x08; 
    //Clear P2 port output 
    P2OUT = 0; 
    //Set DCO frequency to 1M Hz 
    DCOCTL = CALDCO_1MHZ; 
    BCSCTL1 = CALBC1_1MHZ; 
    //Enable interrupt_EINT 
    ();  


void SetForPress(void) 

    //P1.0 and P1.1 are inputs 
    P1DIR = 0xFC; 
    //Clear the output of P1 port, pull down 
    P1OUT = 0; 
    //Enable the resistors 
of P1.0 and P1.1     P1REN |= 0x03;  
    //Enable keyboard 
    P2OUT |= 0x07;  
    //Low to high interrupt mode 
    P1IES &= ~0x03; 
    //Clear interrupt flag 
    P1IFG = 0; 
    //Enable interrupt 
    P1IE |= 0x03; 
    //Clear error flag 
    Error_Flags = 0; 
    //Clear transmission flag 
    Trans_Flags = 0; 


void Debounce(void) 

    //SMCLK/8, clear TA 
    TACTL = TASSEL1+TACLR+ID0+ID1; 
    //Enable CC R0 interrupt 
    TACCTL0 = CCIE;  
    //Set the delay value 
    TACCR0 = 5000 - 1;  
    //Start UP mode 
    TACTL |= MC0; 
    //Enter LPM0 mode 
    LPM0; 
    //Stop and clear 
    TACTL = TACLR; 
    //Clear CCTL0 register 
    TACCTL0 = 0; 


void KeyScan(void) 

    unsigned int i; 
     
    //Initialize row 
    RowMask = 0x01; 
    KeyHex = 0; 
    //Clear row value 
    P1OUT &= ~0x03;  
     
    for (i=0; i < 3; i++)  
    {                     
        P2OUT &= ~0x07; 
        P1DIR |= 0x03;  
        P1OUT &= ~0x03; 
        P1DIR &= ~0x03; 
        P2OUT = RowMask; 
        if (P1IN & 0x03)  
        {                       
            //Is there a key pressedKeyHex 
            |= RowMask; 
            //Processing valueKeyHex 
            |= (P1IN & 0x03) << 4; 
        } 
        //Next 
        rowRowMask = RowMask << 1; 
    } 
    if (KeyHex == 0)  
    {                       
        //Set 
        flagError_Flags |= noKey; 
    } 
    P2OUT |= 0x07; 


void KeyLookup(void) 

    unsigned int i; 
   
    //Initialization 
    valueKeyPressed = 99; 
    for (i = 0; i < 6; i++)  
    {                      
        //Lookup tableif 
        (KeyTab[i] == KeyHex)  
        {              
            KeyPressed = i;  
        } 
    } 
    if (KeyPressed == 99)  
    {                   
        //Value not found 
        //Set flags 
        Error_Flags |= noMatch; 
    } 


void SetupForRelease(void) 

    //Set direction 
    P1DIR |= 0x03; 
    P1OUT |= 0x03; 
    //Key pressed 
    Trans_Flags |= h ELD Down; 


void DetermineRelease(void) 

    //Set input pull-down 
    P1OUT &= ~0x03; 
    P1DIR &= ~0x03;  
    if (P1IN & 0x03)  
    { 
        SetupForRelease();  
    }  
    else  
    { 
        Trans_Flags &= ~heldDown; 
    } 


void Transmit(void) 

    unsigned int i; 
     
    //Get command code 
    Command = FuncTab[KeyPressed]; 
    //Increase the start bit by two bits 
    Command = Command + 0x3000;  
    if (Trans_Flags & toggle)  
    {                
        // Toggle bit 
        Command = Command + 0x0800; 
    } 
     
    //Left shift 
    Command = Command << 2;  
     
    //CCR1  PWM parameter 
    TACCR1 = 7; 
    //Enable CCR0 interrupt 
    TACCTL0 = CCIE; 
    for (i=0; i < 14; i++)  
    {         
        //Judge whether it is 1 or 0 
        if (Command & 0x8000)  
        { 
            //First send 780us low 
            OutputLow(780);                       
            //Then high, count 32 interrupts 
            OutputHigh(32);  
        }  
        else  
        { 
            //First high, count 35 interrupts 
            OutputHigh(35);                       
            OutputLow(765); 
        } 
        //Next bit 
        Command = Command << 1; 
    } 
    //Stop and clear TA 
    TACTL = TACLR; 


unsigned int TestRetransmit(void) 

    //Is there any input in P1 
    if (P1IFG == 0)  
    { 
        //If there is no input, 
        return RETRANSMIT; 
    }  
    else  
    {     
        //If there is input, 
        //Is there a key pressed 
        DetermineRelease(); 
        if (Trans_Flags & heldDown)  
        {           
            //If there is  ,
            return RETRANSMIT; 
        }  
        else  
        {  
            //If there is no input, return to the main program 
            //Clear CCTL0 register 
            TACCTL0 = 0; 
            //Clear CCTL1 register 
            TACCTL1 = 0; 
            //Clear toggle bit 
            Trans_Flags &= ~toggle; 
            return ENDTRANSMIT; 
        } 
    } 


void DelayToNextTransmit(void) 

    //SMCLK/8 clear TA 
    TACTL = TASSEL1+TACLR+ID0+ID1;  
    //89ms interrupt 
    TACCR0 = 11125 - 1; 
    //Enable CCR0 interrupt 
    TACCTL0 = CCIE;  
    //TA starts UP mode 
    TACTL |= MC0; 
    //Delay 
    LPM0; 
    //Stop and clear TA 
    TACTL = TACLR;  
    //Switch the switch bit 
    Trans_Flags ^= toggle; 


void OutputHigh(unsigned int val) 

    unsigned int j; 
    //CCR1 RESET/SET 
    TACCTL1 = OUTMOD_7;  
    //PWM period (40kHz) 
    TACCR0 = 25 - 1; 
    //Clear TA 
    TACTL = TASSEL1+TACLR; 
    //TA starts UP mode 
    TACTL |= MC0; 
    for (j = val; j > 0; j--)  
    {    
        //Interrupt counting 
        //When the period is 40kHz, the time of each interrupt is 25us 
        LPM0;  
    }  


void OutputLow(unsigned int val) 

    // Set CC1 output to 0 
    TACCTL1 = OUTMOD_0; 
    // Clear TA 
    TACTL = TASSEL1+TACLR; 
    // Delay 
    TACCR0 = val;  
    // Set TA to UP mode 
    TACTL |= MC0;  
    // Enter LPM0 mode, wait for CCR0 interrupt 
    LPM0; 


// P1.x interrupt service routine 
#if __VER__ < 200 
    interrupt [PORT1_VECTOR] void P1_ISR(void) 
#else 
    #pragma vector=PORT1_VECTOR 
    __interrupt void P1_ISR(void) 
#endif 

    //Exit LPM4 low power mode 
    LPM4_EXIT;  
    //Clear interrupt flag 
    P1IFG = 0; 
    //Disable interrupt 
    P1IE = 0; 


// CCR0 interrupt service routine 
#if __VER__ < 200 
    interrupt [TIMERA0_VECTOR] void CCR0_ISR(void) 
#else 
    #pragma vector=TIMERA0_VECTOR 
    __interrupt void CCR0_ISR(void) 
#endif 

    //Exit LPM0 low power mode 
    LPM0_EXIT;  


IRRemote.h

#include " MSP 430x21x1.h" 

#define noKey 0x001 
#define noMatch 0x002 
#define heldDown 0x001 
#define toggle 0x002 
#define RETRANSMIT 1 
#define ENDTRANSMIT 0 

// Key Lookup Table 
const char KeyTab[6] = { 
  0x11, // Key 0 
  0x21, // Key 1 
  0x12, // Key 2 
  0x22, // Key 3 
  0x14, // Key 4 
  0x24 // Key 5 
}; 

// Function Lookup Table 
const char FuncTab[6] = { 
  0x0C, // Key 0 -  Power 
  0x0D, // Key 1 - Mute 
  0x20, // Key 2 - Channel + 
  0x10, // Key 3 - Volume + 
  0x21, // Button 4 - Channel - 
  0x11 // Button 5 - Volume - 
}; 

unsigned int RowMask; 
unsigned int KeyHex; 
unsigned int KeyVal; 
unsigned int KeyPressed; 
unsigned int Command; 
unsigned int Trans_Flags; 
unsigned int Error_Flags;                     

void Initialize(void); 
void SetForPress(void); 
void Debounce( void); 
void KeyScan(void); 
void KeyLookup(void); 
void SetupForRelease(void); 
void DetermineRelease(void); 
void Transmit(void); 
unsigned int TestRetransmit(void); 
void DelayToNextTransmit(void); 
void OutputHigh(unsigned int); 
void OutputLow(unsigned int); 


Keywords:MSP430 Reference address:MSP430 MCU key interrupt program

Previous article:Domestic blood glucose meter based on MSP430F435
Next article:MSP430 MCU key program

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号