⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 key.c

📁 AVR 门锁的程序 包含与模块通讯 密码按键
💻 C
字号:
#include "Include.h"

void KeyInit(void)
{
	//PB输出0
	DDRB=0xFF;
	PORTB=0;

	//PD2 PD3为带上拉输入
	PORTD|=0x0C;
	Delay1mS();

	//INT0 INT1设置为低电平中断
	//MCUCR=0x0;

	//允许INT0 INT1 中断
	//GICR|=0xC0;
}

volatile unsigned char NowKey=KEY_N;
volatile unsigned char isHaveKey=0;

//INT0中断
//PB2 6
//PB3 7
//PB4 8
//PB5 9
//PB6 * B
//PB7 # A
SIGNAL(SIG_INTERRUPT0)
{
	GICR&=0x3F;

	DelayNmS(20);//延时

	if(bit_is_set(PIND,2))
	{
		return;
	}
	
	PORTB=0x7F;

	Delay1uS();
	if(bit_is_clear(PIND,2))
	{
		NowKey=KEY_6;
		isHaveKey=1;
		goto INT0End;
	}
	PORTB=PORTB>>1;

	Delay1uS();
	if(bit_is_clear(PIND,2))
	{
		NowKey=KEY_N;
		isHaveKey=1;
		goto INT0End;
	}
	PORTB=PORTB>>1;

	Delay1uS();
	if(bit_is_clear(PIND,2))
	{
		NowKey=KEY_N;
		isHaveKey=1;
		goto INT0End;
	}
	PORTB=PORTB>>1;

	Delay1uS();
	if(bit_is_clear(PIND,2))
	{
		NowKey=KEY_B;
		isHaveKey=1;
		goto INT0End;
	}
	PORTB=PORTB>>1;

	Delay1uS();
	if(bit_is_clear(PIND,2))
	{
		NowKey=KEY_8;
		isHaveKey=1;
		goto INT0End;
	}
	PORTB=PORTB>>1;

	Delay1uS();
	if(bit_is_clear(PIND,2))
	{
		NowKey=KEY_A;
		isHaveKey=1;
		goto INT0End;
	}

INT0End:
	PORTB=0;

	GIFR|=0xC0;//清中断标志
}

//INT1中断
//PB0 Door
//PB1 Reset
//PB2 0
//PB3 1
//PB4 2
//PB5 3
//PB6 4
//PB7 5
SIGNAL(SIG_INTERRUPT1)
{
	GICR&=0x3F;

	DelayNmS(20);//延时

	if(bit_is_set(PIND,3))
	{
		return;
	}
	
	PORTB=0x7F;

	Delay1uS();
	if(bit_is_clear(PIND,3))
	{
		NowKey=KEY_3;
		isHaveKey=1;
		goto INT1End;
	}
	PORTB=PORTB>>1;

	Delay1uS();
	if(bit_is_clear(PIND,3))
	{
		NowKey=KEY_2;
		isHaveKey=1;
		goto INT1End;
	}
	PORTB=PORTB>>1;

	Delay1uS();
	if(bit_is_clear(PIND,3))
	{
		NowKey=KEY_1;
		isHaveKey=1;
		goto INT1End;
	}
	PORTB=PORTB>>1;

	Delay1uS();
	if(bit_is_clear(PIND,3))
	{
		NowKey=KEY_N;
		isHaveKey=1;
		goto INT1End;
	}
	PORTB=PORTB>>1;

	Delay1uS();
	if(bit_is_clear(PIND,3))
	{
		NowKey=KEY_0;
		isHaveKey=1;
		goto INT1End;
	}
	PORTB=PORTB>>1;

	Delay1uS();
	if(bit_is_clear(PIND,3))
	{
		NowKey=KEY_B;
		isHaveKey=1;
		goto INT1End;
	}
	PORTB=PORTB>>1;
	
        Delay1uS();
	if(bit_is_clear(PIND,3))
	{
		NowKey=KEY_R;
		isHaveKey=1;
		goto INT1End;
	}
INT1End:
	PORTB=0;
	GIFR|=0xC0;//清中断标志
}

//等待按键,测试使用,没有超时
void WaitKey(void)
{
	GICR|=0xC0;//打开中断
	while(0==isHaveKey) {;}//等待按键
	loop_until_bit_is_set(PIND,3);
	loop_until_bit_is_set(PIND,2);
	DelayNmS(10);
	isHaveKey=0;
}

unsigned char GetKey(void)
{
	//打开INT0 INT1中断
	GICR|=0xC0;
	Timer=TIME_WAIT_KEY;
	while(0==isHaveKey)
	{
		if(0==Timer)
		{
//			LcdPrint(0xFF);

			return 0;
		}
	}	
	isHaveKey=0;

	loop_until_bit_is_set(PIND,3);
	loop_until_bit_is_set(PIND,2);
	//关闭INT0 INT1中断
	GICR&=0x3F;
	DelayNmS(10);
        
        //Key测试
        //LedGreenOn(NowKey);

	return 1;
}

void ShowKey(unsigned char t)
{
        LedGreenOn(t);
        BellOn();
        DelayNmS(BELL_LONG_TIME);
        BellOff();
        LedOff();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -