Problems with attachInterrupt while in C

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. :slight_smile:

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

You can check out my code here: my-arduino-code/ledarray-v2.c at master · JonGretar/my-arduino-code · GitHub

Hi Jon,

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

Mikal

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.

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

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

=> [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.

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

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.

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

init();

to the top of your main() function.

Mikal

Ahh... That did the trick. :slight_smile:

Thanks. Didn't know about init();