Comments on: I will think before I code… http://gonium.net/md/2006/12/27/i-will-think-before-i-code/ so much time, so little to do. Tue, 30 Nov 2010 21:05:12 +0000 hourly 1 http://wordpress.org/?v=3.0.1 By: 3dotter http://gonium.net/md/2006/12/27/i-will-think-before-i-code/comment-page-1/#comment-2468 3dotter Fri, 22 Jan 2010 17:40:32 +0000 http://gonium.net/md/2006/12/27/i-will-think-before-i-code/#comment-2468 I am also a newbie on Arduino. I need the Arduino (over a serial connection) 1. to receive a wake up time from a Telit GSM modem (an echoed AT command which needs to be parsed by Arduino), 2. to receive an echoed Telit AT#SHDN command, 3. shut down power to the Telit modem, 4. to fall asleep and 5. to wake up according to the parsed time given by the modem to the Arduino chip. In real life this would mean that the Arduino has to sleep sometimes for a couple of hours and then wake up. My question is if the Arduino can do such with the timer interrupts and/or time libraries. Thanks in advance for your answer. I am also a newbie on Arduino. I need the Arduino (over a serial connection) 1. to receive a wake up time from a Telit GSM modem (an echoed AT command which needs to be parsed by Arduino), 2. to receive an echoed Telit AT#SHDN command, 3. shut down power to the Telit modem, 4. to fall asleep and 5. to wake up according to the parsed time given by the modem to the Arduino chip. In real life this would mean that the Arduino has to sleep sometimes for a couple of hours and then wake up. My question is if the Arduino can do such with the timer interrupts and/or time libraries. Thanks in advance for your answer.

]]>
By: Arduino: Interrupción en Timer2 « Anrusal's Blog http://gonium.net/md/2006/12/27/i-will-think-before-i-code/comment-page-1/#comment-2453 Arduino: Interrupción en Timer2 « Anrusal's Blog Thu, 29 Oct 2009 23:11:01 +0000 http://gonium.net/md/2006/12/27/i-will-think-before-i-code/#comment-2453 [...] http://gonium.net/md/2006/12/27/i-will-think-before-i-code/ Dejar un comentario Aún no hay comentarios por mucho Deja un comentario Canal RSS de los comentarios de la entrada. URI para TrackBack. Deja un comentario Haz clic para cancelar la respuesta. Línea y párrafo se rompe automáticamente, direcciones email nunca se muestran, [...] [...] http://gonium.net/md/2006/12/27/i-will-think-before-i-code/ Dejar un comentario Aún no hay comentarios por mucho Deja un comentario Canal RSS de los comentarios de la entrada. URI para TrackBack. Deja un comentario Haz clic para cancelar la respuesta. Línea y párrafo se rompe automáticamente, direcciones email nunca se muestran, [...]

]]>
By: pee panty http://gonium.net/md/2006/12/27/i-will-think-before-i-code/comment-page-1/#comment-2425 pee panty Sat, 18 Jul 2009 20:37:59 +0000 http://gonium.net/md/2006/12/27/i-will-think-before-i-code/#comment-2425 emm. informative ) emm. informative )

]]>
By: basbrun.com » Arduino Timer interrupt http://gonium.net/md/2006/12/27/i-will-think-before-i-code/comment-page-1/#comment-2424 basbrun.com » Arduino Timer interrupt Fri, 10 Jul 2009 13:50:52 +0000 http://gonium.net/md/2006/12/27/i-will-think-before-i-code/#comment-2424 [...] I found the following code and adapted it to the ATMEGA 328. http://gonium.net/md/2006/12/27/i-will-think-before-i-code/ [...] [...] I found the following code and adapted it to the ATMEGA 328. http://gonium.net/md/2006/12/27/i-will-think-before-i-code/ [...]

]]>
By: Roger Haxby http://gonium.net/md/2006/12/27/i-will-think-before-i-code/comment-page-1/#comment-2409 Roger Haxby Mon, 16 Mar 2009 20:56:21 +0000 http://gonium.net/md/2006/12/27/i-will-think-before-i-code/#comment-2409 I borrowed your interrupt code, trying to do a serial read and write driven by the interrupt timings. Thank you. I borrowed your interrupt code, trying to do a serial read and write driven by the interrupt timings. Thank you.

]]>
By: Donal http://gonium.net/md/2006/12/27/i-will-think-before-i-code/comment-page-1/#comment-2406 Donal Thu, 19 Feb 2009 17:40:51 +0000 http://gonium.net/md/2006/12/27/i-will-think-before-i-code/#comment-2406 Hi Mathias, We ended up just counting the number of reads in a specified time and found that they averaged out as they should. Many thanks for your help, Donal. Hi Mathias,

We ended up just counting the number of reads in a specified time and found that they averaged out as they should.

Many thanks for your help,

Donal.

]]>
By: md http://gonium.net/md/2006/12/27/i-will-think-before-i-code/comment-page-1/#comment-2403 md Wed, 04 Feb 2009 02:04:01 +0000 http://gonium.net/md/2006/12/27/i-will-think-before-i-code/#comment-2403 Hi Donal, sorry, I was confused - of course you're right, the code uses interrupts (I somehow thought of my first version of the sketch from which I remembered this behaviour, sorry). The interrupts itself are highly accurate. I would guess that the variations in the timing are a result of the calls to the Serial.println(...) method which is pretty heavyweight. This would explain the milliseconds variation. I don't have an Arduino here, but you can test this simply by precomputing the values to be printed before the Serial.println calls. I'm thinking of something like if (oldsecond != second) { long difference=millis() - starttime; Serial.print(second); Serial.print(". ->"); Serial.print(difference); Serial.println("."); } Please let me know whether this explains the behaviour. If not you can also adjust the timing settings of the interrupt. Please note that Timer2 is used here which has an 8-bit resolution. You could also try to use Timer1 instead which is a 16-bit timer. Depending on your problem you should also read the ATMega168 datasheet - for example, you can built a simple hardware frequency generator based on the timers. Atmel has also a nice introduction to the timer hardware, google for "AVR130: Setup and Use the AVR Timers". HTH -Mathias Hi Donal,

sorry, I was confused – of course you’re right, the code uses interrupts (I somehow thought of my first version of the sketch from which I remembered this behaviour, sorry). The interrupts itself are highly accurate. I would guess that the variations in the timing are a result of the calls to the Serial.println(…) method which is pretty heavyweight. This would explain the milliseconds variation. I don’t have an Arduino here, but you can test this simply by precomputing the values to be printed before the Serial.println calls. I’m thinking of something like

if (oldsecond != second) {
long difference=millis() – starttime;
Serial.print(second);
Serial.print(“. ->”);
Serial.print(difference);
Serial.println(“.”);
}

Please let me know whether this explains the behaviour. If not you can also adjust the timing settings of the interrupt. Please note that Timer2 is used here which has an 8-bit resolution. You could also try to use Timer1 instead which is a 16-bit timer. Depending on your problem you should also read the ATMega168 datasheet – for example, you can built a simple hardware frequency generator based on the timers. Atmel has also a nice introduction to the timer hardware, google for “AVR130: Setup and Use the AVR Timers”.

HTH
-Mathias

]]>
By: Donal http://gonium.net/md/2006/12/27/i-will-think-before-i-code/comment-page-1/#comment-2402 Donal Tue, 03 Feb 2009 20:19:38 +0000 http://gonium.net/md/2006/12/27/i-will-think-before-i-code/#comment-2402 Hi again, many thanks for your reply... I must apologise in advance as I'm a bit of a newbie here... but I was under the impression that this code does use 'timer interrupts'. I thought it was working off an internal timer that overflows every 256 clock cycles and that when it does the ISR would be triggered. The code I'm using is actually adapted for the atmega168 chip and I have included it below. My main question now is... is there a more appropriate 'type' of interrupt to be using here? Many thanks, Donal. #include #include #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; //unsigned long m = millis(); // 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 == 4) { second+=1; int_counter = 0; } }; void setup() { Serial.begin(9600); Serial.println("Initializing timerinterrupt"); //so we just hard code this for now. //Timer2 Settings: Timer Prescaler /64, TCCR2A |= (1<<CS22); TCCR2A &= ~((1<<CS21) | (1<<CS20)); // Use normal mode TCCR2A &= ~((1<<WGM21) | (1<<WGM20)); // Use internal clock - external clock not used in Arduino ASSR |= (0<<AS2); //Timer2 Overflow Interrupt Enable TIMSK2 |= (1<<TOIE2) | (0<<OCIE2A); sei(); //Timer2 Overflow Interrupt Enable TIMSK2 = 1<"); Serial.println(millis() - starttime, DEC); //Serial.println("."); //digitalWrite(ledPin, HIGH); //delay(100); //digitalWrite(ledPin, LOW); oldSecond = second; starttime = millis(); } } Hi again,

many thanks for your reply… I must apologise in advance as I’m a bit of a newbie here… but I was under the impression that this code does use ‘timer interrupts’. I thought it was working off an internal timer that overflows every 256 clock cycles and that when it does the ISR would be triggered. The code I’m using is actually adapted for the atmega168 chip and I have included it below.

My main question now is… is there a more appropriate ‘type’ of interrupt to be using here?

Many thanks,

Donal.

#include
#include

#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;
//unsigned long m = millis();

// 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 == 4) {
second+=1;
int_counter = 0;
}
};

void setup() {
Serial.begin(9600);
Serial.println(“Initializing timerinterrupt”);
//so we just hard code this for now.
//Timer2 Settings: Timer Prescaler /64,
TCCR2A |= (1<<CS22);
TCCR2A &= ~((1<<CS21) | (1<<CS20));
// Use normal mode
TCCR2A &= ~((1<<WGM21) | (1<<WGM20));
// Use internal clock – external clock not used in Arduino
ASSR |= (0<<AS2);
//Timer2 Overflow Interrupt Enable
TIMSK2 |= (1<<TOIE2) | (0<<OCIE2A);

sei();
//Timer2 Overflow Interrupt Enable
TIMSK2 = 1<”);
Serial.println(millis() – starttime, DEC);
//Serial.println(“.”);
//digitalWrite(ledPin, HIGH);
//delay(100);
//digitalWrite(ledPin, LOW);
oldSecond = second;
starttime = millis();
}
}

]]>
By: md http://gonium.net/md/2006/12/27/i-will-think-before-i-code/comment-page-1/#comment-2401 md Tue, 03 Feb 2009 19:05:33 +0000 http://gonium.net/md/2006/12/27/i-will-think-before-i-code/#comment-2401 Hi Donal, I don't think this code is appropriate for measuring time in the millisecond range. Basically it uses busy waiting in order to detect the time interval between two DCF77 signals. If you want to measure time exactly, I suggest you take a look at interrupts. The ATMega168 chip provides three programmable timer interrupts which can be used to sample something with high accuracy. HTH, -Mathias Hi Donal,

I don’t think this code is appropriate for measuring time in the millisecond range. Basically it uses busy waiting in order to detect the time interval between two DCF77 signals. If you want to measure time exactly, I suggest you take a look at interrupts. The ATMega168 chip provides three programmable timer interrupts which can be used to sample something with high accuracy.

HTH,
-Mathias

]]>
By: Donal http://gonium.net/md/2006/12/27/i-will-think-before-i-code/comment-page-1/#comment-2400 Donal Tue, 03 Feb 2009 11:35:42 +0000 http://gonium.net/md/2006/12/27/i-will-think-before-i-code/#comment-2400 Hello, when i run this code on my arduino the ms print out has a variation of about 2 - 3 ms, i.e. i see 1 - 1001 2 - 2004 3 - 3002 etc... is this expected? I am looking for something that can guarantee a consistent 4 ms sampling rate. Is that possible do you think? Hello,

when i run this code on my arduino the ms print out has a variation of about 2 – 3 ms, i.e. i see

1 – 1001
2 – 2004
3 – 3002
etc…

is this expected? I am looking for something that can guarantee a consistent 4 ms sampling rate. Is that possible do you think?

]]>