First LED Program(AVR)!

BLINKING LED

Just as displaying “Hello World ‘ is used to verify writing code for any programming language, Blinking led is also one of the first thing that a AVR or microcontroller learner performs. It’s the starting point of learning to program microcontrollers.

To get the led blink you need following components:

  1. AVR development board( or simply any AVR microcontroller)
  2. Programmer
  3. LED(in case you don’t have development board)
  4. Breadboard(in case you don’t have development board)
  5. wires(in case you don’t have development board)
  6. FRC cable

Since the current form microcontroller pins can directly drive the leds we donot need any other auxiliary circuits to make the leds blink.

How do we make led blink???

For blinking led we first need to make up our mind for what steps we need to follow. The following should make it clear:

  1. First you need to Assign the port of microcontroller as output.
  2. For the specific port you made as output pass a logic 1(5v ) so as to make led glow.
  3. Wait for a certain time.
  4. Again pass  logic 0 (0v) to that specific port so that Led stops glowing.
  5. Wait for certain time.
  6. Repeat step 2-5.

led_blink

SOURCE CODE:

 #include<avr/io.h> //For initializing AVR i/o use this header file

#include<util/delay.h> /*For introducing delay on program use this header file*/

int main()
{

DDRB=0xff; // Assign port B as output 
PORTB=0x00;// All ports of B (B0-B7) are assigned value of 0) 
while(1)  // INFINITE LOOP 
{
PORTB=0xFF; /* All ports of B (B0-B7) are assigned value of 1  i.e   logic 1 and led glows*/ 
_delay_ms(200); /*Delay of 200 mili second , you can also use _delay_us for microsecond*/ 
PORTB=0x00; /* All ports of B (B0-B7) are assigned value of 1 i.e logic 0 and led stops*/
_delay_ms(200); 
}
}

Remarks:

Different patterns of led can be made by changing which LED glows and which doesnot . It is upto the reader to find out and try themselves various ways that LED can be made to blink.

Logical operators may also be used for making the leds blink. Some of them are discussed below.

1. PORTB |=(1<<0);

The literal reading of the statement is “set pin 0 on PORTB high(+5)”. First, PORTB is called. This is followed by the bitwise OR( | ) and the equals sign(=).

PORTB |=(1<<0);
What is happening here is that every pin on PORTB is being compared to the state(s) proposed by the equation in the parenthesis. Inside the parentheses we see the shift left command (<<). The syntax is PORTx |=(the value<<number positions). In this example, x = B and the value = 1 and the number positions shifted is 0 meaning pin0 on PORTB will go high. If the number positions shifted is (1<<3), the bit is shifted three times, followed by zeros.

2. PORTB &= ~(1 << 0);

This line is similar to the turning the pin high with a few differences. The literal reading of the statement is “set pin 0 on PORTB LOW(+0)”. First, PORTB is called. This is followed by the bitwise AND( & ) and the equals sign(=).

PORTB &= ~(1 << 0);
What’s with the Parentheses? it’s exactly the same as above. The code (1<<) says we are shifting 1 zero positions to the left. What makes the difference is that before the parentheses we are using the Ones Compliment operator(~). This turns the 1 into it’s compliment, 0. Instead of saying shift the value of 0 zero positions, we are saying shift the compliment of 1 zero positions

Also For Understanding More about it .. Refer Video Reference at :

If you encounter any Problem, then Feel free to Join the Club and Ask Club Members for help and Support.

Share