main
来自「用AVR实现简单按键的程序与proteus的结合」· 代码 · 共 53 行
TXT
53 行
#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 + =
减小字号Ctrl + -
显示快捷键?