ATMEGA328P外部中断avr gcc赢得';不会发生
当向int0提供5v输入时,应出现外部中断向量。 中断应更改易失性整数标志,以允许连接到端口B上引脚的LED点亮。在Atmel studio中编译时没有错误。问题在于,向int0引脚发送5v电源时,不会发生变化。这是不是中断没有触发ATMEGA328P外部中断avr gcc赢得';不会发生,gcc,interrupt,avr,atmega,Gcc,Interrupt,Avr,Atmega,当向int0提供5v输入时,应出现外部中断向量。 中断应更改易失性整数标志,以允许连接到端口B上引脚的LED点亮。在Atmel studio中编译时没有错误。问题在于,向int0引脚发送5v电源时,不会发生变化。这是不是中断没有触发 #include <avr/io.h> #include <stdio.h> #define F_CPU 16000000UL #include <util/delay.h> #include <avr/interrupt
#include <avr/io.h>
#include <stdio.h>
#define F_CPU 16000000UL
#include <util/delay.h>
#include <avr/interrupt.h>
volatile int pwm_flag=0;
int main(void)
{
DDRD &= ~(1 << DDD2); // Clear the PD2 pin
// PD2 (PCINT0 pin) is now an input
PORTD |= (1 << PORTD2); // turn On the Pull-up
// PD2 is now an input with pull-up enabled
EICRA |= (1 << ISC00)|(1 << ISC10); // set INT0 to trigger on Rising edge
EIMSK |= (1 << INT0); // Turns on INT0
sei(); // turn on interrupts
DDRB = 0xFF;
PORTB = 0x00;
while(1)
{
if(pwm_flag==1)//if flag is raised
{
PORTB = 0xFF;//turn on all pins of portb
pwm_flag=0;//reset flag to 0
}
}
}
ISR (INT0_vect)
{
/* interrupt code here */
pwm_flag =1;//raise flag
}
#包括
#包括
#定义F_CPU 1600000UL
#包括
#包括
易失性int pwm_标志=0;
内部主(空)
{
DDRD&=~(1)
问题在于,向int0引脚发送5v电源时,不会发生变化
如何将5V发送到INT0引脚?您的代码将INT0引脚设置为具有上拉功能的输入,因此,除非您将其短接至地,否则它处于5V
还有,你说没有变化是什么意思?LED是亮还是关
另一件事是这条线:
EICRA |= (1 << ISC00)|(1 << ISC10);
如果您试图从端口B上的引脚向LED提供5v电压,那么这可能是您的问题。大多数微控制器引脚可能会吸收比它们所能提供的电流更多的电流。也许您的LED无法获得所需的电流
您的接线应如下所示:
将LED的正极支路连接至5V。将负极支路连接至100-500欧姆的小电阻器。将电阻器的另一个支路连接至端口B上的引脚
现在,您可以将端口B写入0x00以打开LED,或将端口B写入0xFF以关闭LED
要测试LED是否工作,请在主回路中测试LED,方法是将PORTB写入low(低)和high(高),其间有明显的延迟
如果可行,那么测试你的ISR。如果ISR在这一点上不起作用,那么ISR就是问题所在
请记住,在EICRA的当前配置中,您正在上升沿触发中断。因此,如果引脚已经处于高位,则不会发生中断
我已经更改了下面的代码,所以“低”处于打开状态,“高”处于关闭状态
int main(void){
DDRD &= ~(1 << DDD2); // set PD2 DDR as input
PORTD |= (1 << PORTD2); // set PD2 as input pull-up
EICRA |= (1 << ISC00)|(1 << ISC10); // set INT0 to trigger on rising edge
EIMSK |= (1 << INT0); // Turns on INT0
DDRB = 0xFF; // set PORTB as all outputs
PORTB = 0xFF; // set PORTB high
sei(); // turn on interrupts
while(1){
if(pwm_flag!=0){ // check flag
PORTB = 0x00; // set PORTB low
pwm_flag=0; // reset flag
}
}
ISR (INT0_vect){
pwm_flag = 1; // raise flag
}
int main(无效){
DDRD&=~(1)AVR可以为LED提供和吸收充足的电流。对于小型LED,是的,但我仍然认为吸收电流比提供电流更好。这是个人偏好还是一般性建议?--不过,AVR的数据表上说“端口B输出缓冲区具有对称的驱动特性,具有高的接收器和源容量。”因此,在这方面没有太多错误。
int main(void){
DDRD &= ~(1 << DDD2); // set PD2 DDR as input
PORTD |= (1 << PORTD2); // set PD2 as input pull-up
EICRA |= (1 << ISC00)|(1 << ISC10); // set INT0 to trigger on rising edge
EIMSK |= (1 << INT0); // Turns on INT0
DDRB = 0xFF; // set PORTB as all outputs
PORTB = 0xFF; // set PORTB high
sei(); // turn on interrupts
while(1){
if(pwm_flag!=0){ // check flag
PORTB = 0x00; // set PORTB low
pwm_flag=0; // reset flag
}
}
ISR (INT0_vect){
pwm_flag = 1; // raise flag
}