?? main._c
字號(hào):
//ICC-AVR application builder : 2007-4-17 8:26:01
// Target : M16
// Crystal: 7.3728Mhz
// www.avrvi.com AVR與虛擬儀器
// 最簡(jiǎn)單的鍵盤讀取方式,直接讀鍵盤對(duì)應(yīng)腳的電平
// 硬件連接:
// PA0~PA3 —————— LED0~LED3
// PB0~PB3 —————— 獨(dú)立按鍵A~D
// 實(shí)驗(yàn)效果:
// 按下鍵時(shí),鍵盤對(duì)應(yīng)的燈亮。
#include <iom16v.h>
#include <macros.h>
void port_init(void)
{
PORTA = 0x0F;
DDRA = 0x0F; //PA0~PA3輸出
PORTB = 0x0F; //PB0~PB3使能內(nèi)部上拉
DDRB = 0x00; //PB0~PB3為鍵盤輸入,一定要設(shè)置為輸入
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
}
//延時(shí)函數(shù)
void delay(void)
{
unsigned char i;
for(i=254;i!=0;i--)
{
asm("nop"); //空操作用于延時(shí)
}
}
void main(void)
{
unsigned char temp;
init_devices();
while(1)
{
temp=PINB;
delay(); //延時(shí)去抖
if(temp==PINB) //再次讀鍵盤值,如果和temp相同,則為有效按鍵,繼續(xù)下一步操作
{
PORTA=PINB; //把PINB口的值直接賦給PORTA
}
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -