Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[AVR] Blinking of LED using switch

Status
Not open for further replies.

K Srinivas Pavan Kumar

Member level 3
Joined
Jun 20, 2015
Messages
66
Helped
0
Reputation
0
Reaction score
0
Trophy points
6
Activity points
524
Hi friends,
I am making use of the switch to blink a single LED.It is working good
But in my board ,it consists of 8 switches and 8 leds . when I press first switch,first LED have to blink.when I press second switch,second LED have to blink.and so on can anybody give me the code for this program.I have worked on different patterns but only one LED is blinking
 

Hi,

when I press second switch,second LED have to blink
But what about the first LED now?
Should it stay blinking, or ON, or OFF?

Klaus
 

Note:For single LED


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <avr/io.h>
int main()
{
DDRA=0x01;  // Interfacing LED at PORTA.0
DDRB=0xFE; // Interfacing Switch at PORTB.0
PORTB=0xFF;
 
saha:
if(PINB==0xFE)
{
PORTA=0x01;
}
else
{
PORTA=0x00;
}
goto saha;
}

 
Last edited by a moderator:

For a LED to blink you need atleast a 500 ms delay between ON and OFF values. So, if you use delays then button press detection will not work and so you have to use a 500 ms timer interrupt and then use a switch statement and based on the case of the switch the particular LED is blinked.
 

I see that you are new to programming.

First, scan all the switches and save their status in some variables.

Next set the LEDs, one by one, into stop or blinking mode.

Loop the whole cycle.

Even if you are writing for one switch and one LED, keep the scope for future expansion and add comments profusely
 

Hi,

I'm a friend of interrupts. Especially with scanning multilpe switches and controlling multiple Leds.

But you can do without interrupts.
Just run a loop and include a 20ms wait.
Inside th loop use a counter, incremented every loop. If counter =25, then toogle LED and set counter =0
This gives an about 1Hz blinking frequency.

Klaus
 

I'm a friend of interrupts.

But they can become intimidating to some one who has not yet got the basics firmly in place. And with nested interrupts, it can quickly become a nightmare.

But polling is also useful- when the processes were slow- printers in DOS used polling- it was awe-inspiring!
 


Code C - [expand]
1
goto saha

The use of the goto command, although available in C language, is totally inadvisable because it breaks the progam structure, turning more complicated to analyse the code working.
 
Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top