MSP430 MCU key program

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

MSP 430 MCU key program

#include  
#include "key.h" 

void Init_Port(void) 

    //Set all pins of P1 port to input mode during initialization 
    P1DIR = 0;

  
    //Set all pins of P1 port as general I/O ports 
    P1SEL = 0; 
     
    //Set P1.4 P1.5 P1.6 P1.7 as output direction 
    P1DIR |= BIT4; 
    P1DIR |= BIT5; 
    P1DIR |= BIT6; 
    P1DIR |= BIT7; 

    //Output low level 
    P1OUT = 0x00; 

    //Clear the interrupt register 
    P1IE = 0; 
    P1IES = 0; 
    P1IFG = 0; 
    //Turn on the interrupt function of the pin 
    //The corresponding pin jumps from high to low level to set the corresponding flag 
    P1IE |= BIT0;     
    P1IES |= BIT0;     
    P1IE |= BIT1;     
    P1IES |= BIT1; 
    P1IE |= BIT2;     
    P1IES |= BIT2      ;
    P1IE |= BIT3;     
    P1IES |= BIT3; 
    _EINT();//Turn on interrupt 
    return; 

void Delay(void) 

    int i; 
    for(i = 100;i > 0;i--); //delay a little time 

int KeyProcess(void) 

    int nP10; 
    int nP11; 
    int nP12; 
    int nP13; 
    int nRes = 0; 
    //P1.4 outputs low level 
    P1OUT &= ~(BIT4); 
    nP10 = P1IN & BIT0; 
    if (nP10 == 0) nRes = 13; 
    nP11 = (P1IN & BIT1) >> 1; 
    if (nP11 == 0) nRes = 14; 
    nP12 = (P1IN & BIT2) >> 2; 
    if (nP12 == 0) nRes = 15; 
    nP13 = (P1IN & BIT3) >> 3; 
    if (nP13 == 0) nRes = 16; 
    //P1.5 outputs low level 
    P1OUT &= ~(BIT4); 
    nP10 = P1IN & BIT0; 
    if (nP10 == 0) nRes = 9; 
    nP11 = (P1IN & BIT1) >> 1; 
    if (nP11 == 0) nRes = 10; 
    nP12 = (P1IN & BIT2) >> 2; 
    if (nP12 == 0) nRes = 11; 
    nP13 = (P 1IN & BIT3) >> 3; 
    if (nP13 == 0) nRes = 12; 
    //P1.6 outputs low level 
    P1OUT &= ~(BIT4); 
    nP10 = P1IN & BIT0; 
    if (nP10 == 0) nRes = 5; 
    nP11 = (P1IN & BIT1) >> 1; 
    if (nP11 == 0) nRes = 6; 
    nP12 = (P1IN & BIT2) >> 2; 
    if (nP12 == 0) nRes = 7; 
    nP13 = (P1IN & BIT3) >> 3; 
    if (nP13 == 0) nRes = 8; 
    //P1. 7 Output low level
    P1OUT &= ~(BIT4); 
    nP10 = P1IN & BIT0; 
    if (nP10 == 0) nRes = 1; 
    nP11 = (P1IN & BIT1) >> 1; 
    if (nP11 == 0) nRes = 2; 
    nP12 = (P1IN & BIT2) >> 2; 
    if (nP12 == 0) nRes = 3; 
    nP13 = (P1IN & BIT3) >> 3; 
    if (nP13 == 0) nRes = 4; 

    P1OUT = 0x00;//Restore the previous value. 

    //Read the status of each pin 
    nP10 = P1IN & BIT0; 
    nP11 = (P1IN & BIT1) >> 1; 
    nP12 = (P1IN & BIT2) >> 2; 
    nP13 = (P1IN & BIT3) >> 3; 
    for(;;) 
    { 
        if(nP10 == 1 && nP11 == 1 && nP12 == 1 && nP13 == 1) 
        { 
            //Wait for the key to be released 
            break; 
        } 
    } 
    return nRes; 

//Handle interrupt from port 1 
#if __VER__ < 200 
    interrupt [PORT1_VECTOR] void PORT_ISR(void) 
#else 
    #pragma vector=PORT1_VECTOR 
    __interrupt void PORT_ISR(void) 
#endif 

    Delay(); 
    KeyProcess(); 
    if(P1IFG & BIT0) 
    {         
        P1IFG &= ~(BIT0);// Clear interrupt flag 
    } 
    if(P1IFG & BIT1) 
    { 
        P1IFG &= ~(BIT1);// Clear interrupt flag 
    } 
    if(P1IFG & BIT2) 
    { 
        P1IFG &= ~(BIT2);// Clear interrupt flag 
    } 
    if(P1IFG & BIT3) 
    { 
        P1IFG &= ~(BIT3);// Clear interrupt flag 
    } 

void Init_CLK(void) 

    unsigned int i; 
    BCSCTL1 = 0X00; // Clear the contents of the register 
                    // XT2 oscillator is turned on 
                    // LFTX1 works in low frequency mode  // The division
factor                     of ACLK is 1      do       {      IFG1 &= ~OFIFG; // Clear OSCFault flag      for (i = 0x20; i > 0; i--);                      }      while ((IFG1 & OFIFG) == OFIFG); // If OSCFault = 1         BCSCTL2 = 0X00; // Clear the contents of the register      BCSCTL2 += SELM1; // The clock source of MCLK is TX2CLK, and the division factor is 1      BCSCTL2 += SELS; //The clock source of SMCLK is TX2CLK, and the division factor is 1  } 
                     






                     





key.h

void Init_CLK(void);
int KeyProcess(void);
void Delay(void);
int KeySCAN(void);
void Init_Port(void);


Keywords:MSP430 Reference address:MSP430 MCU key program

Previous article:MSP430 MCU key interrupt program
Next article:Image acquisition solution based on TMS320C6x11 series DSP

Recommended ReadingLatest update time:2024-11-24 18:42

MSP430 (G2553) uses the watchdog timer to generate a periodic signal
#include msp430g2553.h   #include intrinsics.h         volatile unsigned int i = 0; //Global variables used in the interrupt service subroutine are best defined as volatile type. For specific reasons and usage, please refer to my other blog posts.      void main (void)   {       WDTCTL = WDT_MDLY_0_5; // Cycle 0.5ms,
[Microcontroller]
Design of CCD camera dimming photoelectric control system based on MSP430 microcontroller
MagTek card reader chip 21006450 is widely used, and its research will help the actual application of card reader chip driver. The Android system architecture, chip working principle and interface call studied here are the theoretical basis for the design of card reader chip driver, and provide guidance for the implem
[Power Management]
Design of CCD camera dimming photoelectric control system based on MSP430 microcontroller
MSP430 MCU matrix keyboard and digital tube experiment
#include "msp430x44x.h" #define ROW P2OUT            //matrix keyboard row macro definition #define COL P2IN              //matrix keyboard column macro definition #define DPYOUT P3OUT      //digital tube output macro definition unsigned char keyval;    //key value //Code table of common anode digital tube       
[Microcontroller]
MSP430G2553 test program (breathing light)
//************************************************ **************************** //Modified by http://jiwm.blog.163.com     //MSP430G2553 breathing light demonstration program - using Timer_A, Up Mode, DCO SMCLK // //   Introduction: This program uses the UP mode of TIMER A to generate PWM output on the P1.6 pin.
[Microcontroller]
Features of MSP430 MCU
The rapid development and continuous expansion of the application scope of the MSP430 series of microcontrollers mainly depend on the following characteristics. a. Powerful processing capabilities: The MSP430 series microcontroller is a 16-bit microcontroller that uses a reduced instruction set (RISC) stru
[Microcontroller]
MSP430F44X MCU SPI interface driver C language program
#include     char MST_Data=0X00,SLV_Data=0XFF;     void Init(void);     void main(void)     {         unsigned int i;        WDTCTL=WDTPW+WDTHOLD;        Heat();        _UNITE();        P3OUT&=~0X020;        P3OUT|=0X020;        i=50000;        do(i--);        while(i!=0);        while(1)
[Microcontroller]
msp430 matrix button driver
When I wrote this program, I had no reference at all. It was all based on assembly language and was moved to C language step by step. But after testing, I felt that the effect was still very ideal, so I shared it with you, hoping to help some friends~ #include "msp430x14x.h" unsigned char LineScan ={0xef,0xdf,0xbf,0x
[Microcontroller]
MSP430 MCU Timer B Interrupt Experiment
This program is based on the internal timer B interrupt experiment of MSP430 single chip. Function: Use timer B to accurately set the time interval of the running light. #include "MSP430F149.h" #define uchar unsigned char #define uint unsigned int  uchar LedData=0x80; uchar num=50;//Interrupt 50 times to shift the
[Microcontroller]
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号