4*4 keyboard digital tube display program analysis

This routine is also a classic routine on the development board. I modified the framework of the program and it is more suitable for future calls. Specific 4*4 keyboard scanning principle is more basic here is no longer to go into details here, I think it is more important to develop a good habit of writing programs, like 4*4 keyboard scan such a function can be written as a fixed C or H File for later recall. Take a look at the main program:

/***********************************************

Program function: Scan 4X4 keyboard and display the key value on the digital tube

------------------------------------------------

Test description: Press K1 ~ K16 button, observe the digital display

************************************************/

#include "msp430x14x.h"

#include "Keypad.C"

// Digital tube 7-digit segment code: 0--f

Unsigned char scandata[16] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,

0x80, 0x90, 0x88, 0x83, 0xc6, 0xa1, 0x86, 0x8e};

// Record global variables that display the number of bits

Unsigned char cnt = 0;

//Show cache

Unsigned char Dispbuf[2];

// Declaration of external variables

Extern unsigned char key_Pressed;

Extern unsigned char key_val;

Extern unsigned char key_Flag;

/********************Main function*********************/

Void main(void)

{

/* The following six lines close all IO ports */

P1DIR = 0XFF; P1OUT = 0XFF;

P2DIR = 0XFF; P2OUT = 0XFF;

P3DIR = 0XFF; P3OUT = 0XFF;

P4DIR = 0XFF; P4OUT = 0XFF;

P5DIR = 0XFF; P5OUT = 0XFF;

P6DIR = 0XFF; P6OUT = 0XFF;

P6DIR |= BIT2;P6OUT |= BIT2; //Turn off level translation

WDTCTL = WDT_ADLY_1_9; //Set the internal watchdog to work in timer mode, interrupt once for 1.9ms

IE1 |= WDTIE; //Enable watchdog interrupt

_EINT(); //Open global interrupt

Init_Keypad();

While(1)

{

Key_Event();

If(key_Flag == 1)

{

key_Flag = 0;

Dispbuf[1] = key_val / 10;

Dispbuf[0] = key_val % 10;

}

}

}

/*******************************************

Function Name:watchdog_timer

Function: Watchdog interrupt service function, output digital tube here

Segment selection and position selection signals

Parameters: None

Return value: None

********************************************/

#pragma vector=WDT_VECTOR

__interrupt void watchdog_timer(void)

{

P4OUT = 0xff;

P5OUT = scandata[Dispbuf[cnt]]; //Output segment select signal

P4OUT &= ~(1 "(cnt+2)); //Output the position selection signal

Cnt++; //The bit count variable cycles through 0~1

If(cnt == 2) cnt = 0;

}

The main program is relatively simple, the specific principle see the previous note, in the main program and keyboard scanning related functions were hit in the Keypad.C, need to explain is that in the main function need to declare some of the Keypad.C variables In order to use the variables in Keypad.C. The Keypad.C code is as follows:

/************************************************* *******

4*4 keyboard scan function

Author: Sun Hao

Modified: 2010.8.2

Procedure description:

This program is partly modified by the development board routines, which is more convenient for program calls.

In the call before you need to pay attention to the need to do external variables declaration of the variables used in the program in the main function

The variables that need to be declared in the main function are as follows:

// Declaration of external variables

Extern unsigned char key_Pressed; //Button pressed: 1--yes, 0-- no

Extern unsigned char key_val; //store key value

Extern unsigned char key_Flag; // whether the key has been released: 1--yes, 0-- no

In addition, the corresponding key value can be modified in the global variable of the file, and only corresponding modification is needed.

The corresponding value of the array uchar key_Map[] is sufficient.

The call example is as follows:

Init_Keypad(); // Initialize first

While(1)

{

Key_Event(); //Keyscan in an infinite loop

If (key_Flag == 1)// used to determine whether the button is pressed to read the key value to operate

{

key_Flag = 0; // key_Flag needs to be manually cleared

}

}

************************************************** ********/

#include "msp430x14x.h"

// The corresponding function declaration

Void Init_Keypad(void);//Keyboard initialization

Void Check_Key(void);

Void delay();

Void Key_Event(void);//Read keyboard function, keyboard scan through this function when in use

//Select the corresponding port

#define KEYOUT P1OUT

#define KEYIN P1IN

#define KEYDIR P1DIR

Typedef unsigned char uchar;

Typedef unsigned int uint;

/***************Global Variables***************/

Uchar key_Pressed; // whether the key was pressed: 1--yes, 0-- no

Uchar key_val; //store key value

Uchar key_Flag; // whether the key has been released: 1--yes, 0-- no

/ / Set the keyboard logical key and program to calculate the key value mapping

Uchar key_Map[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};

/*******************************************

Function Name:Init_Keypad

Function: Initialize the IO port of the scan keyboard

Parameters: None

Return value: None

********************************************/

Void Init_Keypad(void)

{

KEYDIR = 0xf0; //KEY.0~KEY.3 is set to the input state, KEY.4~KEY.7 is set to the output state

KEYOUT |= 0xf0; // KEY.4~KEY.7 outputs high

key_Flag = 0;

key_Pressed = 0;

Key_val = 0;

}

/*********************************************

* Check_Key (), check the button, confirm the key value

*********************************************/

/*******************************************

Function Name: Check_Key

Function: Scan the IO port of the keyboard to get the key value

Parameters: None

Return value: None

********************************************/

Void Check_Key(void)

{

Uchar row ,col,tmp1,tmp2;

Tmp1 = 0x80;

For(row = 0;row 4;row++) //line scan

{

KEYOUT = 0xf0; // KEY.4~KEY.7 outputs all 1s

KEYOUT -= tmp1; // KEY.4~p1.7 output one of the four is 0

Tmp1 "" = 1;

If ((KEYIN & 0x0f) 0x0f) // Whether KEYIN has a 0 in KEY.0~KEY.3

{

Tmp2 = 0x01; // tmp2 is used to detect which bit is 0

For(col = 0;col 4;col++) // column detection

{

If((KEYIN & tmp2) == 0x00) // Whether this column is equal to 0 is

{

Key_val = key_Map[row * 4 + col]; // Get the key value

Return; // Exit the loop

}

Tmp2 "= 1; // tmp2 right shift 1 bit

}

}

}

}

/*******************************************

Function Name:delay

Function: delay about 15ms, complete the anti-shake function

Parameters: None

Return value: None

********************************************/

Void delay()

{

Uint tmp;

For(tmp = 12000;tmp ” 0;tmp--);

}

/*******************************************

Function Name:Key_Event

Function: Detect keys and get key values

Parameters: None

Return value: None

********************************************/

Void Key_Event(void)

{

Uchar tmp;

KEYOUT &= 0x00; // Set KEYOUT to 0 and wait for key input

Tmp = KEYIN; // Get p1IN

If ((key_Pressed == 0x00)&&((tmp & 0x0f) 0x0f)) //If there is a key pressed

{

key_Pressed = 1; // If there is a key press, set the key_Pressed ID

Delay(); //eliminates jitter

Check_Key(); // Call check_Key() to get key value

}

Else if ((key_Pressed == 1)&&((tmp & 0x0f) == 0x0f)) //If the button has been released

{

key_Pressed = 0; // Clear key_Pressed ID

key_Flag = 1; // Set the key_Flag flag

}

Else

{

_NOP();

}

}

The specific attention to caution has been explained in the comments, in the original program this file is divided into two files Keypad.C and Keypad.h, the keyboard scan function declaration is a header file alone, I think not need. The global variable in the source program is listed separately as a header file gdata.h. This can make the program more organized for reference in the case of more global variables.

Pneumatic Gear Motor

Pneumatic Motor,Pneumatic Gear Motor,Small Pneumatic Motor,Pneumatic Vibration Motor

RUDONG HONGXIN MACHINERY CO.,LTD , https://www.rdhxmfr.com