Pages: [1]   Go Down
Author Topic: Problems with attachInterrupt while in C  (Read 891 times)
0 Members and 1 Guest are viewing this topic.
Reykjavik, Iceland
Offline Offline
Newbie
*
Karma: 0
Posts: 6
Arduino rocks as much as me.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi.

I have been playing with the Arduino board I got for xmas and I hit a little problem while using attachInterrupt in C.

If I rewrite the code in the Arduino IDE then attachInterrupt works so it's not a wiring problem. For various reasons I have been playing in old fashion C and want this to work there. smiley

Is there some trick to this? Is there something really really obvious I am missing?

You can check out my code here: http://github.com/JonGretar/my-arduino-code/tree/master/C/ledarray-v2.c
« Last Edit: December 28, 2008, 04:47:26 pm by JonGretar » Logged

Austin, TX USA
Offline Offline
God Member
*****
Karma: 4
Posts: 997
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi Jon,

It might be easier to diagnose if you would explain what your "little problem" is.  Compiler error?  Malfunction?  Unexpected behavior?

Mikal
Logged

Reykjavik, Iceland
Offline Offline
Newbie
*
Karma: 0
Posts: 6
Arduino rocks as much as me.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hehe... sorry.. My bad.

The interrupt function never runs in the C code. Thus it never changes change_state to HIGH on trigger.

Everything compiles and uploads fine. The loop runs fine. Running got_interrupt() manually works. Just does nothing when I activate the interrupt trigger.
Logged

Austin, TX USA
Offline Offline
God Member
*****
Karma: 4
Posts: 997
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you rename the file to .cpp and compile it exactly as-is with the C++ compiler, does it still not work?
Logged

Reykjavik, Iceland
Offline Offline
Newbie
*
Karma: 0
Posts: 6
Arduino rocks as much as me.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Same thing. Program upload. Runs fine.. Just no interrupts. I have the new Duemilanove if that matters.

Code:
=> [avr-gcc] ledarray-v2.elf
avr-gcc -mmcu=atmega168 -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -I. -gstabs -DF_CPU=16000000 -Os -Wall -Wstrict-prototypes -std=gnu99 -I/Applications/Electronics/arduino-0012/hardware/cores/arduino -o ledarray-v2.elf ledarray-v2.c  -L. core.a -lm

 => [avr-objcopy] ledarray-v2.hex
avr-objcopy -O ihex -R .eeprom ledarray-v2.elf ledarray-v2.hex

 => [avrdude] ledarray-v2.hex
avrdude -p atmega168 -P /dev/tty.usbserial-A60061eR -C /Applications/Electronics/arduino-0012/hardware/tools/avr/etc/avrdude.conf -c stk500v1 -b 19200 -F -u -U flash:w:ledarray-v2.hex

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.05s

avrdude: Device signature = 0x000000
avrdude: Yikes!  Invalid device signature.
avrdude: Expected signature for ATMEGA168 is 1E 94 06
avrdude: NOTE: FLASH memory has been specified, an erase cycle will be performed
         To disable this feature, specify the -D option.
avrdude: erasing chip
avrdude: reading input file "ledarray-v2.hex"
avrdude: input file ledarray-v2.hex auto detected as Intel Hex
avrdude: writing flash (3164 bytes):

Writing | ################################################## | 100% 2.59s

avrdude: 3164 bytes of flash written
avrdude: verifying flash memory against ledarray-v2.hex:
avrdude: load data flash data from input file ledarray-v2.hex:
avrdude: input file ledarray-v2.hex auto detected as Intel Hex
avrdude: input file ledarray-v2.hex contains 3164 bytes
avrdude: reading on-chip flash data:

Reading | ################################################## | 100% 2.38s

avrdude: verifying ...
avrdude: 3164 bytes of flash verified

avrdude done.  Thank you.
Logged

Seattle, WA
Offline Offline
Jr. Member
**
Karma: 0
Posts: 81
Arduino rocks my socks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

If you're playing around in straight up C, you should ditch attachInterrupt and write your own interrupt handlers in  the avr-libc syntax.
Logged

Reykjavik, Iceland
Offline Offline
Newbie
*
Karma: 0
Posts: 6
Arduino rocks as much as me.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Okz. Thanks. I'll dig into the interrupts manually.

Just so I know. Is there some reason for this? attachInterrupt seems to be a C function.
Logged

Austin, TX USA
Offline Offline
God Member
*****
Karma: 4
Posts: 997
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Jon, it occurred to me last night as I was going to sleep that attachInterrupt probably has some dependency on the Arduino initialization sequence.  Try simply adding

Code:
init();

to the top of your main() function.

Mikal
Logged

Reykjavik, Iceland
Offline Offline
Newbie
*
Karma: 0
Posts: 6
Arduino rocks as much as me.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Ahh... That did the trick. smiley

Thanks. Didn't know about init();
Logged

Pages: [1]   Go Up
Jump to: