program name : keyboard ;
Function description: Scan 16 keys (4X4) and display the key values on the digital tube (two digits);
Key points: When scanning the keys, a relatively complex two for() loop nested
scanning scheme is used, which greatly reduces the amount of program. The loop nesting scheme made me entangled for a long time.
The key values are 0~9, up, down, left, right, confirm, and clear;
Experience: Another milestone, tears are running... I seem to have become a code god...;
Experimental time: About 14 hours (2 hours per day X7 days);
Completion time: 2013-11-19 23:58:22
*/
#include
#include
#define uint8 unsigned char //macro definition of 8-bit data type;
#define uint16 unsigned int //macro definition of 16-bit data type;
// 0 , 1 , 2 , 3 , 4 , 5 , 6 ,
uint8 ak[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,
// 7 , 8 , 9 , up , down , left , right ;
0x07,0x7f,0x6f,0x62 ,0x54,0x21,0x0c};
uint8 ge=8,shi=8 ,si=0;
void bit(uint8 h) //digital tube bit selection enable function;
{
PORTB|=0X02; //digital tube bit selection set to 1 (transparent state);
if(h==0)
{
PORTB&=0XFD; //clear (latch state);
}
}
void dat(uint8 u) //digital tube data enable function;
{
PORTB|=0X01;
if(u==0)
{
PORTB&=0XFE;
}
}
void buzz(uint8 k) //Buzzer function;
{
if(k==0)
{
PORTC&=~(0X80);
}
else PORTC|=0X80;
}
void delay(uint8 j) //1=1000 counting cycles;
{
uint16 h=0;
while(j--)
{
for(h=0;h<1000;h++);
}
}
void mega16() //Initialize each IO port;
{
DDRA=0XFF; //PA output;
DDRB=0XFF; //PB output;
PORTA=0X00; //PA output all 0;
delay(1); //Delay, 1000 counting cycles;
bit(0); //Common cathode of digital tube all zero;
PORTA=0X3F; //The digital tube data port loads data and displays 0;
PORTB=1<<2; //Turn on the LED running light;
dat(0); //Latch the digital tube data port data, eight digital tubes display 0;
delay(30); //delay;
DDRD=0X0F; //PD port high four bits input, fourth bit output;
PORTD=0XFF; //PD input port internal pull-up, output port outputs all 1;
DDRC=0XFF; //PC port full output mode;
PORTC=0X00; //PC port outputs all zero;
}
void led()
{
static uint8 g=0;
PORTA=0Xff; //When inputting display data to the digital tube, all common cathodes must be set to 1 first,
//otherwise the digital tube will be shadowed;
bit(0); //Eight digital tubes have common cathodes set to 1 (low level is valid);
switch(g)
{ case 0: PORTA=ak[ge] ;dat(0);PORTA=0XFE;break;//digital tube ones;
case 1: PORTA=ak[shi];dat(0);PORTA=0XFD;break; //digital tube tens;
default: break; //otherwise...
}
bit(0);
g++;
if(g==2)
{
g=0;
}
}
[page]
shu(uint8 c)
{
if(c<=2) {c++;} //1~3;
else if(c==3) {ge=10;shi=10; return 0;} //up;
else if (c==11){ge=c ; shi=c; return 0;} //bottom;
else if(c==7) {ge=12,shi=12; return 0;} //left;
else if (c==15) {ge=13,shi=13; return 0;} //right;
else if(c==13){return 0;} //enter confirmation; ;
else if(c==14) {ge=0; shi=0; return 0;} //Clear;
else if((c>=8)&&(c<=10)){c-=1;} //7~9;
else if (c==12){ge=0;return 0;}
else { ;} //4~6;
ge=c; shi=0;
}
key()
{
uint8 i=0 ,y=0,temp=0x10 ,v=0,ke=0 ,en[]={0xfe,0xfd,0xfb,0xf7};
// temp is used to detect whether a key is pressed in each row; en array is a four-column detection;
for(v=0;v<4;v++) //v plus one represents a column detection;
{
PORTD=en[v]; //Detect four columns;
temp=0x10;
for(y=0;y<4;y++) //y plus one represents a key detection in a column;
{
if((PIND&temp)==0) //If a key is pressed, enter;
{
for(i=0;i<250;i++){led();} //Debounce delay and scan digital tube;
if((PIND&temp)==0) //Confirm again that a key is pressed;
{
buzz(1); //Buzzer on;
for(i=0;i<200;i++){led();} //Beeping delay;
for(i=0;i<200;i++){led();} //Beeping delay;
buzz(0); //Buzzer off;
//==============Generate key value part================
if(v==1)y+=3; //Detect the second line; //
else if(v==2)y+=6; //Detect the third line; //
else if(v==3)y+=9; //Detect the fourth line; //
else {;} //Detect the first line; //
ke=v+y; //Generate key value; //
//===========================================
shu(ke); // Send key value;
while((PIND&temp)==0)led(); //Wait for the button to pop up;
for(i=0;i<250;i++){led();} //Wait;
while((PIND&temp)==0)led(); //Confirm again whether the button pops up;
return 0; //End function;
}
}
temp=temp<<1; //Move left to detect the next button;
}
}
}
void main(void)
{
mega16(); //Initialize each IO port;
buzz(1); //Buzzer on;
delay(15); //Delay 15,000 counting cycles;
buzz(0); //Buzzer off;
PORTA=0xff; //Load all 1s;
bit(0); //Latch all 1 data in the common cathode of the digital tube (digital tube off);
delay(15); //Delay 15,000 counting cycles;
while(1)
{
led(); //Scan the digital tube;
key(); //Scan the keyboard;
}
}
Previous article:AVR microcontroller 8-bit timer LED accurate 1 second flashing program
Next article:AVR MCU external interrupt triggered water lamp program
- Popular Resources
- Popular amplifiers
- Siemens S7-1200-PLC Programming and Application Tutorial (3rd Edition) (Edited by Shi Shouyong)
- 西门子S7-12001500 PLC SCL语言编程从入门到精通 (北岛李工)
- Learning PLC is easy - PLC ladder diagram and statement table with pictures (Li Changjun, Zhou Hua)
- How to read electrical control circuit diagrams (Classic best-selling books on electronics and electrical engineering) (Zheng Fengyi)
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
- FAQ: PolarFire SoC FPGA Secure Boot | Microchip Security Solutions Seminar Series 12
- Some Problems on Measuring AC Current with Current Transformer
- Simulation of staying up late is harmful to health
- [RVB2601 Creative Application Development] + RTC Clock Display Experiment
- From traditional substation to smart substation
- [Raspberry Pi Pico Review] Xiaohui Review
- ST sensor evaluation platform STEVAL_MKI109V3 development board
- Analog Circuit Design Handbook: Advanced Application Guide
- How do you spend the May Day holiday?
- [Chuanglong Technology Allwinner A40i Development Board] Qt performance test