Microcontroller Exercise - Simulating a Phone Keypad

Publisher:彩虹微笑Latest update time:2016-12-29 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Because the course has a single-chip microcomputer, today I took out the TX-B development board I bought last year to play with it. I wrote a single-chip microcomputer program about row and column scanning.
Reference: Matrix keyboard detection

Connection circuit diagram of keyboard and single-chip microcomputer Connection

circuit diagram of 6-bit digital tube and single-chip microcomputer


Common cathode digital tube digital coding

The four rows of the matrix keyboard are connected to P3.0-P3.3 respectively, and the four columns are connected to P3.4-P3.7 respectively.


The code is written using "KeilC51v612", and the code is as follows:

Phone Keypad

  1#include

  2//Simulate the telephone keypad, press a key, and the corresponding value will be displayed. The rows and columns are arranged in the 3 x 4 way of ordinary telephones.

  3unsigned char t, key, a, count;

  4

  5sbit light8 = P1^7;

  6//Latch end of the latch that controls the digital tube segment selection. Latch after the falling edge, that is, first set dula = 1, then set dula = 0 to complete the latch

  7sbit dula=P2^6;  

  8//Latch end of the latch that controls the digital tube bit selection process is similar to dula

  9sbit wela=P2^7; 

 10

 11unsigned char code table[]={0x3f,0x06,0x5b,0x4f,0x66,

 12 0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71}; //0-F digital tube coding

 13void display(unsigned char num) //digital tube display function

 14{

 15 P0=table[num]; //Send the numth display code to the P0 port and then to the bus

 16 dula=1; //Generate a falling edge to latch the data

 17 dula=0;

 18 P0 = 0xc0;

 19 wela=1; //same as dula

 20 wela=0;

 twenty one}

 twenty two

 23//Get the key value by checking the columns. base is the value of the first column of each row. The rows and columns here are arranged in the 3 x 4 format of ordinary phones.

 24unsigned char getKey(unsigned char da, unsigned char base)

 25{

 26 switch(da)

 27 {

 28 case 0xe0: //Detect the first column

 29 return base;

 30 case 0xd0: //Detect the second column

 31 return base + 1;

 32 case 0xb0: //Detect the third column

 33 return base + 2;

 34 default : //Others return the value of the last press

 35 return key;

 36 }

 37}

 38

 39//Wait for the key to be released. If the key is still pressed, light up the LED 8.

 40void wait()

 41{

 42 while(t != 0xf0)

 43 {

 44 t = P3;

 45 t = t & 0xf0;

 46 light8 = 0;

 47 }

 48 light8 = 1;

 49}

 50

 51//Delay function, for example, if i=10, the delay is about 10ms.

 52void delay(unsigned char i)

 53{

 54 unsigned char j, k;

 55 for(j = i; j > 0; j--)

 56 {

 57 for(k = 125; k > 0; k--);

 58 }

 59}

 60

 61void main()

 62{

 63 dula = 0; //Initialize dula and wela

 64 wela = 0;

 65 while(1) 

 66 {

 67 a = 0xfe; //Check the first line first

 68 for(count = 0; count < 4; count ++)

 69 {

 70 P3 = a;

 71 t = P3 & 0xf0; //Read P3 port

 72 if(t != 0xf0) //Judge whether a key is pressed

 73 {

 74 delay(10); //delay 10ms to de-bounce

 75 t = P3 & 0xf0; // Check again whether a key is pressed

 76 if(t != 0xf0)

 77 {

 78 switch(count) //Determine which row has a key pressed

 79 {

 80 case 0:

 81 key = getKey(t, 1); //Get the key value of the first row

 82 break;

 83 case 1:

 84 key = getKey(t, 4);

 85 break;

 86 case 2:

 87 key = getKey(t, 7);

 88 break;

 89 case 3:

 90 if(t == 0xd0) //Check if a key is pressed in row 4, column 2

 91 key = 0;

 92 break;

 93 }

 94 wait();

 95 display(key);

 96 P1 = a; //Prompt which row has a key pressed

 97 }

 98 }

 99 a = (a << 1) | (a >> 7); //Continue to detect the next line, circularly shift left one bit to achieve

100 }

101 }

102}


Haha, my major is very strange, I have to master both software and hardware. I often can't grasp the direction, so I have to learn both. But I still think software is more suitable for me.
Thanks to Brother Guo's experimental board, it is also with this board that I can quickly develop a great interest in single-chip microcomputers.

 Functional diagram of TX-1B experimental board (currently replaced by TX-1C)


Reference address:Microcontroller Exercise - Simulating a Phone Keypad

Previous article:Principle and example program of 12864 dot matrix LCD display module (HJ12864M-1)
Next article:MCU Exercise - AD Conversion

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号