📄 外部中断.c
字号:
//ICC-AVR application builder : 2009-3-10 17:06:48
// Target : M8
// Crystal: 8.0000Mhz
#include <iom8v.h>
#include <macros.h>
unsigned char i=0;
void port_init(void)
{
PORTB = 0x00;
DDRB = 0x00;
PORTC = 0x00; //m103 output only
DDRC = 0x00;
PORTD = 0x00;
DDRD = 0x00;
}
//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
}
/************************************
用 途:微秒级延时程序
Taget :mega8
crystal :8M
介 绍:在8M的晶振上进行us级的延时
入口参数:
*************************************/
void delay_us(int time)
{
do
{
time--;
}
while (time > 1);
}
/************************************
用 途:两位数码管显示一个数
Taget :mega8
crystal :8M
介 绍:共阳数码管
1-PC1(片选)
2-PC0
-----
a-PB0(数据)
b-PB1
...
h-PB6
DP-PB7
入口参数:要显示的数,十进制表示
*************************************/
const unsigned char num[16]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
void show_2_digit(unsigned char digi)
{
unsigned char i;
DDRC=0xff;
DDRB=0xff;
PORTC=0;//关片选
PORTB=~num[(unsigned char )(digi/10)];//显示十位
PORTC=(0x1<<1);//开十位的显示
delay_us(200);
PORTC=0;//关显示
PORTB=~num[(unsigned char )(digi%10)];//显示个位
PORTC=(0x1<<0);//开个位的显示
delay_us(200);
PORTC=0x0;//关显示
}
/************************************
用 途:打开外部中断
介 绍:
入口参数:
出口参数:
*************************************/
void int_init()
{
CLI();
PORTD|=(1<<PD2)|(1<<PD3);
MCUCR|=(1<<ISC11)|(1<<ISC01);//3-0,外部中断定1,0的控制方式
//MCUCR=3-2,0是低电平中断,1是下(降沿)和上(升沿)中断,2是下中断,3是上中断
GICR|=(1<<INT0)|(1<<INT1);//7,6外部中断1,0使能
SEI();
}
/************************************
用 途:中断0服务子程序
介 绍:
入口参数:
出口参数:
*************************************/
#pragma interrupt_handler init_0:2
void init_0()
{
i--;
if (i==0)i=99;
}
/************************************
用 途:中断1服务子程序
介 绍:
入口参数:
出口参数:
*************************************/
#pragma interrupt_handler init_1:3
void init_1()
{
i++;
if (i==100)i=0;
}
void main()
{
port_init();
init_devices();
int_init();//中断初始化
while(1)
{
show_2_digit(i);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -