📄 main.c
字号:
//ICC-AVR application builder : 2007-4-17 8:26:01
// Target : M16
// Crystal: 7.3728Mhz
// www.avrvi.com AVR与虚拟仪器
// 最简单的键盘读取方式,直接读键盘对应脚的电平
// 硬件连接:
// PA0~PA3 —————— LED0~LED3
// PB0~PB3 —————— 独立按键A~D
// 实验效果:
// 按下键时,键盘对应的灯亮。
#include <iom16v.h>
#include <macros.h>
void port_init(void)
{
PORTA = 0x0F;
DDRA = 0x0F; //PA0~PA3输出
PORTB = 0x0F; //PB0~PB3使能内部上拉
DDRB = 0x00; //PB0~PB3为键盘输入,一定要设置为输入
PORTC = 0x00; //m103 output only
DDRC = 0x00;
PORTD = 0xFF;
DDRD = 0x05;
}
//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
}
//延时函数
void delay(void)
{
unsigned char i;
for(i=254;i!=0;i--)
{
asm("nop"); //空操作用于延时
}
}
void main(void)
{
unsigned char temp;
init_devices();
while(1)
{
temp=PINB;
delay(); //延时去抖
if(temp==PINB) //再次读键盘值,如果和temp相同,则为有效按键,继续下一步操作
{
PORTA=PINB; //把PINB口的值直接赋给PORTA
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -