My matrix keyboard usage notes - based on msp430g2553

Publisher:老卫Latest update time:2015-08-18 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
Created  on:  2012-9-6

      Author:  zhang  bin

 

Study Notes

for  msp430g2553

redesigned  by  zhang  bin

2012-09-06

versions : 12_09_01

All  Rights  Reserved

 

 

 

Here I mainly record my method of using matrix keyboard to input data. In system design, data input is often encountered, and matrix keyboard is the most commonly used input device. If there is no problem with the scanning of matrix keyboard, how to use matrix keyboard to input data, for example, I want to input the number 253.45, how to achieve it.

Here are the two implementations I used:

First, it borrows a strange value, that is, a value that cannot be scanned when the keyboard is scanned. This number has nothing to do with the keyboard. It is mainly used to mark that a new key has been pressed. I use the interrupt detection key of msp430g2553. The principle is that when any key is pressed, it will enter the interrupt function, and then judge which key is pressed in the interrupt. The basis for realizing this method is that the 8 IO ports of an IO port group of msp430g2553 (such as P1, P2) share an interrupt vector. In a group, no matter which IO port triggers the interrupt, it will enter the same interrupt. So in this way, you can use the interrupt to determine whether a key has been pressed, and then further judge which key is pressed in the interrupt. This saves a lot of microcontroller resources compared to the method of using loop scanning. When the key is not pressed, there is no need to keep scanning the key in a loop. The program of the keyboard interrupt part is as follows:

//  Port  interrupt  service  routine

#pragma  vector=PORT1_VECTOR

__interrupt  void  Port_1(void)

{

uchar  temp;

 

_DINT();     //Disable interrupt

 

P1DIR  &=  ~(BIT0+BIT1+BIT2+BIT3);  //Set the interrupt as input to eliminate jitter.    Because the IO pin is input by default, this sentence can be omitted, but

  //It is best to add to make the program clear

delay_ms(5);      //delay 5ms, debounce   delay 5ms  or 10ms is OK

 

temp  P1IN  0x0f;   //Extract the 4-bit IO value of the connected button

 

key=99;     //Give key a weird value, that is, a value that is impossible to scan when the keyboard is scanned. This value has nothing to do with the keyboard. Here, it is a value outside 1 to 15.

// Mainly used to mark that a new key has been pressed

 

if(temp==0x01)

{

key=1;

}

else  if(temp==0x02)

{

key=2;

}

else  if(temp == 0x03)

{

key=3;

}

else  if(temp==0x04)

{

key=4;

}

else  if(temp==0x05)

{

key=5;

}

else  if(temp == 0x06)

{

key=6;

}

else  if(temp==0x07)

{

key=7;

}

else  if(temp==0x08)

{

key=8;

}

else  if(temp==0x09)

{

key=9;

}

else  if(temp == 0x0a)

{

key=0;

}

else  if(temp == 0x0b)

{

key=11;

 

t_flag=3;    //t_flag=0 means only temperature detection without control

key_flag  1;    //Set temperature

key_count=0;

        key=99;

set_t=0.0;    //Set the temperature value to 0 first

}

else  if(temp == 0x0c)

{

key=12;

t_flag=4;

key_flag  2;    //Set time

key_count=0;

        key=99;

set_time=0;    //Set the temperature value to 0 first

}

else  if(temp==0x0d)   //40°

{

key=13;

t_flag=1;

}

else  if(temp == 0x0e)

{

key=14;

t_flag=2;     //60

}

else  if(temp == 0x0f)

{

key=15;

t_flag=0;   //t_flag=0 means only temperature detection, no control

}

 

if(key_flag==1)    //Set temperature

{

if(key!=99)

{

set_t=set_t*10+key;

// wr_int(4,1,set_t);

key=99;

key_count++;

// wr_int(3,3,key_count);

if(key_count==4)

{

set_t=(float)set_t/10;

wr_float_3(4,1,set_t);   //Display the set temperature value

key_flag=0;

key_count=0;

}

}

}

 

if(key_flag==2)       //Set time

{

if(key!=99)

{

set_time=set_time*10+key;

wr_int(4,2,set_time);    //Display the set time

key=99;

key_count++;

// wr_int(3,2,key_count);

if(key_count==4)

{

set_time_1=set_time;     //The intermediate buffer value replaces set_time, because set_time is very sensitive in the interrupt and it is easy to be garbled.

// wr_int(3,2,set_time);

int_flag=1;    //Time setting flag bit

key_flag=0;

key_count=0;

}

}

}

 

 

 

//    wr_int(0,1,key);    //display

   P1IFG  &=  ~(BIT0+BIT1+BIT2+BIT3);       //  P1.3  IFG  cleared   software clears interrupt flag

 

_EINT();   //Enable interrupt

 

}

 

The second method is to use an array to temporarily store the pressed values, and then combine them to form the desired values. The number of elements in the array is the total number of digits of the data you want to enter. The procedure is as follows:

//  Port  interrupt  service  routine

#pragma  vector=PORT1_VECTOR    //P1 port interrupt, keyboard interrupt

__interrupt  void  Port_1(void)

{

uchar  i=0;

uchar  j=0;

 

   _DINT();   //Keyboard processing, disable interrupt

   key_interrupt_flag=1;

 

   if((key_flag==1)||(key_flag==2))    //If the set temperature or time key is pressed, start counting

   {

      key_count++;     //Used to count the number of times a key is pressed, so as to input the set temperature and time

   }

 

   key=rd_key();    //Read the key value

//    while((P1IN&BIT3)==0);   //Wait for the button to be released

   for(i=0;i<10;i++)

   {

   if(key==key_code[i])     //Use array to convert key value

   key=i;     //Converted to the corresponding number

   }

   wr_int(2,1,key);   //display

 

   if(key==60)   //* key  to set temperature

   {

     key_flag=1;    //Set to 1

   }

   else  if(key==62)     //#key  setting time

   {

   key_flag=2;

   }

   else  if(key==47)    //B: 40° key

   {

   set_t=40;

   set_t_flag=1;    //The temperature is set to 40°, the flag is 1

   set_t_flag_1=set_t_flag;

   wr_float_1(1,0,set_t);    //Display the set temperature value

   }

   else  if(key==55)    //C:  60° key

   {

   set_t=60;

   set_t_flag=2;    //The temperature is set to 60°, and the flag is 2

   set_t_flag_1=set_t_flag;

   wr_float_1(1,0,set_t);    //Display the set temperature value

   }

 

   if((key_count==1)&&(key<=9))

   {

   a[0]=key;

   }

   else  if((key_count==2)&&(key<=9))

   {

   a[1]=key;

   }

   else  if((key_count==3)&&(key<=9))

   {

   a[2]=key;

   }

   else  if((key_count==4)&&(key<=9))

   {

   a[3]=key;

   }

   if(key_flag==1)     //temperature control

   {

   set_t=a[0]*1000+a[1]*100+a[2]*10+a[3];

   set_t  set_t/10;

   wr_float_1(1,0,set_t);    //Display the set temperature value

 

   control_flag=1;    //temperature control

   control_flag_1=control_flag;    //Save temporarily

 

   wr_int(0,3,control_flag);     //Display status for easy observation and debugging

 

   set_t_flag=3;    //Set the temperature to any temperature, the flag is 3

   set_t_flag_1=set_t_flag;

 

   }

   if(key_flag==2)    //Time control

   {

   set_time=a[0]*1000+a[1]*100+a[2]*10+a[3];

   set_time  set_time/10;

   wr_float_1(5,0,set_time);    //Display the set time value

   control_flag=2;    //Time control

   control_flag_1=control_flag;   //Save temporarily

   wr_int(0,3,control_flag);     //Display status for easy observation and debugging

 

   second=0;    //Reset the seconds and start timing

   wr_int(0,2,second);   //Display seconds

   time_m=0;    //Time period cleared

 

   }

   if(key_count==4)   //Setting completed      . The set temperature and set time can only be in the form of xxx.x

   {

   key_count=0;

   key_flag=0;

   for(j=0;j<4;j++)

   {

   a[j]=0;     // array clear

   }

   }

 

   if(key==39)     //A: Temperature control on or off key  Press once to turn on, press again to cancel

   {

   if(control_flag!=0)

   control_flag=0;

   else  if(control_flag==0)

   {

   control_flag=control_flag_1;    //Restore

   }

   wr_int(0,3,control_flag);     //Display status for easy observation and debugging

 

   if(set_t_flag!=0)

   set_t_flag=0;

   else  if(set_t_flag==0)

   {

   set_t_flag=set_t_flag_1;   //Restore

   }

   }

 

   P1IFG  &=  ~BIT3;       // Clear the interrupt flag

   _EINT();   //Enable interrupt

}

 

 

//Corresponding button functions: *: Set temperature button      #: Set time button    A: Temperature control on or off button  Press once to turn on, press again to cancel      B: 40° button       C:  60° button

Reference address:My matrix keyboard usage notes - based on msp430g2553

Previous article:Temperature control program part report - based on msp430g2553
Next article:My TLC2543 study notes - based on the msp430g2553 microcontroller

Recommended ReadingLatest update time:2024-11-15 07:43

MSP430G2553 clock system configuration
In the MSP430 microcontroller, one clock cycle = the inverse of the MCLK crystal oscillator. If the MCLK is 8MHz, then one clock cycle is 1/8us. One machine cycle = one clock cycle, that is, each action of 430 can complete a basic operation. One instruction cycle = 1~6 machine cycles, depending on the specific instruc
[Microcontroller]
Brief Analysis of Clock Settings of MSP430G2553
Registers of the basic clock system DCOCTL - DCO Control Register DCOx     DCO frequency selection control 1 MODx DCO frequency correction selection, usually set MODx=0 Note: After the MSP430G2553 is powered on and reset, the default RSEL=7, DCO=3. The DCO frequency is approximately between 0.8 and 1.5 MH
[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号