| Author |
Message |
|
|
Posted: Jun 15, 2012 - 05:35 PM |
|

Joined: Jun 15, 2012
Posts: 7
|
|
I want to write a program in avr studio .
and I want to use switch as interrupt if the switch=1 the led will turn on if it 0 then the led will turn off so I connect switch to (INT0) but i don't know how to write the code.
please I need help  |
|
|
| |
|
|
|
|
|
Posted: Jun 15, 2012 - 08:16 PM |
|


Joined: Nov 22, 2002
Posts: 12049
Location: Tangent, OR, USA
|
|
Hello, Losiana -
Welcome to the forum. interrupt not needed.
First question: assembler or c or BASIC?
Metacode might look like this
Code:
MAIN:
if switch_pin = low
LED = off
GOTO Main
else
LED = on
GOTO Main
How this is implemented depends on the environment you use.
By the way, nobody will supply code for you. You need to try SOMETHING, then show us what you have tried. Suggestions will then be offered. Be sure to use the code tags (buttons) when you post your code.
Jim |
_________________ Jim Wagner
Oregon Research Electronics, Consulting Div.
Tangent, OR, USA
"The only thing standing between us and victory is defeat" P.G.Wodhouse in Wooster & Jeeves series
|
| |
|
|
|
|
|
Posted: Jun 15, 2012 - 08:22 PM |
|


Joined: Dec 06, 2007
Posts: 2512
Location: Redmond, WA USA
|
|
|
losiana wrote:
I want to use switch as interrupt if the switch=1 the led will turn on if it 0 then the led will turn off so I connect switch to (INT0) but i don't know how to write the code.
please I need help
Yes, you do need help.
I recommend that you use switch polling with a proper debounce routine instead of interrupts. |
_________________ Larry
Those afraid to embrace the future will quickly fade into the past. - larryvc
|
| |
|
|
|
|
|
Posted: Jun 15, 2012 - 08:53 PM |
|


Joined: Nov 22, 2002
Posts: 12049
Location: Tangent, OR, USA
|
|
If the OP is simply turning an LED on or off, then debounce is not (yet) needed. It will soon be, but not yet.
Jim |
_________________ Jim Wagner
Oregon Research Electronics, Consulting Div.
Tangent, OR, USA
"The only thing standing between us and victory is defeat" P.G.Wodhouse in Wooster & Jeeves series
|
| |
|
|
|
|
|
Posted: Jun 15, 2012 - 09:54 PM |
|


Joined: Dec 06, 2007
Posts: 2512
Location: Redmond, WA USA
|
|
|
losiana wrote:
I want to use switch as interrupt if the switch=1 the led will turn on if it 0 then the led will turn off so I connect switch to (INT0) but i don't know how to write the code.
Though that is true, I was mainly concerned with the "switch as interrupt" and "(INT0)" in the OPs post. |
_________________ Larry
Those afraid to embrace the future will quickly fade into the past. - larryvc
|
| |
|
|
|
|
|
Posted: Jun 15, 2012 - 10:40 PM |
|

Joined: Jun 15, 2012
Posts: 7
|
|
| my code was
Code:
#include <avr/interrupt.h>
enum {led_off,led_on};
int current = led_off;
ISR (INT0_vect)
{
if (current == led_off)
current =led_on ;
else current=led_off;
}
void main ()
{
DDRB = 0xFF;
GICR = 0x40;
MCUCR = 0x03;
sei ();
while (1)
{ switch (current)
{case led_off:
PORTB = 0b00000000; break;
case led_on:
PORTB = 0b11111111; break;
}
}
$ Fixed code tags - JS $ |
|
|
| |
|
|
|
|
|
Posted: Jun 15, 2012 - 11:19 PM |
|


Joined: Mar 27, 2002
Posts: 18561
Location: Lund, Sweden
|
|
|
Code:
int current = led_off;
This needs to be
Code:
volatile int current = led_off;
Search for 'volatile' here for uncountable explanations of this, and why it is needed. Or read up on ity in your favourite textbook on the C programming language.
----------
Code:
ISR (INT0_vect)
{
if (current == led_off)
current =led_on ;
else current=led_off;
}
This is exactly the situation where the "switch bounce" mentioned above comes into play. Your switch will open and close several times during a few milliseconds when you press the button once. Search here at AVRfreaks for "switch bounce", "debounce", "de-bounce" etc for many previous discussions on what this is and what you can do to avoid it. |
|
|
| |
|
|
|
|
|
Posted: Jun 16, 2012 - 01:14 AM |
|


Joined: Nov 22, 2002
Posts: 12049
Location: Tangent, OR, USA
|
|
If you must use an interrupt, you will have to contend with switch bounce, later.
Reason? The switch really switches on and off many times each time you activate the switch and every time it is released. This will cause many interrupts on each push and release. You won't see them in THIS experiment because they happen so fast. But, when you start counting key pushes, you will get really bizarre results.
Jim |
_________________ Jim Wagner
Oregon Research Electronics, Consulting Div.
Tangent, OR, USA
"The only thing standing between us and victory is defeat" P.G.Wodhouse in Wooster & Jeeves series
|
| |
|
|
|
|
|
Posted: Jun 16, 2012 - 11:23 AM |
|


Joined: Mar 27, 2002
Posts: 18561
Location: Lund, Sweden
|
|
|
Quote:
You won't see them in THIS experiment
On the contrary, IMO. When the switch bounces an odd number of times for one press of the button, the LED will be seen to toggle. When the switch bounces an even number of times for one press of the button, it will not. |
|
|
| |
|
|
|
|
|