📄 一般按键.c
字号:
//ICC-AVR application builder : 2009-3-10 17:31:43
// Target : M8
// Crystal: 8.0000Mhz
#include <iom8v.h>
#include <macros.h>
void port_init(void)
{
PORTB = 0x00;
DDRB = 0x00;
PORTC = 0x00; //m103 output only
DDRC = 0x00;
PORTD = 0x00;
DDRD = 0x00;
}
//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
MCUCR = 0x00;
GICR = 0x00;
TIMSK = 0x00; //timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
/************************************
用 途:微秒级延时程序
Taget :mega8
crystal :8M
介 绍:在8M的晶振上进行us级的延时
入口参数:
*************************************/
void delay_us(int time)
{
do
{
time--;
}
while (time > 1);
}
/************************************
用 途:两位数码管显示一个数
Taget :mega8
crystal :8M
介 绍:共阳数码管
1-PC1(片选)
2-PC0
-----
a-PB0(数据)
b-PB1
...
h-PB6
DP-PB7
入口参数:要显示的数,十进制表示
*************************************/
const unsigned char num[16]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
void show_2_digit(unsigned char digi)
{
unsigned char i;
DDRC=0xff;
DDRB=0xff;
PORTC=0;//关片选
PORTB=~num[(unsigned char )(digi/10)];//显示十位
PORTC=(0x1<<1);//开十位的显示
delay_us(200);
PORTC=0;//关显示
PORTB=~num[(unsigned char )(digi%10)];//显示个位
PORTC=(0x1<<0);//开个位的显示
delay_us(200);
PORTC=0x0;//关显示
}
/************************************
用 途:单键扫描程序
Taget :mega8
crystal :8M
介 绍:用的是D的前四个口,共有四个按键
入口参数:
出口参数:为0表示没有按键(1,2,3,4)
*************************************/
unsigned char key_scan()
{
unsigned char k,j=0;
PORTD|=0x0f;
k=PIND;
k=k&0x0f;
if(k==0x0e)
j=1;
else if(k==0x0d)
j=2;
else if(k==0x0b)
j=3;
else if (k==0x07)
j=4;
return j;
}
void main()
{
port_init();
init_devices();
while(1)
{
show_2_digit(key_scan());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -