Beginner interrupt problems with Atmega2560

Go To Last Post
12 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi!
I'm a newbie in programming microcontrollers, only wrote simple codes in school lessons and tests (as led blinking for button pressure, "Knight Rider running leds", and 7segments, in atmega64 based board). I'm interested in this kind of programming, so i bought a board with atmega2560, but this is works differently, and i not even able to set and make the interrupt work. I prefer to program it in C.
Thanks!
(if it is in the wrong topic or something, feel free to move it to another or delete)

36

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I've read that a couple of times but I can't actually spot an actual question in there.

 

Are you saying you have some ISR() code but you are having problems? If so might it not be an idea to actually show the code and say what problems you are having?

 

If you are aiming for "knight rider" then as there is a "movement over time" element to the display I kind of guess it's a timer interrupt you are trying to use and having problems with - right?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

BTW if you weren't using interrupts then something like this that I just threw together might do the Knight Rider thing if it found 8 LEDs on the pins of PORTC (change DDR/PORT at top for a different port)...

#define F_CPU 8000000UL
#include <avr/io.h>
#include <util/delay.h>

#define LED_PORT PORTC
#define LED_DIR	DDRC

typedef enum {
	LEFT,
	RIGHT	
} dir_t;

int main(void)
{
	uint8_t pattern = 0b00000001;
	dir_t dir = LEFT;
	
	LED_DIR = 0xFF; // all 8 pins output
	while (1) {
		switch (dir) {
			case LEFT:
				pattern <<= 1;
				if (pattern & (1 << 7)) {
					dir = RIGHT;
				}
			break;

			case RIGHT:
				pattern >>= 1;
				if (pattern & (1 << 0)) {
					dir = LEFT;
				}
			break;
		}
		LED_PORT = pattern;
		_delay_ms(100);
	}
}

The key thing here is << and >> that are being used to move a single 1 bit (that starts at 0b00000001). Each time it moves 1 place left or right. If moving left and it reaches bit 7 the direction is changed. If moving right and it reaches bit 0 the direction is also changed.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Thirtysix wrote:

... so i bought a board with atmega2560, but this is works differently, and i not even able to set and make the interrupt work. I prefer to program it in C.

 

Are you able to blink an LED at 1Hz on your new board using just a 'delay() function call?

 

 

#1 Hardware Problem? https://www.avrfreaks.net/forum/...

#2 Hardware Problem? Read AVR042.

#3 All grounds are not created equal

#4 Have you proved your chip is running at xxMHz?

#5 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand."

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thirtysix wrote:
a board with atmega2560

What board, exactly ?

 

Give a link!

 

For starting, would probably be best to get an XPlained Pro / Mini / Nano - because you will then get an on-board programmer & debugger, and Atmel Studio will recognise it, and has a whole set of ready-to-go examples specifically for that board.

 

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks for the replies, my board is Arduino MEGA Board 2560 CH340G, and i use Atmel Studio 4 to write the code.
With delay i can blink leds, but i would prefer to do this (and other) with interrupts.
So, my question is, how to set the interrupt(s)?
After set it, should i send the device to energy saving mode?

36

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0


Thirtysix wrote:
i use Atmel Studio 4

Why so ancient?!

 

surprise

 

With delay i can blink leds,

That's a good start.

 

As Brian said, have you proved that it's running at the expected speed?

 

So, my question is, how to set the interrupt(s)?

What have you tried so far?

 

Where are you stuck?

 

There are plenty of examples, tutorials, etc - not least:

 

https://www.avrfreaks.net/forums...

 

And the Product Page for the chip:

 

https://www.avrfreaks.net/commen...

 

https://www.avrfreaks.net/commen...

 

EDIT

 

The datasheet:

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Wed. Apr 8, 2020 - 10:50 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If you have an Arduino Mega board, why are you not using the Arduino IDE?

You can write C code or C++ code as you please with it!

It's all very easy.

 

Jim

 

 

 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thirtysix wrote:
i use Atmel Studio 4 to write the code.
With delay i can blink leds, but i would prefer to do this (and other) with interrupts.
So, my question is, how to set the interrupt(s)?
Well as Studio 4 did not come with a C compiler (one of the many reasons to use AS7 instead!) then can we assume you are talking about writing code in the assembler that came with AS4? If so then in your Asm you need to have a JMP at 0 to clear the vector table then a .org to the vector location for the interrput you want to use. at that location a JMP to an interrupt handler (that ends in RETI) and then beyond the vector table the rest of the code that the entry jump will arrive at.

 

Or by "Studio 4" did you really mean "Studio 4 + added C compiler"? If so is that added C compiler WinAVR or Atmel/Microchip "toolchain" or something else?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Im a noob, so neither these guides, nor the datasheet helped me. =( 
How its looks like when the interrupt is set?
I mean something like this:
TCCR0 = 0b00000111;
TIMSK = 0b00000001;
sei();
(I used this for another developer board, if i had these lines with changing the numbers i can change the interrupt time)

36

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 nor the datasheet helped me.

You must study the datasheet, not just glance at it..take your time...all of the control registers are thoroughly described.  Tutorials will show what to do with the registers, if it isn't apparent.

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I suspect what he wants to do is have the LED toggle in the interrupt after the timer expires? Right now I think he is probably using a for loop or something?

 

I think there is a language problem.  Everyone here speaks 'C'.  If you show "source code" it will help.