Go Down

### Topic: Timer Interrupt Help for LCD watch (Read 1 time)previous topic - next topic

#### Fenestron

##### Nov 13, 2011, 02:10 pm
hi everyone
? have finished a project which contains an LCD and arduino.
this project has a watch, in which i want to use timer inturrupt
to be able to get more precise time calculation.
but ? done the project by using millis() function of arduino library
here you can see my project on my web page containing video of the
project and flow chart.

http://barisdanacioglu.jimdo.com/elektronik/arduino-dersler-projeler/dijital-%C3%A7etele/

does anyone help me about how to use timer interrupt on arduino platform?

here is my code for the watch

Quote

if (millis() - lastTick >= 1000) {
lastTick = millis();
second++;
}

if (second > 59) {
minute++;
second = 0;
}

if (minute > 59) {
hour++;
minute = 0;
}

if (hour > 23) {
day++;
hour = 0;
}

if (day > 999) {
day = 0;
}

#### floresta

#1
##### Nov 13, 2011, 03:20 pm
I cannot see how you will get any more accuracy by using a timer interrupt since millis() already uses the same technique.  It will be a good educational experience for you to write such a program but I don't think you will gain any accuracy.

For a different educational experience you can use the square wave output of an RTC chip to generate an interrupt which will tell you when to read the RTC and update your display.

Note:  Use the 'Code' button (#) to the left of the 'Quote' button to post code, it looks better.

Don

#### Fenestron

#2
##### Nov 13, 2011, 04:15 pm
tnks floresta

RTC chip can be the best solution for me
but I want to know how to use timer interrupt feature of the
atmega328. for example what can ? do if ? want to generate
a periodical square wave and if ? want to change the frequency of the signal
according to a analog read.

#### floresta

#3
##### Nov 13, 2011, 06:06 pmLast Edit: Nov 13, 2011, 06:16 pm by floresta Reason: 1
Quote
but I want to know how to use timer interrupt feature of the atmega328.

That's a good idea.  Here is some information to get you started.

Follow the Reference Diagrams For Programmers link in the Atmel ATmega Subsystem Diagrams section at http://web.alfredstate.edu/weimandn/, and for now look specifically at the Timer 2 diagrams.  Timer 2 is similar to Timer 0 and by using Timer 2 you won't mess up millis() and anything else that is dependent on it.

Then take a look at the sketch below and see if you can follow what is going on.  This particular sketch does not use interrupts but if you can figure it out you should also be able to figure out how to deal with the interrupt bits.

Don

Edit:  Also take a look at the Timer Data in the Atmel ATmega Miscellaneous Info section.

Code: [Select]
`// This program uses the 'Toggle OC2A on Compare Match' mode along with the 'Clear Timer on Compare Match (CTC)' mode //   to generate a square wave.  The disadvantage of this technique is that the output pin is fixed by the hardware.  //   The advantage is that this technique does not use any interrupts and does use any code (or processor time) once//   it is set up.  It does tie up a timer (timer 2 in this case).// The output frequency is determined by the clock frequency, the prescale factor, and the value loaded into OCR2A.// The output frequency = (clock frequency / (2 * prescale factor * (1 + OCR2A)))// The Arduino clock frequency is fixed at 16 MHz so the factors we have to work with are the prescale factor, and //   the value loaded into OCR2A.// For any given prescale factor the maximum output frequency will occur when OCR2A is loaded with 00.// For any given prescale factor the minimum output frequency will occur when OCR2A is loaded with 255.//   For a prescale factor of 1, fmax = 8 MHz and fmin = 31250 Hz//   For a prescale factor of 8, fmax = 1 MHz and fmin = 3906.3 Hz//   For a prescale factor of 32, fmax = 250 KHz and fmin = 976.6 Hz// Reworking the equation gives: OCR2A = (clock frequency / (output frequency * 2 * prescale factor)) -1//   For an output frequency of 150 KHz and a prescale factor of 1, OCR2A must be loaded with 52.33 (use 52)//     The value 52 gives a theoretical output frequency of 150.943 KHz//   For an output frequency of 150 KHz and a prescale factor of 8, OCR2A must be loaded with 5.67 (use 6)//     The value 6 gives a theoretical output frequency of 142.857 KHz#define outpin 11                                  // OC2A pin - cannot be changed#define clockFrequency 16000000                    // Arduino clock frequency#define prescaleFactor 1                           // required in order to get reasonable accuracyvoid setup (){/*// Configure the 'Waveform Generation Mode' to the CTC (Clear Timer on Compare Match) mode by dealing with the WGMxx bits.//   We need mode 2; WGM22 = 0, WGM21 = 1, and WGM20 = 0//   This must be done several steps because the WGM bits are split between two registers   TCCR2B &= ~(1 << WGM22);                         // clear WGM22   TCCR2A |= (1 << WGM21);                          // set WGM21   TCCR2A &= ~(1 << WGM20);                         // clear WGM20   // Configure the 'Compare Output Mode' to 'Toggle OC2A on Compare Match' by dealing with the COMxx bits.//   We need COM2A1 = 0 and COM2A0 = 1     TCCR2A &= ~(1 << COM2A1);                        // clear COM2A1   TCCR2A |= (1 << COM2A0);                         // set COM2A0   // Set the prescaler to divide by 1 by dealing with the CSxx bits//   We need CS22 = 0, CS21 = 0, and CS20 = 1   TCCR2B &= ~(1 << CS22);                          // clear CS22   TCCR2B &= ~(1 << CS21);                          // clear CS21   TCCR2B |=  (1 << CS20);                          // set CS20*/// The previous eight instructions have been replaced by these two.  See the comments above for an explanation.  TCCR2B = B00000001;                               // set CS20, clear CS21, CS22 and WGM22  TCCR2A = B01000010;                               // set COM2A0 and WGM21, clear COM2A1 and WGM20// calculate and load the timer data timer  OCR2A  = ((float)clockFrequency / (noteval * 2 * prescaleFactor)) -1;  // Set up the output pin  pinMode(outpin, OUTPUT);                          // turn on output pin   // the timer runs with no additional code and no use of interrupts// do whatever else you want to do here}void loop (){// do whatever else you want to do here}`

#### Fenestron

#4
##### Nov 13, 2011, 08:38 pm
? just wonder if there is any function to deal with atmega328 Timer Interrupt

I think it is time to learn AVR assembly programming
and atmega328 architecture thanks for your attention floresta  XD

#### floresta

#5
##### Nov 13, 2011, 09:11 pm
Quote
I think it is time to learn AVR assembly programming

The Arduino hardware is perfect for that, I do it all the time.  Spring for an AVRISPMKII programmer or one of it's workalikes, download AVR Studio 5 (or 4) and you are all set.  If you want to go back to the Arduino environment you just reload the bootloader and you are back to where you began.

Don

#### Fenestron

#6
##### Nov 14, 2011, 03:34 pm
Do ? need to have any other hardware?
and where can ? start to study?
do u have have any suggestion?
namely any document or any web page?

#### floresta

#7
##### Nov 14, 2011, 05:40 pm
Quote
Do ? need to have any other hardware?

No.  Just the Arduino or similar board and the AVRISPMKII or similar programmer.

Quote
and where can ? start to study?
do u have have any suggestion?

That depends on your background.

When I start with a new processor I take a program that I know works with the old one and start translating it.  If your original program was well commented the process is usually very straightforward.

When I start using a new subsystem I generate a diagram like the ones I pointed you to in reply #3 in order to figure things out.

Quote
namely any document or any web page?

I haven't found much that is really worthwhile.

Don

Go Up

Please enter a valid email to subscribe