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

📄 main.c

📁 一个关于AVR单片机的例程
💻 C
字号:

//ICC-AVR application builder : 2007-1-29 10:04:38
// Target : M16
// Crystal: 7.3728Mhz
// 用途:演示通过PINB读按键的值,赋值给PORTA,键盘扫描模型。
// 作者:古欣
// AVR与虚拟仪器 [url]http://www.avrvi.com[/url]
// 使用7.3728M外部晶振,请短接跳线JP2的1和2,电源跳线连接3.3V或者5V
// 开发板连接:LED0~3——PA0~3,独立按键A~D接PB0~3

#include <iom16v.h>
#include <macros.h>

#define led_port PORTA
//定义输出端口
#define led_ddr DDRA
//定义输出控制寄存器
#define key_PORT PORTB
#define key_DDR DDRB



void port_init(void)
{
PORTA = 0x0F;  //LED灭
DDRA = 0x00;
PORTB = 0xFF; //按键的端口为输入
DDRB = 0x00;
PORTC = 0x00; //m103 output only
DDRC = 0x00;
PORTD = 0x00;
DDRD = 0x00;
led_ddr=0xff; //设置LED的端口为输出
key_PORT=0x0F; //使能按键的IO口的内部上拉
}

//call this routine to initialize all peripherals
//此处为ICC系统生成,未做更改
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,j;
/*
for(i=20;i>0;i--)
{
for(j=200;j>0;j--)
;
}
*/
;
}

void main(void)
{
unsigned char tmp=0; //定义变量
init_devices();     //初始化
while(1)
{
  tmp=(PINB&0x0F); //与0x0F 只取低四位的值
  if(tmp!=0x0F)
  {
  Delay();     //延时去抖
  if((PINB&0x0F)==tmp) //再次读IO口,如果和上次的值相等,则说明确实是键盘按下
  {
  PORTA = tmp&0x0F; //将键盘的值输出到PORTA上
  }
  }
Delay();
}
}

⌨️ 快捷键说明

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