1. Code:
#include
typedef unsigned char u8;
typedef unsigned int u16;
sbit seg_sel = P1^4;
sbit bit_sel = P1^5;
#define DATA P0
u8 code seg_tab[]={
0xc0,0xf9,0xa4,0xb0,
0x99,0x92,0x82,0xf8,
0x80,0x90,0x88,0x83,
0xc6,0xa1,0x86,0x8e
};
u8 code bit_tab[]={
0x10,0x20,0x40,0x80,
0x01,0x02,0x04,0x08
};
void delay(u16 num)
{
u16 x,y;
for(x=num; x>0; x--)
for(y=110; y>0; y--)
{
;//延时1ms
}
}
void display_led(u8 which_bit, u8 which_number)
{
bit_sel = 1;//Q[7..0]=D[7..0]
DATA = bit_tab[which_bit];
bit_sel = 0;//BIT[7..0]=0x80
seg_sel = 1;//Q[7..0]=D[7..0]
DATA = seg_tab[which_number];
seg_sel = 0;
delay(2);
}
u8 key_scan (void)
{
u8 temp,temp2;
P2 = 0xf0;//让P2输出0xf0
temp = P2;//读入P2的值
if (temp != 0xf0) //当P2读入的值不等于0xf0
{
delay(5);//延迟5ms
temp = P2;//再读入P2口的值
if (temp != 0xf0)//如果P2读入的值不等于
{ //0xf0,就说明有按键按下
temp2 = temp & 0xf0;//保留P2读入值得高四位
P2 = 0x0f;//再让P2口输出0x0f
delay(1);
temp = P2;//再读入P2口的值
temp2 |= temp;//保留此读入值的低四位
return temp2;
}
//while(P2!=0x0f);
}
}
u8 encode(u8 cod)
{
u8 num;
switch(cod)
{
case 0xee: num = 0;break;
case 0xde: num = 1;break;
case 0xbe: num = 2;break;
case 0x7e: num = 3;break;
case 0xed: num = 4;break;
case 0xdd: num = 5;break;
case 0xbd: num = 6;break;
case 0x7d: num = 7;break;
case 0xeb: num = 8;break;
case 0xdb: num = 9;break;
case 0xbb: num = 10;break;
case 0x7b: num = 11;break;
case 0xe7: num = 12;break;
case 0xd7: num = 13;break;
case 0xb7: num = 14;break;
case 0x77: num = 15;break;
default : break;
}
return num;
}
void main (void)
{
u8 cod,num;
P0 = 0xff;
P1 = 0xff; //Port initialization
seg_sel = 0; //LE disable latch
bit_sel = 0;
while (1)
{
cod = key_scan(); //Judgement and encoding
num = encode(cod); //Decoding program. Get the number
display_led(1, num); //Display program
}
}
2. Circuit diagram:
3. Effect
Previous article:External interrupt experiment
Next article:LED digital tube display experiment
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- 【NXP Rapid IoT Review】+ Trial Computer Programming & Mobile Phone Control
- TMS320C6747 Fixed-point/Floating-point Digital Signal Processor
- Chip Manufacturing 7-Chip Manufacturing
- FPGA water lamp design vhdl source program
- USB packet capture module protocol
- The PWM wave output of TL494 in the boost circuit is abnormal. Please help me
- About the problem of AGND and DGND of microcontroller
- "Summer Benefits to Celebrate Dragon Boat Festival" Selected Resources for the Grab-a-Hot Event
- Reshaping the engineer experience: Tektronix invites you to attend the global launch of the new 2-series MSO at 2 pm on June 8 (today)!
- How to draw multiple boards with the same functional schematics on one PCB