//ICC-AVR application builder : 2015/5/5 23:40:48
// Target : T13
// Crystal: 9.6000Mhz
#include <iot13v.h>
#include <macros.h>
#define Led_on PORTB&=0xfe
#define Led_off PORTB|=0x01
#define key1 (PINB&0x08)
#define key2 (PINB&0x10)
#define Light (PINB&0x04)
#define uchar unsigned char
#define uint unsigned int
#define Mode_one_on 50
#define Mode_one_off 50
#define Mode_two_on 10
#define Mode_two_off 80
#define DJ 1000
uint time=0;
uchar mode=1;
uchar time_on;
uchar time_off;
static uchar shanshuo_time;
void Sleep(void)
{
Led_off;
GIMSK |= 0X60; //外部中断0使能
SEI();
MCUCR|=0x30; //掉电模式
asm("sleep");
}
/*************************************LED灯控制********************************/
void LED(void)
{
if(mode==1)
{
if(shanshuo_time<Mode_one_on)Led_on;
if((shanshuo_time>Mode_one_on)&&(shanshuo_time<(Mode_one_on+Mode_one_off)));Led_off;
if(shanshuo_time>=(Mode_one_on+Mode_one_off))
{
shanshuo_time=0;
}
}
if(mode==2&&Light!=0)
{
if(shanshuo_time<Mode_two_on)Led_on;
if((shanshuo_time>Mode_two_on)&&(shanshuo_time<(Mode_two_on+Mode_two_off)))Led_off;
if(shanshuo_time>=(Mode_two_on+Mode_two_off))
{
Led_off;
shanshuo_time=0;
}
}
if(mode==3)Sleep();
}
/*****************按键检查******************************/
uchar Keyscan(void)
{
uchar key_mode;
if(key1==0)key_mode=1;
if(key2==0)key_mode=2;
if(key1!=0&&key2!=0)key_mode=3;
return key_mode;
}
void port_init(void)
{
PORTB = 0x3F;
DDRB = 0x01;
}
//TIMER0 initialize - prescale:1024
// WGM: Normal
// desired value: 10mSec
// actual value: 9.920mSec (0.8%)
void timer0_init(void)
{
TCCR0B = 0x00; //stop
OCR0A = 0x5D;
OCR0B = 0x5D;
TCNT0 = 0xA3; //set count
TCCR0A = 0x00;
TCCR0B = 0x05; //start timer
}
#pragma interrupt_handler timer0_ovf_isr:iv_TIM0_OVF
void timer0_ovf_isr(void)
{
TCNT0 = 0xA3; //reload counter value
time++;
shanshuo_time++;
if(time>=DJ)Sleep();
}
#pragma interrupt_handler int0_isr:iv_INT0
void int0_isr(void)
{
//external interupt on INT0
time=0;
}
//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
timer0_init();
MCUCR = 0x01;
PCMSK=0X02;
TIMSK0 = 0x02; //timer interrupt sources
GIMSK = 0x60; //interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
void main(void)
{
init_devices();
while(1)
{
mode=Keyscan();
LED();
}
}
为何我的外部中断,会引起复位呢?实在搞不懂 |