The first and simplest project for any microcontroller is LED Blinking.
Circuit Diagram
Components Required
- ATmega32 microcontroller
- Programmer Board
- LEDs
- Crystal - less than 16 MHz.
- Capacitors - 2
- +5 V supply
- Breadboard
- Connecting Wires
Optional
- Resistors - 330 Ohms to be connected in series with the LEDs(not shown in the circuit diagram)
Description
- Here, all pins of PORT B are declared as output ports as all the LEDs are connected to the pins of PORTB.
- A crystal is connected to the XTAL1 and XTAL2 pins to provide the clock pulse.
- Reset is connected to +5V.
Source Code
#include <avr/io.h>
#include <util/delay.h>
#define F_CPU 1000000UL // Defines the frequency of external crystal
int main()
{
/* The Data direction register of all Pins of Port B is Set as Output*/
DDRB = 0xFF;
PORTB = 0x00; // Initially all pins of Port B are set as Output Low
while (1) // Infinite Loop
{
PORTB = 0b11111110; // all LEDs will glow except the first LED
_delay_ms(500); // delay of 500 milliseconds
PORTB = 0b11111101; // all LEDs will glow except the second LED
_delay_ms(500); // delay of 500 milliseconds
PORTB = 0b11111011; // all LEDs will glow except the third LED
_delay_ms(500); // delay of 500 milliseconds
PORTB = 0b11110111; // all LEDs will glow except the fourth LED
_delay_ms(500); // delay of 500 milliseconds
PORTB = 0b11101111; // all LEDs will glow except the fifth LED
_delay_ms(500); // delay of 500 milliseconds
PORTB = 0b11011111; // all LEDs will glow except the sixth LED
_delay_ms(500); // delay of 500 milliseconds
PORTB = 0b10111111; // all LEDs will glow except the seventh LED
_delay_ms(500); // delay of 500 milliseconds
PORTB = 0b01111111; // all LEDs will glow except the eight LED
_delay_ms(500); // delay of 500 milliseconds
}
return 0;
}
Thankyou for Reading
No comments:
Post a Comment