INT0 not launching (ATMEGA 328P)

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

I tried to make some C code but, in order to set up RESET and INT0 vectors, as far as I know I need an assembly file to set the pointers using .ORG.

The thing is, although I programmed it very meticulously , it does not work.

This is my first time with AVR C and assembly, so I am sorry if this question is dumb or badly detailed from your POV.

Anyway, these are my files:

 

MAIN.C.

#include <avr/io.h>

void INT0_setup(void);
void RE0_handler(void);

int main(void)
{	
    DDRB |= 0b00100000; // I have a led there, so I set it up as OUTPUT
		
    INT0_setup();
    PORTB |= 0b00100000; // Turn the LED on
    while (1) 
    {
    }   
}

void INT0_setup(void){ // We make INT0 trigger with rising edges
        EIMSK = 0b00000001; 
	EICRA = 0b00000011;
	asm volatile("sei");
}

void RE0_handler(void){ // This is the ISR, we turn off the LED when interrupted
	PORTB &= 0b11011111;
}

STATICS.s, the file used to change certain addresses manually.

.EXTERN RE0_handler
.EXTERN main

.ORG 0x00
	rjmp main

.ORG 0x02
	rjmp RE0_handler

The code compiles without a complaint.

Since I am using an Arduino board attached to the MCU, I use the built in LED and the digital pin #2 as the pulse receiver.

I connected a pulldown resistor to the pin (to reduce noise) and a button + smaller resistor to the +5V terminal in order to send an 5-volt pulse when activated.

 

Is there anything wrong in my project? Ask for as much information as you think is needed, and thank you for your help.

This topic has a solution.
Last Edited: Mon. Sep 21, 2020 - 11:51 AM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

All can be written in C.

 

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

void INT0_setup(void);
void RE0_handler(void);

int main(void){
    DDRB |= 0b00100000; // I have a led there, so I set it up as OUTPUT
    
    INT0_setup();
    PORTB |= 0b00100000; // Turn the LED on
    while (1);
}

void INT0_setup(void){ // We make INT0 trigger with rising edges
    EIMSK = 0b00000001;
    EICRA = 0b00000011;
    sei();
}

void RE0_handler(void){ // This is the ISR, we turn off the LED when interrupted
    PORTB &= 0b11011111;
}

ISR(INT0_vect){
    RE0_handler();
}

 

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

Oh, thank you very much! Did not know about that header file. ArduinoIDE is not transparent at all in terms of files and code used, as you may already know.

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

The whole point of the Arduino IDE is that it does all that for you behind the scenes - so that you don't have to worry about those arcane details

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

Vyber90 wrote:
ArduinoIDE is not transparent at all in terms of files and code used

awneil wrote:
The whole point of the Arduino IDE is that it does all that for you behind the scenes - so that you don't have to worry about those arcane details

 

And yet it is all available to be seen, nothing is truly hidden, although as Andy points out, much is done behind the curtain so the user does not need to worry about it.

Glad the OP now has it figured out!

 

And Welcome to AVRFreaks!

 

(Possum Lodge oath) Quando omni flunkus, moritati.

"I thought growing old would take longer"

 

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

ki0bk wrote:

nothing is truly hidden

Im not a IDE person at all. Instead I would rather using terminal and CLI programs. That is why, although it may not be actively hidden, I have no idea of where the information may be. No problemo, by the way. Everything in this world is about investing enough time on it!

ki0bk wrote:

And Welcome to AVRFreaks!

Hey, thank you! I am sure I will enjoy being here! (my first post has been replied by non-toxic humans, this did not happen in stackoverflow... ;-))

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

Vyber90 wrote:
I have no idea of where the information may be

Let us know what platform your using and a Freak can guide you to where the Arduino directory is located, it's all there.

Jim

 

(Possum Lodge oath) Quando omni flunkus, moritati.

"I thought growing old would take longer"

 

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

By the way, the code mentioned in the solution does not work for me.

What the hell two resistors and I am already messing it up. These 2 years without building a single electric circuit...

Is it necessary to set the INT0 pin as an iNPUT in order to make it work?

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

ki0bk wrote:

Vyber90 wrote:
I have no idea of where the information may be

Let us know what platform your using and a Freak can guide you to where the Arduino directory is located, it's all there.

Jim

Thank you, but I am not disturbing anybody for non-needed information. I will examine the folders by myself in order to see where the interesting stuff lies in. Thank you so much anyway!

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

Vyber90 wrote:

By the way, the code mentioned in the solution does not work for me.

What the hell two resistors and I am already messing it up. These 2 years without building a single electric circuit...

Is it necessary to set the INT0 pin as an iNPUT in order to make it work?

I'm such a dumb person. I did not upload the code. I am so sorry. I am not 100% into this at the moment, to be honest. It works, it works. Thank you guys, for such a warm welcome!

Last Edited: Mon. Sep 21, 2020 - 01:18 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Vyber90 wrote:
the code mentioned in the solution does not work for me.

So it's not actually the solution, then?

 

As described in Tip #5, you can un-mark it if it turns out not to be the answer after all.

 

Perhaps time to post the schematic of what you're actually doing - and the code as you now have it.

 

See Tip #1 for that ...

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

Vyber90 wrote:
Oh, thank you very much! Did not know about that header file.
There's a manual. avr/interrupt.h is just one of many headers in AVR-LibC (also used "under the hood" in Arduino) that provide various aspects of chip support:

 

https://www.nongnu.org/avr-libc/...

 

The sei()/ISR() info at:

 

https://www.nongnu.org/avr-libc/...

Vyber90 wrote:
the code mentioned in the solution does not work for me.
Can you add a bit more definition to the term "does not work" ?

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

awneil wrote:

Vyber90 wrote:
the code mentioned in the solution does not work for me.

So it's not actually the solution, then?

clawson wrote:

Can you add a bit more definition to the term "does not work" ?

As I mentioned in a later post, the fault was mine. I did not upload the code correctly and, for some reason, I thought it should have worked. I am so sorry for wasting your time.