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

📄 中断和端口.c

📁 AVR单片机中断操作
💻 C
字号:
#include <iom162v.h>//包含ATMEGA162的寄存器定义,和中断向量表
#include <macros.h>
#define uint unsigned int
unsigned int  i=0;
void Delay(uint n)
{
  
   while(n--)
   for(i=0;i<800;i++);    
} 
void port_init(void)
{
 PORTA = 0xFE;
 DDRA  = 0x01;
 PORTB = 0xFF;
 DDRB  = 0x00;
 PORTC = 0xFF; 
 DDRC  = 0x00;
 PORTD = 0x0C; //使INT0,INT1对应口上拉电阻有效
 DDRD  = 0x00; //必须设置INT0,INT1对应口为输入
}

#pragma interrupt_handler int0_isr:2
void int0_isr(void)
{
 //external interupt on INT0
 i++;  PORTB=~(1<<PB1); //在中断里进行操作
}

#pragma interrupt_handler int1_isr:3
void int1_isr(void)
{
 //external interupt on INT1
 PORTA = 0x01; PORTB=(1<<PB1);     //在中断里进行操作  PORTA =(1<<PA1);
}

//call this routine to initialize all peripherals
void init_devices(void)
{
 //stop errant interrupts until set up
 CLI(); //disable all interrupts
 port_init();

 MCUCR = 0x08;  //INT1 的下降沿产生异步中断请求,INT0上升延
 GICR  = 0xC0;  //INT0和INT1使能
 TIMSK = 0x00; //timer interrupt sources
 SEI(); //re-enable interrupts
 //all peripherals are now initialized
}

void main(void)
{
 init_devices();
 PORTB=0x02;//输出为1;
 DDRB=0x02;//PORTB.1为输出方式;
/*
或者
PORTB=(1<<PB1);//输出为1;
DDRB=(1<<PB1);//PORTB.1为输出方式;
*/  
PORTD=(1<<PD1);//输入上拉;
DDRD=~(1<<PD1);//PORTD.1为输入方式;
while (1)
      {
      if(PIND&(1<<PD1))//读取管脚状态
      {
        PORTB&=~(1<<PB1);// PORTB.1输出为0 
        Delay(20); 
        PORTB=(1<<PB1);// PORTB.1输出为1
        Delay(20);
      }
      else
      {
       PORTB=(1<<PB1);// PORTB.1输出为1 
      }
      }
}

⌨️ 快捷键说明

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