Strange behavior with if else inside interrupt

Go To Last Post
22 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello 

 

I am newbie programming uprocessor but I figured I could start learning using Atmel Studio and a Arduino board (although I am not using the Arduino libraries in Atmel Studio). I have good experience programming in C++ (and some with C)

Basically what i am trying to do is just turn a LED one when i fire an interrupt and turn it off the next time i fire that interrupt. 

As you can see below i created a "global" boolean variable which i use to flip flop every time i fire the interrupt, but that is not what is happening. When i load this into the arduino and fire the interrupt, the statements inside BOTH the if and the else run, meaning the LED will turn on the immediately turn off.

I looked into the assembly file and if i understood it correctly, it seems that the compiler is in fact doing what i mentioned above. 

My question is then, how can do this "flip-flop" with a interrupt? Or how can i modify my code to work?

 

#define F_CPU 16000000UL


#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include <stdbool.h>

volatile bool on_off = true;

void SetUpPortB();

ISR(INT0_vect)
{
	if (on_off == true)
	{
		PORTB |= (1 << 0);
		on_off = false;
		_delay_ms(100);
	}
	else
	{
		PORTB &= ~(1 << 0);
		on_off = true;
		_delay_ms(100);
	}
}


int main(void)
{
	/* Replace with your application code */
	//on_off = 1;
	SetUpPortB();

	while (1)
	{
		
	}
}


void SetUpPortB()
{
	DDRB |= (1 << 0); //set PB0 as output
	DDRD &= ~(1 << DDD2); //set PD0 as input
	PORTD |= (1 << PORTD2); //turn on pull up mode

	EICRA |= ISC11; //RISING EDGE generates interrupt
	EIMSK |= (1 << INT0);

	sei();
}
    if (on_off == true)
00000053  LDS R24,0x0100		Load direct from data space 
00000055  TST R24		Test for Zero or Minus 
00000056  BREQ PC+0x0E		Branch if equal 
		PORTB |= (1 << 0);
00000057  SBI 0x05,0		Set bit in I/O register 
		on_off = false;
00000058  STS 0x0100,R1		Store direct to data space 
--- c:\program files (x86)\atmel\studio\7.0\toolchain\avr8\avr8-gnu-toolchain\avr\include\util/delay.h 
	__builtin_avr_delay_cycles(__ticks_dc);
0000005A  SER R18		Set Register 
0000005B  LDI R24,0xE1		Load immediate 
0000005C  LDI R25,0x04		Load immediate 
0000005D  SUBI R18,0x01		Subtract immediate 
0000005E  SBCI R24,0x00		Subtract immediate with carry 
0000005F  SBCI R25,0x00		Subtract immediate with carry 
00000060  BRNE PC-0x03		Branch if not equal 
00000061  RJMP PC+0x0001		Relative jump 
00000062  NOP 		No operation 
00000063  RJMP PC+0x000E		Relative jump 
--- C:\Users\hpupo\Documents\Atmel Studio\7.0\FirstArduinoTest\FirstArduinoTest\Debug/.././main.c 
		PORTB &= ~(1 << 0);
00000064  CBI 0x05,0		Clear bit in I/O register 
		on_off = true;
00000065  LDI R24,0x01		Load immediate 
00000066  STS 0x0100,R24		Store direct to data space 
--- c:\program files (x86)\atmel\studio\7.0\toolchain\avr8\avr8-gnu-toolchain\avr\include\util/delay.h 
	__builtin_avr_delay_cycles(__ticks_dc);

thank you 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

a "global" boolean variable which i use to flip flop every time i fire the interrupt,

HOW are you firing the interrupt, with a switch? If so read up about contact bounce.

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

Last Edited: Wed. Dec 6, 2017 - 05:54 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I looked into the assembly file and if i understood it correctly, it seems that the compiler is in fact doing what i mentioned above. 

You haven't shown the whole ISR, but no I don't think it is.

 

As @js mentioned, buttons bounce.

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"Read a lot.  Write a lot."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

While it's not a great design idea, he has _delay_ms(100) in both branches of the ISR (so it should be common in fact) but that should mitigate bounce shouldn't it?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

hpupo wrote:

	EICRA |= ISC11; //RISING EDGE generates interrupt

 

WRONG.

'This forum helps those who help themselves.'

 

pragmatic  adjective dealing with things sensibly and realistically in a way that is based on practical rather than theoretical consideration.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

While it's not a great design idea, he has _delay_ms(100) in both branches of the ISR (so it should be common in fact) but that should mitigate bounce shouldn't it?

No, as he's still triggering from an interrupt.  The ISR will block for 100 ms, then exit and immediately re-enter to service the now-pending interrupt from the first bounce.

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"Read a lot.  Write a lot."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yeah I see that now. So I guess the solution is to delay then clear any pending flag?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I guess the solution is to delay then clear any pending flag?

That's one solution.  Of course a better solution would be to avoid using external interrupts for this at all.

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"Read a lot.  Write a lot."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I am properly de-bouncing the button with a RC circuit (i should have mentioned that). I will hook up a scope to be sure, but i am very confident that's not the issue The interesting part is if copy the code to the Arduino IDE and run it, everything works flawlessly.

@joeymorin, i will copy the whole asm code below. Also, what would be ideal design for something like this? 

@clawson, the delays are there for testing only, no other reason. I can remove and still have the same problem. As i said above i am not relaying on them for de-bouncing.

@Brian, i know it is technically wrong and i meant to fix it, but since i am still setting the correct bits (and i am not using INT1), it still works, so i am not sure the relevance to the problem i am having.

00000018  JMP 0x00000049		Jump 
0000001A  JMP 0x00000049		Jump 
0000001C  JMP 0x00000049		Jump 
0000001E  JMP 0x00000049		Jump 
00000020  JMP 0x00000049		Jump 
00000022  JMP 0x00000049		Jump 
00000024  JMP 0x00000049		Jump 
00000026  JMP 0x00000049		Jump 
00000028  JMP 0x00000049		Jump 
0000002A  JMP 0x00000049		Jump 
0000002C  JMP 0x00000049		Jump 
0000002E  JMP 0x00000049		Jump 
00000030  JMP 0x00000049		Jump 
00000032  JMP 0x00000049		Jump 
--- ../../../../crt1/gcrt1.S ---------------------------------------------------
00000034  CLR R1		Clear Register 
00000035  OUT 0x3F,R1		Out to I/O location 
00000036  SER R28		Set Register 
00000037  LDI R29,0x08		Load immediate 
00000038  OUT 0x3E,R29		Out to I/O location 
00000039  OUT 0x3D,R28		Out to I/O location 
--- No source file -------------------------------------------------------------
0000003A  LDI R17,0x01		Load immediate 
0000003B  LDI R26,0x00		Load immediate 
0000003C  LDI R27,0x01		Load immediate 
0000003D  LDI R30,0x12		Load immediate 
0000003E  LDI R31,0x01		Load immediate 
0000003F  RJMP PC+0x0003		Relative jump 
00000040  LPM R0,Z+		Load program memory and postincrement 
00000041  ST X+,R0		Store indirect and postincrement 
00000042  CPI R26,0x02		Compare with immediate 
00000043  CPC R27,R17		Compare with carry 
00000044  BRNE PC-0x04		Branch if not equal 
--- ../../../../crt1/gcrt1.S ---------------------------------------------------
00000045  CALL 0x00000084		Call subroutine 
00000047  JMP 0x00000087		Jump 
00000049  JMP 0x00000000		Jump 
--- C:\Users\hpupo\Documents\Atmel Studio\7.0\FirstArduinoTest\FirstArduinoTest\Debug/.././main.c 
{
0000004B  PUSH R1		Push register on stack 
0000004C  PUSH R0		Push register on stack 
0000004D  IN R0,0x3F		In from I/O location 
0000004E  PUSH R0		Push register on stack 
0000004F  CLR R1		Clear Register 
00000050  PUSH R18		Push register on stack 
00000051  PUSH R24		Push register on stack 
00000052  PUSH R25		Push register on stack 
	if (on_off == true)
00000053  LDS R24,0x0100		Load direct from data space 
00000055  TST R24		Test for Zero or Minus 
00000056  BREQ PC+0x0E		Branch if equal 
		PORTB |= (1 << 0);
00000057  SBI 0x05,0		Set bit in I/O register 
		on_off = false;
00000058  STS 0x0100,R1		Store direct to data space 
--- c:\program files (x86)\atmel\studio\7.0\toolchain\avr8\avr8-gnu-toolchain\avr\include\util/delay.h 
	__builtin_avr_delay_cycles(__ticks_dc);
0000005A  SER R18		Set Register 
0000005B  LDI R24,0xE1		Load immediate 
0000005C  LDI R25,0x04		Load immediate 
0000005D  SUBI R18,0x01		Subtract immediate 
0000005E  SBCI R24,0x00		Subtract immediate with carry 
0000005F  SBCI R25,0x00		Subtract immediate with carry 
00000060  BRNE PC-0x03		Branch if not equal 
00000061  RJMP PC+0x0001		Relative jump 
00000062  NOP 		No operation 
00000063  RJMP PC+0x000E		Relative jump 
--- C:\Users\hpupo\Documents\Atmel Studio\7.0\FirstArduinoTest\FirstArduinoTest\Debug/.././main.c 
		PORTB &= ~(1 << 0);
00000064  CBI 0x05,0		Clear bit in I/O register 
		on_off = true;
00000065  LDI R24,0x01		Load immediate 
00000066  STS 0x0100,R24		Store direct to data space 
--- c:\program files (x86)\atmel\studio\7.0\toolchain\avr8\avr8-gnu-toolchain\avr\include\util/delay.h 
	__builtin_avr_delay_cycles(__ticks_dc);
00000068  SER R18		Set Register 
00000069  LDI R24,0xE1		Load immediate 
0000006A  LDI R25,0x04		Load immediate 
0000006B  SUBI R18,0x01		Subtract immediate 
0000006C  SBCI R24,0x00		Subtract immediate with carry 
0000006D  SBCI R25,0x00		Subtract immediate with carry 
0000006E  BRNE PC-0x03		Branch if not equal 
0000006F  RJMP PC+0x0001		Relative jump 
00000070  NOP 		No operation 
--- C:\Users\hpupo\Documents\Atmel Studio\7.0\FirstArduinoTest\FirstArduinoTest\Debug/.././main.c 
}
00000071  POP R25		Pop register from stack 
00000072  POP R24		Pop register from stack 
00000073  POP R18		Pop register from stack 
00000074  POP R0		Pop register from stack 
00000075  OUT 0x3F,R0		Out to I/O location 
00000076  POP R0		Pop register from stack 
00000077  POP R1		Pop register from stack 
00000078  RETI 		Interrupt return 
	DDRB |= (1 << 0); //set PB0 as output
00000079  SBI 0x04,0		Set bit in I/O register 
	DDRD &= ~(1 << DDD2); //set PD0 as input
0000007A  CBI 0x0A,2		Clear bit in I/O register 
	PORTD |= (1 << PORTD2); //turn on pull up mode
0000007B  SBI 0x0B,2		Set bit in I/O register 
	EICRA |= ISC11; //RISING EDGE generates interrupt
0000007C  LDI R30,0x69		Load immediate 
0000007D  LDI R31,0x00		Load immediate 
0000007E  LDD R24,Z+0		Load indirect with displacement 
0000007F  ORI R24,0x03		Logical OR with immediate 
00000080  STD Z+0,R24		Store indirect with displacement 
	EIMSK |= (1 << INT0);
00000081  SBI 0x1D,0		Set bit in I/O register 
	sei();
00000082  SEI 		Global Interrupt Enable 
00000083  RET 		Subroutine return 
	SetUpPortB();
00000084  CALL 0x00000079		Call subroutine 
00000086  RJMP PC-0x0000		Relative jump 
--- No source file -------------------------------------------------------------
00000087  CLI 		Global Interrupt Disable 
00000088  RJMP PC-0x0000		Relative jump 
00000089  ??? 		Memory out of bounds or read error 
0000008A  NOP 		Undefined 
0000008B  NOP 		Undefined 
0000008C  NOP 		Undefined 
0000008D  NOP 		Undefined 
0000008E  NOP 		Undefined 
0000008F  NOP 		Undefined 
00000090  NOP 		Undefined 
00000091  NOP 		Undefined 
00000092  NOP 		Undefined 
00000093  NOP 		Undefined 
00000094  NOP 		Undefined 
--- No source file -------------------------------------------------------------
00000095  NOP 		Undefined 
00000096  NOP 		Undefined 
00000097  NOP 		Undefined 
00000098  NOP 		Undefined 
00000099  NOP 		Undefined 
0000009A  NOP 		Undefined 
0000009B  NOP 		Undefined 
0000009C  NOP 		Undefined 
0000009D  NOP 		Undefined 
0000009E  NOP 		Undefined 
0000009F  NOP 		Undefined 
000000A0  NOP 		Undefined 
000000A1  NOP 		Undefined 
000000A2  NOP 		Undefined 
000000A3  NOP 		Undefined 
000000A4  NOP 		Undefined 
000000A5  NOP 		Undefined 
000000A6  NOP 		Undefined 
000000A7  NOP 		Undefined 
000000A8  NOP 		Undefined 
000000A9  NOP 		Undefined 
000000AA  NOP 		Undefined 
--- No source file -------------------------------------------------------------
000000AB  NOP 		Undefined 
000000AC  NOP 		Undefined 
000000AD  NOP 		Undefined 
000000AE  NOP 		Undefined 
000000AF  NOP 		Undefined 
000000B0  NOP 		Undefined 
000000B1  NOP 		Undefined 
000000B2  NOP 		Undefined 
000000B3  NOP 		Undefined 
000000B4  NOP 		Undefined 
000000B5  NOP 		Undefined 
000000B6  NOP 		Undefined 
000000B7  NOP 		Undefined 
000000B8  NOP 		Undefined 
000000B9  NOP 		Undefined 
000000BA  NOP 		Undefined 
000000BB  NOP 		Undefined 
000000BC  NOP 		Undefined 
000000BD  NOP 		Undefined 
000000BE  NOP 		Undefined 
000000BF  NOP 		Undefined 
000000C0  NOP 		Undefined 
--- No source file -------------------------------------------------------------
000000C1  NOP 		Undefined 
000000C2  NOP 		Undefined 
000000C3  NOP 		Undefined 
000000C4  NOP 		Undefined 
000000C5  NOP 		Undefined 
000000C6  NOP 		Undefined 
000000C7  NOP 		Undefined 
000000C8  NOP 		Undefined 
000000C9  NOP 		Undefined 
000000CA  NOP 		Undefined 
000000CB  NOP 		Undefined 
000000CC  NOP 		Undefined 
000000CD  NOP 		Undefined 
000000CE  NOP 		Undefined 
000000CF  NOP 		Undefined 
000000D0  NOP 		Undefined 
000000D1  NOP 		Undefined 
000000D2  NOP 		Undefined 
000000D3  NOP 		Undefined 
000000D4  NOP 		Undefined 
000000D5  NOP 		Undefined 
000000D6  NOP 		Undefined 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

hpupo wrote:

@Brian, i know it is technically wrong and i meant to fix it, but since i am still setting the correct bits (and i am not using INT1), it still works, so i am not sure the relevance to the problem i am having.

 

But the default action for an external interrupt is level triggered. How to you guarantee that your incoming low level is less than 100ms to avoid multiple interrupts?

'This forum helps those who help themselves.'

 

pragmatic  adjective dealing with things sensibly and realistically in a way that is based on practical rather than theoretical consideration.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I think you've missed the gist of Brian's post:

hpupo wrote:

	EICRA |= ISC11; //RISING EDGE generates interrupt

WRONG.

The part that's wrong is that you're using the bitname by itself, rather than creating a bitmask with it.  It should be:

    	EICRA |= (1<<ISC11);

And even then, that doesn't set rising edge.  It sets falling edge:

 

As is, the value of ISC11 is 3, whereas the value of (1<<ISC11) is 8.  The consequence of this is that instead of setting the ISC11 bit in EICRA, you'll be setting both ISC01 and ISC00.  Neither is associated with INT1, but rather INT0.  The two bits for INT1 will remain 0, selecting a 'level' interrupt as Brian has said.

 

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"Read a lot.  Write a lot."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Here's the relevant asm:

	if (on_off == true)
00000053  LDS R24,0x0100		Load direct from data space
00000055  TST R24		Test for Zero or Minus
00000056  BREQ PC+0x0E		Branch if equal
		PORTB |= (1 << 0);
00000057  SBI 0x05,0		Set bit in I/O register
		on_off = false;
00000058  STS 0x0100,R1		Store direct to data space
--- c:\program files (x86)\atmel\studio\7.0\toolchain\avr8\avr8-gnu-toolchain\avr\include\util/delay.h
	__builtin_avr_delay_cycles(__ticks_dc);
0000005A  SER R18		Set Register
0000005B  LDI R24,0xE1		Load immediate
0000005C  LDI R25,0x04		Load immediate
0000005D  SUBI R18,0x01		Subtract immediate
0000005E  SBCI R24,0x00		Subtract immediate with carry
0000005F  SBCI R25,0x00		Subtract immediate with carry
00000060  BRNE PC-0x03		Branch if not equal
00000061  RJMP PC+0x0001		Relative jump
00000062  NOP 		No operation
00000063  RJMP PC+0x000E		Relative jump
--- C:\Users\hpupo\Documents\Atmel Studio\7.0\FirstArduinoTest\FirstArduinoTest\Debug/.././main.c
		PORTB &= ~(1 << 0);
00000064  CBI 0x05,0		Clear bit in I/O register
		on_off = true;
00000065  LDI R24,0x01		Load immediate
00000066  STS 0x0100,R24		Store direct to data space
--- c:\program files (x86)\atmel\studio\7.0\toolchain\avr8\avr8-gnu-toolchain\avr\include\util/delay.h
	__builtin_avr_delay_cycles(__ticks_dc);
00000068  SER R18		Set Register
00000069  LDI R24,0xE1		Load immediate
0000006A  LDI R25,0x04		Load immediate
0000006B  SUBI R18,0x01		Subtract immediate
0000006C  SBCI R24,0x00		Subtract immediate with carry
0000006D  SBCI R25,0x00		Subtract immediate with carry
0000006E  BRNE PC-0x03		Branch if not equal
0000006F  RJMP PC+0x0001		Relative jump
00000070  NOP 		No operation
--- C:\Users\hpupo\Documents\Atmel Studio\7.0\FirstArduinoTest\FirstArduinoTest\Debug/.././main.c
}
00000071  POP R25        Pop register from stack

 

I've highlighted the execution path for the case where on_off is false.  Note that it is changed to 1 (true).  For clarity, I've not included the busy loop in the highlighting.

 

Now the same code with highlighting for the case where on_off is true:

	if (on_off == true)
00000053  LDS R24,0x0100		Load direct from data space
00000055  TST R24		Test for Zero or Minus
00000056  BREQ PC+0x0E		Branch if equal
		PORTB |= (1 << 0);
00000057  SBI 0x05,0		Set bit in I/O register
		on_off = false;
00000058  STS 0x0100,R1		Store direct to data space
--- c:\program files (x86)\atmel\studio\7.0\toolchain\avr8\avr8-gnu-toolchain\avr\include\util/delay.h
	__builtin_avr_delay_cycles(__ticks_dc);
0000005A  SER R18		Set Register
0000005B  LDI R24,0xE1		Load immediate
0000005C  LDI R25,0x04		Load immediate
0000005D  SUBI R18,0x01		Subtract immediate
0000005E  SBCI R24,0x00		Subtract immediate with carry
0000005F  SBCI R25,0x00		Subtract immediate with carry
00000060  BRNE PC-0x03		Branch if not equal
00000061  RJMP PC+0x0001		Relative jump
00000062  NOP 		No operation
00000063  RJMP PC+0x000E		Relative jump
--- C:\Users\hpupo\Documents\Atmel Studio\7.0\FirstArduinoTest\FirstArduinoTest\Debug/.././main.c
		PORTB &= ~(1 << 0);
00000064  CBI 0x05,0		Clear bit in I/O register
		on_off = true;
00000065  LDI R24,0x01		Load immediate
00000066  STS 0x0100,R24		Store direct to data space
--- c:\program files (x86)\atmel\studio\7.0\toolchain\avr8\avr8-gnu-toolchain\avr\include\util/delay.h
	__builtin_avr_delay_cycles(__ticks_dc);
00000068  SER R18		Set Register
00000069  LDI R24,0xE1		Load immediate
0000006A  LDI R25,0x04		Load immediate
0000006B  SUBI R18,0x01		Subtract immediate
0000006C  SBCI R24,0x00		Subtract immediate with carry
0000006D  SBCI R25,0x00		Subtract immediate with carry
0000006E  BRNE PC-0x03		Branch if not equal
0000006F  RJMP PC+0x0001		Relative jump
00000070  NOP 		No operation
--- C:\Users\hpupo\Documents\Atmel Studio\7.0\FirstArduinoTest\FirstArduinoTest\Debug/.././main.c
}
00000071  POP R25        Pop register from stack

Note that it is changed to 0 (false) (The compiler maintains r1 equal to 0).

 

So there is no issue with the compiler generating incorrect code.  It has generated exactly what you asked for, which is correct, by the way.  The issue lies elsewhere in your code, not in the ISR.

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"Read a lot.  Write a lot."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello joyemorin

 

Actually, that's a mistake on my part. which i meant to write was 

EICRA = ISC11

Meaning, i want to set bit 0 and bit 1 of the EICRA register to one so i have INT0 triggering on the rising edge (i am only using INT0). That would be correct, right? 

I am going to test that out tonight and see if it changes anything. 

But i am still puzzled by the fact that the same exact "mistake" works if i compile using the Arduino IDE

 

Thanks 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Actually, that's a mistake on my part. which i meant to write was 

EICRA = ISC11

Meaning, i want to set bit 0 and bit 1 of the EICRA register to one so i have INT0 triggering on the rising edge (i am only using INT0). That would be correct, right? 

NO!

 

While it >>will<< result in rising edge for INT0, it is very wrong to use the value of a bit number >>as though it were a mask<<, especially since the bin name you used is completely misleading i.e. pertains to INT1 while you intend to affect INT0.

 

If you want to set ISC01 and ISC00, then set them!:

EICRA = (1<<ISC01) | (1<<ISC00);

... or:

EICRA = (3<<ISC00);

 

Anything else is terribly confusing.

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"Read a lot.  Write a lot."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

Last Edited: Wed. Dec 6, 2017 - 05:45 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

hpupo wrote:

But i am still puzzled by the fact that the same exact "mistake" works if i compile using the Arduino IDE

 

Ah, but you OR in the bit assuming that the register is zero. What does the Arduino framework set it to?

'This forum helps those who help themselves.'

 

pragmatic  adjective dealing with things sensibly and realistically in a way that is based on practical rather than theoretical consideration.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

But i am still puzzled by the fact that the same exact "mistake" works if i compile using the Arduino IDE

The 'mistake is that the value of ISC11 is 3, and the expression (1<<ISC01) | (1<<ISC00) evaluates to the same, 3.  The former is incorrect in that it does not convey your meaning.  It only 'happens' to work.  The latter is correct in that it also conveys your meaning.

 

What does the Arduino framework set it to?

It doesn't touch it, actually.

 

Nevertheless, the point has been made that assignment is better than RMW in this case.

 

However, I agree that point never seems to be made strongly enough, since it happens so often around here ;-)

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"Read a lot.  Write a lot."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

Last Edited: Wed. Dec 6, 2017 - 07:54 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

hpupo wrote:
But i am still puzzled by the fact that the same exact "mistake" works if i compile using the Arduino IDE
joeymorin wrote:
It doesn't touch it, actually.

So, if you are still puzzled, I/we like puzzles.  Post the smallest complete test program that demonstrates the situation, in both styles.  Tell connections and how you are testing, and determining that one approach "works" and the other does not.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Is the puzzle not solved?:

The 'mistake is that the value of ISC11 is 3, and the expression (1<<ISC01) | (1<<ISC00) evaluates to the same, 3.  The former is incorrect in that it does not convey your meaning.  It only 'happens' to work.  The latter is correct in that it also conveys your meaning.

 

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"Read a lot.  Write a lot."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

joeymorin wrote:
Is the puzzle not solved?:
theusch wrote:
hpupo wrote: But i am still puzzled by the fact that the same exact "mistake" works if i compile using the Arduino IDE

Same "exact" code, and two different results?  Sounds puzzling to me.  But perhaps/probably OP doesn't really care to continue digging, until bit manipulation is clear.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Remember that Arduino is C++

 

That aside, default compiler options may well differ ...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

awneil wrote:
Remember that Arduino is C++

Fine.  Then let's see the code for "the same exact "mistake" works if i compile using the Arduino IDE ".  Preferably complete small test programs for each that demonstrate the situation.  And/or the generated code for each.  Give information on toolchain, version, and build options.  Show build results, including warnings.

 

Indeed, there have been recent/current discussions with subtle [for me at least] differences in C++ handling of "const" variations.  This could be one.  A puzzle...

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Y'all have missed something. It's not the same code. He's referring to the same mistake in both AS and Arduino. That mistake is using the value of ISC11, which is 3, instead of an expression formed by orring two bit masks (1<<ISC01) | (1<<ISC00) which has the same value. See #16.

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"Read a lot.  Write a lot."

"We see a lot of arses on handlebars around here." - [J Ekdahl]