TINY85A一开全局中断就响应INT0请有时间看的朋友分析一下
编译器是CodeWizardAVR,搭建了一个简单的最小化INT0实验,那些端口移位都是用来观察效果的。软件仿真我发现只要一开全局中断就进入INT0中断服务程序了,我的INTO是设置成低电平触发呢!怎么还没等我触发就进入INT0中断服务程序了呢?实际硬件测试也是这样的效果,请有时间看的朋友帮忙分析一下吧,谢谢!这是完整文件/*****************************************************
This program was produced by the
CodeWizardAVR V2.05.1b Evaluation
Automatic Program Generator
?Copyright 1998-2011 Pavel Haiduc, HP InfoTech s.r.l.
http://www.hpinfotech.com
Project :
Version :
Date : 2014-5-7
Author: Freeware, for evaluation and non-commercial use only
Company :
Comments:
Chip type : ATtiny85
AVR Core Clock frequency: 8.000000 MHz
Memory model : Small
External RAM size : 0
Data Stack size : 128
*****************************************************/
#include <tiny85.h>
#include<delay.h>
// External Interrupt 0 service routine
interrupt void ext_int0_isr(void)
{
// Place your code here
PORTB&=~((1<<3)|(1<<4));
//delay_ms(1000);
PORTB|=(1<<3)|(1<<4);
//delay_ms(1000);
PORTB&=~((1<<3)|(1<<4));
//delay_ms(1000);
PORTB|=(1<<3)|(1<<4);
//delay_ms(1000);
GIMSK=0x00; //禁用INT0中断,等合适的时候再打开
}
// Declare your global variables here
void main(void)
{
// Declare your local variables here
// Crystal Oscillator division factor: 1
#pragma optsize-
CLKPR=0x80;
CLKPR=0x00;
#ifdef _OPTIMIZE_SIZE_
#pragma optsize+
#endif
// Input/Output Ports initialization
// Port B initialization
// Func5=In Func4=Out Func3=Out Func2=Out Func1=In Func0=Out
// State5=T State4=1 State3=1 State2=0 State1=P State0=0
PORTB=0x1A;
DDRB=0x1D;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: 125.000 kHz
// Mode: CTC top=OCR0A
// OC0A output: Disconnected
// OC0B output: Disconnected
TCCR0A=0x00;
TCCR0B=0x00;
TCNT0=0x00;
OCR0A=0x00;
OCR0B=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer1 Stopped
// Mode: Normal top=0xFF
// OC1A output: Disconnected
// OC1B output: Disconnected
// Timer1 Overflow Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
PLLCSR=0x00;
TCCR1=0x00;
GTCCR=0x00;
TCNT1=0x00;
OCR1A=0x00;
OCR1B=0x00;
OCR1C=0x00;
// External Interrupt(s) initialization
// INT0: On
// INT0 Mode: Low level
// Interrupt on any change on pins PCINT0-5: Off
//GIMSK=0x40;
//MCUCR=0x00;
//GIFR=0x40;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x00;
// Universal Serial Interface initialization
// Mode: Disabled
// Clock source: Register & Counter=no clk.
// USI Counter Overflow Interrupt: Off
USICR=0x00;
// Analog Comparator initialization
// Analog Comparator: Off
ACSR=0x80;
ADCSRB=0x00;
DIDR0=0x00;
// ADC initialization
// ADC disabled
ADCSRA=0x00;
while (1)
{
PORTB&=~((1<<3)|(1<<4));
//delay_ms(200);
PORTB|=(1<<3)|(1<<4);
//delay_ms(200);
PORTB&=~((1<<3)|(1<<4));
//delay_ms(200);
PORTB|=(1<<3)|(1<<4);
//delay_ms(200);
/*使能INT0中断*/
GIMSK=0x40;
MCUCR=0x00;
GIFR=0x40;
MCUCR |= (1<<SM1) | (0<<SM0);// 掉电模式
MCUCR |= (1<<SE); //睡眠使能
#asm("sei")// 开放全局中断
//delay_ms(200);
PORTB|=(1<<2);
//delay_ms(200);
PORTB|=(1<<2);
//delay_ms(200);
PORTB|=(1<<2);
#asm("sleep") //休眠
PORTB ^= 1<<2; //唤醒后监测
//delay_ms(500);
}
}
没看你的代码,我瞎猜,可能是启用前没有清除中断标识 本帖最后由 biying 于 2014-5-8 03:33 编辑
tohell 发表于 2014-5-8 02:42
没看你的代码,我瞎猜,可能是启用前没有清除中断标识
猜错!我已经清除了。最新测试,重新从别一段程序里取来的代码(是用PCINT1作为外部中断输入),新建了一个工程后外部中断正常了,可是相同的代码复制到原来出错的工程里,依然不能用,这是为何?难道编译器会“记仇”?这是正常的代码包,这是相同代码不正常的代码包。但是如果把PCINT1改为INT0的话,也就是只改这里// External Interrupt(s) initialization
// INT0: Off
// Interrupt on any change on pins PCINT0-5: On
//GIMSK=0x20; //启用PCINT1
//MCUCR=0x00;
//PCMSK=0x02;
//GIFR=0x20;
GIMSK=0x40;//记用INT0
MCUCR=0x00;
GIFR=0x40;
但是两个包都是一样的不正常。INT0,你到底要怎么样才能才能让我用? 好几次了,遇到过相同的代码,新建工程后粘贴过去编译下载测试就正常,现在又碰到,真搞不懂是CodeWizardAVR的错,还是我这个版本有BUG。连avrstudio也一起软件仿真出错,应该是CodeWizardAVR编译出来的文件有问题。 同样的代码,ICCV7 for AVR里改为INT0后居然是正常的!难道真是CodeWizardAVR的错?这是ICC的代码包 中断服务程序改好名字也不行
/*****************************************************
This program was produced by the
CodeWizardAVR V2.05.1b Evaluation
Automatic Program Generator
?Copyright 1998-2011 Pavel Haiduc, HP InfoTech s.r.l.
http://www.hpinfotech.com
Project :
Version :
Date : 2014-5-8
Author: Freeware, for evaluation and non-commercial use only
Company :
Comments:
Chip type : ATtiny85
AVR Core Clock frequency: 8.000000 MHz
Memory model : Small
External RAM size : 0
Data Stack size : 128
*****************************************************/
#include <tiny85.h>
#include<delay.h>
// Pin change interrupt service routine
//interrupt void pin_change_isr(void)
interrupt void ext_int0_isr(void)
{
// Place your code here
PORTB&=0xfe;
}
// Declare your global variables here
void main(void)
{
// Declare your local variables here
// Crystal Oscillator division factor: 1
#pragma optsize-
CLKPR=0x80;
CLKPR=0x00;
#ifdef _OPTIMIZE_SIZE_
#pragma optsize+
#endif
// Input/Output Ports initialization
// Port B initialization
// Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out
// State5=1 State4=1 State3=1 State2=1 State1=1 State0=1
//PORTB=0x3F;
//DDRB=0x3F;
DDRB = 0x3F; //将所有I/O口均初始化为输出状态
PORTB = 0x3F;
DDRB&=0XFD; //PCINT1设为输入
PORTB=0X3F;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=0xFF
// OC0A output: Disconnected
// OC0B output: Disconnected
TCCR0A=0x00;
TCCR0B=0x00;
TCNT0=0x00;
OCR0A=0x00;
OCR0B=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer1 Stopped
// Mode: Normal top=0xFF
// OC1A output: Disconnected
// OC1B output: Disconnected
// Timer1 Overflow Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
PLLCSR=0x00;
TCCR1=0x00;
GTCCR=0x00;
TCNT1=0x00;
OCR1A=0x00;
OCR1B=0x00;
OCR1C=0x00;
// External Interrupt(s) initialization
// INT0: Off
// Interrupt on any change on pins PCINT0-5: On
//GIMSK=0x20;
//MCUCR=0x00;
//PCMSK=0x02;
//GIFR=0x20;
GIMSK=0x40;
MCUCR=0x00;
GIFR=0x40;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x00;
// Universal Serial Interface initialization
// Mode: Disabled
// Clock source: Register & Counter=no clk.
// USI Counter Overflow Interrupt: Off
USICR=0x00;
// Analog Comparator initialization
// Analog Comparator: Off
ACSR=0x80;
ADCSRB=0x00;
DIDR0=0x00;
// ADC initialization
// ADC disabled
ADCSRA=0x00;
// Global enable interrupts
#asm("sei")
while (1)
{
// Place your code here
#asm("sei") // 开放全局中断 while(1);
}
}
当要设为PCINT1中断时,第53到56行的端口处理为什么要这么麻烦?为何不直接设置为
PORTB=0x3F;
DDRB=0x3D;
软件仿真时倒是看到以上两句处理的话,PINB2为0,相当于触发外部中断;但如果像第53到56行那样处理后,PINB2为1,也就是没有触发外部中断。我觉得用M16时没这么麻烦呢 重新安装cvavre_2.05.1_b后重新编译下载居然INT0正常了,可是多编译多次后居然出现提示:Linker error: the program has no 'main' function,关掉软件重新打开再编译双没有提示了,此时再下载INT0又不正常了。 现在依然不能用INT0,用PCINT1正常
/*****************************************************
This program was produced by the
CodeWizardAVR V2.05.1b Evaluation
Automatic Program Generator
?Copyright 1998-2011 Pavel Haiduc, HP InfoTech s.r.l.
http://www.hpinfotech.com
Project :
Version :
Date : 2014-5-8
Author: Freeware, for evaluation and non-commercial use only
Company :
Comments:
Chip type : ATtiny85
AVR Core Clock frequency: 8.000000 MHz
Memory model : Small
External RAM size : 0
Data Stack size : 128
*****************************************************/
/*****************************************************
This program was produced by the
CodeWizardAVR V2.05.1b Evaluation
Automatic Program Generator
?Copyright 1998-2011 Pavel Haiduc, HP InfoTech s.r.l.
http://www.hpinfotech.com
Project :
Version :
Date : 2014-5-8
Author: Freeware, for evaluation and non-commercial use only
Company :
Comments:
Chip type : ATtiny85
AVR Core Clock frequency: 8.000000 MHz
Memory model : Small
External RAM size : 0
Data Stack size : 128
*****************************************************/
#include <tiny85.h>
#include<delay.h>
// Pin change interrupt service routine
interrupt void pin_change_isr(void)
//interrupt void ext_int0_isr(void)
{
// Place your code here
PORTB&=0xfe;
}
// Declare your global variables here
void main(void)
{
// Declare your local variables here
// Crystal Oscillator division factor: 1
#pragma optsize-
CLKPR=0x80;
CLKPR=0x00;
#ifdef _OPTIMIZE_SIZE_
#pragma optsize+
#endif
// Input/Output Ports initialization
// Port B initialization
// Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out
// State5=1 State4=1 State3=1 State2=1 State1=1 State0=1
PORTB=0x3F;DDRB=0x3D;
//DDRB = 0x3F;PORTB = 0x3F;DDRB&=0XFD;PORTB=0X3F;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=0xFF
// OC0A output: Disconnected
// OC0B output: Disconnected
TCCR0A=0x00;
TCCR0B=0x00;
TCNT0=0x00;
OCR0A=0x00;
OCR0B=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer1 Stopped
// Mode: Normal top=0xFF
// OC1A output: Disconnected
// OC1B output: Disconnected
// Timer1 Overflow Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
PLLCSR=0x00;
TCCR1=0x00;
GTCCR=0x00;
TCNT1=0x00;
OCR1A=0x00;
OCR1B=0x00;
OCR1C=0x00;
// External Interrupt(s) initialization
// INT0: Off
// Interrupt on any change on pins PCINT0-5: On
GIMSK=0x20;MCUCR=0x00;PCMSK=0x02;GIFR=0x20;
//GIMSK=0x40;MCUCR=0x00;GIFR=0x40;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x00;
// Universal Serial Interface initialization
// Mode: Disabled
// Clock source: Register & Counter=no clk.
// USI Counter Overflow Interrupt: Off
USICR=0x00;
// Analog Comparator initialization
// Analog Comparator: Off
ACSR=0x80;
ADCSRB=0x00;
DIDR0=0x00;
// ADC initialization
// ADC disabled
ADCSRA=0x00;
// Global enable interrupts
#asm("sei")
while (1);
} 换笔记本电脑安装同一个文件的软件,手动输入代码编译下载,INT0依然不能用 biying 发表于 2014-5-8 09:07
同样的代码,ICCV7 for AVR里改为INT0后居然是正常的!难道真是CodeWizardAVR的错?这是ICC的代码包 ...
比较两个工具生成的汇编文件看看 惭愧啊,汇编还没忙得学呢
页:
[1]