MCU + 8255 keyboard scanning c51 program

Publisher:不懂之人Latest update time:2012-09-08 Source: 51heiKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

The following is the circuit diagram: The keyboard scanning function of the 51 single-chip microcomputer + 8255 chip only does simple input and output, and uses the P1 port to control the light-emitting diode. If the key value is 7, the key code is 0x07;
Click to browse the next page

Source program:

#include
#include
#define uchar unsigned char
#define uint unsigned int
#define LScan XBYTE[0x7f00] //column scan address
#define HScan XBYTE[0x7f02] //row scan address

//Delay function
void delay(uint i)
{uint j;
  for(j=i;j>0;j--)
   { ; }
}

//Function to detect whether a key is pressed
uchar CheckKey() //Returns 0xff if a key is pressed, otherwise returns 0
{ uchar i;
 LScan = 0x00;
  i=(HScan & 0x0f);
  if(i==0x0f) return(0);
  else return(0xff);
 }
//********Keyboard scanning subfunction***********
uchar KeyScan()          
{ //If no key is pressed, return oxff; if yes, return the key code        
    uchar ScanCode;
 uchar CodeValue;
 uchar k;
 uchar i,j;
 if(CheckKey()==0) return(0xff); //No key pressed, return 0xff
 else
  {
   delay(200); //delay
   if(CheckKey()==0)
     return(0xff); //No key pressed, return 0xff
   else
    {
     ScanCode=0x01; //Set the column scan code, the initial value of the lowest bit is 0
     for(i=0;i<8;i++) // Scan column by column 8 times
      {
       k=0x01; //Assign initial value to row scan code
       LScan=~ScanCode; //Send column scan code
       CodeValue=i; //The key code is the value of i. The key code of each column in the zeroth row is 0, 1, 2, ... 7, which is consistent with the value of i
       for(j=0;j<4;j++)
        {
         if((HScan & k) ==0) //Is it in the current column?
          {
           while(CheckKey()!=0); //If yes, wait for the key to be released
           return(CodeValue); //Return key code
          }
         else //Otherwise, the key code is increased by 8, and the key codes on each row of the same column differ by exactly 8
          { // Shift column scan code k right one position and scan the next row
           CodeValue+=8;
           k<<=1;
          }
                   
         }
         ScanCode<<=1; //After scanning each row, the column scan code is shifted right by one position and the next column is scanned
      }   
         }
  }
}


 main()
 { uchar Key;
       P1=0x00;
                //Initialize, turn off the digital tube first
  XBYTE[0x7f03]=0x81; //8255 initialization, set port A output, port C low 4 bits input
  while(1)
   {Key=KeyScan();
      if(Key!=0xff)
     P1=Key;
   }
}
Keywords:MCU Reference address:MCU + 8255 keyboard scanning c51 program

Previous article:12864-ST7920 LCD Graphics Introduction
Next article:Design of electronic speed and mileage anti-theft alarm

Recommended ReadingLatest update time:2024-11-16 19:56

Knowledge of microcontroller interrupts
To put it simply, interrupts are to make the CPU of the microcontroller suspend the current task and switch to the task that caused the interrupt. When I first started learning microcontrollers, I was very reluctant to learn about interrupts because I had to remember a lot of register-related things such as IE, IP,
[Microcontroller]
How to "build a conversation" between different types of microcontrollers? From these examples
Communication methods between several commonly used microcontrollers ① Use hardware UART for asynchronous serial communication. This is an effective and reliable communication method that takes up less port lines; but unfortunately, many small microcontrollers do not have hardware UART, and some only have one UART.
[Microcontroller]
Circuit design of GSM module (TC35I) controlled by single chip microcomputer
Although both the MCU and TC35I module are TTL level, the TTL positive level logic of the TC35 module input and output is not +5V, but +2.9V, but the TTL positive level logic of the MCU input and output is +5V. Therefore, if you directly connect the RX, TX of TC35I to the TX, RX of the MCU, of course, you also need to
[Microcontroller]
Study on Watchdog in Single Chip Microcomputer
Software reliability has always been a key issue. Anyone who uses software may experience computer crashes or program runaway problems, and this situation also exists in embedded systems. Due to the limited anti-interference ability of single-chip microcomputers, in industrial field instruments and meters, crashes are
[Microcontroller]
Study on Watchdog in Single Chip Microcomputer
Analysis of ISP Solution for AT89S Series MCU
    1. ISP Overview     At present, technicians engaged in the design and development of single-chip microcomputers generally use the following common methods to develop single-chip microcomputer products: first, use single-chip microcomputer simulation equipment to simulate and debug hardware and software, and then u
[Microcontroller]
Analysis of ISP Solution for AT89S Series MCU
This automotive-grade MCU manufacturer launches ISO26262-ASIL certified new products to expand into the global automotive electronics market
In the past two years, the trend of smart electric vehicles has been unstoppable, which has greatly increased the demand for MCU products. This coupled with the wave of local supply has driven domestic automotive MCU manufacturers to continuously improve their technical capabilities to expand the growth space of domes
[Automotive Electronics]
This automotive-grade MCU manufacturer launches ISO26262-ASIL certified new products to expand into the global automotive electronics market
Reasons why the 51 single chip microcomputer cannot oscillate normally
How to ensure that the crystal oscillator starts oscillating normally? 1. There are many ways to judge. Using an oscilloscope to view the waveform is the most direct. You can also use the voltage range of a digital multimeter to measure the voltage. Because the duty cycle of the crystal oscillator waveform is 50%, t
[Microcontroller]
Reasons why the 51 single chip microcomputer cannot oscillate normally
Communication experiment between computer and single chip microcomputer
  This article is about the application of the communication experiment between computers and single-chip microcomputers. It mainly includes the explanation of the experimental principle, hardware circuit diagram, source program, etc.   Contents:   1. Application of MCU serial communication   2. PC controls the M
[Microcontroller]
Communication experiment between computer and single chip microcomputer
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号