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

📄 main

📁 用AVR实现简单按键的程序与proteus的结合
💻
字号:
#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 + -