Timer Interrupt For Arduino

Hi Guys ! Just started using arduino few days ago for my school project. I am having a difficult time understanding the codes. Could someone help me out with this? : In the main loop, flash led 1 for 60hz. Initiate a timer interrupt of 1 ms period to turn on led 2 for a period of 0.01ms. Thx ! :slight_smile:

Which codes? You forgot to attach your sketch.

Timer interrupts are a very complex way of turning LEDs on and off. It's best not to use them.

codes like this. I have no idea what this code is talking about. And I have no choice, my lecturer want me to do this for my project.

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

#define LEDPIN 3

int led = 13;

void setup()
{
pinMode(LEDPIN, OUTPUT);
pinMode(led, OUTPUT);
// initialize Timer1
cli(); // disable global interrupts
TCCR1A = 0; // set entire TCCR1A register to 0
TCCR1B = 0; // same for TCCR1B

// set compare match register to desired timer count:
OCR1A = 15624;
// turn on CTC mode:
TCCR1B |= (1 << WGM12);
// Set CS10 and CS12 bits for 1024 prescaler:
TCCR1B |= (1 << CS10);
TCCR1B |= (1 << CS12);
// enable timer compare interrupt:
TIMSK1 |= (1 << OCIE1A);
// enable global interrupts:
sei();
}

void loop()
{

}

ISR(TIMER1_COMPA_vect)
{
digitalWrite(LEDPIN, !digitalRead(LEDPIN));
}

Have a look at Several Things at a Time and at Planning and Implementing a Program. Both of them flash LEDs and use millis() to do the timming. If you want shorter periods you can use micros() the same way.

If you really want to learn how to use the hardware Timers in the Atmega 328 the first thing to do is study the datasheet.

Your Post arrived while I was writing this. Things like TCCR1A are the names of registers in the Atmega 328 and they are all described in the datasheet.

...R

OK, we don't like doing your homework for you but it may be instructional to learn how to read an Arduino sketch.

  1. What is loop() doing? In this case it's empty, so it does nothing.

  2. Since loop() is empty, what other functions might be running? Aha! There's one called ISR which reads the output state of the LED pin and then writes the opposite. The "!" is the negation operator in C.

  3. So how does ISR() get called if it's not in loop()? Well, ISR is not really a function. It's a macro which is used for interrupt service routines. In this case, it is servicing the TIMER1_COMPA_vect interrupt. This is obviously something to do with timer 1.

  4. So what configures timer 1? The setup() function has lots of nice comments telling you what each line does. Read the datasheet and search for the words used there, like "CTC mode" and "prescaler".

I still maintain that it's best not to use timers for this but the requirement of turning LED 2 on for only 0.01ms is pretty tough to do without timers. That's not really a time period that a person can see, let alone measure, so perhaps it has some practical use with other peripheral hardware which your school can't afford to buy for you.

hmm.. I think I get it abit now. So what does this mean? ( Initiate a timer interrupt of 1 ms period to turn on led 2 for a period of 0.01ms.)?

Is that the assignment you have been given or is it a general question?

It is the assignment I am given. This is the full question 'In the main loop, flash led 1 for 60hz. Initiate a timer interrupt of 1 ms period to turn on led 2 for a period of 0.01ms.'

You can read this https://arduinodiy.wordpress.com/2012/02/28/timer-interrupts/

chuankianGB: 1.'In the main loop, flash led 1 for 60hz.

  1. Initiate a timer interrupt of 1 ms period to turn on led 2 for a period of 0.01ms.'
  1. flash led 1 for 60hz
void loop()
{
   digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delayMicroseconds(8333);              // wait for a 8.3m second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delayMicroseconds(8333);              // wait for a 8.3m second
}

chuankianGB: Initiate a timer interrupt of 1 ms period to turn on led 2 for a period of 0.01ms.'

If you read this literally it is impossible. You cannot achieve a period of 0.01 millisecs with a 1 millisec timer.

Are you sure you are reading the description of your exercise correctly ?

...R

Need to use micros(). 1uS = 0.001mS 10uS = 0.01mS. I tihink micros() granularity is 4uS tho. Can 10 be counted? Does sound like something is not being interpreted correctly tho.

chuankianGB: It is the assignment I am given. This is the full question 'In the main loop, flash led 1 for 60hz. Initiate a timer interrupt of 1 ms period to turn on led 2 for a period of 0.01ms.'

Ask your teacher whether these numbers and dimensions are really correct.

Use a timer for the 1 ms, software timing for the 10 us.

switch the led on, switch the led off.

You have to burn 160 cycles, how long does digitalWrite take?

If digitalWrite is too slow, manipulate the portbit directly.

Thats not too easy to tune, but the 10 uS are too fast for interrupts.

You could use an arduino to measure the pulse length, would make a nice litte assignment...

Thx to everybody who have replied my question. I would ask him when he is around. So if the question is change to initiate a timer interrupt to turn on led 2 for a period of 0.01ms, how could I go about doing it?

What Whandall is trying to say is you use a timer to tell you when to start but you use something else to hold the LED on for the required period. Since it's only 160 clock cycles on the Arduino UNO, you could just write 160 NOP (no operation) instructions to get the exact timing.

It's an odd requirement. As I said, there's no way for a human to perceive this time period. How are you going to be measured? An oscilloscope? Do you have access to the oscilloscope to develop and debug your program?

Not sure why my lecturer give me such a hard question provided that I just started arduino 2 days ago =.= and yes I have access to an oscilloscope.

Still dun understand what whandall is trying to say :(

Have you come across the concept of "duty cycle" yet? Can you see why the original code creates a 50% duty cycle and the assignment question is quite distinctly NOT a 50% duty?

The original code is a code recommended to me by a friend. It need not be the right code. If it is not the right code than how can i go about doing it?

chuankianGB: Thx to everybody who have replied my question. I would ask him when he is around. So if the question is change to initiate a timer interrupt to turn on led 2 for a period of 0.01ms, how could I go about doing it?

Don't waste time on that until AFTER you have clarified the situation with your Tutor.

...R