bug with timer0 interrupt?

Jonathan Geisler <[email protected]>
Newsgroups gmane.comp.hardware.avr.gcc
Message-ID <[email protected]>
I'm encountering a strange error with avr-gcc 4.6.3 and 4.7.2 using
the timer interrupt.  The code I'm using is attached.  As you can see,
I was trying to tie in to the Arduino environment (with Eclipse) so
that I could help my students wean off that set of functions to being
able to use the hardware directly.

When I set up the library, I encountered a strange error and was able
to trace it back to this simple program that sets up the timer with a
prescalar of 1024 and then increments an unsigned long counter when
that timer overflows.  When the counter reaches a specific value, I
toggle the built-in LED on the Arduino board connected to PORTB.

The strange behavior is that if I initialize the counter to 0, the
code doesn't work--appearing to hang.  If I initialize the counter to
1, everything works perfectly.  I have verified that if I make a
similar change to the Arduino libraries that the original code begins
to work properly.

I'm not sure what the exact error is since I can use the Arduino
environment on my machine without a problem, but when I try to compile
it through Eclipse it gives me this error.  I'll include the build log
as an attachment in case particular compiler/linker flags are triggering
the error.

						-- Jonathan Geisler --

_______________________________________________
AVR-GCC-list mailing list
[email protected]
https://lists.nongnu.org/mailman/listinfo/avr-gcc-list
main.cpp (text/x-c, 741 B)
/*
 * main.cpp
 *
 *  Created on: Feb 8, 2013
 *      Author: Jonathan Geisler
 */

//#define JGG

#ifdef JGG
#include "Arduino.h"
#else
#include <avr/interrupt.h>
#endif

void setup() {
#ifdef JGG
	pinMode(13, OUTPUT);
#else
	TCCR0A = 0;
	TCCR0B = 5;
	TIMSK0 = 1;
	sei();

	DDRB |= 0x20;
#endif
}

volatile unsigned long count = 1;  // bad executable if initialized to 0!!!

void loop() {
#ifdef JGG
	digitalWrite(13, HIGH);
	delay(1000);
	digitalWrite(13, LOW);
	delay(1000);
#else
	if (count > 50) {
		PORTB ^= 0x20;
		count = 0;
	}
#endif
}

#ifndef JGG
ISR(TIMER0_OVF_vect) {
	++count;
}

void init() {
	/* do nothing */
}
#endif

int main() {
	init();

#ifdef USBCON
	USBDevice.attach();
#endif

	setup();

	while (1) {
		loop();
	}
}
build.log (text/plain, 968 B)
make all 
Building file: ../main.cpp
Invoking: AVR C++ Compiler
avr-g++ -I"/home/jgeisler/src/eclipse/Arduino_Core/src" -DARDUINO=103 -Wall -Os -ffunction-sections -fdata-sections -fno-exceptions -mmcu=atmega328p -DF_CPU=16000000UL -MMD -MP -MF"main.d" -MT"main.d" -c -o "main.o" "../main.cpp"
Finished building: ../main.cpp
 
Building target: Arduino-on-Eclipse-1.elf
Invoking: AVR C++ Linker
avr-gcc -Wl,-Map,Arduino-on-Eclipse-1.map,--cref -Wl,--gc-sections -Os -L"/home/jgeisler/src/eclipse/Arduino_Core/328P_16MHz" -mmcu=atmega328p -o "Arduino-on-Eclipse-1.elf"  ./main.o   -lArduino_Core -lm
Finished building target: Arduino-on-Eclipse-1.elf
 
Invoking: AVR Create Extended Listing
avr-objdump -h -S Arduino-on-Eclipse-1.elf  >"Arduino-on-Eclipse-1.lss"
Finished building: Arduino-on-Eclipse-1.lss
 
Create Flash image (ihex format)
avr-objcopy -R .eeprom -O ihex Arduino-on-Eclipse-1.elf  "Arduino-on-Eclipse-1.hex"
Finished building: Arduino-on-Eclipse-1.hex
signature.asc (application/pgp-signature, 490 B)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.19 (GNU/Linux)

iQEcBAEBCAAGBQJRHP1dAAoJEOwSkIyhEMwrSSoH/RUlKeG6vH1iWQzhvayLy0QT
jfQOwm282kZCs72xWAhu1xR3rVYHa7x/byFpjDIOgQ+bsEAfKE7OgpLB2lvk3JcD
soOlMIYuFfsYM/TmrlnLmLm4j5U7QcwFW9MQsVSd8o34lhULCVRaDM9LRfg7IVvu
0nmDCvqsbEAQkH+uzYfg/r9A4AkHIYODKQwDl12xHvkmxQKuf4qyVujk2GgA8aun
4e3eY9jrTjyhAH6nRaFIQstS22n8NCN07U4RKmHjL9e4bLRpwi38Vfk2ba+D95co
ZwMDPPbwolfX7h4R9BtzaNWq4ZulBS+nrpbyOwqdnMK6BvyfzC0k161FdpWvtQc=
=QeJx
-----END PGP SIGNATURE-----
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.