Normal method:
#include
#include
#define uchar unsigned char//
#define uint unsigned int//
#define setb(val,bitn) (val|=(1<<(bitn)))//Set a bit of an IO port to 1
#define clr(val,bitn) (val&=~(1<<(bitn)))//Set a bit of an IO port to 0
#define get(val,bitn) (val&(1<<(bitn)))//Read a bit status such as if(get(PORTC,7)) determines whether PC7 port is 1
#pragma data:code//Array storage location
const table[]={
0xc0,0xf9,0xa4,0xb0,0x99,
0x92,0x82,0xf8,0x80,0x90,
0x88,0x83,0xc6,0xa1,0x86,
0x8e
};//Common anode digital tube 0--9 code
uchar num,yin,temp;//Key value code and identification code
void delay(uchar);//Delay function
void disy();//Display function
void skk()
{
for(yin=0;yin<2;yin++)
{
PORTD=0xfe;//Set the first line to 0, that is, detect the first line
temp=PIND;//Give the value to tem
temp=temp&0xf0;//AND gate with 0xf0,
while(temp!=0xf0)//Judge whether it is 0xf0
{
delay(10);//Delay
temp=PIND;//Give P1 value to tem
temp=temp&0xf0;//AND gate with the value again
while(temp!=0xf0)//Judge whether it is 0xf0 again
{
temp=PIND;//It is indeed pressed, and the tem value is restored so that the switch statement can be judged next time. Otherwise, there is no corresponding value. For example, if it was ee before, if the second key is pressed, it will not be de
switch(temp)//switch statement
{
case 0xee:num=0;//Compare with num value to see if they are equal. If they are equal, exit the switch statement
break;//Exit the switch statement
case 0xde:num=1;//Compare with num value to see if they are equal. If they are equal, exit the switch statement
break;//Exit the switch statement
case 0xbe:num=2;//Compare with num value to see if they are equal. If they are equal, exit the switch statement
break;//Exit the switch statement
case 0x7e:num=3;//Compare with num value to see if they are equal. If they are equal, exit the switch statement
break;//Exit the switch statement
}
while(temp!=0xf0)//Release the judgment to exit the above two while
{
temp=PIND;//assign value to temp
temp=temp&0xf0;//AND gate
}
}
}
}
for(yin=0;yin<2;yin++)
{
PORTD=0xfd;//set the first line to 0, that is, detect the first line
temp=PIND;//assign value to tem
temp=temp&0xf0;//AND gate with 0xf0,
while(temp!=0xf0)//judge whether it is 0xf0
{
delay(10);//delay
temp=PIND;//assign P1 value to tem again
temp=temp&0xf0;//AND gate with 0xf0
while(temp!=0xf0)//judge whether it is 0xf0 again
{
temp=PIND;//it is indeed pressed, put the tem value back, so as to judge the switch statement next time, otherwise there is no corresponding value, such as ee originally, if the second key is pressed, it will not be de
switch(temp)//switch statement
{
case 0xed:num=4;//Compare with num value to see if they are equal, if they are equal, exit switch statement
break;//Exit switch statement
case 0xdd:num=5;//Compare with num value to see if they are equal, if they are equal, exit switch statement
break;//Exit switch statement
case 0xbd:num=6;//Compare with num value to see if they are equal, if they are equal, exit switch statement
break;//Exit switch statement
case 0x7d:num=7;//Compare with num value to see if they are equal, if they are equal, exit switch statement
break;//Exit switch statement
}
while(temp!=0xf0)//Release judgment to exit the above two while
{
temp=PIND;//Assign value to temp
temp=temp&0xf0;//AND gate
}
}
}
}
for(yin=0;yin<2;yin++)
{
PORTD=0xfb;//Set the first line to 0, that is, check the first line
temp=PIND;//Assign value to tem
temp=temp&0xf0;//AND gate with 0xf0,
while(temp!=0xf0)//Judge whether it is 0xf0
{
delay(10);//Delay
temp=PIND;//Assign P1 value to tem
temp=temp&0xf0;//AND gate with value again
while(temp!=0xf0)//Judge whether it is 0xf0 again
{
temp=PIND;//It is indeed pressed, and the tem value is restored so that the switch statement can be judged next time. Otherwise, there is no corresponding value. For example, if it was ee before, if the second key is pressed, it will not be de
switch(temp)//switch statement
{
case 0xeb:num=8;//Compare with num value to see if they are equal. If they are equal, exit the switch statement
break;//Exit the switch statement
case 0xdb:num=9;//Compare with num value to see if they are equal. If they are equal, exit the switch statement
break;//Exit the switch statement
case 0xbb:num=10;//Compare with num value to see if they are equal. If they are equal, exit the switch statement
break;//Exit the switch statement
case 0x7b:num=11;//Compare with num value to see if they are equal. If they are equal, exit the switch statement
break;//Exit the switch statement
}
while(temp!=0xf0)//Release the judgment to exit the above two while
{
temp=PIND;//Assign value to temp
temp=temp&0xf0;//AND gate
}
}
}
}
for(yin=0;yin<2;yin++)
{
PORTD=0xf7;//Set the first line to 0, i.e., check the first line
temp=PIND;//Assign value to tem
temp=temp&0xf0;//AND gate with 0xf0,
while(temp!=0xf0)//Judge whether it is 0xf0
{
delay(10);//Delay
temp=PIND;//Assign P1 value to tem again
temp=temp&0xf0;//AND gate with 0xf0
while(temp!=0xf0)//Judge whether it is 0xf0 again
{
temp=PIND;//It is indeed pressed, put the tem value back, so as to judge the switch statement next time, otherwise there is no corresponding value, such as ee originally, if the second key is pressed, it will not be de
switch(temp)//Switch statement
{
case 0xe7:num=12;//Compare with num value to see if they are equal, if they are equal, exit switch statement
break;//Exit switch statement
case 0xd7:num=13;//Compare with num value to see if they are equal, if they are equal, exit switch statement
break;//Exit switch statement
case 0xb7:num=14;//Compare with num value to see if they are equal, if they are equal, exit switch statement
break;//Exit switch statement
case 0x77:num=15;//Compare with num value to see if they are equal, if they are equal, exit switch statement
break;//Exit switch statement
}
while(temp!=0xf0)//Release judgment to exit the above two while
{
temp=PIND;//Assign value to temp
temp=temp&0xf0;//AND gate
}
}
}
}
}[page]
void main()
{
DDRD|=0xff;//Set PB port as output
PORTD|=0xff;//PB output is high potential
DDRD&=0x0f;//Set high
while(1)//Large loop
{
skk();
disy();//Call display subroutine
}
}
void delay(uchar z)//Delay subfunction
{
uchar x,y;
for(x=100;x>0;x--)
for(y=z;y>0;y--);
}
void disy()//Display subfunction
{
DDRC=0xff;
DDRB=0xff;
PORTB=table[num];
PORTC=0x00;
}
Simple method:
#include
#include
#define uchar unsigned char
#define uint unsigned int
#pragma data:code//Define the program to be stored in ROM
const table[]={
0xc0,0xf9,0xa4,0xb0,0x99,
0x92,0x82,0xf8,0x80,0x90,
0x88,0x83,0xc6,0xa1,0x86,
0x8e,0xff
};
/***************************************************/
/*******************Delay subroutine******************/
/***************************************************/
void delay(uchar z)
{
uchar x,y;
for(x=0;x<50;x++)
for(y=0;y
/***************************************************/
/*******************Display subroutine******************/
/***************************************************/
void xian(uchar num)
{
uchar i;
PORTB=table[num];//ten digits
PORTC=0x00;
}
/***************************************************/
/***************Key press processing procedure***************/
/***************************************************/
uchar key_press()
{
uchar i;
DDRD=0XFF;//Set PD port to output
PORTD=0XF0;//Set the upper four bits of PD to high level for judgment
DDRD=0X0F;//Set the upper 4 bits to input and the lower 4 bits to output
i=PIND;//Read the IO port status
if(i==0XF0)//If it is equal to 0XF0, return 0
{
DDRD=0XFF;//Read the IO port value return
0;
}
else//Otherwise it will return 1
{
DDRD=0XFF;
return 1;
}
}
/***********************************************/
/***************Key value processing program***************/
/***************************************************/
uchar key_scan()
{
uchar key,i=0X7F,j;//Set variable I to 01111111B
if(key_press())//Judge whether the key is pressed
{
delay(10);//If it is pressed, eliminate the bounce
if(key_press())//Judge again whether the key is pressed
{
do//Execute once first
{
i=(i<<1|i>>7);/*Shift the content of variable I left by 1 bit, 11111110
and then shift I right by 7 bits, 11111110, that is, 11111111
11111101 11111011 11110111*/
PORTD=i;//Send the shifted value to the PD port
DDRD=0X0F;//Set the high bit of the PD port as input and the low bit as output
key=PIND;//Read the high bit value of the IO port
j=key&0XF0;//Only need to read the high 4 bits
}
while(j==0XF0);//If the key is not pressed, execute the above do statement
while(key_press());//If it is pressed, release it
switch(key)//If a key is pressed, determine which value it is
{
case 0xEE:
key=0x0;
break;
case 0xDE:
key=0x1;
break;
case 0xBE:
key=0x2;
break;
case 0x7E:
key=0x3;
break;
case 0xED:
key=0x4;
break;
case 0xDD:
key=0x5;
break;
case 0xBD:
key=0x6;
break;
case 0x7D:
key=0x7;
break;
case 0xEB:
key=8;
break ;
case 0xDB:
key=9;
break;
case 0xBB:
key=0xA;
break;
case 0x7B:
key=0xB;
break;
case 0xE7:
key=0xC;
break;
case 0xD7:
key=0xD;
break;
case 0xB7:
key =0xE;
break;
case 0x77:
key=0xF;
break;
}
}
}
return key;//return key value
}
/**************************** ************************/
/************************Main program*** ******************/
/****************************** *******************/
void main()
{
uchar ik=16;//define local variable
DDRB=0xff;//set PB port as output
DDRC=0xff;//set PC port as output
while(1)
{
ik=key_scan();//set the key return value to the variable IK
if(ik<16)//If the return value is less than 16, it will be displayed on the digital tube
xian(ik); //Call the display subroutine
else//If the return value is not less than 16, a black screen will be displayed
xian(16);
}
}
Previous article:AVR MCU timer count
Next article:AVR microcontroller password lock program (original)
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- ASML predicts that its revenue in 2030 will exceed 457 billion yuan! Gross profit margin 56-60%
- Detailed explanation of intelligent car body perception system
- How to solve the problem that the servo drive is not enabled
- Why does the servo drive not power on?
- What point should I connect to when the servo is turned on?
- How to turn on the internal enable of Panasonic servo drive?
- What is the rigidity setting of Panasonic servo drive?
- How to change the inertia ratio of Panasonic servo drive
- What is the inertia ratio of the servo motor?
- Is it better for the motor to have a large or small moment of inertia?
- Learn about C2000 32-bit microcontrollers
- Migrate ssh service to EK200-zlib-openssl-openssh
- Cost less than 5 yuan, reliable single-fire power supply solution (using NP101A chip, including PDF...
- Mobile station development board TI MSP430FR5969LaunchPad is here!
- ADS LNA
- Summary and sharing of PCB files imported into BRD in AD software,,,,
- 【micropython】How to use serial port interrupt
- Share: Solution to the problem that the PLL dies when the DSP debugging of TMS320C6713 is enabled
- Basic sample code for microcontrollers using C/C++
- [RTT & Renesas high-performance CPK-RA6M4] After FSP, the board_cfg.h header file cannot be found and the Jlink driver is missing