Show Posts
Pages: [1]
1  Forum 2005-2010 (read only) / Bugs & Suggestions / Re: Interupts on: December 27, 2006, 06:28:50 am
I started to link my interrupt examples in the playground:


http://www.arduino.cc/playground/Code/Interrupts


Feel free to add your examples...
2  Forum 2005-2010 (read only) / Development / Re: Arduino timer interrupt on: January 06, 2007, 12:55:47 pm
Hi yiyi,

Quote
In your example, it only allows for 1000 interrupts per second. Is there a way to increase this value for applications that require more interrupts? Writing an 8bit software pwm requires a minimum of 256 interrupts to complete the pwm, making less than 4 pwm cycles per second. This doesn't seem to be fast enough to make an led look like its not blinking.

It seems timer1 is a 16bit timer, instead of timer2 which is 8 bits, but I am not certain on how to port your code to use this other timer.    Could you, or someone else, possibly give an example of this code using timer1 instead of timer2?

I am not sure whether I understand you correctly. In general, it should be pretty easy to convert my code to use timer1 - just set the right bits for timer1, and use the same interrupt handler routine. You can find the correct bits in the datasheet.

BUT: If you want to do PWM, this might not be the easiest way. The timer hardware has pre-build wires to do PWM - basically, you can tell the hardware to do the PWM with a given frequency for you. This is also the reason why pin 9-11 can do PWM - there are three timers in the ATMega8.

Have you read the Arduino tutorial? I think this might be what you're looking for...

http://www.arduino.cc/en/Tutorial/DimmingLEDs
http://www.arduino.cc/en/Reference/AnalogWrite
3  Forum 2005-2010 (read only) / Development / Re: Arduino timer interrupt on: December 27, 2006, 05:27:31 am
Quote
This is a total shot in the dark,


But a good one.

Code:
for (int i=0; i<100; i++) {
   Serial.println("I will think before I code.");
}

;-) It works like a charm now, you can find the full code here:

http://gonium.net/md/2006/12/27/i-will-think-before-i-code/

Thanks!

-Mathias
4  Forum 2005-2010 (read only) / Development / Arduino timer interrupt on: December 24, 2006, 05:48:44 pm
Hi!

Continuing with my interrupt experiements, I have a question about the timer interrupts. I use the following code to increment a counter every second:

Code:
#include <avr/interrupt.h>
#include <avr/io.h>

#define INIT_TIMER_COUNT 6
#define RESET_TIMER2 TCNT2 = INIT_TIMER_COUNT

int ledPin = 13;
int int_counter = 0;
volatile int second = 0;
int oldSecond = 0;
long starttime = 0;

// Aruino runs at 16 Mhz, so we have 1000 Overflows per second...
// 1/ ((16000000 / 64) / 256) = 1 / 1000
ISR(TIMER2_OVF_vect) {
  RESET_TIMER2;
  int_counter += 1;
  if (int_counter == 1000) {
    second+=1;
    int_counter = 0;
  }
};

void setup() {
  Serial.begin(9600);
  Serial.println("Initializing timerinterrupt");
  //Timer2 Settings: Timer Prescaler /64,
  TCCR2 |= ((1<<CS22) | (0<<CS21) | (0<<CS20));  
  // Use normal mode
  TCCR2 |= (0<<WGM21) | (0<<WGM20);
  // Use internal clock - external clock not used in Arduino
  ASSR |= (0<<AS2);
  TIMSK |= (1<<TOIE2) | (0<<OCIE2);        //Timer2 Overflow Interrupt Enable  
  RESET_TIMER2;              
  sei();
  starttime = millis();
}

void loop() {
  if (oldSecond != second) {
    Serial.print(second);
    Serial.print(". ->");
    Serial.print(millis() - starttime);
    Serial.println(".");
    digitalWrite(ledPin, HIGH);
    delay(100);
    digitalWrite(ledPin, LOW);
    oldSecond = second;
  }
}

Basically, the code works. BUT: The variable "second" is only increased every two seconds. If I use

Code:
...
   if (int_counter == 500)
...

in the ISR routine, the counter is increased approximately each second (with a slight skew). I don't understand this behaviour: The Arduino runs at 16 Mhz. If I use a prescaler of /64 and an overflow occurs every 250 interrupts (256 - 6, since I reset the Timer to 6), I have 1000 interrupts per second

-> (16000000 / 64) / 250 = 1000

But this corresponds to roughly two seconds when I run the program. So: Something is wrong. Any hints?

-Mathias
5  Forum 2005-2010 (read only) / Development / Arduino remote clock library update on: January 06, 2007, 06:23:29 pm
The version 0.2 is out, basically fixing the things left out in the first release. I integrated interrupt handling and error checking - you can get the code here:

http://gonium.net/md/2007/01/06/arduino-dcf77-v02-released/

Have fun,
-Mathias
6  Forum 2005-2010 (read only) / Development / Arduino remote clock library on: November 08, 2006, 06:55:04 am
Hi!

I don't know where the best place for announcements is, so I'll do it here: I created a first version of a library to read the german DCF77 signal. This signal is broadcasted over almost all europe and allows you to receive the official german time.

Read more in this post:

http://gonium.net/md/2006/11/05/arduino-dcf77-radio-clock-receiver/

-Mathias
7  Forum 2005-2010 (read only) / Development / Arduino external interrupt example on: December 20, 2006, 04:30:51 pm
Hi!

I created a small example on how to use interrupts with Arduino, find it here:

http://gonium.net/md/2006/12/20/handling-external-interrupts-with-arduino/


It shows how to install an interrupt handler for external interrupts with the ATMega8 and the AVR-GCC libraries. The principle should be the same for other interrupts like e.g. timer interrupts.

-Mathias
8  Forum 2005-2010 (read only) / Development / Re: arduino library build system feedback on: November 05, 2006, 02:49:55 pm
Sorry, I enabled the notification of the forum and I expected to receive an email on any replies, so this took me a while...

Quote
This is good feedback to get, but I need a few more details to understand exactly what was happening.  Can you post the linker errors you were getting?  Which file had a .c suffix (one in a library or in the sketch)?


Unfortunately, I do not have the old files because I continued with C++ code. As far as I remember, the compiler complained about undefined references. The .c file was in the library, the reference was reported to be missing within the sketch in the "loop" function.

I think you can reproduce this by renaming the Test.cpp from the example library to Test.c - apart from the other errors, you get

Code:
o: In function `loop':
undefined reference to `Test::doSomething()'

which is the same I received.

Quote
The problem may have to do with the difference in linker behavior between C and C++.  A .c file with function named foo() will be compiled into an object file (.o) with a function named foo().  The same function in a C++ file will get "mangled" when it's compiled, the function in the .o file will be called something weird like __Blah_foo.  Thus, when a C++ file is linked against an object file generated from a C file, the C++ compiler needs to be told of this fact, so it won't look for mangled function names.  An easy way to do this is putting:

Code:
#ifdef __cplusplus
extern "C"{
#endif

in the header file corresponding to your .c file before the function declarations.  After the function declarations, say:

Code:
#ifdef __cplusplus
} // extern "C"
#endif

This is interesting, I didn't know. Thank you!
9  Forum 2005-2010 (read only) / Development / arduino library build system feedback on: October 30, 2006, 03:35:40 pm
hi!

first of all, i am really happy with the arduino, nice little thing...  ;D

but it gave me a hard time when i wanted to integrate some c-code - so for your reference or (even better) so that you can change this behaviour, i'll give you more details.

i used the test.zip i found on the website to get started. i was sure the code runs (tested it outside the arduino environment), but i kept receiving linker errors in arduino. the problem was the following: since i wrote c code, the filename suffix was ".c". but the arduino toolchain doesn't seem to retain the #include-directive when c files are given in the library, so my pde was translated to an unusable cpp file. when i changed my filenames to ".cpp" suffixes, everything went fine.

so, is there an explanation for this behaviour (i'm new to arduino, so maybe i don't see the point) or is this a not-intended behaviour?

thanks,
-mathias
Pages: [1]